alarm-notify.sh.in 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  1. #!/usr/bin/env bash
  2. #shellcheck source=/dev/null disable=SC2086,SC2154
  3. # netdata
  4. # real-time performance and health monitoring, done right!
  5. # (C) 2017 Costa Tsaousis <costa@tsaousis.gr>
  6. # SPDX-License-Identifier: GPL-3.0-or-later
  7. #
  8. # Script to send alarm notifications for netdata
  9. #
  10. # Features:
  11. # - multiple notification methods
  12. # - multiple roles per alarm
  13. # - multiple recipients per role
  14. # - severity filtering per recipient
  15. #
  16. # Supported notification methods:
  17. # - emails by @ktsaou
  18. # - slack.com notifications by @ktsaou
  19. # - alerta.io notifications by @kattunga
  20. # - discordapp.com notifications by @lowfive
  21. # - pushover.net notifications by @ktsaou
  22. # - pushbullet.com push notifications by Tiago Peralta @tperalta82 #1070
  23. # - telegram.org notifications by @hashworks #1002
  24. # - twilio.com notifications by Levi Blaney @shadycuz #1211
  25. # - kafka notifications by @ktsaou #1342
  26. # - pagerduty.com notifications by Jim Cooley @jimcooley #1373
  27. # - messagebird.com notifications by @tech_no_logical #1453
  28. # - hipchat notifications by @ktsaou #1561
  29. # - fleep notifications by @Ferroin
  30. # - prowlapp.com notifications by @Ferroin
  31. # - irc notifications by @manosf
  32. # - custom notifications by @ktsaou
  33. # - syslog messages by @Ferroin
  34. # - Microsoft Team notification by @tioumen
  35. # - RocketChat notifications by @Hermsi1337 #3777
  36. # - Google Hangouts Chat notifications by @EnzoAkira and @hendrikhofstadt
  37. # -----------------------------------------------------------------------------
  38. # testing notifications
  39. if { [ "${1}" = "test" ] || [ "${2}" = "test" ]; } && [ "${#}" -le 2 ]; then
  40. if [ "${2}" = "test" ]; then
  41. recipient="${1}"
  42. else
  43. recipient="${2}"
  44. fi
  45. [ -z "${recipient}" ] && recipient="sysadmin"
  46. id=1
  47. last="CLEAR"
  48. test_res=0
  49. for x in "WARNING" "CRITICAL" "CLEAR"; do
  50. echo >&2
  51. echo >&2 "# SENDING TEST ${x} ALARM TO ROLE: ${recipient}"
  52. "${0}" "${recipient}" "$(hostname)" 1 1 "${id}" "$(date +%s)" "test_alarm" "test.chart" "test.family" "${x}" "${last}" 100 90 "${0}" 1 $((0 + id)) "units" "this is a test alarm to verify notifications work" "new value" "old value" "evaluated expression" "expression variable values" 0 0
  53. #shellcheck disable=SC2181
  54. if [ $? -ne 0 ]; then
  55. echo >&2 "# FAILED"
  56. test_res=1
  57. else
  58. echo >&2 "# OK"
  59. fi
  60. last="${x}"
  61. id=$((id + 1))
  62. done
  63. exit $test_res
  64. fi
  65. export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
  66. export LC_ALL=C
  67. # -----------------------------------------------------------------------------
  68. PROGRAM_NAME="$(basename "${0}")"
  69. logdate() {
  70. date "+%Y-%m-%d %H:%M:%S"
  71. }
  72. log() {
  73. local status="${1}"
  74. shift
  75. echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
  76. }
  77. warning() {
  78. log WARNING "${@}"
  79. }
  80. error() {
  81. log ERROR "${@}"
  82. }
  83. info() {
  84. log INFO "${@}"
  85. }
  86. fatal() {
  87. log FATAL "${@}"
  88. exit 1
  89. }
  90. debug=${NETDATA_ALARM_NOTIFY_DEBUG-0}
  91. debug() {
  92. [ "${debug}" = "1" ] && log DEBUG "${@}"
  93. }
  94. docurl() {
  95. if [ -z "${curl}" ]; then
  96. error "${curl} is unset."
  97. return 1
  98. fi
  99. if [ "${debug}" = "1" ]; then
  100. echo >&2 "--- BEGIN curl command ---"
  101. printf >&2 "%q " ${curl} "${@}"
  102. echo >&2
  103. echo >&2 "--- END curl command ---"
  104. local out code ret
  105. out=$(mktemp /tmp/netdata-health-alarm-notify-XXXXXXXX)
  106. code=$(${curl} ${curl_options} --write-out "%{http_code}" --output "${out}" --silent --show-error "${@}")
  107. ret=$?
  108. echo >&2 "--- BEGIN received response ---"
  109. cat >&2 "${out}"
  110. echo >&2
  111. echo >&2 "--- END received response ---"
  112. echo >&2 "RECEIVED HTTP RESPONSE CODE: ${code}"
  113. rm "${out}"
  114. echo "${code}"
  115. return ${ret}
  116. fi
  117. ${curl} ${curl_options} --write-out "%{http_code}" --output /dev/null --silent --show-error "${@}"
  118. return $?
  119. }
  120. # -----------------------------------------------------------------------------
  121. # List of all the notification mechanisms we support.
  122. # Used in a couple of places to write more compact code.
  123. method_names="
  124. email
  125. pushover
  126. pushbullet
  127. telegram
  128. slack
  129. alerta
  130. flock
  131. discord
  132. hipchat
  133. twilio
  134. messagebird
  135. pd
  136. fleep
  137. syslog
  138. custom
  139. msteam
  140. kavenegar
  141. prowl
  142. irc
  143. awssns
  144. rocketchat
  145. sms
  146. hangouts
  147. "
  148. # -----------------------------------------------------------------------------
  149. # this is to be overwritten by the config file
  150. custom_sender() {
  151. info "not sending custom notification for ${status} of '${host}.${chart}.${name}'"
  152. }
  153. # -----------------------------------------------------------------------------
  154. # check for BASH v4+ (required for associative arrays)
  155. if [ ${BASH_VERSINFO[0]} -lt 4 ]; then
  156. fatal "BASH version 4 or later is required (this is ${BASH_VERSION})."
  157. fi
  158. # -----------------------------------------------------------------------------
  159. # defaults to allow running this script by hand
  160. [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
  161. [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
  162. [ -z "${NETDATA_CACHE_DIR}" ] && NETDATA_CACHE_DIR="@cachedir_POST@"
  163. [ -z "${NETDATA_REGISTRY_URL}" ] && NETDATA_REGISTRY_URL="https://registry.my-netdata.io"
  164. [ -z "${NETDATA_REGISTRY_CLOUD_BASE_URL}" ] && NETDATA_REGISTRY_CLOUD_BASE_URL="https://netdata.cloud"
  165. # -----------------------------------------------------------------------------
  166. # parse command line parameters
  167. if [[ ${1} = "unittest" ]]; then
  168. unittest=1 # enable unit testing mode
  169. roles="${2}" # the role that should be used for unit testing
  170. cfgfile="${3}" # the location of the config file to use for unit testing
  171. status="${4}" # the current status : REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
  172. old_status="${5}" # the previous status: REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
  173. else
  174. roles="${1}" # the roles that should be notified for this event
  175. args_host="${2}" # the host generated this event
  176. unique_id="${3}" # the unique id of this event
  177. alarm_id="${4}" # the unique id of the alarm that generated this event
  178. event_id="${5}" # the incremental id of the event, for this alarm id
  179. when="${6}" # the timestamp this event occurred
  180. name="${7}" # the name of the alarm, as given in netdata health.d entries
  181. chart="${8}" # the name of the chart (type.id)
  182. family="${9}" # the family of the chart
  183. status="${10}" # the current status : REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
  184. old_status="${11}" # the previous status: REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
  185. value="${12}" # the current value of the alarm
  186. old_value="${13}" # the previous value of the alarm
  187. src="${14}" # the line number and file the alarm has been configured
  188. duration="${15}" # the duration in seconds of the previous alarm state
  189. non_clear_duration="${16}" # the total duration in seconds this is/was non-clear
  190. units="${17}" # the units of the value
  191. info="${18}" # a short description of the alarm
  192. value_string="${19}" # friendly value (with units)
  193. # shellcheck disable=SC2034
  194. # variable is unused, but https://github.com/netdata/netdata/pull/5164#discussion_r255572947
  195. old_value_string="${20}" # friendly old value (with units), previously named "old_value_string"
  196. calc_expression="${21}" # contains the expression that was evaluated to trigger the alarm
  197. calc_param_values="${22}" # the values of the parameters in the expression, at the time of the evaluation
  198. total_warnings="${23}" # Total number of alarms in WARNING state
  199. total_critical="${24}" # Total number of alarms in CRITICAL state
  200. fi
  201. # -----------------------------------------------------------------------------
  202. # find a suitable hostname to use, if netdata did not supply a hostname
  203. if [ -z ${args_host} ]; then
  204. this_host=$(hostname -s 2>/dev/null)
  205. host="${this_host}"
  206. args_host="${this_host}"
  207. else
  208. host="${args_host}"
  209. fi
  210. # -----------------------------------------------------------------------------
  211. # screen statuses we don't need to send a notification
  212. # don't do anything if this is not WARNING, CRITICAL or CLEAR
  213. if [ "${status}" != "WARNING" ] && [ "${status}" != "CRITICAL" ] && [ "${status}" != "CLEAR" ]; then
  214. info "not sending notification for ${status} of '${host}.${chart}.${name}'"
  215. exit 1
  216. fi
  217. # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
  218. if [ "${clear_alarm_always}" != "YES" ] && [ "${old_status}" != "WARNING" ] && [ "${old_status}" != "CRITICAL" ] && [ "${status}" = "CLEAR" ]; then
  219. info "not sending notification for ${status} of '${host}.${chart}.${name}' (last status was ${old_status})"
  220. exit 1
  221. fi
  222. # -----------------------------------------------------------------------------
  223. # load configuration
  224. # By default fetch images from the global public registry.
  225. # This is required by default, since all notification methods need to download
  226. # images via the Internet, and private registries might not be reachable.
  227. # This can be overwritten at the configuration file.
  228. images_base_url="https://registry.my-netdata.io"
  229. # curl options to use
  230. curl_options=""
  231. # hostname handling
  232. use_fqdn="NO"
  233. # needed commands
  234. # if empty they will be searched in the system path
  235. curl=
  236. sendmail=
  237. # enable / disable features
  238. for method_name in ${method_names^^}; do
  239. declare SEND_${method_name}="YES"
  240. declare DEFAULT_RECIPIENT_${method_name}
  241. done
  242. for method_name in ${method_names}; do
  243. declare -A role_recipients_${method_name}
  244. done
  245. # slack configs
  246. SLACK_WEBHOOK_URL=
  247. # Microsoft Team configs
  248. MSTEAM_WEBHOOK_URL=
  249. # rocketchat configs
  250. ROCKETCHAT_WEBHOOK_URL=
  251. # alerta configs
  252. ALERTA_WEBHOOK_URL=
  253. ALERTA_API_KEY=
  254. # flock configs
  255. FLOCK_WEBHOOK_URL=
  256. # discord configs
  257. DISCORD_WEBHOOK_URL=
  258. # pushover configs
  259. PUSHOVER_APP_TOKEN=
  260. # pushbullet configs
  261. PUSHBULLET_ACCESS_TOKEN=
  262. PUSHBULLET_SOURCE_DEVICE=
  263. # twilio configs
  264. TWILIO_ACCOUNT_SID=
  265. TWILIO_ACCOUNT_TOKEN=
  266. TWILIO_NUMBER=
  267. # hipchat configs
  268. HIPCHAT_SERVER=
  269. HIPCHAT_AUTH_TOKEN=
  270. # messagebird configs
  271. MESSAGEBIRD_ACCESS_KEY=
  272. MESSAGEBIRD_NUMBER=
  273. # kavenegar configs
  274. KAVENEGAR_API_KEY=
  275. KAVENEGAR_SENDER=
  276. # telegram configs
  277. TELEGRAM_BOT_TOKEN=
  278. # kafka configs
  279. SEND_KAFKA="YES"
  280. KAFKA_URL=
  281. KAFKA_SENDER_IP=
  282. # pagerduty.com configs
  283. PD_SERVICE_KEY=
  284. # fleep.io configs
  285. FLEEP_SENDER="${host}"
  286. # Amazon SNS configs
  287. AWSSNS_MESSAGE_FORMAT=
  288. # syslog configs
  289. SYSLOG_FACILITY=
  290. # email configs
  291. EMAIL_SENDER=
  292. EMAIL_CHARSET=$(locale charmap 2>/dev/null)
  293. EMAIL_THREADING=
  294. EMAIL_PLAINTEXT_ONLY=
  295. # irc configs
  296. IRC_NICKNAME=
  297. IRC_REALNAME=
  298. IRC_NETWORK=
  299. # hangouts configs
  300. declare -A HANGOUTS_WEBHOOK_URI
  301. # load the stock and user configuration files
  302. # these will overwrite the variables above
  303. if [ ${unittest} ]; then
  304. if source "${cfgfile}"; then
  305. error "Failed to load requested config file."
  306. exit 1
  307. fi
  308. else
  309. for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/health_alarm_notify.conf" "${NETDATA_USER_CONFIG_DIR}/health_alarm_notify.conf"; do
  310. if [ -f "${CONFIG}" ]; then
  311. debug "Loading config file '${CONFIG}'..."
  312. source "${CONFIG}" || error "Failed to load config file '${CONFIG}'."
  313. else
  314. warning "Cannot find file '${CONFIG}'."
  315. fi
  316. done
  317. fi
  318. # If we didn't autodetect the character set for e-mail and it wasn't
  319. # set by the user, we need to set it to a reasonable default. UTF-8
  320. # should be correct for almost all modern UNIX systems.
  321. if [ -z ${EMAIL_CHARSET} ]; then
  322. EMAIL_CHARSET="UTF-8"
  323. fi
  324. # If we've been asked to use FQDN's for the URL's in the alarm, do so,
  325. # unless we're sending an alarm for a slave system which we can't get the
  326. # FQDN of easily.
  327. if [ "${use_fqdn}" = "YES" ] && [ "${host}" = "$(hostname -s 2>/dev/null)" ]; then
  328. host="$(hostname -f 2>/dev/null)"
  329. fi
  330. # -----------------------------------------------------------------------------
  331. # filter a recipient based on alarm event severity
  332. filter_recipient_by_criticality() {
  333. local method="${1}" x="${2}" r s
  334. shift
  335. r="${x/|*/}" # the recipient
  336. s="${x/*|/}" # the severity required for notifying this recipient
  337. # no severity filtering for this person
  338. [ "${r}" = "${s}" ] && return 0
  339. # the severity is invalid
  340. s="${s^^}"
  341. if [ "${s}" != "CRITICAL" ]; then
  342. error "SEVERITY FILTERING for ${x} VIA ${method}: invalid severity '${s,,}', only 'critical' is supported."
  343. return 0
  344. fi
  345. # create the status tracking directory for this user
  346. [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] &&
  347. mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
  348. case "${status}" in
  349. CRITICAL)
  350. # make sure he will get future notifications for this alarm too
  351. touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
  352. debug "SEVERITY FILTERING for ${x} VIA ${method}: ALLOW: the alarm is CRITICAL (will now receive next status change)"
  353. return 0
  354. ;;
  355. WARNING)
  356. if [ -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]; then
  357. # we do not remove the file, so that he will get future notifications of this alarm
  358. debug "SEVERITY FILTERING for ${x} VIA ${method}: ALLOW: recipient has been notified for this alarm in the past (will still receive next status change)"
  359. fi
  360. ;;
  361. *)
  362. if [ -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]; then
  363. # remove the file, so that he will only receive notifications for CRITICAL states for this alarm
  364. rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
  365. debug "SEVERITY FILTERING for ${x} VIA ${method}: ALLOW: recipient has been notified for this alarm (will only receive CRITICAL notifications from now on)"
  366. fi
  367. ;;
  368. esac
  369. debug "SEVERITY FILTERING for ${x} VIA ${method}: BLOCK: recipient should not receive this notification"
  370. return 1
  371. }
  372. # -----------------------------------------------------------------------------
  373. # verify the delivery methods supported
  374. # check slack
  375. [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
  376. # check rocketchat
  377. [ -z "${ROCKETCHAT_WEBHOOK_URL}" ] && SEND_ROCKETCHAT="NO"
  378. # check alerta
  379. [ -z "${ALERTA_WEBHOOK_URL}" ] && SEND_ALERTA="NO"
  380. # check flock
  381. [ -z "${FLOCK_WEBHOOK_URL}" ] && SEND_FLOCK="NO"
  382. # check discord
  383. [ -z "${DISCORD_WEBHOOK_URL}" ] && SEND_DISCORD="NO"
  384. # check pushover
  385. [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
  386. # check pushbullet
  387. [ -z "${PUSHBULLET_ACCESS_TOKEN}" ] && SEND_PUSHBULLET="NO"
  388. # check twilio
  389. { [ -z "${TWILIO_ACCOUNT_TOKEN}" ] || [ -z "${TWILIO_ACCOUNT_SID}" ] || [ -z "${TWILIO_NUMBER}" ]; } && SEND_TWILIO="NO"
  390. # check hipchat
  391. [ -z "${HIPCHAT_AUTH_TOKEN}" ] && SEND_HIPCHAT="NO"
  392. # check messagebird
  393. { [ -z "${MESSAGEBIRD_ACCESS_KEY}" ] || [ -z "${MESSAGEBIRD_NUMBER}" ]; } && SEND_MESSAGEBIRD="NO"
  394. # check kavenegar
  395. { [ -z "${KAVENEGAR_API_KEY}" ] || [ -z "${KAVENEGAR_SENDER}" ]; } && SEND_KAVENEGAR="NO"
  396. # check telegram
  397. [ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
  398. # check kafka
  399. { [ -z "${KAFKA_URL}" ] || [ -z "${KAFKA_SENDER_IP}" ]; } && SEND_KAFKA="NO"
  400. # check irc
  401. [ -z "${IRC_NETWORK}" ] && SEND_IRC="NO"
  402. # check hangouts
  403. [ ${#HANGOUTS_WEBHOOK_URI[@]} -eq 0 ] && SEND_HANGOUTS="NO"
  404. # check fleep
  405. #shellcheck disable=SC2153
  406. { [ -z "${FLEEP_SERVER}" ] || [ -z "${FLEEP_SENDER}" ]; } && SEND_FLEEP="NO"
  407. if [ "${SEND_PUSHOVER}" = "YES" ] ||
  408. [ "${SEND_SLACK}" = "YES" ] ||
  409. [ "${SEND_ROCKETCHAT}" = "YES" ] ||
  410. [ "${SEND_ALERTA}" = "YES" ] ||
  411. [ "${SEND_PD}" = "YES" ] ||
  412. [ "${SEND_FLOCK}" = "YES" ] ||
  413. [ "${SEND_DISCORD}" = "YES" ] ||
  414. [ "${SEND_HIPCHAT}" = "YES" ] ||
  415. [ "${SEND_TWILIO}" = "YES" ] ||
  416. [ "${SEND_MESSAGEBIRD}" = "YES" ] ||
  417. [ "${SEND_KAVENEGAR}" = "YES" ] ||
  418. [ "${SEND_TELEGRAM}" = "YES" ] ||
  419. [ "${SEND_PUSHBULLET}" = "YES" ] ||
  420. [ "${SEND_KAFKA}" = "YES" ] ||
  421. [ "${SEND_FLEEP}" = "YES" ] ||
  422. [ "${SEND_PROWL}" = "YES" ] ||
  423. [ "${SEND_HANGOUTS}" = "YES" ] ||
  424. [ "${SEND_CUSTOM}" = "YES" ] ||
  425. [ "${SEND_MSTEAM}" = "YES" ]; then
  426. # if we need curl, check for the curl command
  427. if [ -z "${curl}" ]; then
  428. curl="$(command -v curl 2>/dev/null)"
  429. fi
  430. if [ -z "${curl}" ]; then
  431. error "Cannot find curl command in the system path. Disabling all curl based notifications."
  432. SEND_PUSHOVER="NO"
  433. SEND_PUSHBULLET="NO"
  434. SEND_TELEGRAM="NO"
  435. SEND_SLACK="NO"
  436. SEND_MSTEAM="NO"
  437. SEND_ROCKETCHAT="NO"
  438. SEND_ALERTA="NO"
  439. SEND_PD="NO"
  440. SEND_FLOCK="NO"
  441. SEND_DISCORD="NO"
  442. SEND_TWILIO="NO"
  443. SEND_HIPCHAT="NO"
  444. SEND_MESSAGEBIRD="NO"
  445. SEND_KAVENEGAR="NO"
  446. SEND_KAFKA="NO"
  447. SEND_FLEEP="NO"
  448. SEND_PROWL="NO"
  449. SEND_HANGOUTS="NO"
  450. SEND_CUSTOM="NO"
  451. fi
  452. fi
  453. if [ "${SEND_SMS}" = "YES" ]; then
  454. if [ -z "${sendsms}" ]; then
  455. sendsms="$(command -v sendsms 2>/dev/null)"
  456. fi
  457. if [ -z "${sendsms}" ]; then
  458. SEND_SMS="NO"
  459. fi
  460. fi
  461. # if we need sendmail, check for the sendmail command
  462. if [ "${SEND_EMAIL}" = "YES" ] && [ -z "${sendmail}" ]; then
  463. sendmail="$(command -v sendmail 2>/dev/null)"
  464. if [ -z "${sendmail}" ]; then
  465. debug "Cannot find sendmail command in the system path. Disabling email notifications."
  466. SEND_EMAIL="NO"
  467. fi
  468. fi
  469. # if we need logger, check for the logger command
  470. if [ "${SEND_SYSLOG}" = "YES" ] && [ -z "${logger}" ]; then
  471. logger="$(command -v logger 2>/dev/null)"
  472. if [ -z "${logger}" ]; then
  473. debug "Cannot find logger command in the system path. Disabling syslog notifications."
  474. SEND_SYSLOG="NO"
  475. fi
  476. fi
  477. # if we need aws, check for the aws command
  478. if [ "${SEND_AWSSNS}" = "YES" ] && [ -z "${aws}" ]; then
  479. aws="$(command -v aws 2>/dev/null)"
  480. if [ -z "${aws}" ]; then
  481. debug "Cannot find aws command in the system path. Disabling Amazon SNS notifications."
  482. SEND_AWSSNS="NO"
  483. fi
  484. fi
  485. # -----------------------------------------------------------------------------
  486. # find the recipients' addresses per method
  487. # netdata may call us with multiple roles, and roles may have multiple but
  488. # overlapping recipients - so, here we find the unique recipients.
  489. for method_name in ${method_names}; do
  490. send_var="SEND_${method_name^^}"
  491. if [ "${!send_var}" = "NO" ]; then
  492. continue
  493. fi
  494. declare -A arr_var=()
  495. for x in ${roles//,/ }; do
  496. # the roles 'silent' and 'disabled' mean:
  497. # don't send a notification for this role
  498. if [ "${x}" = "silent" ] || [ "${x}" = "disabled" ]; then
  499. continue
  500. fi
  501. role_recipients="role_recipients_${method_name}[$x]"
  502. default_recipient_var="DEFAULT_RECIPIENT_${method_name^^}"
  503. a="${!role_recipients}"
  504. [ -z "${a}" ] && a="${!default_recipient_var}"
  505. for r in ${a//,/ }; do
  506. [ "${r}" != "disabled" ] && filter_recipient_by_criticality ${method_name} "${r}" && arr_var[${r/|*/}]="1"
  507. done
  508. done
  509. # build the list of recipients
  510. to_var="to_${method_name}"
  511. declare to_${method_name}="${!arr_var[*]}"
  512. [ -z "${!to_var}" ] && declare ${send_var}="NO"
  513. done
  514. # -----------------------------------------------------------------------------
  515. # handle fixup of the email recipient list.
  516. fix_to_email() {
  517. to_email=
  518. while [ -n "${1}" ]; do
  519. [ -n "${to_email}" ] && to_email="${to_email}, "
  520. to_email="${to_email}${1}"
  521. shift 1
  522. done
  523. }
  524. # ${to_email} without quotes here
  525. fix_to_email ${to_email}
  526. # -----------------------------------------------------------------------------
  527. # handle output if we're running in unit test mode
  528. if [ ${unittest} ]; then
  529. for method_name in ${method_names}; do
  530. to_var="to_${method_name}"
  531. echo "results: ${method_name}: ${!to_var}"
  532. done
  533. exit 0
  534. fi
  535. # -----------------------------------------------------------------------------
  536. # check that we have at least a method enabled
  537. proceed=0
  538. for method in "${SEND_EMAIL}" \
  539. "${SEND_PUSHOVER}" \
  540. "${SEND_TELEGRAM}" \
  541. "${SEND_SLACK}" \
  542. "${SEND_ROCKETCHAT}" \
  543. "${SEND_ALERTA}" \
  544. "${SEND_FLOCK}" \
  545. "${SEND_DISCORD}" \
  546. "${SEND_TWILIO}" \
  547. "${SEND_HIPCHAT}" \
  548. "${SEND_MESSAGEBIRD}" \
  549. "${SEND_KAVENEGAR}" \
  550. "${SEND_PUSHBULLET}" \
  551. "${SEND_KAFKA}" \
  552. "${SEND_PD}" \
  553. "${SEND_FLEEP}" \
  554. "${SEND_PROWL}" \
  555. "${SEND_CUSTOM}" \
  556. "${SEND_IRC}" \
  557. "${SEND_HANGOUTS}" \
  558. "${SEND_AWSSNS}" \
  559. "${SEND_SYSLOG}" \
  560. "${SEND_SMS}" \
  561. "${SEND_MSTEAM}"; do
  562. if [ "${method}" == "YES" ]; then
  563. proceed=1
  564. break
  565. fi
  566. done
  567. if [ "$proceed" -eq 0 ]; then
  568. fatal "All notification methods are disabled. Not sending notification for host '${host}', chart '${chart}' to '${roles}' for '${name}' = '${value}' for status '${status}'."
  569. fi
  570. # -----------------------------------------------------------------------------
  571. # get the date the alarm happened
  572. date=$(date --date=@${when} "${date_format}" 2>/dev/null)
  573. [ -z "${date}" ] && date=$(date "${date_format}" 2>/dev/null)
  574. [ -z "${date}" ] && date=$(date --date=@${when} 2>/dev/null)
  575. [ -z "${date}" ] && date=$(date 2>/dev/null)
  576. # ----------------------------------------------------------------------------
  577. # prepare some extra headers if we've been asked to thread e-mails
  578. if [ "${SEND_EMAIL}" == "YES" ] && [ "${EMAIL_THREADING}" != "NO" ]; then
  579. email_thread_headers="In-Reply-To: <${chart}-${name}@${host}>\\r\\nReferences: <${chart}-${name}@${host}>"
  580. else
  581. email_thread_headers=
  582. fi
  583. # -----------------------------------------------------------------------------
  584. # function to URL encode a string
  585. urlencode() {
  586. local string="${1}" strlen encoded pos c o
  587. strlen=${#string}
  588. for ((pos = 0; pos < strlen; pos++)); do
  589. c=${string:pos:1}
  590. case "${c}" in
  591. [-_.~a-zA-Z0-9])
  592. o="${c}"
  593. ;;
  594. *)
  595. printf -v o '%%%02x' "'${c}"
  596. ;;
  597. esac
  598. encoded+="${o}"
  599. done
  600. REPLY="${encoded}"
  601. echo "${REPLY}"
  602. }
  603. # -----------------------------------------------------------------------------
  604. # function to convert a duration in seconds, to a human readable duration
  605. # using DAYS, MINUTES, SECONDS
  606. duration4human() {
  607. local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
  608. d=$((s / 86400))
  609. s=$((s - (d * 86400)))
  610. h=$((s / 3600))
  611. s=$((s - (h * 3600)))
  612. m=$((s / 60))
  613. s=$((s - (m * 60)))
  614. if [ ${d} -gt 0 ]; then
  615. [ ${m} -ge 30 ] && h=$((h + 1))
  616. [ ${d} -gt 1 ] && ds="days"
  617. [ ${h} -gt 1 ] && hs="hours"
  618. if [ ${h} -gt 0 ]; then
  619. ret="${d} ${ds} and ${h} ${hs}"
  620. else
  621. ret="${d} ${ds}"
  622. fi
  623. elif [ ${h} -gt 0 ]; then
  624. [ ${s} -ge 30 ] && m=$((m + 1))
  625. [ ${h} -gt 1 ] && hs="hours"
  626. [ ${m} -gt 1 ] && ms="minutes"
  627. if [ ${m} -gt 0 ]; then
  628. ret="${h} ${hs} and ${m} ${ms}"
  629. else
  630. ret="${h} ${hs}"
  631. fi
  632. elif [ ${m} -gt 0 ]; then
  633. [ ${m} -gt 1 ] && ms="minutes"
  634. [ ${s} -gt 1 ] && ss="seconds"
  635. if [ ${s} -gt 0 ]; then
  636. ret="${m} ${ms} and ${s} ${ss}"
  637. else
  638. ret="${m} ${ms}"
  639. fi
  640. else
  641. [ ${s} -gt 1 ] && ss="seconds"
  642. ret="${s} ${ss}"
  643. fi
  644. REPLY="${ret}"
  645. echo "${REPLY}"
  646. }
  647. # -----------------------------------------------------------------------------
  648. # email sender
  649. send_email() {
  650. local ret opts=() sender_email="${EMAIL_SENDER}" sender_name=
  651. if [ "${SEND_EMAIL}" = "YES" ]; then
  652. if [ -n "${EMAIL_SENDER}" ]; then
  653. if [[ ${EMAIL_SENDER} =~ ^\".*\"\ \<.*\>$ ]]; then
  654. # the name includes double quotes
  655. sender_email="$(echo "${EMAIL_SENDER}" | cut -d '<' -f 2 | cut -d '>' -f 1)"
  656. sender_name="$(echo "${EMAIL_SENDER}" | cut -d '"' -f 2)"
  657. elif [[ ${EMAIL_SENDER} =~ ^\'.*\'\ \<.*\>$ ]]; then
  658. # the name includes single quotes
  659. sender_email="$(echo "${EMAIL_SENDER}" | cut -d '<' -f 2 | cut -d '>' -f 1)"
  660. sender_name="$(echo "${EMAIL_SENDER}" | cut -d "'" -f 2)"
  661. elif [[ ${EMAIL_SENDER} =~ ^.*\ \<.*\>$ ]]; then
  662. # the name does not have any quotes
  663. sender_email="$(echo "${EMAIL_SENDER}" | cut -d '<' -f 2 | cut -d '>' -f 1)"
  664. sender_name="$(echo "${EMAIL_SENDER}" | cut -d '<' -f 1)"
  665. fi
  666. fi
  667. [ -n "${sender_email}" ] && opts+=(-f "${sender_email}")
  668. [ -n "${sender_name}" ] && opts+=(-F "${sender_name}")
  669. if [ "${debug}" = "1" ]; then
  670. echo >&2 "--- BEGIN sendmail command ---"
  671. printf >&2 "%q " "${sendmail}" -t "${opts[@]}"
  672. echo >&2
  673. echo >&2 "--- END sendmail command ---"
  674. fi
  675. "${sendmail}" -t "${opts[@]}"
  676. ret=$?
  677. if [ ${ret} -eq 0 ]; then
  678. info "sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
  679. return 0
  680. else
  681. error "failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
  682. return 1
  683. fi
  684. fi
  685. return 1
  686. }
  687. # -----------------------------------------------------------------------------
  688. # pushover sender
  689. send_pushover() {
  690. local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
  691. if [ "${SEND_PUSHOVER}" = "YES" ] && [ -n "${apptoken}" ] && [ -n "${usertokens}" ] && [ -n "${title}" ] && [ -n "${message}" ]; then
  692. # https://pushover.net/api
  693. priority=-2
  694. case "${status}" in
  695. CLEAR) priority=-1 ;; # low priority: no sound or vibration
  696. WARNING) priority=0 ;; # normal priority: respect quiet hours
  697. CRITICAL) priority=1 ;; # high priority: bypass quiet hours
  698. *) priority=-2 ;; # lowest priority: no notification at all
  699. esac
  700. for user in ${usertokens}; do
  701. httpcode=$(docurl \
  702. --form-string "token=${apptoken}" \
  703. --form-string "user=${user}" \
  704. --form-string "html=1" \
  705. --form-string "title=${title}" \
  706. --form-string "message=${message}" \
  707. --form-string "timestamp=${when}" \
  708. --form-string "url=${url}" \
  709. --form-string "url_title=Open netdata dashboard to view the alarm" \
  710. --form-string "priority=${priority}" \
  711. https://api.pushover.net/1/messages.json)
  712. if [ "${httpcode}" = "200" ]; then
  713. info "sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
  714. sent=$((sent + 1))
  715. else
  716. error "failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
  717. fi
  718. done
  719. [ ${sent} -gt 0 ] && return 0
  720. fi
  721. return 1
  722. }
  723. # -----------------------------------------------------------------------------
  724. # pushbullet sender
  725. send_pushbullet() {
  726. local userapikey="${1}" source_device="${2}" recipients="${3}" url="${4}" title="${5}" message="${6}" httpcode sent=0 user
  727. if [ "${SEND_PUSHBULLET}" = "YES" ] && [ -n "${userapikey}" ] && [ -n "${recipients}" ] && [ -n "${message}" ] && [ -n "${title}" ]; then
  728. #https://docs.pushbullet.com/#create-push
  729. for user in ${recipients}; do
  730. httpcode=$(docurl \
  731. --header 'Access-Token: '${userapikey}'' \
  732. --header 'Content-Type: application/json' \
  733. --data-binary @<(
  734. cat <<EOF
  735. {"title": "${title}",
  736. "type": "link",
  737. "email": "${user}",
  738. "body": "$(echo -n ${message})",
  739. "url": "${url}",
  740. "source_device_iden": "${source_device}"}
  741. EOF
  742. ) "https://api.pushbullet.com/v2/pushes" -X POST)
  743. if [ "${httpcode}" = "200" ]; then
  744. info "sent pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
  745. sent=$((sent + 1))
  746. else
  747. error "failed to send pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
  748. fi
  749. done
  750. [ ${sent} -gt 0 ] && return 0
  751. fi
  752. return 1
  753. }
  754. # -----------------------------------------------------------------------------
  755. # kafka sender
  756. send_kafka() {
  757. local httpcode sent=0
  758. if [ "${SEND_KAFKA}" = "YES" ]; then
  759. httpcode=$(docurl -X POST \
  760. --data "{host_ip:\"${KAFKA_SENDER_IP}\",when:${when},name:\"${name}\",chart:\"${chart}\",family:\"${family}\",status:\"${status}\",old_status:\"${old_status}\",value:${value},old_value:${old_value},duration:${duration},non_clear_duration:${non_clear_duration},units:\"${units}\",info:\"${info}\"}" \
  761. "${KAFKA_URL}")
  762. if [ "${httpcode}" = "204" ]; then
  763. info "sent kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}'"
  764. sent=$((sent + 1))
  765. else
  766. error "failed to send kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}' with HTTP error code ${httpcode}."
  767. fi
  768. [ ${sent} -gt 0 ] && return 0
  769. fi
  770. return 1
  771. }
  772. # -----------------------------------------------------------------------------
  773. # pagerduty.com sender
  774. send_pd() {
  775. local recipients="${1}" sent=0
  776. unset t
  777. case ${status} in
  778. CLEAR) t='resolve' ;;
  779. WARNING) t='trigger' ;;
  780. CRITICAL) t='trigger' ;;
  781. esac
  782. if [ ${SEND_PD} = "YES" ] && [ -n "${t}" ]; then
  783. for PD_SERVICE_KEY in ${recipients}; do
  784. d="${status} ${name} = ${value_string} - ${host}, ${family}"
  785. payload="$(
  786. cat <<EOF
  787. {
  788. "service_key": "${PD_SERVICE_KEY}",
  789. "event_type": "${t}",
  790. "incident_key" : "${alarm_id}",
  791. "description": "${d}",
  792. "details": {
  793. "value_w_units": "${value_string}",
  794. "when": "${when}",
  795. "duration" : "${duration}",
  796. "roles": "${roles}",
  797. "alarm_id" : "${alarm_id}",
  798. "name" : "${name}",
  799. "chart" : "${chart}",
  800. "family" : "${family}",
  801. "status" : "${status}",
  802. "old_status" : "${old_status}",
  803. "value" : "${value}",
  804. "old_value" : "${old_value}",
  805. "src" : "${src}",
  806. "non_clear_duration" : "${non_clear_duration}",
  807. "units" : "${units}",
  808. "info" : "${info}"
  809. }
  810. }
  811. EOF
  812. )"
  813. httpcode=$(docurl -X POST --data "${payload}" "https://events.pagerduty.com/generic/2010-04-15/create_event.json")
  814. if [ "${httpcode}" = "200" ]; then
  815. info "sent pagerduty notification for: ${host} ${chart}.${name} is ${status}'"
  816. sent=$((sent + 1))
  817. else
  818. error "failed to send pagerduty notification for: ${host} ${chart}.${name} is ${status}, with HTTP error code ${httpcode}."
  819. fi
  820. done
  821. [ ${sent} -gt 0 ] && return 0
  822. fi
  823. return 1
  824. }
  825. # -----------------------------------------------------------------------------
  826. # twilio sender
  827. send_twilio() {
  828. local accountsid="${1}" accounttoken="${2}" twilionumber="${3}" recipients="${4}" title="${5}" message="${6}" httpcode sent=0 user
  829. if [ "${SEND_TWILIO}" = "YES" ] && [ -n "${accountsid}" ] && [ -n "${accounttoken}" ] && [ -n "${twilionumber}" ] && [ -n "${recipients}" ] && [ -n "${message}" ] && [ -n "${title}" ]; then
  830. #https://www.twilio.com/packages/labs/code/bash/twilio-sms
  831. for user in ${recipients}; do
  832. httpcode=$(docurl -X POST \
  833. --data-urlencode "From=${twilionumber}" \
  834. --data-urlencode "To=${user}" \
  835. --data-urlencode "Body=${title} ${message}" \
  836. -u "${accountsid}:${accounttoken}" \
  837. "https://api.twilio.com/2010-04-01/Accounts/${accountsid}/Messages.json")
  838. if [ "${httpcode}" = "201" ]; then
  839. info "sent Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
  840. sent=$((sent + 1))
  841. else
  842. error "failed to send Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
  843. fi
  844. done
  845. [ ${sent} -gt 0 ] && return 0
  846. fi
  847. return 1
  848. }
  849. # -----------------------------------------------------------------------------
  850. # hipchat sender
  851. send_hipchat() {
  852. local authtoken="${1}" recipients="${2}" message="${3}" httpcode sent=0 room color msg_format notify
  853. # remove <small></small> from the message
  854. message="${message//<small>/}"
  855. message="${message//<\/small>/}"
  856. if [ "${SEND_HIPCHAT}" = "YES" ] && [ -n "${HIPCHAT_SERVER}" ] && [ -n "${authtoken}" ] && [ -n "${recipients}" ] && [ -n "${message}" ]; then
  857. # Valid values: html, text.
  858. # Defaults to 'html'.
  859. msg_format="html"
  860. # Background color for message. Valid values: yellow, green, red, purple, gray, random. Defaults to 'yellow'.
  861. case "${status}" in
  862. WARNING) color="yellow" ;;
  863. CRITICAL) color="red" ;;
  864. CLEAR) color="green" ;;
  865. *) color="gray" ;;
  866. esac
  867. # Whether this message should trigger a user notification (change the tab color, play a sound, notify mobile phones, etc).
  868. # Each recipient's notification preferences are taken into account.
  869. # Defaults to false.
  870. notify="true"
  871. for room in ${recipients}; do
  872. httpcode=$(docurl -X POST \
  873. -H "Content-type: application/json" \
  874. -H "Authorization: Bearer ${authtoken}" \
  875. -d "{\"color\": \"${color}\", \"from\": \"${host}\", \"message_format\": \"${msg_format}\", \"message\": \"${message}\", \"notify\": \"${notify}\"}" \
  876. "https://${HIPCHAT_SERVER}/v2/room/${room}/notification")
  877. if [ "${httpcode}" = "204" ]; then
  878. info "sent HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
  879. sent=$((sent + 1))
  880. else
  881. error "failed to send HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}' with HTTP error code ${httpcode}."
  882. fi
  883. done
  884. [ ${sent} -gt 0 ] && return 0
  885. fi
  886. return 1
  887. }
  888. # -----------------------------------------------------------------------------
  889. # messagebird sender
  890. send_messagebird() {
  891. local accesskey="${1}" messagebirdnumber="${2}" recipients="${3}" title="${4}" message="${5}" httpcode sent=0 user
  892. if [ "${SEND_MESSAGEBIRD}" = "YES" ] && [ -n "${accesskey}" ] && [ -n "${messagebirdnumber}" ] && [ -n "${recipients}" ] && [ -n "${message}" ] && [ -n "${title}" ]; then
  893. #https://developers.messagebird.com/docs/messaging
  894. for user in ${recipients}; do
  895. httpcode=$(docurl -X POST \
  896. --data-urlencode "originator=${messagebirdnumber}" \
  897. --data-urlencode "recipients=${user}" \
  898. --data-urlencode "body=${title} ${message}" \
  899. --data-urlencode "datacoding=auto" \
  900. -H "Authorization: AccessKey ${accesskey}" \
  901. "https://rest.messagebird.com/messages")
  902. if [ "${httpcode}" = "201" ]; then
  903. info "sent Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
  904. sent=$((sent + 1))
  905. else
  906. error "failed to send Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
  907. fi
  908. done
  909. [ ${sent} -gt 0 ] && return 0
  910. fi
  911. return 1
  912. }
  913. # -----------------------------------------------------------------------------
  914. # kavenegar sender
  915. send_kavenegar() {
  916. local API_KEY="${1}" kavenegarsender="${2}" recipients="${3}" title="${4}" message="${5}" httpcode sent=0 user
  917. if [ "${SEND_KAVENEGAR}" = "YES" ] && [ -n "${API_KEY}" ] && [ -n "${kavenegarsender}" ] && [ -n "${recipients}" ] && [ -n "${message}" ] && [ -n "${title}" ]; then
  918. # http://api.kavenegar.com/v1/{API-KEY}/sms/send.json
  919. for user in ${recipients}; do
  920. httpcode=$(docurl -X POST http://api.kavenegar.com/v1/${API_KEY}/sms/send.json \
  921. --data-urlencode "sender=${kavenegarsender}" \
  922. --data-urlencode "receptor=${user}" \
  923. --data-urlencode "message=${title} ${message}")
  924. if [ "${httpcode}" = "200" ]; then
  925. info "sent Kavenegar SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
  926. sent=$((sent + 1))
  927. else
  928. error "failed to send Kavenegar SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
  929. fi
  930. done
  931. [ ${sent} -gt 0 ] && return 0
  932. fi
  933. return 1
  934. }
  935. # -----------------------------------------------------------------------------
  936. # telegram sender
  937. send_telegram() {
  938. local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid emoji disableNotification=""
  939. if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
  940. case "${status}" in
  941. WARNING) emoji="⚠️" ;;
  942. CRITICAL) emoji="🔴" ;;
  943. CLEAR) emoji="✅" ;;
  944. *) emoji="⚪️" ;;
  945. esac
  946. if [ "${SEND_TELEGRAM}" = "YES" ] && [ -n "${bottoken}" ] && [ -n "${chatids}" ] && [ -n "${message}" ]; then
  947. for chatid in ${chatids}; do
  948. # https://core.telegram.org/bots/api#sendmessage
  949. httpcode=$(docurl ${disableNotification} \
  950. --data-urlencode "parse_mode=HTML" \
  951. --data-urlencode "disable_web_page_preview=true" \
  952. --data-urlencode "text=${emoji} ${message}" \
  953. "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${chatid}")
  954. if [ "${httpcode}" = "200" ]; then
  955. info "sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
  956. sent=$((sent + 1))
  957. elif [ "${httpcode}" = "401" ]; then
  958. error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
  959. else
  960. error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
  961. fi
  962. done
  963. [ ${sent} -gt 0 ] && return 0
  964. fi
  965. return 1
  966. }
  967. # -----------------------------------------------------------------------------
  968. # Microsoft Team sender
  969. send_msteam() {
  970. local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
  971. [ "${SEND_MSTEAM}" != "YES" ] && return 1
  972. case "${status}" in
  973. WARNING) icon="${MSTEAM_ICON_WARNING}" && color="${MSTEAM_COLOR_WARNING}" ;;
  974. CRITICAL) icon="${MSTEAM_ICON_CRITICAL}" && color="${MSTEAM_COLOR_CRITICAL}" ;;
  975. CLEAR) icon="${MSTEAM_ICON_CLEAR}" && color="${MSTEAM_COLOR_CLEAR}" ;;
  976. *) icon="${MSTEAM_ICON_DEFAULT}" && color="${MSTEAM_COLOR_DEFAULT}" ;;
  977. esac
  978. for channel in ${channels}; do
  979. ## More details are available here regarding the payload syntax options : https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference
  980. ## Online designer : https://acdesignerbeta.azurewebsites.net/
  981. payload="$(
  982. cat <<EOF
  983. {
  984. "@context": "http://schema.org/extensions",
  985. "@type": "MessageCard",
  986. "themeColor": "${color}",
  987. "title": "$icon Alert ${status} from netdata for ${host}",
  988. "text": "${host} ${status_message}, ${chart} (_${family}_), *${alarm}*",
  989. "potentialAction": [
  990. {
  991. "@type": "OpenUri",
  992. "name": "Netdata",
  993. "targets": [
  994. { "os": "default", "uri": "${goto_url}" }
  995. ]
  996. }
  997. ]
  998. }
  999. EOF
  1000. )"
  1001. # Replacing in the webhook CHANNEL string by the MS Teams channel name from conf file.
  1002. webhook="${webhook//CHANNEL/${channel}}"
  1003. httpcode=$(docurl -H "Content-Type: application/json" -d "${payload}" "${webhook}")
  1004. if [ "${httpcode}" = "200" ]; then
  1005. info "sent Microsoft team notification for: ${host} ${chart}.${name} is ${status} to '${webhook}'"
  1006. sent=$((sent + 1))
  1007. else
  1008. error "failed to send Microsoft team notification for: ${host} ${chart}.${name} is ${status} to '${webhook}', with HTTP error code ${httpcode}."
  1009. fi
  1010. done
  1011. [ ${sent} -gt 0 ] && return 0
  1012. return 1
  1013. }
  1014. # slack sender
  1015. send_slack() {
  1016. local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
  1017. [ "${SEND_SLACK}" != "YES" ] && return 1
  1018. case "${status}" in
  1019. WARNING) color="warning" ;;
  1020. CRITICAL) color="danger" ;;
  1021. CLEAR) color="good" ;;
  1022. *) color="#777777" ;;
  1023. esac
  1024. for channel in ${channels}; do
  1025. # Default entry in the recipient is without a hash in front (backwards-compatible). Accept specification of channel or user.
  1026. if [ "${channel::1}" != "#" ] && [ "${channel::1}" != "@" ]; then channel="#$channel"; fi
  1027. # If channel is equal to "#" then do not send the channel attribute at all. Slack also defines channels and users in webhooks.
  1028. if [ "${channel}" = "#" ]; then
  1029. ch=""
  1030. chstr="without specifying a channel"
  1031. else
  1032. ch="\"channel\": \"${channel}\","
  1033. chstr="to '${channel}'"
  1034. fi
  1035. payload="$(
  1036. cat <<EOF
  1037. {
  1038. $ch
  1039. "username": "netdata on ${host}",
  1040. "icon_url": "${images_base_url}/images/banner-icon-144x144.png",
  1041. "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
  1042. "attachments": [
  1043. {
  1044. "fallback": "${alarm} - ${chart} (${family}) - ${info}",
  1045. "color": "${color}",
  1046. "title": "${alarm}",
  1047. "title_link": "${goto_url}",
  1048. "text": "${info}",
  1049. "fields": [
  1050. {
  1051. "title": "${chart}",
  1052. "short": true
  1053. },
  1054. {
  1055. "title": "${family}",
  1056. "short": true
  1057. }
  1058. ],
  1059. "thumb_url": "${image}",
  1060. "footer": "by ${host}",
  1061. "ts": ${when}
  1062. }
  1063. ]
  1064. }
  1065. EOF
  1066. )"
  1067. httpcode=$(docurl -X POST --data-urlencode "payload=${payload}" "${webhook}")
  1068. if [ "${httpcode}" = "200" ]; then
  1069. info "sent slack notification for: ${host} ${chart}.${name} is ${status} ${chstr}"
  1070. sent=$((sent + 1))
  1071. else
  1072. error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} ${chstr}, with HTTP error code ${httpcode}."
  1073. fi
  1074. done
  1075. [ ${sent} -gt 0 ] && return 0
  1076. return 1
  1077. }
  1078. # -----------------------------------------------------------------------------
  1079. # rocketchat sender
  1080. send_rocketchat() {
  1081. local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
  1082. [ "${SEND_ROCKETCHAT}" != "YES" ] && return 1
  1083. case "${status}" in
  1084. WARNING) color="warning" ;;
  1085. CRITICAL) color="danger" ;;
  1086. CLEAR) color="good" ;;
  1087. *) color="#777777" ;;
  1088. esac
  1089. for channel in ${channels}; do
  1090. payload="$(
  1091. cat <<EOF
  1092. {
  1093. "channel": "#${channel}",
  1094. "alias": "netdata on ${host}",
  1095. "avatar": "${images_base_url}/images/banner-icon-144x144.png",
  1096. "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
  1097. "attachments": [
  1098. {
  1099. "color": "${color}",
  1100. "title": "${alarm}",
  1101. "title_link": "${goto_url}",
  1102. "text": "${info}",
  1103. "fields": [
  1104. {
  1105. "title": "${chart}",
  1106. "short": true,
  1107. "value": "chart"
  1108. },
  1109. {
  1110. "title": "${family}",
  1111. "short": true,
  1112. "value": "family"
  1113. }
  1114. ],
  1115. "thumb_url": "${image}",
  1116. "ts": "${when}"
  1117. }
  1118. ]
  1119. }
  1120. EOF
  1121. )"
  1122. httpcode=$(docurl -X POST --data-urlencode "payload=${payload}" "${webhook}")
  1123. if [ "${httpcode}" = "200" ]; then
  1124. info "sent rocketchat notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
  1125. sent=$((sent + 1))
  1126. else
  1127. error "failed to send rocketchat notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
  1128. fi
  1129. done
  1130. [ ${sent} -gt 0 ] && return 0
  1131. return 1
  1132. }
  1133. # -----------------------------------------------------------------------------
  1134. # alerta sender
  1135. send_alerta() {
  1136. local webhook="${1}" channels="${2}" httpcode sent=0 channel severity resource event payload auth
  1137. [ "${SEND_ALERTA}" != "YES" ] && return 1
  1138. case "${status}" in
  1139. CRITICAL) severity="critical" ;;
  1140. WARNING) severity="warning" ;;
  1141. CLEAR) severity="cleared" ;;
  1142. *) severity="indeterminate" ;;
  1143. esac
  1144. if [[ ${chart} == httpcheck* ]]; then
  1145. resource=$chart
  1146. event=$name
  1147. else
  1148. resource="${host}:${family}"
  1149. event="${chart}.${name}"
  1150. fi
  1151. for channel in ${channels}; do
  1152. payload="$(
  1153. cat <<EOF
  1154. {
  1155. "resource": "${resource}",
  1156. "event": "${event}",
  1157. "environment": "${channel}",
  1158. "severity": "${severity}",
  1159. "service": ["Netdata"],
  1160. "group": "Performance",
  1161. "value": "${value_string}",
  1162. "text": "${info}",
  1163. "tags": ["alarm_id:${alarm_id}"],
  1164. "attributes": {
  1165. "roles": "${roles}",
  1166. "name": "${name}",
  1167. "chart": "${chart}",
  1168. "family": "${family}",
  1169. "source": "${src}",
  1170. "moreInfo": "<a href=\"${goto_url}\">View Netdata</a>"
  1171. },
  1172. "origin": "netdata/${host}",
  1173. "type": "netdataAlarm",
  1174. "rawData": "${BASH_ARGV[@]}"
  1175. }
  1176. EOF
  1177. )"
  1178. if [ -n "${ALERTA_API_KEY}" ]; then
  1179. auth="Key ${ALERTA_API_KEY}"
  1180. fi
  1181. httpcode=$(docurl -X POST "${webhook}/alert" -H "Content-Type: application/json" -H "Authorization: $auth" --data "${payload}")
  1182. if [ "${httpcode}" = "200" ] || [ "${httpcode}" = "201" ]; then
  1183. info "sent alerta notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
  1184. sent=$((sent + 1))
  1185. elif [ "${httpcode}" = "202" ]; then
  1186. info "suppressed alerta notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
  1187. else
  1188. error "failed to send alerta notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
  1189. fi
  1190. done
  1191. [ ${sent} -gt 0 ] && return 0
  1192. return 1
  1193. }
  1194. # -----------------------------------------------------------------------------
  1195. # flock sender
  1196. send_flock() {
  1197. local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
  1198. [ "${SEND_FLOCK}" != "YES" ] && return 1
  1199. case "${status}" in
  1200. WARNING) color="warning" ;;
  1201. CRITICAL) color="danger" ;;
  1202. CLEAR) color="good" ;;
  1203. *) color="#777777" ;;
  1204. esac
  1205. for channel in ${channels}; do
  1206. httpcode=$(docurl -X POST "${webhook}" -H "Content-Type: application/json" -d "{
  1207. \"sendAs\": {
  1208. \"name\" : \"netdata on ${host}\",
  1209. \"profileImage\" : \"${images_base_url}/images/banner-icon-144x144.png\"
  1210. },
  1211. \"text\": \"${host} *${status_message}*\",
  1212. \"timestamp\": \"${when}\",
  1213. \"attachments\": [
  1214. {
  1215. \"description\": \"${chart} (${family}) - ${info}\",
  1216. \"color\": \"${color}\",
  1217. \"title\": \"${alarm}\",
  1218. \"url\": \"${goto_url}\",
  1219. \"text\": \"${info}\",
  1220. \"views\": {
  1221. \"image\": {
  1222. \"original\": { \"src\": \"${image}\", \"width\": 400, \"height\": 400 },
  1223. \"thumbnail\": { \"src\": \"${image}\", \"width\": 50, \"height\": 50 },
  1224. \"filename\": \"${image}\"
  1225. }
  1226. }
  1227. }
  1228. ]
  1229. }")
  1230. if [ "${httpcode}" = "200" ]; then
  1231. info "sent flock notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
  1232. sent=$((sent + 1))
  1233. else
  1234. error "failed to send flock notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
  1235. fi
  1236. done
  1237. [ ${sent} -gt 0 ] && return 0
  1238. return 1
  1239. }
  1240. # -----------------------------------------------------------------------------
  1241. # discord sender
  1242. send_discord() {
  1243. local webhook="${1}/slack" channels="${2}" httpcode sent=0 channel color payload username
  1244. [ "${SEND_DISCORD}" != "YES" ] && return 1
  1245. case "${status}" in
  1246. WARNING) color="warning" ;;
  1247. CRITICAL) color="danger" ;;
  1248. CLEAR) color="good" ;;
  1249. *) color="#777777" ;;
  1250. esac
  1251. for channel in ${channels}; do
  1252. username="netdata on ${host}"
  1253. [ ${#username} -gt 32 ] && username="${username:0:29}..."
  1254. payload="$(
  1255. cat <<EOF
  1256. {
  1257. "channel": "#${channel}",
  1258. "username": "${username}",
  1259. "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
  1260. "icon_url": "${images_base_url}/images/banner-icon-144x144.png",
  1261. "attachments": [
  1262. {
  1263. "color": "${color}",
  1264. "title": "${alarm}",
  1265. "title_link": "${goto_url}",
  1266. "text": "${info}",
  1267. "fields": [
  1268. {
  1269. "title": "${chart}",
  1270. "value": "${family}"
  1271. }
  1272. ],
  1273. "thumb_url": "${image}",
  1274. "footer_icon": "${images_base_url}/images/banner-icon-144x144.png",
  1275. "footer": "${host}",
  1276. "ts": ${when}
  1277. }
  1278. ]
  1279. }
  1280. EOF
  1281. )"
  1282. httpcode=$(docurl -X POST --data-urlencode "payload=${payload}" "${webhook}")
  1283. if [ "${httpcode}" = "200" ]; then
  1284. info "sent discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
  1285. sent=$((sent + 1))
  1286. else
  1287. error "failed to send discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
  1288. fi
  1289. done
  1290. [ ${sent} -gt 0 ] && return 0
  1291. return 1
  1292. }
  1293. # -----------------------------------------------------------------------------
  1294. # fleep sender
  1295. send_fleep() {
  1296. local httpcode sent=0 webhooks="${1}" data message
  1297. if [ "${SEND_FLEEP}" = "YES" ]; then
  1298. message="${host} ${status_message}, \`${chart}\` (${family}), *${alarm}*\\n${info}"
  1299. for hook in ${webhooks}; do
  1300. data="{ "
  1301. data="${data} 'message': '${message}', "
  1302. data="${data} 'user': '${FLEEP_SENDER}' "
  1303. data="${data} }"
  1304. httpcode=$(docurl -X POST --data "${data}" "https://fleep.io/hook/${hook}")
  1305. if [ "${httpcode}" = "200" ]; then
  1306. info "sent fleep data for: ${host} ${chart}.${name} is ${status} and user '${FLEEP_SENDER}'"
  1307. sent=$((sent + 1))
  1308. else
  1309. error "failed to send fleep data for: ${host} ${chart}.${name} is ${status} and user '${FLEEP_SENDER}' with HTTP error code ${httpcode}."
  1310. fi
  1311. done
  1312. [ ${sent} -gt 0 ] && return 0
  1313. fi
  1314. return 1
  1315. }
  1316. # -----------------------------------------------------------------------------
  1317. # Prowl sender
  1318. send_prowl() {
  1319. local httpcode sent=0 data message keys prio=0 alarm_url event
  1320. if [ "${SEND_PROWL}" = "YES" ]; then
  1321. message="$(urlencode "${host} ${status_message}, \`${chart}\` (${family}), *${alarm}*\\n${info}")"
  1322. message="description=${message}"
  1323. keys="$(urlencode "$(echo "${1}" | tr ' ' ,)")"
  1324. keys="apikey=${keys}"
  1325. app="application=Netdata"
  1326. case "${status}" in
  1327. CRITICAL)
  1328. prio=2
  1329. ;;
  1330. WARNING)
  1331. prio=1
  1332. ;;
  1333. esac
  1334. prio="priority=${prio}"
  1335. alarm_url="$(urlencode ${goto_url})"
  1336. alarm_url="url=${alarm_url}"
  1337. event="$(urlencode "${host} ${status_message}")"
  1338. event="event=${event}"
  1339. data="${keys}&${prio}&${alarm_url}&${app}&${event}&${message}"
  1340. httpcode=$(docurl -X POST --data "${data}" "https://api.prowlapp.com/publicapi/add")
  1341. if [ "${httpcode}" = "200" ]; then
  1342. info "sent prowl data for: ${host} ${chart}.${name} is ${status}"
  1343. sent=1
  1344. else
  1345. error "failed to send prowl data for: ${host} ${chart}.${name} is ${status} with with error code ${httpcode}."
  1346. fi
  1347. [ ${sent} -gt 0 ] && return 0
  1348. fi
  1349. return 1
  1350. }
  1351. # -----------------------------------------------------------------------------
  1352. # irc sender
  1353. send_irc() {
  1354. local NICKNAME="${1}" REALNAME="${2}" CHANNELS="${3}" NETWORK="${4}" SERVERNAME="${5}" MESSAGE="${6}" sent=0 channel color send_alarm reply_codes error
  1355. if [ "${SEND_IRC}" = "YES" ] && [ -n "${NICKNAME}" ] && [ -n "${REALNAME}" ] && [ -n "${CHANNELS}" ] && [ -n "${NETWORK}" ] && [ -n "${SERVERNAME}" ]; then
  1356. case "${status}" in
  1357. WARNING) color="warning" ;;
  1358. CRITICAL) color="danger" ;;
  1359. CLEAR) color="good" ;;
  1360. *) color="#777777" ;;
  1361. esac
  1362. SNDMESSAGE="${MESSAGE//$'\n'/", "}"
  1363. for CHANNEL in ${CHANNELS}; do
  1364. error=0
  1365. send_alarm=$(echo -e "USER ${NICKNAME} guest ${REALNAME} ${SERVERNAME}\\nNICK ${NICKNAME}\\nJOIN ${CHANNEL}\\nPRIVMSG ${CHANNEL} :${SNDMESSAGE}\\nQUIT\\n" \ | nc "${NETWORK}" 6667)
  1366. reply_codes=$(echo "${send_alarm}" | cut -d ' ' -f 2 | grep -o '[0-9]*')
  1367. for code in ${reply_codes}; do
  1368. if [ "${code}" -ge 400 ] && [ "${code}" -le 599 ]; then
  1369. error=1
  1370. break
  1371. fi
  1372. done
  1373. if [ "${error}" -eq 0 ]; then
  1374. info "sent irc notification for: ${host} ${chart}.${name} is ${status} to '${CHANNEL}'"
  1375. sent=$((sent + 1))
  1376. else
  1377. error "failed to send irc notification for: ${host} ${chart}.${name} is ${status} to '${CHANNEL}', with error code ${code}."
  1378. fi
  1379. done
  1380. fi
  1381. [ ${sent} -gt 0 ] && return 0
  1382. return 1
  1383. }
  1384. # -----------------------------------------------------------------------------
  1385. # Amazon SNS sender
  1386. send_awssns() {
  1387. local targets="${1}" message='' sent=0 region=''
  1388. local default_format="${status} on ${host} at ${date}: ${chart} ${value_string}"
  1389. [ "${SEND_AWSSNS}" = "YES" ] || return 1
  1390. message=${AWSSNS_MESSAGE_FORMAT:-${default_format}}
  1391. for target in ${targets}; do
  1392. # Extract the region from the target ARN. We need to explicitly specify the region so that it matches up correctly.
  1393. region="$(echo ${target} | cut -f 4 -d ':')"
  1394. if ${aws} sns publish --region "${region}" --subject "${host} ${status_message} - ${name//_/ } - ${chart}" --message "${message}" --target-arn ${target} &>/dev/null; then
  1395. info "sent Amazon SNS notification for: ${host} ${chart}.${name} is ${status} to '${target}'"
  1396. sent=$((sent + 1))
  1397. else
  1398. error "failed to send Amazon SNS notification for: ${host} ${chart}.${name} is ${status} to '${target}'"
  1399. fi
  1400. done
  1401. [ ${sent} -gt 0 ] && return 0
  1402. return 1
  1403. }
  1404. # -----------------------------------------------------------------------------
  1405. # syslog sender
  1406. send_syslog() {
  1407. local facility=${SYSLOG_FACILITY:-"local6"} level='info' targets="${1}"
  1408. local priority='' message='' host='' port='' prefix=''
  1409. local temp1='' temp2=''
  1410. [ "${SEND_SYSLOG}" = "YES" ] || return 1
  1411. if [ "${status}" = "CRITICAL" ]; then
  1412. level='crit'
  1413. elif [ "${status}" = "WARNING" ]; then
  1414. level='warning'
  1415. fi
  1416. for target in ${targets}; do
  1417. priority="${facility}.${level}"
  1418. message=''
  1419. host=''
  1420. port=''
  1421. prefix=''
  1422. temp1=''
  1423. temp2=''
  1424. prefix=$(echo ${target} | cut -d '/' -f 2)
  1425. temp1=$(echo ${target} | cut -d '/' -f 1)
  1426. if [ ${prefix} != ${temp1} ]; then
  1427. if (echo ${temp1} | grep -q '@'); then
  1428. temp2=$(echo ${temp1} | cut -d '@' -f 1)
  1429. host=$(echo ${temp1} | cut -d '@' -f 2)
  1430. if [ ${temp2} != ${host} ]; then
  1431. priority=${temp2}
  1432. fi
  1433. port=$(echo ${host} | rev | cut -d ':' -f 1 | rev)
  1434. if (echo ${host} | grep -E -q '\[.*\]'); then
  1435. if (echo ${port} | grep -q ']'); then
  1436. port=''
  1437. else
  1438. host=$(echo ${host} | rev | cut -d ':' -f 2- | rev)
  1439. fi
  1440. else
  1441. if [ ${port} = ${host} ]; then
  1442. port=''
  1443. else
  1444. host=$(echo ${host} | cut -d ':' -f 1)
  1445. fi
  1446. fi
  1447. else
  1448. priority=${temp1}
  1449. fi
  1450. fi
  1451. message="${prefix} ${status} on ${host} at ${date}: ${chart} ${value_string}"
  1452. if [ ${host} ]; then
  1453. logger_options="${logger_options} -n ${host}"
  1454. if [ ${port} ]; then
  1455. logger_options="${logger_options} -P ${port}"
  1456. fi
  1457. fi
  1458. ${logger} -p ${priority} ${logger_options} "${message}"
  1459. done
  1460. return $?
  1461. }
  1462. # -----------------------------------------------------------------------------
  1463. # SMS sender
  1464. send_sms() {
  1465. local recipients="${1}" errcode errmessage sent=0
  1466. # Human readable SMS
  1467. local msg="${host} ${status_message}: ${chart} (${family}), ${alarm}"
  1468. # limit it to 160 characters
  1469. msg="${msg:0:160}"
  1470. if [ "${SEND_SMS}" = "YES" ] && [ -n "${sendsms}" ] && [ -n "${recipients}" ] && [ -n "${msg}" ]; then
  1471. # http://api.kavenegar.com/v1/{API-KEY}/sms/send.json
  1472. for phone in ${recipients}; do
  1473. errmessage=$($sendsms $phone "$msg" 2>&1)
  1474. errcode=$?
  1475. if [ ${errcode} -eq 0 ]; then
  1476. info "sent smstools3 SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
  1477. sent=$((sent + 1))
  1478. else
  1479. error "failed to send smstools3 SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with error code ${errcode}: ${errmessage}."
  1480. fi
  1481. done
  1482. [ ${sent} -gt 0 ] && return 0
  1483. fi
  1484. return 1
  1485. }
  1486. # -----------------------------------------------------------------------------
  1487. # hangouts sender
  1488. send_hangouts() {
  1489. local rooms="${1}" httpcode sent=0 room color payload webhook
  1490. [ "${SEND_HANGOUTS}" != "YES" ] && return 1
  1491. case "${status}" in
  1492. WARNING) color="#ffa700" ;;
  1493. CRITICAL) color="#d62d20" ;;
  1494. CLEAR) color="#008744" ;;
  1495. *) color="#777777" ;;
  1496. esac
  1497. for room in ${rooms}; do
  1498. if [ -z "${HANGOUTS_WEBHOOK_URI[$room]}" ] ; then
  1499. info "Can't send Hangouts notification for: ${host} ${chart}.${name} to room ${room}. HANGOUTS_WEBHOOK_URI[$room] not defined"
  1500. else
  1501. webhook="${HANGOUTS_WEBHOOK_URI[$room]}"
  1502. payload="$(
  1503. cat <<EOF
  1504. {
  1505. "cards": [
  1506. {
  1507. "header": {
  1508. "title": "Netdata on ${host}",
  1509. "imageUrl": "${images_base_url}/images/banner-icon-144x144.png",
  1510. "imageStyle": "IMAGE"
  1511. },
  1512. "sections": [
  1513. {
  1514. "header": "<b>${host}</b>",
  1515. "widgets": [
  1516. {
  1517. "keyValue": {
  1518. "topLabel": "Status Message",
  1519. "content": "<b>${status_message}</b>",
  1520. "contentMultiline": "true",
  1521. "iconUrl": "${image}",
  1522. "onClick": {
  1523. "openLink": {
  1524. "url": "${goto_url}"
  1525. }
  1526. }
  1527. }
  1528. },
  1529. {
  1530. "keyValue": {
  1531. "topLabel": "${chart} | ${family}",
  1532. "content": "<font color=${color}>${alarm}</font>",
  1533. "contentMultiline": "true"
  1534. }
  1535. }
  1536. ]
  1537. },
  1538. {
  1539. "widgets": [
  1540. {
  1541. "textParagraph": {
  1542. "text": "<font color=\"#0057e7\">@ ${date}\n<b>${info}</b></font>"
  1543. }
  1544. }
  1545. ]
  1546. },
  1547. {
  1548. "widgets": [
  1549. {
  1550. "buttons": [
  1551. {
  1552. "textButton": {
  1553. "text": "Go to ${host}",
  1554. "onClick": {
  1555. "openLink": {
  1556. "url": "${goto_url}"
  1557. }
  1558. }
  1559. }
  1560. }
  1561. ]
  1562. }
  1563. ]
  1564. }
  1565. ]
  1566. }
  1567. ]
  1568. }
  1569. EOF
  1570. )"
  1571. httpcode=$(docurl -H "Content-Type: application/json" -X POST -d "${payload}" "${webhook}")
  1572. if [ "${httpcode}" = "200" ]; then
  1573. info "sent hangouts notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
  1574. sent=$((sent + 1))
  1575. else
  1576. error "failed to send hangouts notification for: ${host} ${chart}.${name} is ${status} to '${room}', with HTTP error code ${httpcode}."
  1577. fi
  1578. fi
  1579. done
  1580. [ ${sent} -gt 0 ] && return 0
  1581. return 1
  1582. }
  1583. # -----------------------------------------------------------------------------
  1584. # prepare the content of the notification
  1585. # the url to send the user on click
  1586. urlencode "${args_host}" >/dev/null
  1587. url_host="${REPLY}"
  1588. urlencode "${chart}" >/dev/null
  1589. url_chart="${REPLY}"
  1590. urlencode "${family}" >/dev/null
  1591. url_family="${REPLY}"
  1592. urlencode "${name}" >/dev/null
  1593. url_name="${REPLY}"
  1594. redirect_params="host=${url_host}&chart=${url_chart}&family=${url_family}&alarm=${url_name}&alarm_unique_id=${unique_id}&alarm_id=${alarm_id}&alarm_event_id=${event_id}&alarm_when=${when}"
  1595. GOTOCLOUD=0
  1596. if [ "${NETDATA_REGISTRY_URL}" == "https://registry.my-netdata.io" ]; then
  1597. if [ -z "${NETDATA_REGISTRY_UNIQUE_ID}" ]; then
  1598. if [ -f "@registrydir_POST@/netdata.public.unique.id" ]; then
  1599. NETDATA_REGISTRY_UNIQUE_ID="$(cat "@registrydir_POST@/netdata.public.unique.id")"
  1600. fi
  1601. fi
  1602. if [ -n "${NETDATA_REGISTRY_UNIQUE_ID}" ]; then
  1603. GOTOCLOUD=1
  1604. fi
  1605. fi
  1606. if [ ${GOTOCLOUD} -eq 0 ]; then
  1607. goto_url="${NETDATA_REGISTRY_URL}/goto-host-from-alarm.html?${redirect_params}"
  1608. else
  1609. goto_url="${NETDATA_REGISTRY_CLOUD_BASE_URL}/alarms/redirect?agentID=${NETDATA_REGISTRY_UNIQUE_ID}&${redirect_params}"
  1610. fi
  1611. # the severity of the alarm
  1612. severity="${status}"
  1613. # the time the alarm was raised
  1614. duration4human ${duration} >/dev/null
  1615. duration_txt="${REPLY}"
  1616. duration4human ${non_clear_duration} >/dev/null
  1617. non_clear_duration_txt="${REPLY}"
  1618. raised_for="(was ${old_status,,} for ${duration_txt})"
  1619. # the key status message
  1620. status_message="status unknown"
  1621. # the color of the alarm
  1622. color="grey"
  1623. # the alarm value
  1624. alarm="${name//_/ } = ${value_string}"
  1625. # the image of the alarm
  1626. image="${images_base_url}/images/banner-icon-144x144.png"
  1627. # prepare the title based on status
  1628. case "${status}" in
  1629. CRITICAL)
  1630. image="${images_base_url}/images/alert-128-red.png"
  1631. status_message="is critical"
  1632. color="#ca414b"
  1633. ;;
  1634. WARNING)
  1635. image="${images_base_url}/images/alert-128-orange.png"
  1636. status_message="needs attention"
  1637. color="#ffc107"
  1638. ;;
  1639. CLEAR)
  1640. image="${images_base_url}/images/check-mark-2-128-green.png"
  1641. status_message="recovered"
  1642. color="#77ca6d"
  1643. ;;
  1644. esac
  1645. if [ "${status}" = "CLEAR" ]; then
  1646. severity="Recovered from ${old_status}"
  1647. if [ ${non_clear_duration} -gt ${duration} ]; then
  1648. raised_for="(alarm was raised for ${non_clear_duration_txt})"
  1649. fi
  1650. # don't show the value when the status is CLEAR
  1651. # for certain alarms, this value might not have any meaning
  1652. alarm="${name//_/ } ${raised_for}"
  1653. elif { [ "${old_status}" = "WARNING" ] && [ "${status}" = "CRITICAL" ]; }; then
  1654. severity="Escalated to ${status}"
  1655. if [ ${non_clear_duration} -gt ${duration} ]; then
  1656. raised_for="(alarm is raised for ${non_clear_duration_txt})"
  1657. fi
  1658. elif { [ "${old_status}" = "CRITICAL" ] && [ "${status}" = "WARNING" ]; }; then
  1659. severity="Demoted to ${status}"
  1660. if [ ${non_clear_duration} -gt ${duration} ]; then
  1661. raised_for="(alarm is raised for ${non_clear_duration_txt})"
  1662. fi
  1663. else
  1664. raised_for=
  1665. fi
  1666. # prepare HTML versions of elements
  1667. info_html=
  1668. [ -n "${info}" ] && info_html=" <small><br/>${info}</small>"
  1669. raised_for_html=
  1670. [ -n "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
  1671. # -----------------------------------------------------------------------------
  1672. # send the slack notification
  1673. # slack aggregates posts from the same username
  1674. # so we use "${host} ${status}" as the bot username, to make them diff
  1675. send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
  1676. SENT_SLACK=$?
  1677. # -----------------------------------------------------------------------------
  1678. # send the hangouts notification
  1679. # hangouts aggregates posts from the same room
  1680. # so we use "${host} ${status}" as the room, to make them diff
  1681. send_hangouts "${to_hangouts}"
  1682. SENT_HANGOUTS=$?
  1683. # -----------------------------------------------------------------------------
  1684. # send the Microsoft notification
  1685. # Microsoft team aggregates posts from the same username
  1686. # so we use "${host} ${status}" as the bot username, to make them diff
  1687. send_msteam "${MSTEAM_WEBHOOK_URL}" "${to_msteam}"
  1688. SENT_MSTEAM=$?
  1689. # -----------------------------------------------------------------------------
  1690. # send the rocketchat notification
  1691. # rocketchat aggregates posts from the same username
  1692. # so we use "${host} ${status}" as the bot username, to make them diff
  1693. send_rocketchat "${ROCKETCHAT_WEBHOOK_URL}" "${to_rocketchat}"
  1694. SENT_ROCKETCHAT=$?
  1695. # -----------------------------------------------------------------------------
  1696. # send the alerta notification
  1697. # alerta aggregates posts from the same username
  1698. # so we use "${host} ${status}" as the bot username, to make them diff
  1699. send_alerta "${ALERTA_WEBHOOK_URL}" "${to_alerta}"
  1700. SENT_ALERTA=$?
  1701. # -----------------------------------------------------------------------------
  1702. # send the flock notification
  1703. # flock aggregates posts from the same username
  1704. # so we use "${host} ${status}" as the bot username, to make them diff
  1705. send_flock "${FLOCK_WEBHOOK_URL}" "${to_flock}"
  1706. SENT_FLOCK=$?
  1707. # -----------------------------------------------------------------------------
  1708. # send the discord notification
  1709. # discord aggregates posts from the same username
  1710. # so we use "${host} ${status}" as the bot username, to make them diff
  1711. send_discord "${DISCORD_WEBHOOK_URL}" "${to_discord}"
  1712. SENT_DISCORD=$?
  1713. # -----------------------------------------------------------------------------
  1714. # send the pushover notification
  1715. send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
  1716. <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>&nbsp;
  1717. <small><b>${chart}</b><br/>Chart<br/>&nbsp;</small>
  1718. <small><b>${family}</b><br/>Family<br/>&nbsp;</small>
  1719. <small><b>${severity}</b><br/>Severity<br/>&nbsp;</small>
  1720. <small><b>${date}${raised_for_html}</b><br/>Time<br/>&nbsp;</small>
  1721. <a href=\"${goto_url}\">View Netdata</a><br/>&nbsp;
  1722. <small><small>The source of this alarm is line ${src}</small></small>
  1723. "
  1724. SENT_PUSHOVER=$?
  1725. # -----------------------------------------------------------------------------
  1726. # send the pushbullet notification
  1727. send_pushbullet "${PUSHBULLET_ACCESS_TOKEN}" "${PUSHBULLET_SOURCE_DEVICE}" "${to_pushbullet}" "${goto_url}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}\\n
  1728. Severity: ${severity}\\n
  1729. Chart: ${chart}\\n
  1730. Family: ${family}\\n
  1731. ${date}\\n
  1732. The source of this alarm is line ${src}"
  1733. SENT_PUSHBULLET=$?
  1734. # -----------------------------------------------------------------------------
  1735. # send the twilio SMS
  1736. send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
  1737. Severity: ${severity}
  1738. Chart: ${chart}
  1739. Family: ${family}
  1740. ${info}"
  1741. SENT_TWILIO=$?
  1742. # -----------------------------------------------------------------------------
  1743. # send the messagebird SMS
  1744. send_messagebird "${MESSAGEBIRD_ACCESS_KEY}" "${MESSAGEBIRD_NUMBER}" "${to_messagebird}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
  1745. Severity: ${severity}
  1746. Chart: ${chart}
  1747. Family: ${family}
  1748. ${info}"
  1749. SENT_MESSAGEBIRD=$?
  1750. # -----------------------------------------------------------------------------
  1751. # send the kavenegar SMS
  1752. send_kavenegar "${KAVENEGAR_API_KEY}" "${KAVENEGAR_SENDER}" "${to_kavenegar}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
  1753. Severity: ${severity}
  1754. Chart: ${chart}
  1755. Family: ${family}
  1756. ${info}"
  1757. SENT_KAVENEGAR=$?
  1758. # -----------------------------------------------------------------------------
  1759. # send the telegram.org message
  1760. # https://core.telegram.org/bots/api#formatting-options
  1761. send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${host} ${status_message} - <b>${name//_/ }</b>
  1762. ${chart} (${family})
  1763. <a href=\"${goto_url}\">${alarm}</a>
  1764. <i>${info}</i>"
  1765. SENT_TELEGRAM=$?
  1766. # -----------------------------------------------------------------------------
  1767. # send the kafka message
  1768. send_kafka
  1769. SENT_KAFKA=$?
  1770. # -----------------------------------------------------------------------------
  1771. # send the pagerduty.com message
  1772. send_pd "${to_pd}"
  1773. SENT_PD=$?
  1774. # -----------------------------------------------------------------------------
  1775. # send the fleep message
  1776. send_fleep "${to_fleep}"
  1777. SENT_FLEEP=$?
  1778. # -----------------------------------------------------------------------------
  1779. # send the Prowl message
  1780. send_prowl "${to_prowl}"
  1781. SENT_PROWL=$?
  1782. # -----------------------------------------------------------------------------
  1783. # send the irc message
  1784. send_irc "${IRC_NICKNAME}" "${IRC_REALNAME}" "${to_irc}" "${IRC_NETWORK}" "${host}" "${host} ${status_message} - ${name//_/ } - ${chart} ----- ${alarm}
  1785. Severity: ${severity}
  1786. Chart: ${chart}
  1787. Family: ${family}
  1788. ${info}"
  1789. SENT_IRC=$?
  1790. # -----------------------------------------------------------------------------
  1791. # send the SMS message with smstools3
  1792. send_sms "${to_sms}"
  1793. SENT_SMS=$?
  1794. # -----------------------------------------------------------------------------
  1795. # send the custom message
  1796. send_custom() {
  1797. # is it enabled?
  1798. [ "${SEND_CUSTOM}" != "YES" ] && return 1
  1799. # do we have any sender?
  1800. [ -z "${1}" ] && return 1
  1801. # call the custom_sender function
  1802. custom_sender "${@}"
  1803. }
  1804. send_custom "${to_custom}"
  1805. SENT_CUSTOM=$?
  1806. # -----------------------------------------------------------------------------
  1807. # send hipchat message
  1808. send_hipchat "${HIPCHAT_AUTH_TOKEN}" "${to_hipchat}" " \
  1809. ${host} ${status_message}<br/> \
  1810. <b>${alarm}</b> ${info_html}<br/> \
  1811. <b>${chart}</b> (family <b>${family}</b>)<br/> \
  1812. <b>${date}${raised_for_html}</b><br/> \
  1813. <a href=\\\"${goto_url}\\\">View netdata dashboard</a> \
  1814. (source of alarm ${src}) \
  1815. "
  1816. SENT_HIPCHAT=$?
  1817. # -----------------------------------------------------------------------------
  1818. # send the Amazon SNS message
  1819. send_awssns "${to_awssns}"
  1820. SENT_AWSSNS=$?
  1821. # -----------------------------------------------------------------------------
  1822. # send the syslog message
  1823. send_syslog "${to_syslog}"
  1824. SENT_SYSLOG=$?
  1825. # -----------------------------------------------------------------------------
  1826. # send the email
  1827. IFS='' read -r -d '' email_plaintext_part <<EOF
  1828. Content-Type: text/plain; encoding=${EMAIL_CHARSET}
  1829. Content-Disposition: inline
  1830. Content-Transfer-Encoding: 8bit
  1831. ${host} ${status_message}
  1832. ${alarm} ${info}
  1833. ${raised_for}
  1834. Chart : ${chart}
  1835. Family : ${family}
  1836. Severity: ${severity}
  1837. URL : ${goto_url}
  1838. Source : ${src}
  1839. Date : ${date}
  1840. Notification generated on ${host}
  1841. Evaluated Expression : ${calc_expression}
  1842. Expression Variables : ${calc_param_values}
  1843. The host has ${total_warnings} WARNING and ${total_critical} CRITICAL alarm(s) raised.
  1844. EOF
  1845. if [[ "${EMAIL_PLAINTEXT_ONLY}" == "YES" ]]; then
  1846. send_email <<EOF
  1847. To: ${to_email}
  1848. Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
  1849. MIME-Version: 1.0
  1850. Content-Type: multipart/alternative; boundary="multipart-boundary"
  1851. ${email_thread_headers}
  1852. This is a MIME-encoded multipart message
  1853. --multipart-boundary
  1854. ${email_plaintext_part}
  1855. --multipart-boundary--
  1856. EOF
  1857. else
  1858. IFS='' read -r -d '' email_html_part <<EOF
  1859. Content-Type: text/html; encoding=${EMAIL_CHARSET}
  1860. Content-Disposition: inline
  1861. Content-Transfer-Encoding: 8bit
  1862. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1863. <html xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; padding: 0;">
  1864. <body style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; width: 100% !important; min-height: 100%; line-height: 1.6; background: #f6f6f6; margin:0; padding: 0;">
  1865. <table>
  1866. <tbody>
  1867. <tr>
  1868. <td style="vertical-align: top;" valign="top"></td>
  1869. <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
  1870. <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
  1871. <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
  1872. <tbody>
  1873. <tr>
  1874. <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
  1875. <div style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
  1876. </td>
  1877. </tr>
  1878. <tr>
  1879. <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">
  1880. <h1 style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
  1881. </td>
  1882. </tr>
  1883. <tr>
  1884. <td style="vertical-align: top;" valign="top">
  1885. <div style="margin: 0; padding: 20px; max-width: 700px;">
  1886. <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
  1887. <tbody>
  1888. <tr>
  1889. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding:0 0 20px;" align="left" valign="top">
  1890. <span>${chart}</span>
  1891. <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
  1892. </td>
  1893. </tr>
  1894. <tr style="margin: 0; padding: 0;">
  1895. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
  1896. <span><b>${alarm}</b>${info_html}</span>
  1897. <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
  1898. </td>
  1899. </tr>
  1900. <tr>
  1901. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
  1902. <span>${family}</span>
  1903. <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
  1904. </td>
  1905. </tr>
  1906. <tr style="margin: 0; padding: 0;">
  1907. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
  1908. <span>${severity}</span>
  1909. <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
  1910. </td>
  1911. </tr>
  1912. <tr style="margin: 0; padding: 0;">
  1913. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top"><span>${date}</span>
  1914. <span>${raised_for_html}</span> <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Time</span>
  1915. </td>
  1916. </tr>
  1917. <tr style="margin: 0; padding: 0;">
  1918. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
  1919. <span>${calc_expression}</span>
  1920. <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Evaluated Expression</span>
  1921. </td>
  1922. </tr>
  1923. <tr style="margin: 0; padding: 0;">
  1924. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
  1925. <span>${calc_param_values}</span>
  1926. <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Expression Variables</span>
  1927. </td>
  1928. </tr>
  1929. <tr style="margin: 0; padding: 0;">
  1930. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
  1931. The host has ${total_warnings} WARNING and ${total_critical} CRITICAL alarm(s) raised.
  1932. </td>
  1933. </tr>
  1934. <tr style="margin: 0; padding: 0;">
  1935. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
  1936. <a href="${goto_url}" style="font-size: 14px; color: #ffffff; text-decoration: none; line-height: 1.5; font-weight: bold; text-align: center; display: inline-block; text-transform: capitalize; background: #35568d; border-width: 1px; border-style: solid; border-color: #2b4c86; margin: 0; padding: 10px 15px;" target="_blank">View Netdata</a>
  1937. </td>
  1938. </tr>
  1939. <tr style="text-align: center; margin: 0; padding: 0;">
  1940. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 11px; vertical-align: top; margin: 0; padding: 10px 0 0 0; color: #666666;" align="center" valign="bottom">The source of this alarm is line <code>${src}</code><br/>(alarms are configurable, edit this file to adapt the alarm to your needs)
  1941. </td>
  1942. </tr>
  1943. <tr style="text-align: center; margin: 0; padding: 0;">
  1944. <td style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; 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
  1945. <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance and health monitoring, on <code>${host}</code>.
  1946. </td>
  1947. </tr>
  1948. </tbody>
  1949. </table>
  1950. </div>
  1951. </td>
  1952. </tr>
  1953. </tbody>
  1954. </table>
  1955. </div>
  1956. </td>
  1957. </tr>
  1958. </tbody>
  1959. </table>
  1960. </body>
  1961. </html>
  1962. EOF
  1963. send_email <<EOF
  1964. To: ${to_email}
  1965. Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
  1966. MIME-Version: 1.0
  1967. Content-Type: multipart/alternative; boundary="multipart-boundary"
  1968. ${email_thread_headers}
  1969. This is a MIME-encoded multipart message
  1970. --multipart-boundary
  1971. ${email_plaintext_part}
  1972. --multipart-boundary
  1973. ${email_html_part}
  1974. --multipart-boundary--
  1975. EOF
  1976. fi
  1977. SENT_EMAIL=$?
  1978. # -----------------------------------------------------------------------------
  1979. # let netdata know
  1980. for state in "${SENT_EMAIL}" \
  1981. "${SENT_PUSHOVER}" \
  1982. "${SENT_TELEGRAM}" \
  1983. "${SENT_SLACK}" \
  1984. "${SENT_HANGOUTS}" \
  1985. "${SENT_ROCKETCHAT}" \
  1986. "${SENT_ALERTA}" \
  1987. "${SENT_FLOCK}" \
  1988. "${SENT_DISCORD}" \
  1989. "${SENT_TWILIO}" \
  1990. "${SENT_HIPCHAT}" \
  1991. "${SENT_MESSAGEBIRD}" \
  1992. "${SENT_KAVENEGAR}" \
  1993. "${SENT_PUSHBULLET}" \
  1994. "${SENT_KAFKA}" \
  1995. "${SENT_PD}" \
  1996. "${SENT_FLEEP}" \
  1997. "${SENT_PROWL}" \
  1998. "${SENT_CUSTOM}" \
  1999. "${SENT_IRC}" \
  2000. "${SENT_AWSSNS}" \
  2001. "${SENT_SYSLOG}" \
  2002. "${SENT_SMS}" \
  2003. "${SENT_MSTEAM}"; do
  2004. if [ "${state}" -eq 0 ]; then
  2005. # we sent something
  2006. exit 0
  2007. fi
  2008. done
  2009. # we did not send anything
  2010. exit 1