Skip to content

Instantly share code, notes, and snippets.

@asteriskie
Created November 2, 2023 19:31
Show Gist options
  • Save asteriskie/d3638f1954f1681caf2f9b013db6a20b to your computer and use it in GitHub Desktop.
Save asteriskie/d3638f1954f1681caf2f9b013db6a20b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Notify via Discord - Shell Script
#----This is written for CheckMK to send notifications via Discord
#There was a python version of this but in a docker container...python doesn't have some modules. SO I wrote it for bash.
# Discord webhook URL
DISCORD_WEBHOOK_URL="$NOTIFY_PARAMETER_1"
# Discord User
USERNAME="Check_MK"
AVATARURL="https://checkmk.com/favicon-96x96.png"
getcolor() {
case $1 in
'CRIT' | 'DOWN' ) color=15597568;;
'WARNING' ) color=16768256;;
'OK' | 'UP' ) color=52224;;
'UNKNOWN' |'UNREACHABLE' ) color=13421772;;
'PROBLEM' ) color=16711680;;
'RECOVERY' ) color=16711680;;
'*' ) color=0;;
esac
echo $color
}
facts_service=""
case $NOTIFY_WHAT in
'SERVICE' )
TITLE="Check_MK Service Alert"
state=${NOTIFY_SERVICESTATE}
color=$(getcolor $state)
subtitle="Service Notification: ${NOTIFY_SERVICEDESC}"
read -r -d '' facts_service <<EOS
{"name": "Service", "value": "${NOTIFY_SERVICEDESC}"}
EOS
output=$NOTIFY_SERVICEOUTPUT
;;
'HOST' )
TITLE="Check_MK Host Alert"
state=$NOTIFY_HOSTSTATE
color=$(getcolor $state)
subtitle="Host Notification"
output=$NOTIFY_HOSTOUTPUT
;;
esac
read -r -d '' facts <<EOS
[
$facts_service,
{"name": "Host", "value": "$NOTIFY_HOSTNAME"},
{"name": "State:", "value":"$state"}
]
EOS
read -r -d '' MSG <<EOS
{
"username": "$USERNAME",
"avatar_url": "$AVATARURL",
"embeds": [
{
"title": "$subtitle",
"color": $color,
"fields": $facts,
"footer": {
"text": "$output"
}
}
]
}
EOS
#echo $MSG
curl -H "Content-Type: application/json" -d "$MSG" "$DISCORD_WEBHOOK_URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment