commit 58e4a0d44ac62c67cd7496a1721dcd2768eb355a
Author: Elan Ruusamäe <[email protected]>
Date:   Mon Feb 1 11:27:11 2016 +0200

    v1.1: add negate (-n) support

 check_file_exists.sh                     | 76 ++++++++++++++++++++++++++------
 monitoring-plugin-check_file_exists.spec |  4 +-
 2 files changed, 65 insertions(+), 15 deletions(-)
---
diff --git a/monitoring-plugin-check_file_exists.spec 
b/monitoring-plugin-check_file_exists.spec
index 1811620..20ee9d8 100644
--- a/monitoring-plugin-check_file_exists.spec
+++ b/monitoring-plugin-check_file_exists.spec
@@ -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
diff --git a/check_file_exists.sh b/check_file_exists.sh
index bc91daf..35ac363 100755
--- a/check_file_exists.sh
+++ b/check_file_exists.sh
@@ -1,27 +1,77 @@
-#! /bin/bash
+#!/bin/sh
 #
 # Author : Diego Martin Gardella [[email protected]]
 # Desc : Plugin to verify if a file exists
 #
-#
+# v1.0: Initial version by Diego Martin Gardella [[email protected]]
+# v1.1: Add negate support, by Elan Ruusamäe <[email protected]>
 
 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
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/monitoring-plugin-check_file_exists.git/commitdiff/58e4a0d44ac62c67cd7496a1721dcd2768eb355a

_______________________________________________
pld-cvs-commit mailing list
[email protected]
http://lists.pld-linux.org/mailman/listinfo/pld-cvs-commit

Reply via email to