@cryptotaxi247 / netdata-1 / commits / 7433c6cde

Add support for alert notifications to ntfy.sh (#14875)

Dim-P committed Apr 10, 2023 at 08:38 UTC 7433c6cde9016f2c37d97be159e2656d8de77fd7
6 files changed +166 -4
health/notifications/Makefile.am
+1
@@ -37,6 +37,7 @@ include irc/Makefile.inc
37 include kavenegar/Makefile.inc
38 include messagebird/Makefile.inc
39 include msteams/Makefile.inc
40 +include ntfy/Makefile.inc
41 include opsgenie/Makefile.inc
42 include pagerduty/Makefile.inc
43 include pushbullet/Makefile.inc
health/notifications/alarm-notify.sh.in
+61 -3
@@ -39,6 +39,7 @@
39 # - Stackpulse Event by @thiagoftsm
40 # - Opsgenie by @thiaoftsm #9858
41 # - Gotify by @coffeegrind123
42 +# - ntfy.sh by @Dim-P
43
44 # -----------------------------------------------------------------------------
45 # testing notifications
@@ -176,6 +177,7 @@ sms
177 hangouts
178 dynatrace
179 matrix
180 +ntfy
181 "
182
183 # -----------------------------------------------------------------------------
@@ -654,6 +656,9 @@ filter_recipient_by_criticality() {
656 # check gotify
657 { [ -z "${GOTIFY_APP_TOKEN}" ] || [ -z "${GOTIFY_APP_URL}" ]; } && SEND_GOTIFY="NO"
658
659 +# check ntfy
660 +[ -z "${DEFAULT_RECIPIENT_NTFY}" ] && SEND_NTFY="NO"
661 +
662 # check stackpulse
663 [ -z "${STACKPULSE_WEBHOOK}" ] && SEND_STACKPULSE="NO"
664
@@ -692,7 +697,8 @@ if [ "${SEND_PUSHOVER}" = "YES" ] ||
697 [ "${SEND_DYNATRACE}" = "YES" ] ||
698 [ "${SEND_STACKPULSE}" = "YES" ] ||
699 [ "${SEND_OPSGENIE}" = "YES" ] ||
695 - [ "${SEND_GOTIFY}" = "YES" ]; then
700 + [ "${SEND_GOTIFY}" = "YES" ] ||
701 + [ "${SEND_NTFY}" = "YES" ]; then
702 # if we need curl, check for the curl command
703 if [ -z "${curl}" ]; then
704 curl="$(command -v curl 2>/dev/null)"
@@ -723,6 +729,7 @@ if [ "${SEND_PUSHOVER}" = "YES" ] ||
729 SEND_STACKPULSE="NO"
730 SEND_OPSGENIE="NO"
731 SEND_GOTIFY="NO"
732 + SEND_NTFY="NO"
733 fi
734 fi
735
@@ -863,7 +870,8 @@ for method in "${SEND_EMAIL}" \
870 "${SEND_DYNATRACE}" \
871 "${SEND_STACKPULSE}" \
872 "${SEND_OPSGENIE}" \
866 - "${SEND_GOTIFY}" ; do
873 + "${SEND_GOTIFY}" \
874 + "${SEND_NTFY}" ; do
875
876 if [ "${method}" == "YES" ]; then
877 proceed=1
@@ -2402,6 +2410,50 @@ EOF
2410 return 0
2411 }
2412
2413 +# -----------------------------------------------------------------------------
2414 +# ntfy sender
2415 +
2416 +send_ntfy() {
2417 + local httpcode priority recipients=${1} sent=0 msg
2418 +
2419 + [ "${SEND_NTFY}" != "YES" ] && return 1
2420 +
2421 + case "${status}" in
2422 + WARNING) emoji="warning" ;;
2423 + CRITICAL) emoji="red_circle" ;;
2424 + CLEAR) emoji="white_check_mark" ;;
2425 + *) emoji="white_circle" ;;
2426 + esac
2427 +
2428 + case ${status} in
2429 + WARNING) priority="high";;
2430 + CRITICAL) priority="urgent";;
2431 + *) priority="default" ;;
2432 + esac
2433 +
2434 + for recipient in ${recipients}; do
2435 + msg="${host} ${status_message}: ${alarm} - ${info}"
2436 + httpcode=$(docurl -X POST \
2437 + -H "Icon: https://raw.githubusercontent.com/netdata/netdata/master/web/gui/dashboard/images/favicon-196x196.png" \
2438 + -H "Title: ${host}: ${name}" \
2439 + -H "Tags: ${emoji}" \
2440 + -H "Priority: ${priority}" \
2441 + -H "Actions: view, View node, ${goto_url}, clear=true;" \
2442 + -d "${msg}" \
2443 + ${recipient})
2444 + if [ "${httpcode}" == "200" ]; then
2445 + info "sent ntfy notification for: ${host} ${chart}.${name} is ${status} to '${recipient}'"
2446 + sent=$((sent + 1))
2447 + else
2448 + error "failed to send ntfy notification for: ${host} ${chart}.${name} is ${status} to '${recipient}', with HTTP response status code ${httpcode}."
2449 + fi
2450 + done
2451 +
2452 + [ ${sent} -gt 0 ] && return 0
2453 +
2454 + return 1
2455 +}
2456 +
2457 # -----------------------------------------------------------------------------
2458 # prepare the content of the notification
2459
@@ -3608,6 +3660,11 @@ SENT_OPSGENIE=$?
3660 send_gotify
3661 SENT_GOTIFY=$?
3662
3663 +# -----------------------------------------------------------------------------
3664 +# send messages to ntfy
3665 +send_ntfy "${DEFAULT_RECIPIENT_NTFY}"
3666 +SENT_NTFY=$?
3667 +
3668 # -----------------------------------------------------------------------------
3669 # let netdata know
3670 for state in "${SENT_EMAIL}" \
@@ -3638,7 +3695,8 @@ for state in "${SENT_EMAIL}" \
3695 "${SENT_DYNATRACE}" \
3696 "${SENT_STACKPULSE}" \
3697 "${SENT_OPSGENIE}" \
3641 - "${SENT_GOTIFY}"; do
3698 + "${SENT_GOTIFY}" \
3699 + "${SENT_NTFY}"; do
3700 if [ "${state}" -eq 0 ]; then
3701 # we sent something
3702 exit 0
health/notifications/gotify/README.md
+1 -1
@@ -32,7 +32,7 @@ You will need:
32
33 Edit `health_alarm_notify.conf`, changes to this file do not require restarting Netdata:
34
35 -1. Set `SET_GOTIFY` to `YES`
35 +1. Set `SEND_GOTIFY` to `YES`
36 2. Set `GOTIFY_APP_TOKEN` to the app token you generated
37 3. `GOTIFY_APP_URL` to point to your Gotify instance, for example `https://push.example.domain/`
38
health/notifications/health_alarm_notify.conf
+25
@@ -22,6 +22,7 @@
22 # - message to Microsoft Teams (through webhook)
23 # - message to Rocket.Chat (through webhook)
24 # - message to Google Hangouts Chat (through webhook)
25 +# - push notifications to your mobile phone or desktop (ntfy.sh)
26 #
27 # The 'to' line given at netdata alarms defines a *role*, so that many
28 # people can be notified for each role.
@@ -853,6 +854,18 @@ MATRIX_ACCESSTOKEN=
854 # The format is !roomid:homeservername
855 DEFAULT_RECIPIENT_MATRIX=""
856
857 +#------------------------------------------------------------------------------
858 +# ntfy.sh global notification options
859 +
860 +# enable/disable sending ntfy notifications
861 +SEND_NTFY="YES"
862 +
863 +# if a role's recipients are not configured, a notification will be sent to
864 +# this ntfy server / topic combination (empty = do not send a notification for
865 +# unconfigured roles).
866 +# Multiple recipients can be given like this: "https://SERVER1/TOPIC1 https://SERVER2/TOPIC2 ..."
867 +DEFAULT_RECIPIENT_NTFY=""
868 +
869 #------------------------------------------------------------------------------
870 # custom notifications
871 #
@@ -997,6 +1010,8 @@ role_recipients_stackpulse[sysadmin]="${DEFAULT_RECIPIENT_STACKPULSE}"
1010
1011 role_recipients_gotify[sysadmin]="${DEFAULT_RECIPIENT_GOTIFY}"
1012
1013 +role_recipients_ntfy[sysadmin]="${DEFAULT_RECIPIENT_NTFY}"
1014 +
1015 # -----------------------------------------------------------------------------
1016 # DNS related alarms
1017
@@ -1056,6 +1071,8 @@ role_recipients_stackpulse[domainadmin]="${DEFAULT_RECIPIENT_STACKPULSE}"
1071
1072 role_recipients_gotify[domainadmin]="${DEFAULT_RECIPIENT_GOTIFY}"
1073
1074 +role_recipients_ntfy[domainadmin]="${DEFAULT_RECIPIENT_NTFY}"
1075 +
1076 # -----------------------------------------------------------------------------
1077 # database servers alarms
1078 # mysql, redis, memcached, postgres, etc
@@ -1116,6 +1133,8 @@ role_recipients_stackpulse[dba]="${DEFAULT_RECIPIENT_STACKPULSE}"
1133
1134 role_recipients_gotify[dba]="${DEFAULT_RECIPIENT_GOTIFY}"
1135
1136 +role_recipients_ntfy[dba]="${DEFAULT_RECIPIENT_NTFY}"
1137 +
1138 # -----------------------------------------------------------------------------
1139 # web servers alarms
1140 # apache, nginx, lighttpd, etc
@@ -1176,6 +1195,8 @@ role_recipients_stackpulse[webmaster]="${DEFAULT_RECIPIENT_STACKPULSE}"
1195
1196 role_recipients_gotify[webmaster]="${DEFAULT_RECIPIENT_GOTIFY}"
1197
1198 +role_recipients_ntfy[webmaster]="${DEFAULT_RECIPIENT_NTFY}"
1199 +
1200 # -----------------------------------------------------------------------------
1201 # proxy servers alarms
1202 # squid, etc
@@ -1236,6 +1257,8 @@ role_recipients_stackpulse[proxyadmin]="${DEFAULT_RECIPIENT_STACKPULSE}"
1257
1258 role_recipients_gotify[proxyadmin]="${DEFAULT_RECIPIENT_GOTIFY}"
1259
1260 +role_recipients_ntfy[proxyadmin]="${DEFAULT_RECIPIENT_NTFY}"
1261 +
1262 # -----------------------------------------------------------------------------
1263 # peripheral devices
1264 # UPS, photovoltaics, etc
@@ -1293,3 +1316,5 @@ role_recipients_matrix[sitemgr]="${DEFAULT_RECIPIENT_MATRIX}"
1316 role_recipients_stackpulse[sitemgr]="${DEFAULT_RECIPIENT_STACKPULSE}"
1317
1318 role_recipients_gotify[sitemgr]="${DEFAULT_RECIPIENT_GOTIFY}"
1319 +
1320 +role_recipients_ntfy[sitemgr]="${DEFAULT_RECIPIENT_NTFY}"
health/notifications/ntfy/Makefile.inc new
+12
@@ -0,0 +1,12 @@
1 +# SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +# THIS IS NOT A COMPLETE Makefile
4 +# IT IS INCLUDED BY ITS PARENT'S Makefile.am
5 +# IT IS REQUIRED TO REFERENCE ALL FILES RELATIVE TO THE PARENT
6 +
7 +# install these files
8 +dist_noinst_DATA += \
9 + ntfy/README.md \
10 + ntfy/Makefile.inc \
11 + $(NULL)
12 +
health/notifications/ntfy/README.md new
+66
@@ -0,0 +1,66 @@
1 +# Ntfy agent alert notifications
2 +
3 +Learn how to send alerts to an ntfy server using Netdata's Agent alert notification feature, which supports dozens of endpoints, user roles, and more.
4 +
5 +> ### Note
6 +>
7 +> This file assumes you have read the [Introduction to Agent alert notifications](https://github.com/netdata/netdata/blob/master/health/notifications/README.md), detailing how the Netdata Agent's alert notification method works.
8 +
9 +[Ntfy](https://ntfy.sh/) (pronounce: notify) is a simple HTTP-based [pub-sub](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) notification service. It allows you to send notifications to your phone or desktop via scripts from any computer, entirely without signup, cost or setup. It's also [open source](https://github.com/binwiederhier/ntfy) if you want to run your own server.
10 +
11 +This is what you will get:
12 +
13 +<img src="https://user-images.githubusercontent.com/5953192/230661442-a180abe2-c8bd-496e-88be-9038e62fb4f7.png" alt="Example alarm notifications in Ntfy" width="60%"></img>
14 +
15 +## Prerequisites
16 +
17 +You will need:
18 +
19 +- (Optional) A [self-hosted ntfy server](https://docs.ntfy.sh/faq/#can-i-self-host-it), in case you don't want to use https://ntfy.sh
20 +- A new [topic](https://ntfy.sh/#subscribe) for the notifications to be published to
21 +- terminal access to the Agent you wish to configure
22 +
23 +## Configure Netdata to send alert notifications to Ntfy
24 +
25 +> ### Info
26 +>
27 +> This file mentions editing configuration files.
28 +>
29 +> - To edit configuration files in a safe way, we provide the [`edit config` script](https://github.com/netdata/netdata/blob/master/docs/configure/nodes.md#use-edit-config-to-edit-configuration-files) located in your [Netdata config directory](https://github.com/netdata/netdata/blob/master/docs/configure/nodes.md#the-netdata-config-directory) (typically is `/etc/netdata`) that creates the proper file and opens it in an editor automatically.
30 +> Note that to run the script you need to be inside your Netdata config directory.
31 +>
32 +> It is recommended to use this way for configuring Netdata.
33 +
34 +Edit `health_alarm_notify.conf`, changes to this file do not require restarting Netdata:
35 +
36 +1. Set `SEND_NTFY` to `YES`
37 +2. Set `DEFAULT_RECIPIENT_NTFY` to the URL formed by the server-topic combination you want the alert notifications to be sent to. Unless you are hosting your own server, the server should always be set to [https://ntfy.sh](https://ntfy.sh)
38 +
39 + You can define multiple recipient URLs like this: `https://SERVER1/TOPIC1 https://SERVER2/TOPIC2`
40 + All roles will default to this variable if left unconfigured.
41 +
42 +> ### Warning
43 +> All topics published on https://ntfy.sh are public, so anyone can subscribe to them and follow your notifications. To avoid that, ensure the topic is unique enough using a long, randomly generated ID, like in the following examples.
44 +>
45 +
46 +An example of a working configuration with two topics as recipients, using the [https://ntfy.sh](https://ntfy.sh) server would be:
47 +
48 +```conf
49 +SEND_NTFY="YES"
50 +DEFAULT_RECIPIENT_NTFY="https://ntfy.sh/netdata-X7seHg7d3Tw9zGOk https://ntfy.sh/netdata-oIPm4IK1IlUtlA30"
51 +```
52 +
53 +You can then have different servers and/or topics per **role**, by editing `DEFAULT_RECIPIENT_NTFY` with the server-topic combination you want, in the following entries at the bottom of the same file:
54 +
55 +```conf
56 +role_recipients_ntfy[sysadmin]="https://SERVER1/TOPIC1"
57 +role_recipients_ntfy[domainadmin]="https://SERVER2/TOPIC2"
58 +role_recipients_ntfy[dba]="https://SERVER3/TOPIC3"
59 +role_recipients_ntfy[webmaster]="https://SERVER4/TOPIC4"
60 +role_recipients_ntfy[proxyadmin]="https://SERVER5/TOPIC5"
61 +role_recipients_ntfy[sitemgr]="https://SERVER6/TOPIC6"
62 +```
63 +
64 +## Test the notification method
65 +
66 +To test this alert refer to the ["Testing Alert Notifications"](https://github.com/netdata/netdata/blob/master/health/notifications/README.md#testing-alert-notifications) section of the Agent alert notifications page.