v1.1: add negate (-n) support AC-branch auto/ac/monitoring-plugin-check_file_exists-1.1-1 auto/th/monitoring-plugin-check_file_exists-1.1-1
authorElan Ruusamäe <glen@delfi.ee>
Mon, 1 Feb 2016 09:27:11 +0000 (11:27 +0200)
committerElan Ruusamäe <glen@delfi.ee>
Mon, 1 Feb 2016 09:27:11 +0000 (11:27 +0200)
check_file_exists.sh
monitoring-plugin-check_file_exists.spec

index bc91daf..35ac363 100755 (executable)
@@ -1,27 +1,77 @@
-#! /bin/bash
+#!/bin/sh
 #
 # Author : Diego Martin Gardella [dgardella@gmail.com]
 # Desc : Plugin to verify if a file exists
 #
-#
+# v1.0: Initial version by Diego Martin Gardella [dgardella@gmail.com]
+# v1.1: Add negate support, by Elan Ruusamäe <glen@pld-linux.org>
 
 PROGNAME=`basename $0`
 PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
 
 . $PROGPATH/utils.sh
 
-if [ "$1" = "" ]
-then
-       echo -e " Use : $PROGNAME <file_name> -- Ex : $PROGNAME /etc/hosts \n "
-       exit $STATE_UNKNOWN
-fi
+usage() {
+       echo "Usage: $PROGRAM [-n] [file]
+
+Options:
+  -n, --negate    negate the result
+"
+}
+
+state_name() {
+       case "$STATE" in
+               $STATE_OK)
+                       echo OK
+                       ;;
+               $STATE_CRITICAL)
+                       echo CRITICAL
+                       ;;
+       esac
+}
+
+exists() {
+       $negate && STATE=$STATE_CRITICAL || STATE=$STATE_OK
+       echo "$(state_name): $1 EXISTS :: `head -3 $1`" # shows the first three lines of the file
+}
 
+not_exists() {
+       $negate && STATE=$STATE_OK || STATE=$STATE_CRITICAL
+       echo "$(state_name): $1 Does NOT exist"
+}
+
+# parse command line args
+t=$(getopt -o n --long negate -n "$PROGNAME" -- "$@")
+[ $? != 0 ] && exit $?
+eval set -- "$t"
+
+negate=false
+while :; do
+       case "$1" in
+       -n|--negate)
+               negate=true
+       ;;
+       --)
+               shift
+               break
+       ;;
+       *)
+               echo >&2 "$PROGRAM: Internal error: [$1] not recognized!"
+               exit 1
+       ;;
+       esac
+       shift
+done
+
+STATE=$STATE_UNKNOWN
+if [ "$1" = "" ]; then
+       usage
+       exit $STATE
+fi
 
-if [ -f $1 ]
-then
-       echo "OK - $1 : EXISTS :: `head -3 $1`" # shows the first three lines of the file
-       exit $STATE_OK
+if [ -f "$1" ]; then
+       exists "$1"
 else
-       echo "CRITICAL : $1 Does NOT exists "
-       exit $STATE_CRITICAL
+       not_exists "$1"
 fi
+exit $STATE
index 1811620..20ee9d8 100644 (file)
@@ -1,8 +1,8 @@
 %define                plugin  check_file_exists
 Summary:       Monitoring plugin to check if a file exists or not
 Name:          monitoring-plugin-%{plugin}
-Version:       1.0
-Release:       2
+Version:       1.1
+Release:       1
 License:       GPL
 Group:         Networking
 Source0:       %{plugin}.sh
This page took 0.563288 seconds and 4 git commands to generate.