This repository was archived by the owner on Jul 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_lin_process.sh
More file actions
executable file
·160 lines (153 loc) · 6.61 KB
/
check_lin_process.sh
File metadata and controls
executable file
·160 lines (153 loc) · 6.61 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
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
#!/bin/bash
# Script name: check_lin_process.sh
# Version: v2.09.160315
# Created on: 17/08/2015
# Author: Willem D'Haese
# Purpose: Bash script that counts processes and returns total
# memory and cpu perfdata.
# On GitHub: https://github.com/OutsideIT/check_lin_process
# Copyright:
# 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/>.
Verbose=0
WriteLog () {
if [ -z "$1" ] ; then echo "WriteLog: Log parameter #1 is zero length. Please debug..." ; exit 1
else
if [ -z "$2" ] ; then echo "WriteLog: Severity parameter #2 is zero length. Please debug..." ; exit 1
else
if [ -z "$3" ] ; then echo "WriteLog: Message parameter #3 is zero length. Please debug..." ; exit 1 ; fi
fi
fi
Now=$(date '+%Y-%m-%d %H:%M:%S,%3N')
if [ $1 = "Verbose" -a $Verbose = 1 ] ; then echo "$Now: $2: $3"
elif [ $1 = "Verbose" -a $Verbose = 0 ] ; then :
elif [ $1 = "Output" ] ; then echo "${Now}: $2: $3"
elif [ -f $1 ] ; then echo "${Now}: $2: $3" >> $1
fi
}
Process=""
Name=""
Minimum=0
Maximum=100
ProcessCount=0
Exitcode=3
Output=""
CountMinExitcode=0
CountMaxExitcode=0
Warning=80
Critical=90
Count="cmd"
while :; do
case "$1" in
-h|--help)
DisplayHelp="true" ; shift ; ;;
-p|--Process)
shift ; Process="$1" ; shift ; ;;
-N|--Name)
shift ; Name="$1" ; shift ; ;;
-w|--Warning)
shift ; Warning="$1" ; shift ; ;;
-c|--Critical)
shift ; Critical="$1" ; shift ; ;;
-m|--Minimum)
shift ; Minimum="$1" ; shift ; ;;
-M|--Maximum)
shift ; Maximum="$1" ; shift ; ;;
-C|-Count)
shift ; Count="$1" ; shift ; ;;
-A|--AverageCpu)
AverageCpu="true" ; shift ; ;;
-*)
echo "you specified a non-existant option. Please debug." ; exit 2 ; ;;
*)
break ; ;;
esac
done
if [[ "$DisplayHelp" == "true" ]] ; then
echo "
-h|--help, Display help information
-N|--Name, Specify a fancy name for the process
-p|--Process, Specify a process to be monitored
-w|--Warning, Specify a warning level for the check, default is 60%
-c|--Critical, Specify a critical level for the check, default is 70%
-m|--Minimum, Minimum number of processes expected to run, default 0
-M|--Maximum, Maximum amount of processes expected to run, default 100
-A|--AverageCpu, Divide process cpu count by the number of process cores
-C|--Count, Method to define process find (To do)"
exit 0
fi
if [[ "$Process" == "" ]] ; then
echo "No process was specified. The '-p' switch must be used to specify the process"
exit 3
fi
if [[ "$Name" == "" ]] ; then
Name=$Process
fi
CpuCount=$(cat /proc/cpuinfo | grep processor | wc -l)
WriteLog Verbose Info "CPU Cores: $CpuCount"
if [[ "$Count" == "cmd" ]] ; then
CheckCpu=$(ps -C $Process -o%cpu= | paste -sd+ | bc)
WriteLog Verbose Info "CPU Total %: $CheckCpu"
if [[ "$AverageCpu" == "true" ]] ; then
CheckCpu=$(echo "$CheckCpu/$CpuCount" | bc -l)
WriteLog Verbose Info "CPU Averaged: $CheckCpu"
fi
RoundedCpuResult=$(echo $CheckCpu | awk '{print int($1+0.5)}')
WriteLog Verbose Info "CPU Rounded: $RoundedCpuResult"
CheckMem=$(ps -C $Process -o%mem= | paste -sd+ | bc)
RoundedMemResult=$(echo $CheckMem | awk '{print int($1+0.5)}')
RealProcessCount=$(ps -C $Process --no-heading | wc -l)
fi
# ps -ef to retrieve the process count was in fact incorrect
#ProcessCount=`ps -ef | grep -v grep | grep $Process | wc -l`
#RealProcessCount=$(($ProcessCount-2))
WriteLog Verbose Info "Rounded CPU Result: $RoundedCpuResult , ROunde Memory Result: $RoundedMemResult"
if [ "$RoundedCpuResult" == "" -o "$RoundedMemResult" == "" ] ; then
Output="The $Name process doesn't appear to be running, as CPU or memory is undefined. Please debug. "
Exitcode=2 ;
else
if [[ $RealProcessCount -lt $Minimum ]] ; then
Output="${Output}$Name process count of $RealProcessCount is lesser then Minimum threshold of ${Minimum}. "
CountMinExitcode=2
fi
if [[ $RealProcessCount -gt $Maximum ]] ; then
Output="${Output}$Name process count of $RealProcessCount is larger then Maximum threshold of ${Maximum}. "
CountMaxExitcode=2
fi
if [ "$RoundedCpuResult" -ge "$Critical" ] ; then
Output="${Output}$Name CPU usage exceeded critical threshold. "
Exitcode=2
elif [ "$RoundedCpuResult" -ge "$Warning" ] ; then
Output="${Output}$Name CPU usage exceeded warning threshold. "
Exitcode=1
fi
if [ "$RoundedMemResult" -ge "$Critical" ] ; then
Output="${Output}$Name memory usage exceeded critical threshold. "
Exitcode=2
elif [ "$RoundedMemResult" -ge "$Warning" ] ; then
Output="${Output}$Name memory usage exceeded warning threshold. "
Exitcode=1
fi
if [ "$RoundedCpuResult" -lt "$Warning" -a "$RoundedMemResult" -lt "$Warning" ] ; then
WriteLog Verbose Info "OK? RoundedCpuResult: $RoundedCpuResult , RoundedMemResult: $RoundedMemResult Warning: $Warning Critical: $Critical CountExitcode: $CountExitcode CountMinExitcode: $CountMinExitcode CountExitMaxcode: $CountExitMaxcode"
if [ $CountMinExitcode -eq 2 -o $CountMaxExitcode -eq 2 ] ; then
Exitcode=2
else
Output="${Output}$Name "
Exitcode=0
fi
fi
Details="{CPU: ${RoundedCpuResult}%}{Memory: ${RoundedMemResult}%}{Count: ${RealProcessCount}} | '${Name}_cpu'=${RoundedCpuResult}%;$Warning;$Critical;0;100 '${Name}_mem'=${RoundedMemResult}%;$Warning;$Critical;0;100 '${Name}_count'=$RealProcessCount;$Maximum;$Maximum;0;99999"
fi
case "$Exitcode" in
0) Output="OK: ${Output}$Details" ;;
1) Output="WARNING: ${Output}$Details" ;;
2) Output="CRITICAL: ${Output}$Details" ;;
3) Output="UNKNOWN: ${Output}$Details" ;;
*) Output="UNKNOWN: Exitcode ${Exitcode}. ${Output}$Details" ;;
esac
echo $Output
exit $Exitcode