forked from rohit01/nrpe-plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ps_grep_count_monitoring.sh
executable file
·211 lines (191 loc) · 7 KB
/
ps_grep_count_monitoring.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
#!/bin/bash
#
# Nagios NRPE plugin to monitor unusual activities in system processes by
# greping strings and alerting based on the number of counts
#
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
AUTHOR="Rohit Gupta - @rohit01"
PROGNAME=`basename $0`
VERSION="Version 1.0,"
print_version() {
echo "$PROGNAME: $VERSION $AUTHOR"
}
print_help() {
print_version
echo ""
echo "$PROGNAME is a custom Nagios plugin to monitor unusual activities"
echo "in system processes by greping strings and alerting based on the"
echo "number of counts"
echo ""
echo "Options:"
echo " -m/--mandatory)"
echo " Comma separated string values which must be present in the"
echo " process listing. Typically | grep '<value>' | will be done"
echo " -o/--optional)"
echo " Comma separated string values in which at least one string must"
echo " be present in the process listing"
echo " -i/--casesensitive)"
echo " Strings will be considered case insensitive if this argument"
echo " is passed. Default: case sensitive"
echo " -w/--warning)"
echo " Warning level for no of matching processes found"
echo " Default: 1"
echo " -c/--critical)"
echo " Critical level for no of matching processes found"
echo " Default: 5"
echo " -t/--greptype)"
echo " Possible values: less/more. Default: less"
echo " less: less matching processes is better. ok < warning < critical"
echo " more: more matching processes is better. critical < warning < ok"
echo ""
echo "Examples:"
echo " $PROGNAME -m postgres -o 'INSERT waiting,ALTER TABLE waiting'"
}
## Set defaults for optional arguments
mandatory=''
optional=''
casesensitive=''
warning='1'
critical='5'
greptype='less'
exit_formalities() {
if [ "X$1" != "X" ]; then
echo "$1"
fi
if [ "X${TEMP_FILE}" != "X" ]; then
rm -f "${TEMP_FILE}" || true
fi
exit "$2"
}
message=''
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit_formalities "${message}" "${ST_UK}"
;;
--version|-v)
print_version
exit_formalities "${message}" "${ST_UK}"
;;
--mandatory|-m)
mandatory="$2"
shift
;;
--optional|-o)
optional="$2"
shift
;;
--casesensitive|-i)
casesensitive="-i "
;;
--warning|-w)
warning="$2"
shift
;;
--critical|-c)
critical="$2"
shift
;;
--greptype|-t)
greptype="$2"
shift
;;
*)
echo "Unknown argument: $1"
echo ""
print_help
exit_formalities "${message}" "${ST_UK}"
;;
esac
shift
done
######## Validate arguments passed ########
if echo "${mandatory}" | grep "^\s*$" >/dev/null; then
mandatory=''
if echo "${optional}" | grep "^\s*$" >/dev/null; then
message="At least one argument among --mandatory/--optional should be specified. Use option -h for more details"
exit_formalities "${message}" "${ST_UK}"
fi
elif echo "${mandatory}" | grep -v "^[a-zA-Z0-9 ,]*$" >/dev/null; then
message="Invalid value: '${mandatory}' for option -m/--mandatory. Possible values: Alphabets and letters separated by comma"
exit_formalities "${message}" "${ST_UK}"
elif echo "${optional}" | grep -v "^[a-zA-Z0-9 ,]*$" >/dev/null; then
message="Invalid value: '${optional}' for option -o/--optional. Possible values: Alphabets and letters separated by comma"
exit_formalities "${message}" "${ST_UK}"
elif echo "${warning}" | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
message="Invalid value: '${warning}' for option -w/--warning. Possible value: Positive integer"
exit_formalities "${message}" "${ST_UK}"
elif echo "${critical}" | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
message="Invalid value: '${critical}' for option -c/--critical. Possible value: Positive integer"
exit_formalities "${message}" "${ST_UK}"
elif echo "${greptype}" | grep -v -e 'less' -e 'more' >/dev/null; then
message="Invalid value: '${greptype}' for option -t/--greptype. Possible value: less,more"
exit_formalities "${message}" "${ST_UK}"
fi
if [ "X${greptype}" = "Xless" ]; then
if [ ${warning} -gt ${critical} ]; then
message="Parameter Error: warning: '${warning}' must be greater than critical: '${critical}'"
exit_formalities "${message}" "${ST_UK}"
fi
elif [ "X${greptype}" = "Xmore" ]; then
if [ ${warning} -lt ${critical} ]; then
message="Parameter Error: warning: '${warning}' must be less than critical: '${critical}'"
exit_formalities "${message}" "${ST_UK}"
fi
fi
###########################################
form_command() {
unix_command='ps aux'
grep_case="grep ${casesensitive}"
if echo "${mandatory}" | grep -v "^\s*$" >/dev/null; then
temp="$(echo "${mandatory}" | sed "s/[[:space:]]*,[,[:space:]]*/,/g" | sed 's/^,//' | sed 's/,$//')"
temp="$(echo "${temp}" | sed "s/,/' | ${grep_case} '/g")"
unix_command="${unix_command} | ${grep_case} '${temp}'"
fi
if echo "${optional}" | grep -v "^\s*$" >/dev/null; then
temp="$(echo "${optional}" | sed "s/[[:space:]]*,[,[:space:]]*/,/g" | sed 's/^,//' | sed 's/,$//')"
temp="$(echo "${temp}" | sed "s/,/' -e '/g")"
unix_command="${unix_command} | ${grep_case} -e '${temp}'"
fi
echo "${unix_command} | grep -v 'grep' | grep -v '${PROGNAME}' | wc -l"
}
check_exit_status() {
if [ $? -ne 0 ]; then
echo "CRITICAL - Command: '${$1}' failed"
exit ${ST_CR}
fi
}
execute_check() {
execute_command="$(form_command)"
no_of_process="$(bash -c "${execute_command}")"
check_exit_status "${execute_command}"
echo "${no_of_process}" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//'
}
##############################################################################
############################ EXECUTE THE TEST ################################
##############################################################################
no_of_process="$(execute_check)"
if [ "X${greptype}" = "Xless" ]; then
num_test='-ge'
sign='>='
elif [ "X${greptype}" = "Xmore" ]; then
num_test='-le'
sign='<='
else
message="Script Error: Invalid value(${greptype}) for variable greptype"
exit_formalities "${message}" "${ST_UK}"
fi
if [ ${no_of_process} ${num_test} ${critical} ]; then
message="CRITICAL - No of matching process: ${no_of_process} (${no_of_process} ${sign} ${critical} CR)"
exit_formalities "${message}" "${ST_CR}"
elif [ ${no_of_process} ${num_test} ${warning} ]; then
message="WARNING - No of matching process: ${no_of_process} (${no_of_process} ${sign} ${warning} WR)"
exit_formalities "${message}" "${ST_WR}"
else
message="OK - No of matching process: ${no_of_process}"
exit_formalities "${message}" "${ST_OK}"
fi