-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_sapcontrol.sh
347 lines (317 loc) · 10.7 KB
/
check_sapcontrol.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/bash
version=1.2
verbose=0
# mandatory parameters:
hostflag=0
sidflag=0
sysnrflag=0
typeflag=0
# find out name of script
scriptname=$(/usr/bin/basename "$0")
# source utils.sh
if [ -f /usr/local/nagios/libexec/utils.sh ]; then
. /usr/local/nagios/libexec/utils.sh
else
. /usr/lib/nagios/plugins/utils.sh
fi
# add var-definitions
sedpath=/bin/sed
awkpath=/bin/awk
sapctlpath=/usr/sap/hostctrl/exe/sapcontrol
functiontype=GetProcessList
# define functions
function printUsage() {
echo
echo "This plugin checks the status of a SAP-system by using sapcontrol"
echo
echo "Usage: $scriptname [-H hostname|IP] [-S SID] [-N SYS-nr] [-T systemtype] [-i tenant-id] [-s service-instance] [-h] [-v] [-V]"
echo "Options:"
echo " -H hostname or IP of SAP-system"
echo " -S SAP-Id"
echo " -N SYS-Number (network-port)"
echo " -T SAP-systemtype [ABAP_DW|ABAP_ENQ|ABAP_MSG|ABAP_GW|ABAP_ICM|JAVA_MSG|JAVA_ENQ|JAVA_GW|JAVA_JSTART|JAVA_SRV0|JAVA_SRV1|JAVA_SRV2|JAVA_SRV3|JAVA_SRV|HDB_NS|HDB_IDX]"
echo " -i tenant-Id"
echo " -s SAP central service instance"
echo " -v verbose output [0|1|2|3]"
echo " -V Version"
echo " -h this help"
}
function printVersion() {
echo
echo "$scriptname Version $version"
echo
}
function checkOptions() {
while getopts ":hVv:N:s:i:H:S:i:T:" opt; do
case $opt in
H) HOST=$OPTARG
hostflag=1;
#echo "hostname is $HOST"
;;
S) SID=$OPTARG
sidflag=1;
;;
N) NR=$OPTARG
sysnrflag=1;
;;
T) TYPE=$OPTARG
typeflag=1;
;;
i) TENANTID=$OPTARG
;;
s) SCSNR=$OPTARG
;;
v) verbose=$OPTARG
;;
h) printUsage
exit "$STATE_UNKNOWN"
;;
V) printVersion
exit "$STATE_UNKNOWN"
;;
\?)echo
echo "Invalid option: $OPTARG"
printUsage
exit "$STATE_UNKNOWN"
;;
:) echo
echo "Invalid option: $OPTARG requires an argument"
printUsage
exit "$STATE_UNKNOWN"
;;
*) printUsage
;;
esac
done
}
function checkNumberArgs() {
mandarg=$((hostflag + sidflag + sysnrflag + typeflag))
if [ "$OPTIND" -eq 1 ] || [ "$mandarg" -lt 4 ];
then
echo
echo "Error: Not enough arguments provided or mandatory arguments missing."
printUsage
exit "$STATE_UNKNOWN"
fi
}
checkOptions "$@"
checkNumberArgs
if [ -e "$sapctlpath" ] || [ -x "$sapctlpath" ]; then
sapctl="sudo -u icinga /usr/sap/hostctrl/exe/sapcontrol"
else
echo "$sapctlpath not found or executable"
exit "$STATE_UNKNOWN"
fi
#set default-state
STATE="UNDEFINED"
PARAMETERS=$*
#export LD_LIBRARY_PATH="/usr/sap/Q10/SYS/exe/run:/usr/sap/Q10/SYS/exe/uc/linuxx86_64:/usr/sap/Q10/hdbclient"
case $TYPE in
ABAP_DW)
regstring="disp+work"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep "disp+work")
OUTPUT=$($sapctl -host "$HOST" -nr "$NR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $3}'|$sedpath 's/,//g')
;;
ABAP_ENQ)
regstring="enserver"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep "enserver")
OUTPUT=$($sapctl -host "$HOST" -nr "$SCSNR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $3}'|$sedpath 's/,//g')
;;
ABAP_MSG)
regstring="msg_server"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep "msg_server")
OUTPUT=$($sapctl -host "$HOST" -nr "$SCSNR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $3}'|$sedpath 's/,//g')
;;
ABAP_GW)
regstring="gwrd"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep "gwrd")
OUTPUT=$($sapctl -host "$HOST" -nr "$NR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $3}'|$sedpath 's/,//g')
;;
ABAP_ICM)
regstring="icman"
# 20230925, i007163, added sudo to sapcontrol-call
#OUTPUT=$(sudo -u icinga /usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep "icman")
OUTPUT=$($sapctl -host "$HOST" -nr "$NR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $3}'|$sedpath 's/,//g')
;;
JAVA_ENQ)
regstring="enserver"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $SCSNR -function GetProcessList|grep "enserver")
OUTPUT=$($sapctl -host "$HOST" -nr "$SCSNR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $3}'|$sedpath 's/,//g')
;;
JAVA_MSG)
regstring="msg_server"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $SCSNR -function GetProcessList|grep "msg_server")
OUTPUT=$($sapctl -host "$HOST" -nr "$SCSNR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $3}'|$sedpath 's/,//g')
;;
JAVA_GW)
regstring="gwrd"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep "gwrd")
OUTPUT=$($sapctl -host "$HOST" -nr "$NR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $3}'|$sedpath 's/,//g')
;;
JAVA_JSTART)
regstring="jstart"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep "jstart")
OUTPUT=$($sapctl -host "$HOST" -nr "$NR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $4}'|$sedpath 's/,//g')
;;
JAVA_SRV0)
regstring="server0"
functiontype="J2EEGetProcessList"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $SCSNR -function J2EEGetProcessList|grep "server0")
OUTPUT=$($sapctl -host "$HOST" -nr "$SCSNR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $9}'|$sedpath 's/,//g')
;;
JAVA_SRV1)
regstring="server1"
functiontype="J2EEGetProcessList"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $SCSNR -function J2EEGetProcessList|grep "server1")
OUTPUT=$($sapctl -host "$HOST" -nr "$SCSNR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $9}'|$sedpath 's/,//g')
;;
JAVA_SRV2)
regstring="server2"
functiontype="J2EEGetProcessList"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $SCSNR -function J2EEGetProcessList|grep "server2")
OUTPUT=$($sapctl -host "$HOST" -nr "$SCSNR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $9}'|$sedpath 's/,//g')
;;
JAVA_SRV3)
regstring="server3"
functiontype="J2EEGetProcessList"
# OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $SCSNR -function J2EEGetProcessList|grep "server3")
OUTPUT=$($sapctl -host "$HOST" -nr "$SCSNR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $9}'|$sedpath 's/,//g')
;;
JAVA_SRV)
regstring="J2EE Server"
functiontype="J2EEGetProcessList"
bsrv=0 # Bad Java Services
gsrv=0 # Good Java Services
declare -a myArray
if [[ "$verbose" -gt 2 ]]
then
mapfile myArray < "./ifile_JAVA_SRV.txt"
else
mapfile myArray < <($sapctl -host $HOST -nr $SCSNR -function $functiontype | grep $regstring)
fi
if [[ "$verbose" -gt 1 ]]
then
printf '%s' "${myArray[@]}"
fi
for i in ${!myArray[@]};
do
STATE=$(echo ${myArray[$i]}|$awkpath 'NF{print $9}'|$sedpath 's/,//g')
if [[ "$STATE" == "Running" ]]
then
((gsrv++))
else
((bsrv++))
fi
done
if [[ "$verbose" -gt 1 ]]
then
echo "Number good JAVA : $gsrv"
echo "Number bad JAVA : $bsrv"
fi
if [[ "gsrv" -gt 0 ]] && [[ "bsrv" -eq 0 ]]
then
STATE='GREEN'
elif [[ "gsrv" -gt 0 ]] && [[ "bsrv" -eq 1 ]]
then
STATE='YELLOW'
else
STATE='RED'
fi
;;
HDB_NS)
# HANA DB Nameserver
regstring="Nameserver"
#OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep Nameserver)
OUTPUT=$($sapctl -host "$HOST" -nr "$NR" -function $functiontype|grep $regstring)
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $4}'|$sedpath 's/,//g')
;;
HDB_IDX)
# HANA DB Indexserver
regstring="Index"
# when executng HDB_IDX with SID=ALL we loop over all Tenants
if [[ "$SID" == "ALL" ]]
then
bidx=0 # Bad Index Server
gsrv=0 # Good Index Server
declare -a myArray
if [[ "$verbose" -gt 2 ]]
then
mapfile myArray < "./ifile_HDB_IDX.txt"
else
mapfile myArray < <($sapctl -host "$HOST" -nr "$NR" -function $functiontype|grep $regstring)
fi
if [[ "$verbose" -gt 1 ]]
then
printf '%s' "${myArray[@]}"
fi
for i in ${!myArray[@]};
do
STATE=$(echo ${myArray[$i]}|$awkpath 'NF{print $5}'|$sedpath 's/,//g')
if [[ "$STATE" == "Running" ]]
then
((gidx++))
else
((bidx++))
fi
done
if [[ "$verbose" -gt 1 ]]
then
echo "Number good IDX : $gidx"
echo "Number bad IDX : $bidx"
fi
if [[ "gidx" -gt 0 ]] && [[ "bidx" -eq 0 ]]
then
STATE='GREEN'
else
STATE='RED'
fi
else
#OUTPUT=$(/usr/sap/hostctrl/exe/sapcontrol -host $HOST -nr $NR -function GetProcessList|grep Index|grep $SID)
#OUTPUT=$($sapctl -host $HOST -nr $NR -function $functiontype|grep $regstring|grep $SID)
# if we want only one tenant we use the tenantID-parameter for grep:
OUTPUT=$($sapctl -host "$HOST" -nr "$NR" -function $functiontype|grep $regstring|grep "$TENANTID")
STATE=$(echo "$OUTPUT"|$awkpath 'NF{print $4}'|$sedpath 's/,//g')
fi
;;
*)
echo -n "unknown Type"
exit "$STATE_UNKNOWN"
;;
esac
if [[ "$verbose" -gt 0 ]]
then
echo "$PARAMETERS"
fi
if [[ "$verbose" -gt 1 ]]
then
echo "$OUTPUT"
echo "$STATE"
fi
if [[ "$STATE" == "GREEN" ]] || [[ "$STATE" == "Running" ]]
then
#echo -e "\033[42m\033[30mOK\033[0m"
echo "OK - sapcontrol-state for $regstring is $STATE"
exit "$STATE_OK"
elif [[ "$STATE" == "YELLOW" ]]
then
#echo -e "\033[43m\033[30mWARNING\033[0m"
echo "WARNING - sapcontrol-state $regstring is $STATE"
exit "$STATE_WARNING"
else
#echo -e "\033[41m\033[30mERROR\033[0m"
echo "ERROR - sapcontrol-state $regstring is $STATE"
exit "$STATE_CRITICAL"
fi