alarm-notify.sh.in 82 KB

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