This repository has been archived by the owner on Mar 2, 2023. It is now read-only.
/
slack-service-notification.sh
executable file
·66 lines (61 loc) · 1.72 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
#!/bin/bash
# Configuration values
ICINGA_URL="ICINGA URL"
SLACK_WEBHOOK_URL="SLACK URL"
#Set the message icon based on ICINGA service state
case $SERVICESTATE in
CRITICAL)
_ICON=':bomb:'
_COLOR='danger'
;;
WARNING)
_ICON=':warning:'
_COLOR='warning'
;;
OK)
_ICON=':beer:'
_COLOR='good'
;;
UNKNOWN)
_ICON=':question:'
;;
*)
_ICON=':white_medium_square:'
;;
esac
# Helping variables
_HOSTNAME='<'${ICINGA_URL}'/icingaweb2/monitoring/host/services?host='${HOSTNAME}'|'${HOSTDISPLAYNAME}'>'
_SERVICE='<'${ICINGA_URL}'/icingaweb2/monitoring/service/show?host='${HOSTNAME}'&service='${SERVICEDESC}'|'${SERVICEDISPLAYNAME}'>'
_DASH_SERVICE=${ICINGA_URL}'/icingaweb2/dashboard#!/icingaweb2/monitoring/service/show?host='${HOSTNAME}'&service='${SERVICEDESC}
# Build the payload
PAYLOAD=$(cat << ENDJSON
{
"icon_emoji": "$_ICON",
"text": ":mega: New notification from icinga2",
"attachments": [
{
"title": "$SERVICEOUTPUT",
"title_link": "$_DASH_SERVICE",
"color": "$_COLOR",
"author_name": "icinga2",
"author_link": "http://www.icinga.org",
"author_icon": "https://wiki.icinga.org/download/attachments/131074/global.logo",
"fields": [
{
"title": "HOST",
"value": "$_HOSTNAME",
"short": true
},
{
"title": "SERVICE",
"value": "$_SERVICE",
"short": true
}
]
}
]
}
ENDJSON
)
#Send message to Slack
curl --connect-timeout 30 --max-time 60 -sS -H "Content-Type: application/json" -X POST -d "$PAYLOAD" "$SLACK_WEBHOOK_URL"