-
Notifications
You must be signed in to change notification settings - Fork 12
/
check_cpu.sh
173 lines (150 loc) · 5.68 KB
/
check_cpu.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
#!/usr/bin/env bash
##########################################################
#
# Copyright (C) 2015 Eduardo Dimas (https://github.com/eddimas/nagios-plugins)
# Copyright (C) 2009 Mike Adolphs (http://www.matejunkie.com/)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##########################################################
PROGNAME=$(basename -s .sh $0)
TIMESTAMP=$(date +%Y-%m-%d_%H.%M.%S)
VERSION="Version 1.2,"
AUTHOR="2015, Eduardo Dimas (https://github.com/eddimas/nagios-plugins)"
TEMP_FILE="/var/tmp/${PROGNAME}_${TIMESTAMP}.log"; touch ${TEMP_FILE}
COMMAND='cat /proc/stat'
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
interval=5
print_version() {
printf "%s %s\n" "$VERSION" "$AUTHOR"
}
help() {
cat << END
Usage :
$PROGNAME -l [STRING] -H [STRING] -i [VALUE] -w [VALUE] -c [VALUE]
OPTION DESCRIPTION
----------------------------------
-h Help
-l [STRING] Remote user
-H [STRING] Host name
-i [VALUE] Defines the period where the statistic data will be collected.
Default is: 5 samples, one sample by second
-w [VALUE] Sets a warning level for CPU user.
Default is: on
-c [VALUE] Sets a critical level for CPU user.
Default is: on
----------------------------------
Note : [VALUE] must be an integer.
END
}
if [ $# -ne 10 ]
then
help;
exit 3;
fi
while getopts "l:H:i:w:c:" OPT
do
case $OPT in
l) USERNAME="$OPTARG" ;;
H) HOSTNAME="$OPTARG" ;;
i) interval="$OPTARG" ;;
w) warn="$OPTARG" ;;
c) crit="$OPTARG" ;;
*) help ;;
esac
done
val_wcdiff() {
if [ ${warn} -gt ${crit} ]
then
wcdiff=1
fi
}
get_cpuvals() {
nTimes=$1
SSH_COMMAND=$()
for ((x=0;x<=${nTimes};x++)); do
ssh -l ${USERNAME} ${HOSTNAME} -C ${COMMAND} > ${TEMP_FILE}
TEMP_VAR=();
for ((y=2;y<=8;y++)); do
TEMP_VAR+=( $(grep -m1 '^cpu' ${TEMP_FILE} |awk -v var="$y" '{print $var}') );
done
cpu_user+=( ${TEMP_VAR[0]} );
cpu_nice+=( ${TEMP_VAR[1]} );
cpu_sys+=( ${TEMP_VAR[2]} );
cpu_idle+=( ${TEMP_VAR[3]} );
cpu_iowait+=( ${TEMP_VAR[4]} );
cpu_irq+=( ${TEMP_VAR[5]} );
cpu_softirq+=( ${TEMP_VAR[6]} );
cpu_total+=( $(expr ${cpu_user} + ${cpu_nice} + ${cpu_sys} + ${cpu_idle} + ${cpu_iowait} + ${cpu_irq} + ${cpu_softirq}) );
avg_cpu_user=$( expr $avg_cpu_user + ${cpu_user[$x]} ) ;
avg_cpu_nice=$( expr $avg_cpu_nice + ${cpu_nice[$x]} ) ;
avg_cpu_sys=$( expr $avg_cpu_sys + ${cpu_sys[$x]} ) ;
avg_cpu_idle=$( expr $avg_cpu_idle + ${cpu_idle[$x]} ) ;
avg_cpu_iowait=$( expr $avg_cpu_iowait + ${cpu_iowait[$x]} ) ;
avg_cpu_irq=$( expr $avg_cpu_irq + ${cpu_irq[$x]} ) ;
avg_cpu_softirq=$( expr $avg_cpu_softirq + ${cpu_softirq[x]} ) ;
avg_cpu_total=$( expr $avg_cpu_total + ${cpu_total[$x]} ) ;
sleep 1
done;
cpu_user=$(echo "scale=2; (1000*(${avg_cpu_user}/${nTimes})/(${avg_cpu_total}/${nTimes}))/10" | bc -l | sed 's/^\./0./');
cpu_nice=$(echo "scale=2; (1000*(${avg_cpu_nice}/${nTimes})/(${avg_cpu_total}/${nTimes}))/10" | bc -l | sed 's/^\./0./');
cpu_sys=$(echo "scale=2; (1000*(${avg_cpu_sys}/${nTimes})/(${avg_cpu_total}/${nTimes}))/10" | bc -l | sed 's/^\./0./');
cpu_idle=$(echo "scale=2; (1000*(${avg_cpu_idle}/${nTimes})/(${avg_cpu_total}/${nTimes}))/10" | bc -l | sed 's/^\./0./');
cpu_iowait=$(echo "scale=2; (1000*(${avg_cpu_iowait}/${nTimes})/(${avg_cpu_total}/${nTimes}))/10" | bc -l | sed 's/^\./0./');
cpu_irq=$(echo "scale=2; (1000*(${avg_cpu_irq}/${nTimes})/(${avg_cpu_total}/${nTimes}))/10" | bc -l | sed 's/^\./0./');
cpu_softirq=$(echo "scale=2; (1000*(${avg_cpu_softirq}/${nTimes})/(${avg_cpu_total}/${nTimes}))/10" | bc -l | sed 's/^\./0./');
cpu_total=$(echo "scale=2; (1000*(${avg_cpu_total}/${nTimes})/(${avg_cpu_total}/${nTimes}))/10" | bc -l | sed 's/^\./0./');
cpu_usage=$(echo "(${cpu_user}+${cpu_nice}+${cpu_sys}+${cpu_iowait}+${cpu_irq}+${cpu_softirq})/1" | bc);
rm ${TEMP_FILE}
}
#CPU OK : user=0% system=0% iowait=0% idle=99%
#Performance Data: cpu_user=0%;80;90; cpu_sys=0%;70;90; cpu_iowait=0%;40;60; cpu_idle=99%;
do_output() {
output="user:${cpu_user}%, sys:${cpu_sys}%, iowait:${cpu_iowait}%, idle:${cpu_idle}%"
}
do_perfdata() {
perfdata="cpu_user=${cpu_user}%;${warn};${crit}; cpu_sys=${cpu_sys}%;${warn};${crit}; iowait=${cpu_iowait}%;${warn};${crit};"
}
if [ -n "$warn" -a -n "$crit" ]
then
val_wcdiff
if [ "$wcdiff" = 1 ]
then
printf "Please adjust your warning/critical thresholds. The warning must be lower than the critical level!"
exit ${ST_UK}
fi
fi
get_cpuvals ${interval}
do_output; do_perfdata
if [ -n "$warn" -a -n "$crit" ]
then
if [ "$cpu_usage" -ge "$warn" -a "$cpu_usage" -lt "$crit" ]
then
printf "WARNING - %s | %s\n" "${output}" "${perfdata}"
exit ${ST_WR}
elif [ "$cpu_usage" -ge "$crit" ]
then
printf "CRITICAL - %s | %s\n" "${output}" "${perfdata}"
exit ${ST_CR}
else
printf "OK - %s | %s\n" "${output}" "${perfdata}"
exit ${ST_OK}
fi
else
printf "OK - %s | %s\n" "${output}" "${perfdata}"
exit ${ST_OK}
fi