-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathslack-host-notification.sh
69 lines (63 loc) · 1.62 KB
/
slack-host-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
#!/bin/bash
ICINGA_HOSTNAME="<YOUR_ICINGAWEB2_HOSTNAME>"
SLACK_WEBHOOK_URL="<YOUR_SLACK_WEBHOOK_INTEGRATION_URL>"
SLACK_CHANNEL="#alerts"
SLACK_BOTNAME="icinga2"
# Set the color based on host state
if [ "$HOSTSTATE" = "DOWN" ]
then
COLOR="#FF5566"
elif [ "$HOSTSTATE" = "UP" ]
then
COLOR="#44BB77"
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="${HOSTOUTPUT}"
else
INFORMATION="${NOTIFICATIONCOMMENT}"
fi
# Send message to Slack
read -d '' PAYLOAD << EOF
{
"channel": "${SLACK_CHANNEL}",
"username": "${SLACK_BOTNAME}",
"attachments": [
{
"fallback": "${NOTIFICATIONTYPE} - ${HOSTSTATE}: ${HOSTDISPLAYNAME}",
"color": "${COLOR}",
"fields": [
{
"title": "Type",
"value": "${NOTIFICATIONTYPE}",
"short": true
},
{
"title": "State",
"value": "${HOSTSTATE}",
"short": true
},
{
"title": "Host",
"value": "<${ICINGA_HOSTNAME}/monitoring/host/services?host=${HOSTNAME}|${HOSTDISPLAYNAME}>",
"short": true
},
{
"title": "Information",
"value": "${HOSTOUTPUT}\n${NOTIFICATIONCOMMENT}",
"short": false
}
]
}
]
}
EOF
curl --connect-timeout 30 --max-time 60 -s -S -X POST -H 'Content-type: application/json' --data "${PAYLOAD}" "${SLACK_WEBHOOK_URL}"