#!/usr/bin/env bash
+sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
+if [ -z "${sendmail}" ]
+then
+ echo >&2 "I cannot send emails - there is no sendmail command available."
+fi
+
+sendmail_from_pipe() {
+ "${sendmail}" -t
+}
+
type="${1}" # WARNING or CRITICAL
name="${2}" # the name of the alarm, as given in netdata health.d entries
chart="${3}" # the name of the chart (type.id)
date="$(date)"
duration4human() {
- local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
- d=$(( s / 86400 ))
- s=$(( s - (d * 86400) ))
- h=$(( s / 3600 ))
- s=$(( s - (h * 3600) ))
- m=$(( s / 60 ))
- s=$(( s - (m * 60) ))
+ local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
+ d=$(( s / 86400 ))
+ s=$(( s - (d * 86400) ))
+ h=$(( s / 3600 ))
+ s=$(( s - (h * 3600) ))
+ m=$(( s / 60 ))
+ s=$(( s - (m * 60) ))
- if [ ${d} -gt 0 ]
- then
- [ ${m} -ge 30 ] && h=$(( h + 1 ))
- [ ${d} -gt 1 ] && ds="days"
- [ ${h} -gt 1 ] && hs="hours"
- if [ ${h} -gt 0 ]
- then
- echo "${d} ${ds} and ${h} ${hs}"
- else
- echo "${d} ${ds}"
- fi
- elif [ ${h} -gt 0 ]
- then
- [ ${s} -ge 30 ] && m=$(( m + 1 ))
- [ ${h} -gt 1 ] && hs="hours"
- [ ${m} -gt 1 ] && ms="minutes"
- if [ ${m} -gt 0 ]
- then
- echo "${h} ${hs} and ${m} ${ms}"
- else
- echo "${h} ${hs}"
- fi
- elif [ ${m} -gt 0 ]
- then
- [ ${m} -gt 1 ] && ms="minutes"
- [ ${s} -gt 1 ] && ss="seconds"
- if [ ${s} -gt 0 ]
- then
- echo "${m} ${ms} and ${s} ${ss}"
- else
- echo "${m} ${ms}"
- fi
- else
- [ ${s} -gt 1 ] && ss="seconds"
- echo "${s} ${ss}"
- fi
+ if [ ${d} -gt 0 ]
+ then
+ [ ${m} -ge 30 ] && h=$(( h + 1 ))
+ [ ${d} -gt 1 ] && ds="days"
+ [ ${h} -gt 1 ] && hs="hours"
+ if [ ${h} -gt 0 ]
+ then
+ echo "${d} ${ds} and ${h} ${hs}"
+ else
+ echo "${d} ${ds}"
+ fi
+ elif [ ${h} -gt 0 ]
+ then
+ [ ${s} -ge 30 ] && m=$(( m + 1 ))
+ [ ${h} -gt 1 ] && hs="hours"
+ [ ${m} -gt 1 ] && ms="minutes"
+ if [ ${m} -gt 0 ]
+ then
+ echo "${h} ${hs} and ${m} ${ms}"
+ else
+ echo "${h} ${hs}"
+ fi
+ elif [ ${m} -gt 0 ]
+ then
+ [ ${m} -gt 1 ] && ms="minutes"
+ [ ${s} -gt 1 ] && ss="seconds"
+ if [ ${s} -gt 0 ]
+ then
+ echo "${m} ${ms} and ${s} ${ss}"
+ else
+ echo "${m} ${ms}"
+ fi
+ else
+ [ ${s} -gt 1 ] && ss="seconds"
+ echo "${s} ${ss}"
+ fi
}
+if [ "${old_status}" = "RAISED" -a "${status}" = "OFF" ]
+then
+ # an alarm that is now OFF
+ severity="${type} recovered"
+ raised_for="<br/>(was in ${type,,} state for $(duration4human ${duration}))"
+else
+ # an alarm that is now RAISED
+ severity="${type}"
+ raised_for=""
+fi
+
# prepare the title
-raised_for=""
case "${status}" in
RAISED)
if [ "${type}" = "CRITICAL" ]
status_message="back to normal"
fi
color="#77ca6d"
- if [ "${old_status}" = "RAISED" ]
- then
- raised_for="<br/>(was in ${type,,} state for $(duration4human ${duration}))"
- fi
;;
- *) status_message="status unknown"
+ *)
+ status_message="status unknown"
color="grey"
;;
esac
# send the email
-cat <<EOF | /usr/sbin/sendmail -t
+cat <<EOF | sendmail_from_pipe
To: root
Subject: ${type} ${hostname} ${status_message} - ${chart}.${name}
Content-Type: text/html
style="background:#fff;border:1px solid #e9e9e9">
<tbody>
<tr>
- <td bgcolor="#555555"
- style="padding:5px;background-color:#555555;background:repeating-linear-gradient(-45deg,#aaa,#aaa 14px,#ccc 14px,#ccc 24px)!important">
+ <td bgcolor="#fff"
+ style="padding:5px;background-color:#fff;">
<div style="font-size:20px;color: #444;text-decoration: none;font-weight: bold;">netdata alert</div>
</td>
</tr>
<tr>
- <td style="font-size:16px;vertical-align:top;font-weight:400;text-align:center;margin:0;padding:10px;color:#ffffff;background:${color}!important;border:1px solid ${color};border-top-color:${color}" align="center" valign="top">
+ <td bgcolor="${color}"
+ style="font-size:16px;vertical-align:top;font-weight:400;text-align:center;margin:0;padding:10px;color:#ffffff;background:${color}!important;border:1px solid ${color};border-top-color:${color}" align="center" valign="top">
<h1 style="font-weight:400;margin:0">${hostname} ${status_message}</h1>
</td>
</tr>
<tr style="margin:0;padding:0">
<td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
align="left" valign="top">
- <span>${type}</span>
+ <span>${severity}</span>
<span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Severity</span>
</td>
</tr>
style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Time</span>
</td>
</tr>
+ <tr style="text-align:center;margin:0;padding:0">
+ <td style="font-size:10px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
+ align="center" valign="bottom">The source of this alarm is line <code>${src}</code>.
+ </td>
+ </tr>
<tr style="text-align:center;margin:0;padding:0">
<td style="font-size:12px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
align="center" valign="bottom">Sent by