[packages/monitoring-plugin-check_file_exists] v1.1: add negate (-n) support
glen
glen at pld-linux.org
Mon Feb 1 10:27:30 CET 2016
commit 58e4a0d44ac62c67cd7496a1721dcd2768eb355a
Author: Elan Ruusamäe <glen at delfi.ee>
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 [dgardella at gmail.com]
# Desc : Plugin to verify if a file exists
#
-#
+# v1.0: Initial version by Diego Martin Gardella [dgardella at gmail.com]
+# v1.1: Add negate support, by Elan Ruusamäe <glen at 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
================================================================
---- gitweb:
http://git.pld-linux.org/gitweb.cgi/packages/monitoring-plugin-check_file_exists.git/commitdiff/58e4a0d44ac62c67cd7496a1721dcd2768eb355a
More information about the pld-cvs-commit
mailing list