-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.sh
executable file
·334 lines (247 loc) · 7.73 KB
/
checker.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/bin/bash
load_config(){
# Telegram
TELEGRAM_TOKEN=""
TELEGRAM_CHAT=""
TELEGRAM_MESSAGE_ENDPOINT="https://api.telegram.org/bot"$TELEGRAM_TOKEN"/sendMessage?chat_id="$TELEGRAM_CHAT
TELEGRAM_DOCUMENT_ENDPOINT="https://api.telegram.org/bot"$TELEGRAM_TOKEN"/sendDocument?chat_id="$TELEGRAM_CHAT
# Other
DEBUG_MODE=1
# Send log if error
SEND_LOG=1
FILE_LOG="/var/log/syslog"
#Check
# Ram
RAM_CRITICAL=90
RAM_WARNING=80
# Swap
SWAP_CRITICAL=90
SWAP_WARNING=80
# Disk
DISK_CRITICAL=90
DISK_WARNING=80
# CPU
CPU_CRITICAL=90
CPU_WARNING=80
# Ping
PING_HOSTS="google.es;localhost"
PING_NUMBER=3
#Processes
PROCESSES_LIST="ssh;docker"
# Iptables
# Ports
PORTS_LIST="0.0.0.0:ssh"
# Docker
DOCKER_CONTAINER_LIST="2cc4c7443c32:Principal;ebc966fdd1e3:Secundario"
# Users allowed
MAXUSERS=2
# System users
MAX_SYSTEM_USERS=20
}
check_ram(){
totalRam=$(free -m | awk '/Mem:/ { print $2 }')
usedRam=$(free -m | awk '/Mem:/ { print $3 }')
percentRam=$(bc <<< "scale=1; $usedRam*100 / $totalRam")
if (( ${percentRam%.*} > $RAM_CRITICAL ))
then
send_error " 🔴 CRITICAL 🔴 " "The ram is at $percentRam." "error"
elif (( ${percentRam%.*} > $RAM_WARNING ))
then
send_error " ❕ WARNING ❕ " "The ram is at $percentRam." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The ram has been checked: $percentRam%." "debug"
echo "DEBUG - The ram has been checked: $percentRam%."
fi
}
check_swap(){
totalSwap=$(free -m | awk '/Swap:/ { print $2 }')
freeSwap=$(free -m | awk '/Swap:/ { print $3 }')
if (( "$totalSwap" > 0 ))
then
percentSwap=$(bc <<< "scale=1; $freeSwap*100 / $totalSwap")
else
percentSwap=0
fi
if (( ${percentSwap%.*} > $SWAP_CRITICAL ))
then
send_error " 🔴 CRITICAL 🔴 " "The swap is at $percentSwap." "error"
elif (( ${percentSwap%.*} > $SWAP_WARNING ))
then
send_error " ❕ WARNING ❕ " "The swap is at $percentSwap." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The swap has been checked: $percentSwap %." "debug"
echo "DEBUG - The swap has been checked: $percentSwap%."
fi
}
check_disk(){
percentDisk=$(df -h | grep sd | awk '{print $5}' | sort -n | tail -n 1 | cut -d "%" -f1)
if (( $(($percentDisk+0)) -ge $DISK_CRITICAL ))
then
send_error " 🔴 CRITICAL 🔴 " "HD is at $percentDisk%." "error"
elif (( $(($percentDisk+0)) -ge $DISK_WARNING ))
then
send_error " ❕ WARNING ❕ " "HD is at $percentDisk%." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The disk has been checked: $percentDisk%." "debug"
echo "DEBUG - The disk has been checked: $percentDisk%."
fi
}
check_cpu(){
usedCPU=$(top -bn1 | grep "Cpu(s)" | \
sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
awk '{print $1}')
if (( ${usedCPU%.*} > $CPU_CRITICAL ))
then
send_error "🔴 CRITICAL 🔴" "CPU is at $usedCPU%."
elif (( ${usedCPU%.*} > $CPU_WARNING ))
then
send_error " ❕ WARNING ❕ " "CPU is at $usedCPU%."
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The CPU has been checked: $usedCPU%." "debug"
echo "DEBUG - The CPU has been checked: $usedCPU%."
fi
}
check_ping(){
hosts=$(echo $PING_HOSTS | tr ";" "\n")
for host in $hosts
do
if ! ping -c $PING_NUMBER $host > /dev/null
then
send_error " 🔴 CRITICAL 🔴 " "Ping to $host has failed." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The ping to $host has been checked." "debug"
echo "DEBUG - The ping to $host has been checked."
fi
done
}
check_processes(){
processes=$(echo $PROCESSES_LIST | tr ";" "\n")
for proc in $processes
do
running=$(ps ax | grep -v grep | grep $proc | wc -l)
if (( $running <= 0 ))
then
send_error " 🔴 CRITICAL 🔴 " "The $proc process is not running." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The $proc process has been checked." "debug"
echo "DEBUG - The $proc process has been checked."
fi
done
}
check_iptables(){
iptablesUP=$(sudo iptables -n -L -v --line-numbers | egrep "^[0-9]" | wc -l)
if (( $iptablesUP < 1 ))
then
send_error " 🔴 CRITICAL 🔴 " "Iptables rules are disabled." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "It has been checked if the iptables are disabled." "debug"
echo "DEBUG - It has been checked if the iptables are disabled."
fi
}
check_ports(){
ports=$(echo $PORTS_LIST | tr ";" "\n")
for port in $ports
do
if ! netstat -l | grep $port | grep LISTEN > /dev/null
then
send_error " 🔴 CRITICAL 🔴 " "The port [$port] is not open." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The port [$port] has been checked." "debug"
echo "DEBUG - The port [$port] has been checked."
fi
done
}
check_docker(){
containers=$(echo $DOCKER_CONTAINER_LIST | tr ";" "\n")
for container in $containers
do
container_id=$(echo $container | cut -f1 -d":")
if [ $(docker inspect -f '{{.State.Running}}' $container_id) != "true" ]
then
send_error " 🔴 CRITICAL 🔴 " "The docker container '$container' is not running." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The docker container '$container' has been checked." "debug"
echo "DEBUG - The docker container '$container' has been checked."
fi
done
}
check_ssh_connections(){
users=$(who | wc -l)
usersnetstat=$(netstat -natp | grep [0-9][0-9]:22 | wc -l)
if (($users>MAXUSERS))||(($usersnetstat>MAXUSERS))
then
send_error " 🔴 CRITICAL 🔴 " "$users users connected via SSH" "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The number of users connected via SSH has been checked: $users." "debug"
echo "DEBUG - The number of users connected via SSH has been checked: $users."
fi
}
check_system_users(){
users=$(awk -F: '{ print $1}' /etc/passwd | wc -l)
if (($users > $MAX_SYSTEM_USERS))
then
send_error " 🔴 CRITICAL 🔴 " "There are more than $MAX_SYSTEM_USERS users in the system: $users." "error"
fi
# Debug mode
if (( $DEBUG_MODE == 1 ))
then
send_error " 📢 DEBUG 📢 " "The number of system users has been checked: $users." "debug"
echo "DEBUG - The number of system users has been checked: $users."
fi
}
send_error(){
message="$1 - $2"
if [ $SEND_LOG == 1 ] && [ "$3" == "error" ]
then
$(curl -s -X POST $TELEGRAM_DOCUMENT_ENDPOINT -F document=@"$FILE_LOG" -F caption="$message" > /dev/null)
elif [ "$3" == "debug" ]
then
$(curl -s -X POST $TELEGRAM_MESSAGE_ENDPOINT -F disable_notification=true -F text="$message" > /dev/null)
else
$(curl -s -X POST $TELEGRAM_MESSAGE_ENDPOINT -F text="$message" > /dev/null)
fi
}
check() {
load_config # works
check_ram # works
check_swap # works
check_disk # works
check_cpu # works
check_ping # works
check_processes # works
check_iptables # works
check_ports # works
check_docker # works
check_system_users #works
}
check