]> git.pld-linux.org Git - packages/monitoring-plugin-check_file_exists.git/blob - check_file_exists.sh
v1.1: add negate (-n) support
[packages/monitoring-plugin-check_file_exists.git] / check_file_exists.sh
1 #!/bin/sh
2 #
3 # Author : Diego Martin Gardella [dgardella@gmail.com]
4 # Desc : Plugin to verify if a file exists
5 #
6 # v1.0: Initial version by Diego Martin Gardella [dgardella@gmail.com]
7 # v1.1: Add negate support, by Elan Ruusamäe <glen@pld-linux.org>
8
9 PROGNAME=`basename $0`
10 PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
11
12 . $PROGPATH/utils.sh
13
14 usage() {
15         echo "Usage: $PROGRAM [-n] [file]
16
17 Options:
18   -n, --negate    negate the result
19 "
20 }
21
22 state_name() {
23         case "$STATE" in
24                 $STATE_OK)
25                         echo OK
26                         ;;
27                 $STATE_CRITICAL)
28                         echo CRITICAL
29                         ;;
30         esac
31 }
32
33 exists() {
34         $negate && STATE=$STATE_CRITICAL || STATE=$STATE_OK
35         echo "$(state_name): $1 EXISTS :: `head -3 $1`" # shows the first three lines of the file
36 }
37
38 not_exists() {
39         $negate && STATE=$STATE_OK || STATE=$STATE_CRITICAL
40         echo "$(state_name): $1 Does NOT exist"
41 }
42
43 # parse command line args
44 t=$(getopt -o n --long negate -n "$PROGNAME" -- "$@")
45 [ $? != 0 ] && exit $?
46 eval set -- "$t"
47
48 negate=false
49 while :; do
50         case "$1" in
51         -n|--negate)
52                 negate=true
53         ;;
54         --)
55                 shift
56                 break
57         ;;
58         *)
59                 echo >&2 "$PROGRAM: Internal error: [$1] not recognized!"
60                 exit 1
61         ;;
62         esac
63         shift
64 done
65
66 STATE=$STATE_UNKNOWN
67 if [ "$1" = "" ]; then
68         usage
69         exit $STATE
70 fi
71
72 if [ -f "$1" ]; then
73         exists "$1"
74 else
75         not_exists "$1"
76 fi
77 exit $STATE
This page took 0.051153 seconds and 3 git commands to generate.