alarm-notify.sh.in 81 KB

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