-
Notifications
You must be signed in to change notification settings - Fork 35
/
eZServerMonitor.sh
482 lines (398 loc) · 14.7 KB
/
eZServerMonitor.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/bash
# **************************************************** #
# #
# eZ Server Monitor `sh #
# #
# *************************** #
# #
# @name eZ Server Monitor `sh #
# @author ShevAbam #
# @website ezservermonitor.com #
# @created 30 nov. 2017 #
# @version 2.3 #
# #
# **************************************************** #
# ************************************************************ #
# * [ CONFIG ] * #
# ************************************************************ #
# Disk usage - Show or hide virtual mountpoints (tmpfs)
DISK_SHOW_TMPFS=false
# Service who returns WAN IP
GET_WAN_IP="https://www.ezservermonitor.com/myip"
# Hosts to ping
PING_HOSTS=("google.com" "facebook.com" "yahoo.com")
# Services port number to check
# syntax :
# SERVICES_NAME[port_number]="label"
# SERVICES_HOST[port_number]="localhost"
SERVICES_NAME[21]="FTP Server"
SERVICES_HOST[21]="localhost"
SERVICES_NAME[22]="SSH"
SERVICES_HOST[22]="localhost"
SERVICES_NAME[80]="Web Server"
SERVICES_HOST[80]="localhost"
SERVICES_NAME[3306]="Database"
SERVICES_HOST[3306]="localhost"
# Temperatures blocks (true for enable)
TEMP_ENABLED=false
# Text color : RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
THEME_TEXT=GREEN
# Title color : WHITE_ON_GREY, WHITE_ON_RED, WHITE_ON_GREEN, WHITE_ON_BLUE, WHITE_ON_MAGENTA, WHITE_ON_CYAN, BLACK_ON_YELLOW
THEME_TITLE=WHITE_ON_GREY
# ********************************************************** #
# * [ VARS ] * #
# ********************************************************** #
# Constants -- DON'T TOUCH !!
ESM_NAME="eZ Server Monitor \`sh"
ESM_VERSION="2.3"
ESM_AUTHOR="ShevAbam"
ESM_CREATED="30 nov. 2017"
ESM_URL="https://www.ezservermonitor.com"
# Colors
NC="\e[0m"
RED="\e[31;40m"
GREEN="\e[32;40m"
YELLOW="\e[33;40m"
BLUE="\e[34;40m"
MAGENTA="\e[35;40m"
CYAN="\e[36;40m"
WHITE="\e[37;40m"
# Styles
BOLD="\e[1m"
RESET="\e[0m"
WHITE_ON_GREY="\e[100;97m"
WHITE_ON_RED="\e[41;37m"
WHITE_ON_GREEN="\e[42;37m"
WHITE_ON_BLUE="\e[104;37m"
WHITE_ON_MAGENTA="\e[45;37m"
WHITE_ON_CYAN="\e[46;37m"
BLACK_ON_YELLOW="\e[103;30m"
# *************************************************************** #
# * [ FUNCTIONS ] * #
# *************************************************************** #
function makeTitle()
{
echo -e "${BOLD}${!THEME_TITLE} $1 ${RESET}"
}
# Function : system
function system()
{
OS=`uname -s`
if [ -e "/usr/bin/lsb_release" ] ; then
DISTRO=`/usr/bin/lsb_release -ds`
elif [ -e "/etc/system-release" ] ; then
DISTRO=`cat /etc/system-release`
else
DISTRO=`find /etc/*-release -type f -exec cat {} \; | grep NAME | tail -n 1 | cut -d= -f2 | tr -d '"'`;
fi
HOSTNAME=`hostname`
KERNEL_INFO=`/bin/uname -r`
UPTIME=`cat /proc/uptime`
UPTIME=${UPTIME%%.*}
UPTIME_MINUTES=$(( UPTIME / 60 % 60 ))
UPTIME_HOURS=$(( UPTIME / 60 / 60 % 24 ))
UPTIME_DAYS=$(( UPTIME / 60 / 60 / 24 ))
LAST_BOOT_DATE=`who -b | awk '{print $3}'`
LAST_BOOT_TIME=`who -b | awk '{print $4}'`
USERS_NB=`who | wc -l`
CURRENT_DATE=`/bin/date '+%F %T'`
makeTitle "System"
echo -e "${!THEME_TEXT} Hostname\t ${WHITE}$HOSTNAME"
echo -e "${!THEME_TEXT} OS\t\t ${WHITE}$OS $DISTRO"
echo -e "${!THEME_TEXT} Kernel\t ${WHITE}$KERNEL_INFO"
echo -e "${!THEME_TEXT} Uptime\t ${WHITE}$UPTIME_DAYS day(s), $UPTIME_HOURS hours(s), $UPTIME_MINUTES minute(s)"
echo -e "${!THEME_TEXT} Last boot\t ${WHITE}$LAST_BOOT_DATE $LAST_BOOT_TIME"
echo -e "${!THEME_TEXT} Current user(s) ${WHITE}$USERS_NB connected"
echo -e "${!THEME_TEXT} Server datetime ${WHITE}$CURRENT_DATE"
}
# Function : load average
function load_average()
{
PROCESS_NB=`ps -e h | wc -l`
PROCESS_RUN=`ps r h | wc -l`
CPU_NB=`cat /proc/cpuinfo | grep "^processor" | wc -l`
LOAD_1=`cat /proc/loadavg | awk '{print $1}'`
# LOAD_1_PERCENT=`echo $LOAD_1 | awk '{print 100 * $1}'`
LOAD_1_PERCENT=`echo $(($(echo $LOAD_1 | awk '{print 100 * $1}') / $CPU_NB))`
if [ $LOAD_1_PERCENT -ge 100 ] ; then
LOAD_1_PERCENT=100;
fi
if [ $LOAD_1_PERCENT -ge 75 ] ; then
LOAD_1_COLOR=${RED}
elif [ $LOAD_1_PERCENT -ge 50 ] ; then
LOAD_1_COLOR=${YELLOW}
else
LOAD_1_COLOR=${WHITE}
fi
LOAD_2=`cat /proc/loadavg | awk '{print $2}'`
# LOAD_2_PERCENT=`echo $LOAD_2 | awk '{print 100 * $1}'`
LOAD_2_PERCENT=`echo $(($(echo $LOAD_2 | awk '{print 100 * $1}') / $CPU_NB))`
if [ $LOAD_2_PERCENT -ge 100 ] ; then
LOAD_2_PERCENT=100;
fi
if [ $LOAD_2_PERCENT -ge 75 ] ; then
LOAD_2_COLOR=${RED}
elif [ $LOAD_2_PERCENT -ge 50 ] ; then
LOAD_2_COLOR=${YELLOW}
else
LOAD_2_COLOR=${WHITE}
fi
LOAD_3=`cat /proc/loadavg | awk '{print $3}'`
# LOAD_3_PERCENT=`echo $LOAD_3 | awk '{print 100 * $1}'`
LOAD_3_PERCENT=`echo $(($(echo $LOAD_3 | awk '{print 100 * $1}') / $CPU_NB))`
if [ $LOAD_3_PERCENT -ge 100 ] ; then
LOAD_3_PERCENT=100;
fi
if [ $LOAD_3_PERCENT -ge 75 ] ; then
LOAD_3_COLOR=${RED}
elif [ $LOAD_3_PERCENT -ge 50 ] ; then
LOAD_3_COLOR=${YELLOW}
else
LOAD_3_COLOR=${WHITE}
fi
echo
makeTitle "Load Average"
echo -e "${!THEME_TEXT} Since 1 minute $LOAD_1_COLOR $LOAD_1_PERCENT% ($LOAD_1)"
echo -e "${!THEME_TEXT} Since 5 minutes $LOAD_2_COLOR $LOAD_2_PERCENT% ($LOAD_2)"
echo -e "${!THEME_TEXT} Since 15 minutes $LOAD_3_COLOR $LOAD_3_PERCENT% ($LOAD_3)"
echo -e "${!THEME_TEXT} Processes\t ${WHITE}$PROCESS_NB process, including $PROCESS_RUN running"
}
# Function : CPU
function cpu()
{
CPU_NB=`cat /proc/cpuinfo | grep -i "^processor" | wc -l`
CPU_INFO=`cat /proc/cpuinfo | grep -i "^model name" | awk -F": " '{print $2}' | head -1 | sed 's/ \+/ /g'`
CPU_FREQ=`cat /proc/cpuinfo | grep -i "^cpu MHz" | awk -F": " '{print $2}' | head -1`
if [ -z $CPU_FREQ ] ; then
CPU_FREQ=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`
CPU_FREQ=$(( $CPU_FREQ / 1000 ))
fi
CPU_CACHE=`cat /proc/cpuinfo | grep -i "^cache size" | awk -F": " '{print $2}' | head -1`
CPU_BOGOMIPS=`cat /proc/cpuinfo | grep -i "^bogomips" | awk -F": " '{print $2}' | head -1`
echo
makeTitle "CPU"
if [ $CPU_NB -gt 1 ] ; then
echo -e "${!THEME_TEXT} Number\t ${WHITE}$CPU_NB"
fi
echo -e "${!THEME_TEXT} Model\t\t ${WHITE}$CPU_INFO"
echo -e "${!THEME_TEXT} Frequency\t ${WHITE}$CPU_FREQ MHz"
echo -e "${!THEME_TEXT} Cache L2\t ${WHITE}$CPU_CACHE"
echo -e "${!THEME_TEXT} Bogomips\t ${WHITE}$CPU_BOGOMIPS"
}
# Function : memory
function memory()
{
MEM_TOTAL=`grep MemTotal /proc/meminfo | awk '{print $2}'`
MEM_TOTAL=$(( $MEM_TOTAL / 1024 ))
MEM_FREE=`grep MemFree /proc/meminfo | awk '{print $2}'`
MEM_BUFFERS=`grep Buffers /proc/meminfo | awk '{print $2}'`
MEM_CACHED=`grep Cached /proc/meminfo | awk '{print $2}' | head -1`
MEM_FREE=$(( $MEM_FREE + $MEM_BUFFERS + $MEM_CACHED ))
MEM_FREE=$(( $MEM_FREE / 1024 ))
echo
makeTitle "Memory"
echo -e "${!THEME_TEXT} RAM\t\t${WHITE}$MEM_FREE Mb free of $MEM_TOTAL Mb"
}
# Function : network
function network()
{
if [ -e /sbin/ifconfig ]; then
INTERFACES=`/sbin/ifconfig |awk -F '[/ |: ]' '{print $1}' |sed -e '/^$/d'`
else
INTERFACES=`/sbin/ip a | sed '/^[0-9]\:/!d' | cut -d ":" -f 2 | cut -d " " -f 2`
fi
echo
makeTitle "Network"
for INTERFACE in $INTERFACES
do
IP_LAN=`/sbin/ip -f inet -o addr show ${INTERFACE} 2> /dev/null | cut -d\ -f 7 | cut -d/ -f 1`
echo -e "${!THEME_TEXT} IP LAN (${INTERFACE})\t ${WHITE}$IP_LAN"
done
if [ -e "/usr/bin/curl" ] ; then
IP_WAN=`curl -s ${GET_WAN_IP}`
elif [ -e "/usr/bin/wget" ] ; then
IP_WAN=`wget ${GET_WAN_IP} -O - -o /dev/null`
else
IP_WAN="Please, install curl or wget"
fi
echo -e "${!THEME_TEXT} IP WAN\t ${WHITE}$IP_WAN"
}
# Function : ping
function ping()
{
echo
makeTitle "Ping"
for HOST in ${PING_HOSTS[@]}
do
PING=`/bin/ping -qc 1 $HOST | awk -F/ '/^(rtt|round-trip)/ { print $5 }'`
echo -e "${!THEME_TEXT} ${HOST}\t ${WHITE}$PING ms"
done
}
# Function : Disk space (top 5)
function disk_space()
{
HDD_TOP=`df -h | head -1 | sed s/^/" "/`
#HDD_DATA=`df -hl | grep -v "^Filesystem" | grep -v "^Sys. de fich." | sort -k5r | head -5 | sed s/^/" "/`
# HDD_DATA=`df -hl | sed "1 d" | grep -v "^Filesystem" | grep -v "^Sys. de fich." | sort | head -5 | sed s/^/" "/`
if [ ${DISK_SHOW_TMPFS} = true ] ; then
HDD_DATA=`df -hl | sed "1 d" | grep -iv "^Filesystem|Sys." | sort | head -5 | sed s/^/" "/`
else
HDD_DATA=`df -hl | sed "1 d" | grep -iv "^Filesystem|Sys." | grep -vE "^tmpfs|udev" | sort | head -5 | sed s/^/" "/`
fi
echo
makeTitle "Disk space (top 5)"
echo -e "${!THEME_TEXT}$HDD_TOP"
echo -e "${WHITE}$HDD_DATA"
}
# Function : services
function services()
{
echo
makeTitle "Services"
for PORT in "${!SERVICES_NAME[@]}"
do
NAME=${SERVICES_NAME[$PORT]}
HOST=${SERVICES_HOST[$PORT]}
CHECK=`(exec 3<>/dev/tcp/$HOST/$PORT) &>/dev/null; echo $?`
if [ $CHECK = 0 ] ; then
CHECK_LABEL=${WHITE}ONLINE
else
CHECK_LABEL=${RED}OFFLINE
fi
echo -e "${!THEME_TEXT} $NAME ($PORT) : ${CHECK_LABEL}"
done
}
# Function : hard drive temperatures
function hdd_temperatures()
{
if [ ${TEMP_ENABLED} = true ] ; then
echo
makeTitle "Hard drive Temperatures"
DISKS=`ls /sys/block/ | grep -E -i '^(s|h)d'`
# If hddtemp is installed
if [ -e "/usr/sbin/hddtemp" ] ; then
for DISK in $DISKS
do
TEMP_DISK=`hddtemp -n /dev/$DISK`"°C"
echo -e " ${!THEME_TEXT}/dev/$DISK\t${WHITE}$TEMP_DISK"
done
else
echo -e "${WHITE}\nPlease, install hddtemp${WHITE}"
fi
fi
}
# Function : system temperatures
function system_temperatures()
{
if [ ${TEMP_ENABLED} = true ] ; then
echo
makeTitle "System Temperatures"
# If lm-sensors is installed
if [ -e "/usr/bin/sensors" ] ; then
TEMP_CPU=`/usr/bin/sensors | grep -E "^(CPU Temp|Core 0)" | cut -d '+' -f2 | cut -d '.' -f1`"°C"
TEMP_MB=`/usr/bin/sensors | grep -E "^(Sys Temp|Board Temp)" | cut -d '+' -f2 | cut -d '(' -f1`
echo -e " ${!THEME_TEXT}CPU ${WHITE}$TEMP_CPU"
echo -e " ${!THEME_TEXT}Motherboard ${WHITE}$TEMP_MB"
# Raspberry Pi
elif [ -f "/sys/class/thermal/thermal_zone0/temp" ] ; then
TEMP_CPU=`cat /sys/class/thermal/thermal_zone0/temp`
TEMP_CPU=$(( $TEMP_CPU / 1000 ))
echo -e " ${!THEME_TEXT}CPU ${WHITE}$TEMP_CPU°C"
else
echo -e "${WHITE}\nPlease, install lm-sensors${WHITE}"
fi
fi
}
# Function : showAll
function showAll()
{
system
load_average
cpu
memory
network
ping
disk_space
services
hdd_temperatures
system_temperatures
}
# Function : showVersion
function showVersion()
{
echo "$ESM_VERSION"
}
# Function : showHelp
function showHelp()
{
echo
echo "-------"
echo -e "Name : $ESM_NAME\nVersion : $ESM_VERSION\nAuthor : $ESM_AUTHOR\nCreated : $ESM_CREATED"
echo
echo -e "$ESM_NAME is originally a PHP project allows you to display system's information of a Unix machine.\nThis is the bash version."
echo
echo -e "[USAGE]\n"
echo -e " -h, -u, --help, --usage print this help message \n"
echo -e " -v, --version print program version\n"
echo -e " -C, --clear clear console\n Must be inserted before any argument\n"
echo -e " -s, --system system information (OS and distro ; kernel ; hostname ; uptime ; users connected; last boot; datetime)\n"
echo -e " -e, --services checks port number\n"
echo -e " -n, --network network information (IP LAN ; IP WAN)\n"
echo -e " -p, --ping pings several hosts\n Can be configured in the file\n"
echo -e " -c, --cpu processor information (model ; frequency ; cache ; bogomips)\n"
echo -e " -m, --memory RAM information (free and total)\n"
echo -e " -l, --load system load ; processus\n"
echo -e " -t, --temperatures print CPU, system and HDD temperatures\n Can be configured in the file\n"
echo -e " -d, --disk disk space (top 5) ; sorted by alpha\n"
echo -e " -a, --all print all data\n"
echo; echo;
echo -e "More information on : $ESM_URL"
echo "-------"
echo
}
# *************************************************************** #
# * [ LET'S GO !! ] * #
# *************************************************************** #
if [ $# -ge 1 ] ; then
while getopts "Csenpcmltdavhu-:" option
do
case $option in
h | u) showHelp; exit ;;
v) showVersion; exit;;
C) clear ;;
s) system ;;
n) network ;;
p) ping ;;
c) cpu ;;
m) memory ;;
l) load_average ;;
t) hdd_temperatures; system_temperatures ;;
d) disk_space ;;
e) services ;;
a) showAll ;;
-) case $OPTARG in
help | usage) showHelp; exit ;;
version) showVersion; exit ;;
all) showAll; exit ;;
clear) clear ;;
system) system ;;
services) services ;;
load) load_average ;;
cpu) cpu ;;
memory) memory ;;
network) network ;;
ping) ping ;;
disk) disk_space ;;
temperatures) hdd_temperatures; system_temperatures ;;
*) exit ;;
esac ;;
?) echo "Option -$OPTARG inconnue"; exit ;;
*) exit ;;
esac
done
else
#showAll
showHelp;
exit;
fi
echo -e "${RESET}"