]> git.pld-linux.org Git - packages/monitoring-plugin-check_file_exists.git/blame - check_file_exists.sh
v1.1: add negate (-n) support
[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#
58e4a0d4
ER
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>
e3cafd69
ER
8
9PROGNAME=`basename $0`
10PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
11
12. $PROGPATH/utils.sh
13
58e4a0d4
ER
14usage() {
15 echo "Usage: $PROGRAM [-n] [file]
16
17Options:
18 -n, --negate negate the result
19"
20}
21
22state_name() {
23 case "$STATE" in
24 $STATE_OK)
25 echo OK
26 ;;
27 $STATE_CRITICAL)
28 echo CRITICAL
29 ;;
30 esac
31}
32
33exists() {
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}
e3cafd69 37
58e4a0d4
ER
38not_exists() {
39 $negate && STATE=$STATE_OK || STATE=$STATE_CRITICAL
40 echo "$(state_name): $1 Does NOT exist"
41}
42
43# parse command line args
44t=$(getopt -o n --long negate -n "$PROGNAME" -- "$@")
45[ $? != 0 ] && exit $?
46eval set -- "$t"
47
48negate=false
49while :; 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
64done
65
66STATE=$STATE_UNKNOWN
67if [ "$1" = "" ]; then
68 usage
69 exit $STATE
70fi
e3cafd69 71
58e4a0d4
ER
72if [ -f "$1" ]; then
73 exists "$1"
e3cafd69 74else
58e4a0d4 75 not_exists "$1"
e3cafd69 76fi
58e4a0d4 77exit $STATE
This page took 0.308279 seconds and 4 git commands to generate.