From 75a6779bb2aab70f709c10ae808c6b9df9176f8a Mon Sep 17 00:00:00 2001 From: Patrick Schindelmann Date: Mon, 13 Jan 2020 11:22:31 +0100 Subject: [PATCH] Notification: Pushover MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Alle benötigten Scripte und Konfigurations-Dateien wurden hinzugefügt --- notification/pushover.sh | 51 ------ .../commands/pushover-notification.conf | 54 +++++++ .../pushover/hosts/PushoverSampleConfig.conf | 46 ++++++ .../pushover/notifications/pushover.conf | 20 +++ .../scripts/pushover-host-notification.sh | 147 +++++++++++++++++ .../scripts/pushover-service-notification.sh | 150 ++++++++++++++++++ notification/pushover/templates/pushover.conf | 19 +++ notification/pushover/usergroups/alle.conf | 4 + notification/pushover/users/alle.conf | 23 +++ 9 files changed, 463 insertions(+), 51 deletions(-) delete mode 100755 notification/pushover.sh create mode 100644 notification/pushover/commands/pushover-notification.conf create mode 100644 notification/pushover/hosts/PushoverSampleConfig.conf create mode 100644 notification/pushover/notifications/pushover.conf create mode 100755 notification/pushover/scripts/pushover-host-notification.sh create mode 100755 notification/pushover/scripts/pushover-service-notification.sh create mode 100644 notification/pushover/templates/pushover.conf create mode 100644 notification/pushover/usergroups/alle.conf create mode 100644 notification/pushover/users/alle.conf diff --git a/notification/pushover.sh b/notification/pushover.sh deleted file mode 100755 index f66cffe..0000000 --- a/notification/pushover.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash - -#################################################################################################################### -# -# full dokumentation on https://pushover.net/api -# -# POST an HTTPS request to https://api.pushover.net/1/messages.json with the following parameters: -# token (required) - your application's API token -# user (required) - the user/group key (not e-mail address) of your user (or you), -# viewable when logged into our dashboard (often referred to as USER_KEY in our documentation and code examples) -# message (required) - your message -# -# Some optional parameters may be included: -# device - your user's device name to send the message directly to that device, -# rather than all of the user's devices (multiple devices may be separated by a comma) -# title - your message's title, otherwise your app's name is used -# url - a supplementary URL to show with your message -# url_title - a title for your supplementary URL, otherwise just the URL is shown -# priority - send as -# -2 to generate no notification/alert, -# -1 to always send as a quiet notification, -# 1 to display as high-priority and bypass the user's quiet hours, or -# 2 to also require confirmation from the user -# timestamp - a Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API -# sound - the name of one of the sounds supported by device clients to override the user's default sound choice -# -# That's it. Make sure your application is friendly to our API servers and you're all set. -# For more information on each parameter, keep reading or jump to a section at the left. -# -# Need help using our API or found an error in the documentation? Drop us a line. -# -#################################################################################################################### -PUSHOVERTOKEN="asphg1hm9qecnzyrc8i7k5oy1efhuk" -PUSHOVERUSER="u44drxvefhqrehxid8f131d9v5nit4" -PUSHOVERTITLE="Icinga Home" -PUSHOVERMESSAGE="Dies ist eine Meldung -$(date)" -PUSHDEVICE="" -PUSHPRIORITY="1" -CURLPROXY="" - -curl \ - --silent \ - `#--insecure --proxy "$CURLPROXY" ` \ - --form-string "token=$PUSHOVERTOKEN" \ - --form-string "user=$PUSHOVERUSER" \ - --form-string "message=$PUSHOVERMESSAGE" \ - --form-string "title=$PUSHOVERTITLE" \ - --form-string "priority=$PUSHPRIORITY" \ - https://api.pushover.net/1/messages.json - diff --git a/notification/pushover/commands/pushover-notification.conf b/notification/pushover/commands/pushover-notification.conf new file mode 100644 index 0000000..eb72f1c --- /dev/null +++ b/notification/pushover/commands/pushover-notification.conf @@ -0,0 +1,54 @@ +object NotificationCommand "pushover-host-notification" { +import "plugin-notification-command" + +command = [ SysconfDir + "/icinga2/scripts/pushover-host-notification.sh" ] + +env = { + PUSHOVERUSER = "$user.vars.pushover_user$" + PUSHOVERTOKEN = "$user.vars.pushover_token$" + PUSHOVERDEVICE = "$user.vars.pushover_device$" + + PUSHOVERPRIORITY = "$host.vars.pushover_priority$" + PUSHOVERRETRY = "$host.vars.pushover_retry$" + PUSHOVEREXPIRE = "$host.vars.pushover_expire$" + + NOTIFICATIONTYPE = "$notification.type$" + NOTIFICATIONCOMMENT = "$notification.comment$" + NOTIFICATIONAUTHOR = "$notification.author$" + + ICINGALONGDATETIME = "$icinga.long_date_time$" + + HOSTNAME = "$host.name$" + HOSTDISPLAYNAME = "$host.display_name$" + HOSTSTATE = "$host.state$" + HOSTOUTPUT = "$host.output$" + } +} + +object NotificationCommand "pushover-service-notification" { + import "plugin-notification-command" + + command = [ SysconfDir + "/icinga2/scripts/pushover-service-notification.sh" ] + + env = { + PUSHOVERUSER = "$user.vars.pushover_user$" + PUSHOVERTOKEN = "$user.vars.pushover_token$" + PUSHOVERDEVICE = "$user.vars.pushover_device$" + + PUSHOVERPRIORITY = "$service.vars.pushover_priority$" + PUSHOVERRETRY = "$service.vars.pushover_retry$" + PUSHOVEREXPIRE = "$service.vars.pushover_expire$" + + NOTIFICATIONTYPE = "$notification.type$" + NOTIFICATIONCOMMENT = "$notification.comment$" + NOTIFICATIONAUTHOR = "$notification.author$" + + ICINGALONGDATETIME = "$icinga.long_date_time$" + + HOSTNAME = "$host.name$" + SERVICENAME = "$service.name$" + HOSTDISPLAYNAME = "$host.display_name$" + SERVICESTATE = "$service.state$" + SERVICEOUTPUT = "$service.output$" + } +} diff --git a/notification/pushover/hosts/PushoverSampleConfig.conf b/notification/pushover/hosts/PushoverSampleConfig.conf new file mode 100644 index 0000000..fc4cc8c --- /dev/null +++ b/notification/pushover/hosts/PushoverSampleConfig.conf @@ -0,0 +1,46 @@ +/* +# +# Beispielkonfiguration für Pushover +# + +object Host "" { + import "generic-host" + + display_name = "" + address = "" + + vars.os = "" + + #Benachrichtigung via email + vars.notification["mail"] = { + groups = [ "icingaadmins" ] + } + + //Benachrichtigung via Pushover + vars.notification["pushover"] = { + groups = [ "icingaadmins-pushover" ] + } + + #Auf welchem Gerät soll die Meldung ausgegeben werden + #mehrer Devices: [ "device 1", "device 2" ] + vars.pushover_device = [ "" ] + + #Priorität der Pushover-Meldung (https://pushover.net/api#priority) + -2: Lowest Priority + -1: Low Priority + 0: Normal Priority (default) + 1: High Priority + 2: Emergency Priority + vars.pushover_priority = "2" + + #Wie oft (in Sekunden) wird der Pushover-Server die Nachricht senden + #Der Wert muss >= 30 sein + vars.pushover_retry = "30" + + #Wie lange (in Sedkunden) soll der Retry-Prozess wiederholt werden + #Der wert muss muss kleiner-gleich 10800 sein (3Stunden) + vars.pushover_expire = "300" +} +*/ + + diff --git a/notification/pushover/notifications/pushover.conf b/notification/pushover/notifications/pushover.conf new file mode 100644 index 0000000..0a6fa5d --- /dev/null +++ b/notification/pushover/notifications/pushover.conf @@ -0,0 +1,20 @@ +apply Notification "pushover-icingaadmin" to Host { + import "pushover-host-notification" + + user_groups = host.vars.notification.pushover.groups + users = host.vars.notification.pushover.users + + assign where host.vars.notification.pushover + interval = 0 // disable re-notification +} + + +apply Notification "pushover-icingaadmin" to Service { + import "pushover-service-notification" + + user_groups = host.vars.notification.pushover.groups + users = host.vars.notification.pushover.users + + assign where host.vars.notification.pushover + interval = 0 // disable re-notification +} diff --git a/notification/pushover/scripts/pushover-host-notification.sh b/notification/pushover/scripts/pushover-host-notification.sh new file mode 100755 index 0000000..78bc688 --- /dev/null +++ b/notification/pushover/scripts/pushover-host-notification.sh @@ -0,0 +1,147 @@ +#!/bin/bash + +#################################################################################################################### +# +# full dokumentation on https://pushover.net/api +# +# POST an HTTPS request to https://api.pushover.net/1/messages.json with the following parameters: +# token (required) - your application's API token +# user (required) - the user/group key (not e-mail address) of your user (or you), +# viewable when logged into our dashboard (often referred to as USER_KEY in our documentation and code examples) +# message (required) - your message +# +# Some optional parameters may be included: +# device - your user's device name to send the message directly to that device, +# rather than all of the user's devices (multiple devices may be separated by a comma) +# title - your message's title, otherwise your app's name is used +# url - a supplementary URL to show with your message +# url_title - a title for your supplementary URL, otherwise just the URL is shown +# priority - send as +# -2 to generate no notification/alert, +# -1 to always send as a quiet notification, +# 0 (default) to send notification with sound, vibration and display(not in quiet hours) +# 1 to display as high-priority and bypass the user's quiet hours, or +# 2 to also require confirmation from the user +# retry +# expire +# timestamp - a Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API +# sound - the name of one of the sounds supported by device clients to override the user's default sound choice +# +# That's it. Make sure your application is friendly to our API servers and you're all set. +# For more information on each parameter, keep reading or jump to a section at the left. +# +# Need help using our API or found an error in the documentation? Drop us a line. +# +#################################################################################################################### + +logpath="/var/log/pushover_icinga.txt" +ICINGA2HOST="$(hostname)" +CURLPROXY="" +debug="0" + +##################################################### +#Übergebene Parameter +# +# PUSHOVERUSER = "$user.vars.pushover_user$" +# PUSHOVERTOKEN = "$user.vars.pushover_token$" +# PUSHOVERDEVICE = "$user.vars.pushover_device$" +# +# PUSHOVERPRIORITY = "$host.vars.pushover_priority$" +# PUSHOVERRETRY = "$host.vars.pushover_retry$" +# PUSHOVEREXPIRE = "$host.vars.pushover_expire$" +# +# NOTIFICATIONTYPE = "$notification.type$" +# NOTIFICATIONCOMMENT = "$notification.comment$" +# NOTIFICATIONAUTHOR = "$notification.author$" +# +# ICINGALONGDATETIME = "$icinga.long_date_time$" +# +# HOSTNAME = "$host.name$" +# HOSTDISPLAYNAME = "$host.display_name$" +# HOSTSTATE = "$host.state$" +# HOSTOUTPUT = "$host.output$" +# +##################################################### + +#***** Host Monitoring on $ICINGA2HOST ***** +PUSHOVERMESSAGE=$(cat << EOF +***** Host Monitoring on icinga ***** +$HOSTDISPLAYNAME is $HOSTSTATE! +Info: $HOSTOUTPUT +When: $ICINGALONGDATETIME +Host: $HOSTNAME +EOF +) + +#Wenn ein Kommentar eingetragen wurde (Downtimekommentar, Benachrichtigungskommentar), wird dieser angehangen +if [ -n "$NOTIFICATIONCOMMENT" ] +then +PUSHOVERMESSAGE=$(cat << EOF +$PUSHOVERMESSAGE +Comment: $NOTIFICATIONCOMMENT +Author: $NOTIFICATIONAUTHOR +EOF +) +fi + +PUSHOVERTITLE="$NOTIFICATIONTYPE - Host $HOSTDISPLAYNAME is $HOSTSTATE!" + +#Wenn die Priorität 2 vergeben wurde, ist ein retry zwingend erforderlich +#Sollte retry nicht gesetzt sein, wird er auf 30 gesetzt +if [ "$PUSHOVERPRIORITY" = "2" ] && [ "$PUSHOVERRETRY" = "" ] +then + PUSHOVERRETRY="30" +fi + +#Wenn die Priorität 2 vergeben wurde, ist ein expire zwingend erforderlich +#Sollte expire nicht gesetzt sein, wird er auf 300 gesetzt +if [ "$PUSHOVERPRIORITY" = "2" ] && [ "$PUSHOVEREXPIRE" = "" ] +then + PUSHOVEREXPIRE="300" +fi + + +#Kommando, um per curl die Pushover-message zu verschicken +failstate=$(curl \ + --silent \ + --insecure --proxy "$CURLPROXY" \ + --form-string "token=$PUSHOVERTOKEN" \ + --form-string "user=$PUSHOVERUSER" \ + --form-string "message=$PUSHOVERMESSAGE" \ + --form-string "title=$PUSHOVERTITLE" \ + --form-string "priority=$PUSHOVERPRIORITY" \ + --form-string "retry=$PUSHOVERRETRY" \ + --form-string "expire=$PUSHOVEREXPIRE" \ + --form-string "device=$PUSHOVERDEVICE" \ + --location https://api.pushover.net/1/messages.json) + + +#Wenn das debugging eingeschaltet ist, wird die folgende Meldung ausgegeben +#$logpath sollte vorhanden sein und auf nagios:nagios gesetzt sein +if [ "$debug" = "1" ] +then +cat << EOF >> "$logpath" +########################################### +Debugging-Tool +########################################### +DatumZeit: $(date) +PUSHOVERTOKEN: $PUSHOVERTOKEN +PUSHOVERUSER: $PUSHOVERUSER +PUSHOVERTITLE: $PUSHOVERTITLE +PUSHOVERDEVICE: $PUSHOVERDEVICE +PUSHOVERPRIORITY: $PUSHOVERPRIORITY +PUSHOVERRETRY: $PUSHOVERRETRY +PUSHOVEREXPIRE: $PUSHOVEREXPIRE +HOSTDISPLAYNAME: $HOSTDISPLAYNAME +ICINGALONGDATETIME: $ICINGALONGDATETIME +NOTIFICATIONTYPE: $NOTIFICATIONTYPE +NOTIFICATIONCOMMENT: $NOTIFICATIONCOMMENT +NOTIFICATIONAUTHOR: $NOTIFICATIONAUTHOR +ICINGA2HOST: $ICINGA2HOST +HOSTNAME: $HOSTNAME +HOSTSTATE: $HOSTSTATE +HOSTOUTPUT: $HOSTOUTPUT +pushover json output: $failstate + +EOF +fi diff --git a/notification/pushover/scripts/pushover-service-notification.sh b/notification/pushover/scripts/pushover-service-notification.sh new file mode 100755 index 0000000..ccada77 --- /dev/null +++ b/notification/pushover/scripts/pushover-service-notification.sh @@ -0,0 +1,150 @@ +#!/bin/bash + +#################################################################################################################### +# +# full dokumentation on https://pushover.net/api +# +# POST an HTTPS request to https://api.pushover.net/1/messages.json with the following parameters: +# token (required) - your application's API token +# user (required) - the user/group key (not e-mail address) of your user (or you), +# viewable when logged into our dashboard (often referred to as USER_KEY in our documentation and code examples) +# message (required) - your message +# +# Some optional parameters may be included: +# device - your user's device name to send the message directly to that device, +# rather than all of the user's devices (multiple devices may be separated by a comma) +# title - your message's title, otherwise your app's name is used +# url - a supplementary URL to show with your message +# url_title - a title for your supplementary URL, otherwise just the URL is shown +# priority - send as +# -2 to generate no notification/alert, +# -1 to always send as a quiet notification, +# 0 (default) to send notification with sound, vibration and display(not in quiet hours) +# 1 to display as high-priority and bypass the user's quiet hours, or +# 2 to also require confirmation from the user +# retry +# expire +# timestamp - a Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API +# sound - the name of one of the sounds supported by device clients to override the user's default sound choice +# +# That's it. Make sure your application is friendly to our API servers and you're all set. +# For more information on each parameter, keep reading or jump to a section at the left. +# +# Need help using our API or found an error in the documentation? Drop us a line. +# +#################################################################################################################### + +logpath="/var/log/pushover_icinga.txt" +ICINGA2HOST="$(hostname)" +CURLPROXY="" +debug="1" + +##################################################### +#Übergebene Parameter +# +# PUSHOVERUSER = "$user.vars.pushover_user$" +# PUSHOVERTOKEN = "$user.vars.pushover_token$" +# PUSHOVERDEVICE = "$user.vars.pushover_device$" +# +# PUSHOVERPRIORITY = "$service.vars.pushover_priority$" +# PUSHOVERRETRY = "$service.vars.pushover_retry$" +# PUSHOVEREXPIRE = "$service.vars.pushover_expire$" +# +# NOTIFICATIONTYPE = "$notification.type$" +# NOTIFICATIONCOMMENT = "$notification.comment$" +# NOTIFICATIONAUTHOR = "$notification.author$" +# +# ICINGALONGDATETIME = "$icinga.long_date_time$" +# +# HOSTNAME = "$host.name$" +# SERVICENAME = "$service.name$" +# HOSTDISPLAYNAME = "$host.display_name$" +# SERVICESTATE = "$service.state$" +# SERVICEOUTPUT = "$service.output$" +# +##################################################### + +#***** Service Monitoring on $ICINGA2HOST ***** +PUSHOVERMESSAGE=$(cat << EOF +***** Service Monitoring on icinga ***** +$SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is ${SERVICESTATE}! +Info: $SERVICEOUTPUT +When: $ICINGALONGDATETIME +Service: $SERVICENAME +Host: $HOSTNAME +EOF +) + +#Wenn ein Kommentar eingetragen wurde (Downtimekommentar, Benachrichtigungskommentar), wird dieser angehangen +if [ -n "$NOTIFICATIONCOMMENT" ] +then +PUSHOVERMESSAGE=$(cat << EOF +$PUSHOVERMESSAGE +Comment: $NOTIFICATIONCOMMENT +Author: $NOTIFICATIONAUTHOR +EOF +) +fi + +PUSHOVERTITLE="$NOTIFICATIONTYPE - $HOSTDISPLAYNAME - $SERVICEDISPLAYNAME is $SERVICESTATE" + +#Wenn die Priorität 2 vergeben wurde, ist ein retry zwingend erforderlich +#Sollte retry nicht gesetzt sein, wird er auf 30 gesetzt +if [ "$PUSHOVERPRIORITY" = "2" ] && [ "$PUSHOVERRETRY" = "" ] +then + PUSHOVERRETRY="30" +fi + +#Wenn die Priorität 2 vergeben wurde, ist ein expire zwingend erforderlich +#Sollte expire nicht gesetzt sein, wird er auf 300 gesetzt +if [ "$PUSHOVERPRIORITY" = "2" ] && [ "$PUSHOVEREXPIRE" = "" ] +then + PUSHOVEREXPIRE="300" +fi + + +#Kommando, um per curl die Pushover-message zu verschicken +failstate=$(curl \ + --silent \ + --insecure --proxy "$CURLPROXY" \ + --form-string "token=$PUSHOVERTOKEN" \ + --form-string "user=$PUSHOVERUSER" \ + --form-string "message=$PUSHOVERMESSAGE" \ + --form-string "title=$PUSHOVERTITLE" \ + --form-string "priority=$PUSHOVERPRIORITY" \ + --form-string "retry=$PUSHOVERRETRY" \ + --form-string "expire=$PUSHOVEREXPIRE" \ + --form-string "device=$PUSHOVERDEVICE" \ + --location https://api.pushover.net/1/messages.json) + + +#Wenn das debugging eingeschaltet ist, wird die folgende Meldung ausgegeben +#$logpath sollte vorhanden sein und auf nagios:nagios gesetzt sein +if [ "$debug" = "1" ] +then +cat << EOF >> "$logpath" +########################################### +Debugging-Tool +########################################### +DatumZeit: $(date) +PUSHOVERTOKEN: $PUSHOVERTOKEN +PUSHOVERUSER: $PUSHOVERUSER +PUSHOVERTITLE: $PUSHOVERTITLE +PUSHOVERDEVICE: $PUSHOVERDEVICE +PUSHOVERPRIORITY: $PUSHOVERPRIORITY +PUSHOVERRETRY: $PUSHOVERRETRY +PUSHOVEREXPIRE: $PUSHOVEREXPIRE +NOTIFICATIONTYPE: $NOTIFICATIONTYPE +NOTIFICATIONCOMMENT: $NOTIFICATIONCOMMENT +NOTIFICATIONAUTHOR: $NOTIFICATIONAUTHOR +HOSTDISPLAYNAME: $HOSTDISPLAYNAME +SERVICEDISPLAYNAME: $SERVICEDISPLAYNAME +SERVICESTATE: $SERVICESTATE +ICINGALONGDATETIME: $ICINGALONGDATETIME +ICINGA2HOST: $ICINGA2HOST +SERVICEOUTPUT: $SERVICEOUTPUT +SERVICENAME: $SERVICENAME +pushover json output: $failstate + +EOF +fi diff --git a/notification/pushover/templates/pushover.conf b/notification/pushover/templates/pushover.conf new file mode 100644 index 0000000..992a6f8 --- /dev/null +++ b/notification/pushover/templates/pushover.conf @@ -0,0 +1,19 @@ +template Notification "pushover-host-notification" { + command = "pushover-host-notification" + + states = [ Up, Down ] + types = [ Problem, Acknowledgement, Recovery, Custom, + FlappingStart, FlappingEnd, + DowntimeStart, DowntimeEnd, DowntimeRemoved ] + + period = "24x7" +} + +template Notification "pushover-service-notification" { + command = "pushover-service-notification" + + states = [ OK, Warning, Critical, Unknown ] + types = [ Problem, Acknowledgement, Recovery, Custom, FlappingStart, FlappingEnd, DowntimeStart, DowntimeEnd, DowntimeRemoved ] + + period = "24x7" +} diff --git a/notification/pushover/usergroups/alle.conf b/notification/pushover/usergroups/alle.conf new file mode 100644 index 0000000..dda9671 --- /dev/null +++ b/notification/pushover/usergroups/alle.conf @@ -0,0 +1,4 @@ +object UserGroup "icingaadmins-pushover" { + display_name = "Icinga 2 Admin Group - Pushover" +} + diff --git a/notification/pushover/users/alle.conf b/notification/pushover/users/alle.conf new file mode 100644 index 0000000..1f6dc1c --- /dev/null +++ b/notification/pushover/users/alle.conf @@ -0,0 +1,23 @@ +object User "patrick" { + import "generic-user" + + display_name = "Patrick" + # groups = [ "icingaadmins" , "icingaadmins-pushover" ] + groups = [ "icingaadmins-pushover" ] + + email = "root@localhost" + + #Pushover Messages + vars.pushover_user = "" + vars.pushover_token = "" + #Devices + #(mehrere devices = [ "device1", "device2" ] + vars.pushover_device = [ "" ] + + # vars.pushover_priority = "2" + # retry >= 30 + # vars.pushover_retry = "30" + # expire <= 10800 + # vars.pushover_expire = "300" +} +