-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor-ynh
executable file
·188 lines (170 loc) · 6.64 KB
/
monitor-ynh
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
script_dir="$(dirname "$0")"
source $script_dir/monitor-ynh-settings
IPV4=false
if ping -c 1 9.9.9.9 &> /dev/null && dig +short A wikipedia.fr &> /dev/null; then
IPV4=true
fi
IPV6=false
if ping6 -c 1 2620:0:862:ed1a::1 &> /dev/null && dig +short A wikipedia.fr &> /dev/null; then
IPV6=true
fi
#==============================================================================
# Monitoring code
#==============================================================================
# Check if it's always the same HTTP content
# usage: check_content [IP_VERSION] NB_IGNORED URL HASH
check_content () {
if (( $# < 4 ));then
$IPV4 && check_content 4 $@
$IPV6 && check_content 6 $@
else
hash="$(curl -$1 -s $3 | md5sum | cut -d ' ' -f1)"
[ "$hash" == "$4" ]
register_event $? $2 "${FUNCNAME[0]} $*" "[ipv$1] Content of $3 has changed"
sleep 2s
fi
}
# Check if dig A request return the correct answer
# usage: check_dig [IP_VERSION] NB_IGNORED NAMESERVER DOMAIN IPv4
check_dig () {
if (( $# < 5 ));then
$IPV4 && check_dig 4 $@
$IPV6 && check_dig 6 $@
else
hash="$(dig -$1 +short @$3 A $4)"
[ "$hash" == "$5" ]
register_event $? $2 "${FUNCNAME[0]} $*" "[ipv$1] DNS server $3 may not work"
sleep 1s
fi
}
# Check if it returns a 200 answer
# usage: check_dig [IP_VERSION] NB_IGNORED URL
check_200 () {
if (( $# < 3 ));then
$IPV4 && check_200 4 $@
$IPV6 && check_200 6 $@
else
# Firstly we test with -I option to just download headers,
# if it fails we check with a true GET HTTP REQUEST
[ "$(curl -$1 -s -I -w '%{http_code}' -o /dev/null $3)" == "200" ] || [ "$(curl -$1 -s -w '%{http_code}' -o /dev/null $3)" == "200" ]
register_event $? $2 "${FUNCNAME[0]} $*" "[ipv$1] $3 call fails"
sleep 2s
fi
}
# Check if the machine ping
# usage: check_ping [IP_VERSION] NB_IGNORED DOMAIN_OR_IP
check_ping () {
if (( $# < 3 ));then
$IPV4 && check_ping 4 $@
$IPV6 && check_ping 6 $@
else
if [ "$1" -eq "4" ]; then
ping -c 1 $3 &> /dev/null
register_event $? $2 "${FUNCNAME[0]} $*" "[ipv$1] $3 no ping"
else
ping6 -c 1 $3 &> /dev/null
register_event $? $2 "${FUNCNAME[0]} $*" "[ipv$1] $3 no ping"
fi
fi
}
# Check freespace of the host
freespace () {
freespace="$(df --output=avail / | sed -n '2 p')"
(($freespace < $2))
register_event $? $1 "${FUNCNAME[0]} $*" "Missing space $freespace"
}
# Get the id of the monitored things
# Usage: get_id ID_TEXT
get_id () {
echo "$*" | sha256sum | cut -d ' ' -f1
}
# Send an register_event to the admin
# Usage: register_event TEST_VALUE NB_IGNORED ID_TEXT ALERT_TEXT
register_event () {
local var_name=$(get_id $3)
if [ "$1" -eq "0" ]; then
redis-cli set $var_name 0 > /dev/null
else
local count=$(redis-cli --raw incr $var_name)
local nb_ignored=$2
#local reminder=$(( $count - $nb_ignore + 1 ))
reminder "${@: -1}"
#reminder="$(echo obase=2\;$reminder | bc | tail -c +2)0"
if [ $count -eq $2 ]; then
alert " ${@: -1}"
#elif [ $count -gt $nb_ignore ] && [ $reminder -eq 0 ]; then
# alert "[Reminder] ${@: -1}"
fi
fi
}
reminder_msg=""
newline=$'\n'
# Add to reminder
# Usage: reminder ID_TEXT
reminder() {
reminder_msg="${reminder_msg}[KO] $1${newline}"
}
alert_msg=""
# Send on alert
# Usage: alert ID_TEXT
alert() {
alert_msg="${alert_msg}[alert] $1${newline}"
}
#==============================================================================
# Configuration
#==============================================================================
# Check All server ping
check_ping 2 bearnaise.yunohost.org
check_ping 2 samurai.yunohost.org
check_ping 2 aioli.yunohost.org
check_ping 2 demo.yunohost.org
check_ping 2 forum.yunohost.org
#check_ping tartare.yunohost.org #ipv6 issue
# Check critic services (needed for yunohost instances works correctly)
check_dig 2 ns0.yunohost.org toto.noho.st 81.57.118.36
check_dig 2 ns0.yunohost.org toto.nohost.me 162.243.7.71
check_dig 2 ns1.yunohost.org toto.noho.st 81.57.118.36
check_dig 2 ns1.yunohost.org toto.nohost.me 162.243.7.71
check_content 2 https://repo.yunohost.org/ 1477866e988c7180d486afec7bd4341c
check_content 2 https://dyndns.yunohost.org/ c9b426d468e2145a212032c16bebc26f
$IPV4 && check_content 4 2 https://ip.yunohost.org/ 32453f31e928721f8c272c6dda5f86e6
$IPV6 && check_content 6 2 https://ip6.yunohost.org/ 0340bdddba91963e24f153adaf3c4570
$IPV6 && check_content 6 2 https://ipv6.yunohost.org/ 0340bdddba91963e24f153adaf3c4570
check_200 2 https://app.yunohost.org/official.json
check_200 2 https://app.yunohost.org/community.json
check_200 2 https://app.yunohost.org/list.json
check_200 2 https://forge.yunohost.org/debian/
# Check for install resources
check_content 2 https://install-app.yunohost.org/ b215745d9fdd5302d2b1f95083d74604
check_content 2 https://paste.yunohost.org/ ef72468ae6b07f66b3e1ff69bb73f074
check_200 2 https://install.yunohost.org/
#check_content 2 https://install.yunohost.org/ 4c53314cd822fff7423821e08f0c850d
#check_content 2 https://build.yunohost.org/ 207d7c46e03352d98dd533fe38f9bb4c
#check_content 2 https://build.yunohost.org/yunohost-jessie-201701261126-sdraspi-stable.zip.sum cf2e8d4c40fbee7bb57ac4541751e989
#check_content 2 https://build.yunohost.org/yunohost-jessie-0127171259-amd64-stable.iso.sum 6d07211e77e4051b880925719d8bb314
#check_content 2 https://build.yunohost.org/yunohost-jessie-0127171609-i386-stable.iso.sum 2bebadec817843b942a5bf4a7b7b8c7e
# Check for public website, support tools
check_200 2 https://forum.yunohost.org/
check_200 2 https://yunohost.org/
#check_200 2 https://www.yunohost.org/
check_content 2 https://ports.yunohost.org/ ffc3113a26780253c9f005523dc39fc8
check_content 2 https://im.yunohost.org/logs/ 4b0a139edf5c86f2e0e5f5b915082a0d
check_content 2 https://chat.yunohost.org/ feafc193207604dc21bd20ca2077a7ce
check_200 4 https://demo.yunohost.org/yunohost/sso/
# Check for contribution tools
#check_200 2 https://dev.yunohost.org/
check_200 2 https://dash.yunohost.org/
check_200 4 https://ci-apps.yunohost.org/ci/
#check_200 2 https://ci-core.yunohost.org/ci/
check_200 2 https://ci-apps-arm.yunohost.org/ci/
check_200 2 https://translate.yunohost.org/
# Check for internetcube services
check_200 2 https://repo.internetcu.be/
check_200 2 https://labriqueinter.net/
check_200 2 https://repo.labriqueinter.net/
check_content 2 https://install.labriqueinter.net/ 7c2a6659857dcddee846403bdb10b7fe
check_200 2 https://wiki.labriqueinter.net/doku.php
if [[ ! -z "$alert_msg" ]]; then
echo "$alert_msg$newline$reminder_msg" | mail -s "[$PSEUDO-MONITOR] !!!" $MAIL &> /dev/null
fi