From: Elan Ruusamäe Date: Mon, 1 Feb 2016 09:27:11 +0000 (+0200) Subject: v1.1: add negate (-n) support X-Git-Tag: AC-branch X-Git-Url: http://git.pld-linux.org/?p=packages%2Fmonitoring-plugin-check_file_exists.git;a=commitdiff_plain;h=58e4a0d v1.1: add negate (-n) support --- 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@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 PROGNAME=`basename $0` PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'` . $PROGPATH/utils.sh -if [ "$1" = "" ] -then - echo -e " Use : $PROGNAME -- 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 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