etc: Make is-alive-bot.sh more general purpose
@60610fdd779a5e5e22d9e6f47275f3eceed57ed1
--- is-alive-bot.sh
+++ is-alive-bot.sh
... | ... | @@ -13,12 +13,20 @@ |
13 | 13 |
# USER_CHAT_ID=123456789 |
14 | 14 |
USER_CHAT_ID="" |
15 | 15 |
|
16 |
-# Yona Server |
|
17 |
-# YONA_SERVER="http://my-yona-server.com" |
|
18 |
-YONA_SERVER="http://127.0.0.1:9000" |
|
16 |
+# TARGET_SERVER="http://my-yona-server.com" |
|
17 |
+TARGET_SERVER="http://127.0.0.1:9000" |
|
18 |
+ |
|
19 |
+CHECK_URL="$TARGET_SERVER/-_-api/v1/hello" |
|
20 |
+ |
|
21 |
+EXPECTED_STATUS_CODE=200 |
|
19 | 22 |
|
20 | 23 |
# Polling Time (sec) |
21 | 24 |
POLLING_TIME=60 |
25 |
+ |
|
26 |
+# Messages |
|
27 |
+messageOnDown="Yona is unavaliable!" |
|
28 |
+messageOnRevive="Yona comeback!" |
|
29 |
+ |
|
22 | 30 |
|
23 | 31 |
# Configurations end here... |
24 | 32 |
|
... | ... | @@ -32,6 +40,8 @@ |
32 | 40 |
echo "" |
33 | 41 |
echo "ex>" |
34 | 42 |
echo "sh is-alive-bot.sh 328394984:AAFhL69afasfqjtUtIeRSzIagVYw7H3zF4" |
43 |
+ echo "" |
|
44 |
+ echo "See: https://core.telegram.org/bots#3-how-do-i-create-a-bot" |
|
35 | 45 |
exit 1 |
36 | 46 |
fi |
37 | 47 |
else |
... | ... | @@ -44,6 +54,8 @@ |
44 | 54 |
then |
45 | 55 |
echo "---------------------------------------------------" |
46 | 56 |
echo "Find chat id of user or group with your own eyes.. " |
57 |
+ echo "" |
|
58 |
+ echo "..chat\":{ \"id\": 621884 ... <= user or group id!" |
|
47 | 59 |
echo "---------------------------------------------------" |
48 | 60 |
echo "" |
49 | 61 |
curl -sL https://api.telegram.org/bot$API_TOKEN/getUpdates |
... | ... | @@ -63,15 +75,11 @@ |
63 | 75 |
|
64 | 76 |
### preparing for message and server status check |
65 | 77 |
|
66 |
-message="" |
|
67 |
-messageOnDown="Yona is unavaliable!" |
|
68 |
-messageOnRevive="Yona comeback!" |
|
69 |
- |
|
70 | 78 |
sendMessageToTelegram() { |
71 | 79 |
curl -s \ |
72 | 80 |
-X POST \ |
73 | 81 |
https://api.telegram.org/bot$API_TOKEN/sendMessage \ |
74 |
- -d text="$message" \ |
|
82 |
+ -d text="$1" \ |
|
75 | 83 |
-d chat_id=$USER_CHAT_ID > /dev/null |
76 | 84 |
} |
77 | 85 |
|
... | ... | @@ -82,29 +90,26 @@ |
82 | 90 |
echo "Monitoring started... $(date +%Y-%m-%d" "%H:%M:%S)" |
83 | 91 |
echo "" |
84 | 92 |
|
85 |
-message="Monitoring started... - $YONA_SERVER" |
|
86 |
-sendMessageToTelegram |
|
93 |
+sendMessageToTelegram "Monitoring started... - $TARGET_SERVER" |
|
87 | 94 |
|
88 | 95 |
while true |
89 | 96 |
do |
90 | 97 |
NOW=$(date +%Y-%m-%d" "%H:%M:%S) |
91 |
- curl -sL -H "Accept: application/json" $YONA_SERVER/-_-api/v1/hello | grep "\"ok\":true" > /dev/null |
|
92 |
- if [ $? != 0 ] |
|
98 |
+ response=$(curl -I $CHECK_URL 2> /dev/null | head -n 1 | cut -d$' ' -f2) |
|
99 |
+ if [ -z $response ] || [ $response != $EXPECTED_STATUS_CODE ] |
|
93 | 100 |
then |
94 | 101 |
if [ $isDown == false ] |
95 | 102 |
then |
96 | 103 |
echo "Sending message - $messageOnDown - $NOW" |
97 | 104 |
isDown=true |
98 |
- message="$messageOnDown - $YONA_SERVER" |
|
99 |
- sendMessageToTelegram |
|
105 |
+ sendMessageToTelegram "$messageOnDown $response - $TARGET_SERVER" |
|
100 | 106 |
fi |
101 | 107 |
else |
102 | 108 |
if [ $isDown == true ] |
103 | 109 |
then |
104 | 110 |
echo "Sending message - $messageOnRevive - $NOW" |
105 | 111 |
isDown=false |
106 |
- message="$messageOnRevive - $YONA_SERVER" |
|
107 |
- sendMessageToTelegram |
|
112 |
+ sendMessageToTelegram "$messageOnRevive - $TARGET_SERVER" |
|
108 | 113 |
fi |
109 | 114 |
fi |
110 | 115 |
sleep $POLLING_TIME |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?