]> git.pld-linux.org Git - packages/monitoring-plugin-check_file_exists.git/blob - check_file_exists.sh
77a0222f4537f87096f7bf08b835bf863d20f061
[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 exists_dir() {
39     $negate && STATE=$STATE_CRITICAL || STATE=$STATE_OK
40     echo "$(state_name): $1 EXISTS :: Directory" # don't show the first three lines of the file
41 }
42
43 not_exists() {
44         $negate && STATE=$STATE_OK || STATE=$STATE_CRITICAL
45         echo "$(state_name): $1 Does NOT exist"
46 }
47
48 # parse command line args
49 t=$(getopt -o n --long negate -n "$PROGNAME" -- "$@")
50 [ $? != 0 ] && exit $?
51 eval set -- "$t"
52
53 negate=false
54 while :; do
55         case "$1" in
56         -n|--negate)
57                 negate=true
58         ;;
59         --)
60                 shift
61                 break
62         ;;
63         *)
64                 echo >&2 "$PROGRAM: Internal error: [$1] not recognized!"
65                 exit 1
66         ;;
67         esac
68         shift
69 done
70
71 STATE=$STATE_UNKNOWN
72 if [ "$1" = "" ]; then
73         usage
74         exit $STATE
75 fi
76
77 if [ -f "$1" ]; then
78         exists "$1"
79 elif [ -d "$1" ]; then
80     exists_dir "$1"
81 else
82         not_exists "$1"
83 fi
84 exit $STATE
This page took 0.047539 seconds and 2 git commands to generate.