-
Notifications
You must be signed in to change notification settings - Fork 23
/
slack-service-notification.sh
80 lines (74 loc) · 1.98 KB
/
slack-service-notification.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
ICINGA_HOSTNAME="<YOUR_ICINGAWEB2_HOSTNAME>"
SLACK_WEBHOOK_URL="<YOUR_SLACK_WEBHOOK_INTEGRATION_URL>"
SLACK_CHANNEL="#alerts"
SLACK_BOTNAME="icinga2"
# Set the message icon based on ICINGA service state
if [ "$SERVICESTATE" = "CRITICAL" ]
then
COLOR="#FF5566"
elif [ "$SERVICESTATE" = "WARNING" ]
then
COLOR="#FFAA44"
elif [ "$SERVICESTATE" = "OK" ]
then
COLOR="#44BB77"
elif [ "$SERVICESTATE" = "UNKNOWN" ]
then
COLOR="#800080"
else
COLOR=""
fi
# Overwrite color for informational notification types
if [ "$NOTIFICATIONTYPE" = "ACKNOWLEDGEMENT" ] || [ "$NOTIFICATIONTYPE" = "DOWNTIMESTART" ] || [ "$NOTIFICATIONTYPE" = "DOWNTIMEEND" ]
then
COLOR="#7F7F7F"
fi
# Determine information to display
if [ -z "$NOTIFICATIONCOMMENT" ]
then
INFORMATION="${SERVICEOUTPUT}"
else
INFORMATION="${NOTIFICATIONCOMMENT}"
fi
# Send message to Slack
read -d '' PAYLOAD << EOF
{
"channel": "${SLACK_CHANNEL}",
"username": "${SLACK_BOTNAME}",
"attachments": [
{
"fallback": "${NOTIFICATIONTYPE} - ${SERVICESTATE}: ${HOSTDISPLAYNAME} - ${SERVICEDISPLAYNAME}",
"color": "${COLOR}",
"fields": [
{
"title": "Type",
"value": "${NOTIFICATIONTYPE}",
"short": true
},
{
"title": "State",
"value": "${SERVICESTATE}",
"short": true
},
{
"title": "Host",
"value": "<${ICINGA_HOSTNAME}/monitoring/host/services?host=${HOSTNAME}|${HOSTDISPLAYNAME}>",
"short": true
},
{
"title": "Service",
"value": "<${ICINGA_HOSTNAME}/monitoring/service/show?host=${HOSTNAME}&service=${SERVICEDESC}|${SERVICEDISPLAYNAME}>",
"short": true
},
{
"title": "Information",
"value": "${INFORMATION}",
"short": false
},
]
}
]
}
EOF
curl --connect-timeout 30 --max-time 60 -s -S -X POST -H 'Content-type: application/json' --data "${PAYLOAD}" "${SLACK_WEBHOOK_URL}"