-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_cpu
More file actions
125 lines (117 loc) · 4.95 KB
/
check_cpu
File metadata and controls
125 lines (117 loc) · 4.95 KB
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
#!/bin/sh
################################################################################
# Linux CPU utilization nagios plugin #
# Copyright (c) 2019 Frostbyte <frostbytegr@gmail.com> #
# #
# Credits: #
# <github.com/hugme> - sample code for linux nagios checks #
# <devel@nagios-plugins.org> - plugin development guidelines #
# <github.com/cyberang3l> - information on the double inclusion of guest times #
# #
# 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/>. #
################################################################################
# Usage helper function
usage() {
cat << EOF
Usage:
$0 [-w util_warn_threshold] [-c util_warn_threshold]
Example: $0 -w 80 -c 90
EOF
exit 3
}
# Input error helper function
inputError() {
# Throw supplied error message and exit
echo "$1"
exit 3
}
# Parse user-supplied arguments
while getopts ":w:c:" inputArgs; do
case "${inputArgs}" in
w) thresholdWarn="${OPTARG}" ;;
c) thresholdCrit="${OPTARG}" ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
# Validate any user-supplied arguments
[ -z "$thresholdWarn" ] || {
[ -z $(echo $thresholdWarn | tr -d [:digit:]) ] || inputError "The warning threshold value may only contain numbers."
[ $thresholdWarn -le 0 ] || [ $thresholdWarn -ge 100 ] && inputError "The warning threshold value must be between 1 and 99."
}
[ -z "$thresholdCrit" ] || {
[ -z $(echo $thresholdCrit | tr -d [:digit:]) ] || inputError "The critical threshold value may only contain numbers."
[ $thresholdCrit -le 0 ] || [ $thresholdCrit -ge 100 ] && inputError "The critical threshold value must be between 1 and 99."
}
[ ! -z "$thresholdWarn" ] && [ ! -z "$thresholdCrit" ] && [ $thresholdCrit -lt $thresholdWarn ] && inputError "The critical threshold value must be higher than or equal to the warning threshold value."
# If the processor statistics file is missing
procStat="/proc/stat"
[ -f "$procStat" ] || {
# Throw error and exit
echo "The processor statistics file $procStat is missing."
exit 3
}
# Disable any non-specified thresholds
[ -z "$thresholdWarn" ] && thresholdWarn=0
[ -z "$thresholdCrit" ] && thresholdCrit=0
# Calculate and construct a standard nagios response
result=$(awk \
-v thresholdWarn=$thresholdWarn \
-v thresholdCrit=$thresholdCrit \
-v initialMeasurement="$(awk '/^cpu\s+/' "$procStat"; sleep 1)" \
'/^cpu\s+/{
split(initialMeasurement,initialValues," ")
for (valueIndex=2; valueIndex<=9 in initialValues; valueIndex++) totalTime-=initialValues[valueIndex]
for (valueIndex=2; valueIndex<=9; valueIndex++) totalTime+=$valueIndex
for (valueIndex=2; valueIndex<=NF; valueIndex++) cpuUtil[valueIndex]=100*($valueIndex-initialValues[valueIndex])/totalTime
}
END {
cpuUtil[1]=cpuUtil[2]+cpuUtil[3]+cpuUtil[4]+cpuUtil[7]+cpuUtil[8]
if (cpuUtil[5] == 0) cpuUtil[1]+=cpuUtil[9]
result="OK"
exitCode=0
if (cpuUtil[1] > thresholdCrit && thresholdCrit > 0) {
result="CRITICAL"
exitCode=2
} else if (cpuUtil[1] > thresholdWarn && thresholdWarn > 0) {
result="WARNING"
exitCode=1
}
print exitCode" "result" - \
Utilization: "cpuUtil[1]"% \
User: "cpuUtil[2]-cpuUtil[10]"% \
Nice: "cpuUtil[3]-cpuUtil[11]"% \
System: "cpuUtil[4]"% \
Idle: "cpuUtil[5]"% \
IOWait: "cpuUtil[6]"% \
HWInterrupt: "cpuUtil[7]"% \
SWInterrupt: "cpuUtil[8]"% \
Steal: "cpuUtil[9]"% \
Guest: "cpuUtil[10]"% \
GuestNice: "cpuUtil[11]"% | \
util="cpuUtil[1]";"thresholdWarn";"thresholdCrit";0 \
user="cpuUtil[2]-cpuUtil[10]";0;0;0 \
nice="cpuUtil[3]-cpuUtil[11]";0;0;0 \
system="cpuUtil[4]";0;0;0 \
idle="cpuUtil[5]";0;0;0 \
iowait="cpuUtil[6]";0;0;0 \
hwint="cpuUtil[7]";0;0;0 \
swint="cpuUtil[8]";0;0;0 \
steal="cpuUtil[9]";0;0;0 \
guest="cpuUtil[10]";0;0;0 \
guestnice="cpuUtil[11]";0;0;0"
}' "$procStat")
# Respond and exit
echo ${result#* }
exit ${result%% *}