removed striped header; fixed severity in recovery emails; attempt to fix broken colors on certain email clients; dynamically check the path of the sendmail command
Costa Tsaousis committed
Aug 18, 2016 at 00:35 UTC
e59f027a6ed4497a614731026cf8a9122bd52bd9
1 file changed
+77
-54
plugins.d/alarm.sh
+77
-54
@@ -1,5 +1,15 @@
1
#!/usr/bin/env bash
2
3
+sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
4
+if [ -z "${sendmail}" ]
5
+then
6
+ echo >&2 "I cannot send emails - there is no sendmail command available."
7
+fi
8
+
9
+sendmail_from_pipe() {
10
+ "${sendmail}" -t
11
+}
12
+
13
type="${1}" # WARNING or CRITICAL
14
name="${2}" # the name of the alarm, as given in netdata health.d entries
15
chart="${3}" # the name of the chart (type.id)
@@ -20,54 +30,64 @@ hostname="$(hostname)"
30
date="$(date)"
31
32
duration4human() {
23
- local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
24
- d=$(( s / 86400 ))
25
- s=$(( s - (d * 86400) ))
26
- h=$(( s / 3600 ))
27
- s=$(( s - (h * 3600) ))
28
- m=$(( s / 60 ))
29
- s=$(( s - (m * 60) ))
33
+ local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
34
+ d=$(( s / 86400 ))
35
+ s=$(( s - (d * 86400) ))
36
+ h=$(( s / 3600 ))
37
+ s=$(( s - (h * 3600) ))
38
+ m=$(( s / 60 ))
39
+ s=$(( s - (m * 60) ))
40
31
- if [ ${d} -gt 0 ]
32
- then
33
- [ ${m} -ge 30 ] && h=$(( h + 1 ))
34
- [ ${d} -gt 1 ] && ds="days"
35
- [ ${h} -gt 1 ] && hs="hours"
36
- if [ ${h} -gt 0 ]
37
- then
38
- echo "${d} ${ds} and ${h} ${hs}"
39
- else
40
- echo "${d} ${ds}"
41
- fi
42
- elif [ ${h} -gt 0 ]
43
- then
44
- [ ${s} -ge 30 ] && m=$(( m + 1 ))
45
- [ ${h} -gt 1 ] && hs="hours"
46
- [ ${m} -gt 1 ] && ms="minutes"
47
- if [ ${m} -gt 0 ]
48
- then
49
- echo "${h} ${hs} and ${m} ${ms}"
50
- else
51
- echo "${h} ${hs}"
52
- fi
53
- elif [ ${m} -gt 0 ]
54
- then
55
- [ ${m} -gt 1 ] && ms="minutes"
56
- [ ${s} -gt 1 ] && ss="seconds"
57
- if [ ${s} -gt 0 ]
58
- then
59
- echo "${m} ${ms} and ${s} ${ss}"
60
- else
61
- echo "${m} ${ms}"
62
- fi
63
- else
64
- [ ${s} -gt 1 ] && ss="seconds"
65
- echo "${s} ${ss}"
66
- fi
41
+ if [ ${d} -gt 0 ]
42
+ then
43
+ [ ${m} -ge 30 ] && h=$(( h + 1 ))
44
+ [ ${d} -gt 1 ] && ds="days"
45
+ [ ${h} -gt 1 ] && hs="hours"
46
+ if [ ${h} -gt 0 ]
47
+ then
48
+ echo "${d} ${ds} and ${h} ${hs}"
49
+ else
50
+ echo "${d} ${ds}"
51
+ fi
52
+ elif [ ${h} -gt 0 ]
53
+ then
54
+ [ ${s} -ge 30 ] && m=$(( m + 1 ))
55
+ [ ${h} -gt 1 ] && hs="hours"
56
+ [ ${m} -gt 1 ] && ms="minutes"
57
+ if [ ${m} -gt 0 ]
58
+ then
59
+ echo "${h} ${hs} and ${m} ${ms}"
60
+ else
61
+ echo "${h} ${hs}"
62
+ fi
63
+ elif [ ${m} -gt 0 ]
64
+ then
65
+ [ ${m} -gt 1 ] && ms="minutes"
66
+ [ ${s} -gt 1 ] && ss="seconds"
67
+ if [ ${s} -gt 0 ]
68
+ then
69
+ echo "${m} ${ms} and ${s} ${ss}"
70
+ else
71
+ echo "${m} ${ms}"
72
+ fi
73
+ else
74
+ [ ${s} -gt 1 ] && ss="seconds"
75
+ echo "${s} ${ss}"
76
+ fi
77
}
78
79
+if [ "${old_status}" = "RAISED" -a "${status}" = "OFF" ]
80
+then
81
+ # an alarm that is now OFF
82
+ severity="${type} recovered"
83
+ raised_for="<br/>(was in ${type,,} state for $(duration4human ${duration}))"
84
+else
85
+ # an alarm that is now RAISED
86
+ severity="${type}"
87
+ raised_for=""
88
+fi
89
+
90
# prepare the title
70
-raised_for=""
91
case "${status}" in
92
RAISED)
93
if [ "${type}" = "CRITICAL" ]
@@ -92,19 +112,16 @@ case "${status}" in
112
status_message="back to normal"
113
fi
114
color="#77ca6d"
95
- if [ "${old_status}" = "RAISED" ]
96
- then
97
- raised_for="<br/>(was in ${type,,} state for $(duration4human ${duration}))"
98
- fi
115
;;
116
101
- *) status_message="status unknown"
117
+ *)
118
+ status_message="status unknown"
119
color="grey"
120
;;
121
esac
122
123
# send the email
107
-cat <<EOF | /usr/sbin/sendmail -t
124
+cat <<EOF | sendmail_from_pipe
125
To: root
126
Subject: ${type} ${hostname} ${status_message} - ${chart}.${name}
127
Content-Type: text/html
@@ -122,13 +139,14 @@ Content-Type: text/html
139
style="background:#fff;border:1px solid #e9e9e9">
140
<tbody>
141
<tr>
125
- <td bgcolor="#555555"
126
- style="padding:5px;background-color:#555555;background:repeating-linear-gradient(-45deg,#aaa,#aaa 14px,#ccc 14px,#ccc 24px)!important">
142
+ <td bgcolor="#fff"
143
+ style="padding:5px;background-color:#fff;">
144
<div style="font-size:20px;color: #444;text-decoration: none;font-weight: bold;">netdata alert</div>
145
</td>
146
</tr>
147
<tr>
131
- <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">
148
+ <td bgcolor="${color}"
149
+ 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">
150
<h1 style="font-weight:400;margin:0">${hostname} ${status_message}</h1>
151
</td>
152
</tr>
@@ -154,7 +172,7 @@ Content-Type: text/html
172
<tr style="margin:0;padding:0">
173
<td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
174
align="left" valign="top">
157
- <span>${type}</span>
175
+ <span>${severity}</span>
176
<span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Severity</span>
177
</td>
178
</tr>
@@ -165,6 +183,11 @@ Content-Type: text/html
183
style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Time</span>
184
</td>
185
</tr>
186
+ <tr style="text-align:center;margin:0;padding:0">
187
+ <td style="font-size:10px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
188
+ align="center" valign="bottom">The source of this alarm is line <code>${src}</code>.
189
+ </td>
190
+ </tr>
191
<tr style="text-align:center;margin:0;padding:0">
192
<td style="font-size:12px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
193
align="center" valign="bottom">Sent by