-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_stats
executable file
·92 lines (85 loc) · 3.96 KB
/
server_stats
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
#!/bin/sh
# Script to display important server stats
#
# uptime (seconds)
# cpuX (seconds) (X = '', 1, ...)
# loadavg (percent)
# cpu_apache2 (percent)
# cpu_clamd (percent)
# cpu_mysqld (percent)
# cpu_nginx (percent)
# cpu_php (percent)
# cpu_postfix (percent)
# mem (bytes)
# mem% (percent)
# swap (bytes)
# swap% (percent)
# mem_apache2 (bytes)
# mem_clamd (bytes)
# mem_mysqld (bytes)
# mem_nginx (bytes)
# mem_php (bytes)
# mem_postfix (bytes)
# xvdaX_reads (count) (X = '', 1, ...)
# xvdaX_readtime (ms) (X = '', 1, ...)
# xvdaX_writes (count) (X = '', 1, ...)
# xvdaX_writetime (ms) (X = '', 1, ...)
# xvdaX_used (bytes) (X = 1, 2, ...)
# xvdaX_used% (percent) (X = 1, 2, ...)
# ethX_rx (bytes) (X = 0, 1, ...)
# ethX_tx (bytes) (X = 0, 1, ...)
# 01/30/17 - Created by Andrew Puckett
# 06/06/17 - Added mem% and dev% and cleaned up code
# 09/08/17 - Added nginx, php and postfix
# 04/14/18 - Made CPU measurements real-time
# 05/30/19 - Added -q and -e options for daily email report
if [ "$1" = "-q" ]; then
[ $(cat /proc/loadavg | awk '{printf "%s > 0.1\n", $1}' | bc) = "1" ] && exit 1
MEM=$(free | awk '/Mem:/ {print 100*($2-$7)/$2}')
SWAP=$(free | awk '/Swap:/ {if($2 != 0){print 100*$3/$2}}')
[ $(echo "$MEM > 90" | bc) = "1" ] && exit 1
[ -n "$SWAP" ] && [ $(echo "$SWAP > 90" | bc) = "1" ] && exit 1
[ -n "$SWAP" ] && [ $(echo "$MEM > 80 && $SWAP > 80" | bc) = "1" ] && exit 1
[ $(df | awk '/^\/dev/ {if(index($1, "loop") == 0){sub("%","",$5);printf "%s > 75\n", $5}}' | bc) = "1" ] && exit 1
exit 0
fi
if [ "$1" = "-e" ]; then
$0 -q && echo OK || echo WARNING
cat /proc/loadavg | awk '{printf "\n%-20s %s\n", "loadavg", $1*100}'
free | awk '/Mem:/ {printf "%-20s %s\n", "mem%", 100*($2-$7)/$2}'
free | awk '/Swap:/ {if($2 != 0){printf "%-20s %s\n", "swap%", 100*$3/$2}}'
df | awk '/^\/dev/ {if(index($1, "loop") == 0){sub("/dev/","",$1);sub("%","",$5);printf "%-20s %s\n\n", $1"_used%", $5}}'
fi
cat /proc/uptime | awk '{printf "%-20s %s\n", "uptime", $1}'
cat /proc/stat | awk '/cpu/ {printf "%-20s %s\n", $1,($2+$4)/100}'
cat /proc/loadavg | awk '{printf "%-20s %s\n", "loadavg", $1*100}'
TOP=$(top -n 1 -b)
echo "$TOP" | awk 'BEGIN { sum = 0 } /(apache2|httpd)/ { sum += $9 } END { printf "%-20s %s\n", "cpu_apache2", sum }'
echo "$TOP" | awk 'BEGIN { sum = 0 } /clamd/ { sum += $9 } END { printf "%-20s %s\n", "cpu_clamd", sum }'
echo "$TOP" | awk 'BEGIN { sum = 0 } /mysqld/ { sum += $9 } END { printf "%-20s %s\n", "cpu_mysqld", sum }'
echo "$TOP" | awk 'BEGIN { sum = 0 } /nginx/ { sum += $9 } END { printf "%-20s %s\n", "cpu_nginx", sum }'
echo "$TOP" | awk 'BEGIN { sum = 0 } /php-fpm/ { sum += $9 } END { printf "%-20s %s\n", "cpu_php", sum }'
echo "$TOP" | awk 'BEGIN { sum = 0 } /master/ { sum += $9 } END { printf "%-20s %s\n", "cpu_postfix", sum }'
free | awk '/Mem:/ {printf "%-20s %s\n%-20s %s\n", "mem", $2-$7, "mem%", 100*($2-$7)/$2}'
free | awk '/Swap:/ {if($2 != 0){printf "%-20s %s\n%-20s %s\n", "swap", $3, "swap%", 100*$3/$2}}'
if [ $(id -u) = "0" ]; then
for var in apache2 clamd mysqld nginx php-fpm master; do
mem=0
for pid in $(pgrep $var); do
#it's possible that the pid is no longer alive by the time we read it so ignore errors here
mem=$((mem + $(awk '/^Pss:/{ sum += $2 } END { print sum }' /proc/$pid/smaps))) 2>/dev/null
done
case "$var" in
php-fpm) var=php;;
master) var=postfix;;
esac
printf "%-20s %s\n" mem_$var $mem
done
else
echo Run as root for additional data >&2
fi
cat /proc/diskstats | awk '/d|nvm/ {printf "%-20s %s\n%-20s %s\n%-20s %s\n%-20s %s\n", $3"_reads", $4, $3"_readtime", $7, $3"_writes", $8, $3"_writetime", $11}'
df | awk '/^\/dev/ {sub("/dev/","",$1);sub("%","",$5); printf "%-20s %s\n%-20s %s\n", $1"_used", $3*1024, $1"_used%", $5}'
ip -s -o -a link | awk '{sub(":","", $2) } {printf "%-20s %s\n%-20s %s\n", $2"_rx", $28, $2"_tx", $43}'
#some implementations provide different information so adjust the indices accordingly
#ip -s -o -a link | awk '{sub(":","", $2) } {printf "%-20s %s\n%-20s %s\n", $2"_rx", $26, $2"_tx", $41}'