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