forked from happycodinguk/telegram-system-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem-warnings.sh
66 lines (58 loc) · 2.31 KB
/
system-warnings.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
######################################################################
######## SYSTEMD Service RESTART and WARNING SCRIPT ##################
######################################################################
# Hope people enjoy the many hours I have put into this project!
# This project is created with my knowledge and some snippets from
# across the open web. If there are snippets you would like credit for.
# Drop me a line and I'll add you!
# Anyone. Feel free to clone and improve.
####################################################
# DM me on: #
# Telegram: https://t.me/joinchat/xlmtm7jVYR4yODQ0 #
####################################################
source "YOUR_INSTALL_DIR"/telegram.conf
echo "$token" /dev/null 2&>1
echo "$chat_id" /dev/null 2&>1
#These are emoji codes
# go to https://unicode.org/emoji/charts/full-emoji-list.html
# you will need its "U+1F4EB" code MINUS the + As seen below
bell=$'U1F514'
toolbox=$'\U1F9F0'
policelight=$'\U1F6A8'
exclamation=$'\U2757'
disk=$'\U1F4BD'
##list your SYSTEM services you want to check on!
SERVICES=( "SERVICES_TO_MONITOR" )
#Telegram API to send notificaiton.
function telegram_send
{
curl -s -X POST https://api.telegram.org/bot"$token"/sendMessage -d chat_id="$chat_id" -d text="$policelight System Service Warning
$SUBJECT
$toolbox $MESSAGE"
}
while true :
do
for i in "${SERVICES[@]}"
do
if [[ "$(systemctl show -p ActiveState "$i")" =~ "inactive" ]]
then
sleep 5
service "$i" start
MESSAGE="$(tail -5 /var/log/"$i"/error.log)"
SUBJECT="$i is inactive on $(hostname) $(date) - Please investigate!"
echo "$MESSAGE" | telegram_send
sleep "YOUR_RESET_TIMING" #stop executing script for?
fi
if [[ "$(systemctl show -p ActiveState "$i")" =~ "failed" ]]
then
sleep 5
service "$i" start
MESSAGE="$(tail -5 /var/log/"$i"/error.log)"
SUBJECT="$i is failed on $(hostname) $(date) - Please investigate!"
echo "$MESSAGE" | telegram_send
sleep "YOUR_RESET_TIMING" #stop executing script for?
fi
done
sleep 20
done