@cryptotaxi247 / netdata-1 / commits / be21bd049

alarm script that sends notifications by email

Costa Tsaousis committed Aug 17, 2016 at 02:23 UTC be21bd04908fc56cc0ae4df61ab649a6ae34eee1
1 file changed +188
plugins.d/alarm.sh new
+188
@@ -0,0 +1,188 @@
1 +#!/usr/bin/env bash
2 +
3 +type="${1}" # WARNING or CRITICAL
4 +name="${2}" # the name of the alarm, as given in netdata health.d entries
5 +chart="${3}" # the name of the chart (type.id)
6 +status="${4}" # the current status
7 +old_status="${5}" # the previous status
8 +value="${6}" # the current value
9 +old_value="${7}" # the previous value
10 +src="${8}" # the line number and file the alarm has been configured
11 +duration="${9}" # the duration in seconds the previous state took
12 +
13 +# don't do anything if this is not RAISED or OFF
14 +[ "${status}" != "RAISED" -a "${status}" != "OFF" ] && exit 0
15 +
16 +# get the system hostname
17 +hostname="$(hostname)"
18 +
19 +# get the current date
20 +date="$(date)"
21 +
22 +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) ))
30 +
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
67 +}
68 +
69 +# prepare the title
70 +raised_for=""
71 +case "${status}" in
72 + RAISED)
73 + if [ "${type}" = "CRITICAL" ]
74 + then
75 + # CRITICAL - red
76 + status_message="is critical"
77 + color="#ca414b"
78 + else
79 + # WARNING - yellow
80 + status_message="needs attention"
81 + color="#caca4b"
82 + fi
83 + ;;
84 +
85 + OFF)
86 + if [ "${type}" = "CRITICAL" ]
87 + then
88 + # CRITICAL
89 + status_message="recovered"
90 + else
91 + # WARNING
92 + status_message="back to normal"
93 + fi
94 + color="#77ca6d"
95 + if [ "${old_status}" = "RAISED" ]
96 + then
97 + raised_for="<br/>(was in ${type,,} state for $(duration4human ${duration}))"
98 + fi
99 + ;;
100 +
101 + *) status_message="status unknown"
102 + color="grey"
103 + ;;
104 +esac
105 +
106 +# send the email
107 +cat <<EOF | /usr/sbin/sendmail -t
108 +To: root
109 +Subject: ${type} ${hostname} ${status_message} - ${chart}.${name}
110 +Content-Type: text/html
111 +
112 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
113 +<html xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; padding: 0;">
114 +<body style="font-family:'Helvetica Neue','Helvetica',Helvetica,Arial,sans-serif;font-size:14px;width:100%!important;min-height:100%;line-height:1.6;background:#f6f6f6;margin:0;padding:0">
115 +<table>
116 + <tbody>
117 + <tr>
118 + <td style="vertical-align:top;" valign="top"></td>
119 + <td width="700" style="vertical-align:top;display:block!important;max-width:700px!important;clear:both!important;margin:0 auto;padding:0" valign="top">
120 + <div style="max-width:700px;display:block;margin:0 auto;padding:20px">
121 + <table width="100%" cellpadding="0" cellspacing="0"
122 + style="background:#fff;border:1px solid #e9e9e9">
123 + <tbody>
124 + <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">
127 + <div style="font-size:20px;color: #444;text-decoration: none;font-weight: bold;">netdata alert</div>
128 + </td>
129 + </tr>
130 + <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">
132 + <h1 style="font-weight:400;margin:0">${hostname} ${status_message}</h1>
133 + </td>
134 + </tr>
135 + <tr>
136 + <td style="vertical-align:top" valign="top">
137 + <div style="margin:0;padding:20px;max-width:700px">
138 + <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
139 + <tbody>
140 + <tr>
141 + <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
142 + align="left" valign="top">
143 + <span>${chart}</span>
144 + <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Chart</span>
145 + </td>
146 + </tr>
147 + <tr style="margin:0;padding:0">
148 + <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
149 + align="left" valign="top">
150 + <span>${name} = ${value}</span>
151 + <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Alarm</span>
152 + </td>
153 + </tr>
154 + <tr style="margin:0;padding:0">
155 + <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
156 + align="left" valign="top">
157 + <span>${type}</span>
158 + <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Severity</span>
159 + </td>
160 + </tr>
161 + <tr style="margin:0;padding:0">
162 + <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
163 + align="left" valign="top"><span>${date}</span>
164 + <span>${raised_for}</span> <span
165 + style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Time</span>
166 + </td>
167 + </tr>
168 + <tr style="text-align:center;margin:0;padding:0">
169 + <td style="font-size:12px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
170 + align="center" valign="bottom">Sent by
171 + <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
172 + </td>
173 + </tr>
174 + </tbody>
175 + </table>
176 + </div>
177 + </td>
178 + </tr>
179 + </tbody>
180 + </table>
181 + </div>
182 + </td>
183 + </tr>
184 + </tbody>
185 +</table>
186 +</body>
187 +</html>
188 +EOF