-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_irods.sh
executable file
·362 lines (308 loc) · 9.25 KB
/
check_irods.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/bash
#The MIT License (MIT)
#
#Copyright (c) 2015 Johan Guldmyr CSC
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation fi/les (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
#
###
# This is a nagios check that checks if one can write, ils, get and remove to
# a preconfigured irods.
# Tested with IRODS 4.1.7, API Version=d, OS authentication
# Irods updates the available free space once a day.
##
VERSION="0.6"
SUB_VERSION="0"
### Settings
# Nagios return codes
OK="0"
WARN="1"
CRITICAL="2"
UNKNOWN="3"
usage() {
echo "This is an iRODS health check."
echo "Usage: check_irods.sh [-h|-v|-n|-d|-t time|-f file|-p file]"
echo " -h help"
echo " -v version"
echo " -f the path of the iRODS environment json file. See iRODS documentation for details."
echo " -p the path of the file containing the password to connect to iRODS."
echo " -n implies no trash can enabled within the iRODS instance."
echo " -d debug enabled."
echo " -t timeout limit in seconds. The default is 30 s."
}
TEMPFILE=$(mktemp)
if [ "$?" != 0 ]; then
echo "UNKNOWN: problem creating tempfile as user $USER on hostname $HOSTNAME."
exit 3
fi
WORK_DIR=$(mktemp -d)
if [ "$?" != 0 ]; then
echo "UNKNOWN: problem creating tempdir as user $USER on hostname $HOSTNAME."
exit 3
fi
# This removes the path to the file
FILENAMEONIRODS=$(basename $TEMPFILE)
# Set this to 1 for some extra output
DEBUG=0
# Set this to 1 if iRODS has not enabled the trash can
NOTRASHCAN=0
#COMMANDS
iput="iput"
iget="iget"
irm="irm"
irmtrash="irmtrash"
ilsresc="ilsresc"
iinit="iinit"
iquest="iquest"
ils="ils"
rm="rm"
### End of settings
safety() {
which $ils >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo "UNKNOWN: Could not find ils in \$PATH"
exit $UNKNOWN
fi
if [ "$TEMPFILE" == "/" ] || [ "$TEMPFILE" == "/etc" ]; then
echo "UNKNOWN: $TEMPFILE looks bad."
exit 3
fi
$ils >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo "UNKNOWN: Could not list with ils"
exit $UNKNOWN
fi
}
perfdataf() {
which $ilsresc >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo "UNKNOWN: Could not find ilsresc in \$PATH"
exit $UNKNOWN
fi
RESOURCES="$($ilsresc |grep -v "resource group")"
re='^[0-9]+$'
# To find out free space of irods resources that publish this information via ilsresc
for res in $RESOURCES; do
FREESPACE=$($ilsresc -l|grep -A 8 "resource name: $res"|grep "free space"|cut -d ":" -f2|sed -e 's/\ //')
if [[ $FREESPACE =~ $re ]] ; then
perfdata="$perfdata $res$_free_space=$FREESPACE"
fi
done
# Number of users/groups:
users=$($iquest "%s" "select count(USER_ID) where USER_TYPE <> 'rodsgroup'")
groups=$($iquest "%s" "select count(USER_NAME) where USER_TYPE = 'rodsgroup'")
perfdata="$perfdata users=$users groups=$groups"
echo $perfdata|sed -e 's/^,//'
}
writeafile() {
which $iput >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo "UNKNOWN: Could not find iput in \$PATH"
exit $UNKNOWN
fi
date >> "$TEMPFILE"
if [ "$DEBUG" != 0 ]; then
echo "contents of $TEMPFILE:"
cat "$TEMPFILE"
echo "Executing command: $iput -v $TEMPFILE"
$iput -v "$TEMPFILE"
else
$iput "$TEMPFILE"
fi
return "$?"
}
listafile() {
which $ils >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo "UNKNOWN: Could not find ils in \$PATH"
exit $UNKNOWN
fi
if [ "$DEBUG" != 0 ]; then
echo "Executing command: $ils -v $FILENAMEONIRODS"
$ils -v "$FILENAMEONIRODS"
else
$ils "$FILENAMEONIRODS" >/dev/null 2>&1
fi
return "$?"
}
getafile() {
which $iget >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo "UNKNOWN: Could not find iget in \$PATH"
exit $UNKNOWN
fi
cd
if [ "$DEBUG" != 0 ]; then
echo "Executing command: $iget -v $FILENAMEONIRODS"
$iget -v "$FILENAMEONIRODS"
else
$iget "$FILENAMEONIRODS"
fi
IGETSTATUS="$?"
if [ "$DEBUG" != 0 ]; then
$rm -v "$FILENAMEONIRODS"
else
$rm "$FILENAMEONIRODS"
fi
return "$IGETSTATUS"
}
removeafile() {
which $irm >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo "UNKNOWN: Could not find irm in \$PATH"
exit $UNKNOWN
fi
if [ "$TEMPFILE" == "/" ] || [ "$TEMPFILE" == "/etc" ]; then
echo "UNKNOWN: $TEMPFILE looks bad."
exit 3
fi
cd
if [ "$DEBUG" != 0 ]; then
echo "Executing command: $irm -v $FILENAMEONIRODS"
$irm -v "$FILENAMEONIRODS"
else
$irm "$FILENAMEONIRODS"
fi
IRMSTATUS="$?"
if [ "$DEBUG" != 0 ]; then
echo "Executing command: $rm -v $TEMPFILE"
$rm -v "$TEMPFILE"
else
$rm "$TEMPFILE"
fi
return "$IRMSTATUS"
}
removetrash() {
which $irmtrash >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo "UNKNOWN: Could not find irmtrash in \$PATH"
exit $UNKNOWN
fi
if [ "$DEBUG" != 0 ]; then
echo "Executing command: $irmtrash -v"
$irmtrash -v
else
$irmtrash
fi
IRMTSTATUS="$?"
return "$IRMTSTATUS"
}
function run_with_timeout {
cmd="$1"; timeout="$2";
grep -qP '^\d+$' <<< $timeout || timeout=30
(
eval "$cmd" &
child=$!
trap -- "" SIGTERM
(
sleep $timeout
kill $child 2> /dev/null
) &
wait $child
return $?
)
}
execute_checks() {
if [ "$DEBUG" != 0 ]; then
echo "Exporting IRODS_ENVIRONMENT_FILE=$opt_f"
echo "Exporting HOME=$WORK_DIR"
fi
declare -x IRODS_ENVIRONMENT_FILE="$opt_f"
declare -x HOME="$WORK_DIR"
if [ "$DEBUG" != 0 ]; then
echo "Initializing credentials: $iinit < $opt_p"
$iinit < $opt_p
else
$iinit < $opt_p >/dev/null 2>&1
fi
safety
PERFDATA="$(perfdataf)"
writeafile
writestatus="$?"
listafile
liststatus="$?"
getafile
getstatus="$?"
removeafile
removestatus="$?"
if [ $NOTRASHCAN == 0 ]; then
removetrash
removetrashstatus="$?"
else
removetrashstatus=0
fi
unset IRODS_ENVIRONMENT_FILE
if [ "$DEBUG" != 0 ]; then
echo "Executing command: $rm -v -rf $WORK_DIR"
$rm -v -rf "$WORK_DIR"
else
$rm -rf "$WORK_DIR"
fi
### Checking the returns of the functions
if [ "$writestatus" != 0 ] || [ "$liststatus" != 0 ] || [ "$getstatus" != 0 ] || [ "$removestatus" != 0 ] || [ "$removetrashstatus" != 0 ]; then
echo "CRITICAL: writestatus = $writestatus, liststatus = $liststatus, getstatus = $getstatus, removestatus = $removestatus, removetrashstatus = $removetrashstatus | $PERFDATA"
exit $CRITICAL
elif [ "$writestatus" == 0 ] || [ "$liststatus" != 0 ] || [ "$getstatus" == 0 ] || [ "$removestatus" == 0 ] || [ "$removetrashstatus" == 0 ]; then
echo "OK: writestatus = $writestatus, liststatus = $liststatus, getstatus = $getstatus, removestatus = $removestatus, removetrashstatus = $removetrashstatus | $PERFDATA"
exit $OK
fi
}
### Execution
if [ "$#" == "0" ]; then
usage
exit 1
fi
while getopts "vhndt:f:p:" opt; do
if [ "$opt" == "?" ]; then
exit 1
fi
declare "opt_$opt=${OPTARG:-0}"
done
if [ "$opt_h" == "0" ]; then
usage
exit 0
fi
if [ "$opt_v" == "0" ]; then
echo "version $VERSION"
exit 0
fi
if [ "$opt_n" == "0" ]; then
NOTRASHCAN=1
fi
if [ "$opt_d" == "0" ]; then
DEBUG=1
fi
if [ "$opt_f" == "" ]; then
echo "Error: irods env json file is manadatory"
exit 1
fi
if [ "$opt_p" == "" ]; then
echo "Error: irods password file is manadatory"
exit 1
fi
if [ "$opt_t" == "" ]; then
TIMEOUT=30
else
TIMEOUT=$opt_t
fi
run_with_timeout execute_checks $TIMEOUT
if [ "$?" != "0" ]; then
echo "CRITICAL: timed out after $TIMEOUT seconds"
exit $CRITICAL
fi