Created
April 6, 2023 15:19
-
-
Save szz/348afbcf3a47e91e49e3eac25fe294e2 to your computer and use it in GitHub Desktop.
Send an alert via Slack if a UPS event happens. It is usable with nut/upsmon.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Send an alert via Slack if a UPS event happens. It is usable with nut/upsmon. | |
| # licence: CC0 | |
| # Slack notifier sctipz to use with nut | |
| # configure upsmon.conf like this: | |
| # NOTIFYMSG ONLINE "1 UPS %s on line power :electric_plug:" | |
| # NOTIFYMSG ONBATT "0 UPS %s on battery :battery:" | |
| # NOTIFYMSG LOWBATT "0 UPS %s battery is low :low_battery:" | |
| # NOTIFYMSG FSD "0 UPS %s: forced shutdown in progress :small_red_triangle_down:" | |
| # NOTIFYMSG COMMOK "1 Communications with UPS %s established" | |
| # NOTIFYMSG COMMBAD "0 Communications with UPS %s lost" | |
| # NOTIFYMSG SHUTDOWN "0 Auto logout and shutdown proceeding" | |
| # NOTIFYMSG REPLBATT "0 UPS %s battery needs to be replaced" | |
| # NOTIFYMSG NOCOMM "0 UPS %s is unavailable" | |
| # NOTIFYMSG NOPARENT "0 upsmon parent process died - shutdown impossible" | |
| # if a line starts with 1 gets a green bar, otherwise red | |
| # slack token | |
| # more info: | |
| # https://api.slack.com/tutorials/tracks/posting-messages-with-curl | |
| # https://api.slack.com/methods/chat.postMessage | |
| # enable "icon_emoji" needs chat:write.customize permission in the oauth_config » scopes » bot path | |
| TOKEN="xoxb-not-a-real-token-this-will-not-work" #DO NOT COMMIT A REAL TOKEN TO PUBLIC PLACE | |
| WEBHOOOK="https://slack.com/api/chat.postMessage" | |
| CHANNEL="#general" | |
| TEXT=${@} | |
| COLOR_RED="#f00" | |
| COLOR_GREEN="#0f0" | |
| TITLE="UPS Alert @$(hostname)" | |
| COLOR=${COLOR_RED} | |
| if [[ ${TEXT} = 1* ]] | |
| then | |
| COLOR=${COLOR_GREEN} | |
| fi | |
| TEXT=${TEXT:2} | |
| DATA=$( | |
| jq -n \ | |
| --arg channel "${CHANNEL}" \ | |
| --arg title "${TITLE}" \ | |
| --arg text "${TEXT}" \ | |
| --arg color "${COLOR}" \ | |
| '{ | |
| channel: $channel, | |
| icon_emoji: ":zap:", | |
| attachments: [{ | |
| color: $color, | |
| title: $title, | |
| text: $text, | |
| }] | |
| }' | |
| ) | |
| curl \ | |
| -X POST \ | |
| -H "Authorization: Bearer ${TOKEN}" \ | |
| -H "Content-type: application/json; charset=utf-8" \ | |
| --data "${DATA}" \ | |
| "${WEBHOOOK}" > /dev/null 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment