forked from lazyfrosch/icinga2-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram-service-notification.sh
executable file
·74 lines (65 loc) · 2.2 KB
/
telegram-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
#!/usr/bin/env bash
while getopts 4:6:b:c:d:e:f:hi:l:n:o:r:s:t:u:v:w:x: opt
do
case "$opt" in
4) HOSTADDRESS=$OPTARG ;;
6) HOSTADDRESS6=$OPTARG ;;
b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
c) NOTIFICATIONCOMMENT=$OPTARG ;;
d) LONGDATETIME=$OPTARG ;; # required
e) SERVICENAME=$OPTARG ;; # required
f) MAILFROM=$OPTARG ;;
h) Usage ;;
i) ICINGAWEB2URL=$OPTARG ;;
l) HOSTNAME=$OPTARG ;; # required
n) HOSTDISPLAYNAME=$OPTARG ;; # required
o) SERVICEOUTPUT=$OPTARG ;; # required
r) USEREMAIL=$OPTARG ;; # required
s) SERVICESTATE=$OPTARG ;; # required
t) NOTIFICATIONTYPE=$OPTARG ;; # required
u) SERVICEDISPLAYNAME=$OPTARG ;; # required
v) VERBOSE=$OPTARG ;;
w) TELEGRAM_BOT_TOKEN=$OPTARG ;; # required
x) TELEGRAM_CHAT_ID=$OPTARG ;; # required
\?) echo "ERROR: Invalid option -$OPTARG" >&2
Usage ;;
:) echo "Missing option argument for -$OPTARG" >&2
Usage ;;
*) echo "Unimplemented option: -$OPTARG" >&2
Usage ;;
esac
done
if [ -n "$ICINGAWEB2URL" ]; then
HOSTDISPLAYNAME="<a href=\"$ICINGAWEB2URL/monitoring/host/show?host=$HOSTNAME\">$HOSTDISPLAYNAME</a>"
SERVICEDISPLAYNAME="<a href=\"$ICINGAWEB2URL/monitoring/service/show?host=$HOSTNAME&service=$SERVICENAME\">$SERVICEDISPLAYNAME</a>"
fi
template=$(cat <<TEMPLATE
<strong>$NOTIFICATIONTYPE</strong> $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE
<pre>$SERVICEOUTPUT</pre>
When: $LONGDATETIME
Service: $SERVICENAME
Host: $HOSTNAME
TEMPLATE
)
## Check whether IPv4 was specified.
if [ -n "$HOSTADDRESS" ] ; then
template="$template
IPv4: $HOSTADDRESS"
fi
## Check whether IPv6 was specified.
if [ -n "$HOSTADDRESS6" ] ; then
template="$template
IPv6: $HOSTADDRESS6"
fi
## Check whether author and comment was specified.
if [ -n "$NOTIFICATIONCOMMENT" ]; then
template="$template
Comment by $NOTIFICATIONAUTHORNAME:
$NOTIFICATIONCOMMENT"
fi
/usr/bin/curl --silent --output /dev/null \
--data-urlencode "chat_id=${TELEGRAM_CHAT_ID}" \
--data-urlencode "text=${template}" \
--data-urlencode "parse_mode=HTML" \
--data-urlencode "disable_web_page_preview=true" \
"https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage"