File System

check_file_exists

Description:

Simply script to check if a file exists or not.

– return OK if file exists
– return CRITICAL if the file does not exists

Current Version

1.0

Last Release Date

2010-11-10

Compatible With

  • Nagios 3.x

Owner

License

GPL


Project Notes
The plugin checks if a file exists. Returns OK if the file exists , and show the first three lines of the file ( head -3 ) Returns CRITICAL if the file not exists.
Reviews (4) Add a Review
Getting opposite results to command line
by pelicanmedia, June 30, 2018

Hi, Firstly, thanks for the script(s). BUT... I am getting the opposite results on the web console than I am from the command line eg: My check_file_exists file = #!/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 (Elan Ruusamäe ) # v1.2: Also check if file is folder (Simon Smit) PROGNAME=`basename $0` PROGPATH=`echo $0 | sed -e 's,[\/][^\/][^\/]*$,,'` . $PROGPATH/utils.sh 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" } exists_dir() { $negate && STATE=$STATE_CRITICAL || STATE=$STATE_OK echo "$(state_name) - $1 EXISTS :: Directory" } 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 exists "$1" elif [ -d "$1" ]; then exists_dir "$1" else not_exists "$1" fi exit $STATE ========== In my commands.cfg = # 'check_file_exists' command definition define command { command_name check_file_exists command_line $USER1$/check_file_exists $ARG1$ } ========== In my_server_name.cfg = define service{ use generic-service host_name my_server_name service_description Check Backup - Nagios check_command heck_file_exist!/root/backups/nagios/nagios.tar.gz } ========== When I run from the command line... [root@centos libexec]# ./check_file_exists /root/backups/nagios OK - /root/backups/nagios EXISTS :: Directory [root@centos libexec]# ./check_file_exists /root/backups/nagios/nagios.tar.gz OK - /root/backups/nagios/nagios.tar.gz EXISTS [root@centos libexec]# ./check_file_exists /root/backups/nagios/nagios.tar CRITICAL - /root/backups/nagios/nagios.tar Does NOT exist In the web console... Check Backup - Nagios | CRITICAL | 27-06-2018 14:22:37 | 0d 0h 0m 30s | 1/3 | CRITICAL - /root/backups/nagios/nagios.tar.gz Does NOT exist Any ideas as to why I would be getting the opposite result ?? Thanks



Help
by sth_bytelab, August 31, 2016

Hi I am new to NagiosXI and plugins in general so I want to know if I can use this plugin to monitor file creation on a windows 2008 server. I also have a question about how the commandline for the plugin are supposed to look? Thanks



version 1.1 with negate support
by glen, January 31, 2016

hi i've loaded the existing code to git and added negate support via -n or --negate option. so released it as v1.1 here: https://github.com/pld-linux/monitoring-plugin-check_file_exists



Great script, but needed it the other way around
by erling, June 30, 2015

Great script, but I needed a script to check if a file does not exist. On my Ubuntu server I want to check if the file /var/run/reboot-required does not exist and warn me if it does (to notify me the server needs a reboot). To check this I just had to switch the output statements of the script, which I saved as check_file_notexists. Hereby the altered code for check_file_notexists: #! /bin/bash # # Author : Diego Martin Gardella [dgardella@gmail.com] # Modified by Erling Ouweneel to switch OK and CRITICAL # # Desc : Plugin to verify if a file does not exist # # 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 if [ -f $1 ] then echo "CRITICAL - $1 : EXISTS :: `head -3 $1`" # shows the first three lines of the file exit $STATE_CRITICAL else echo "OK : $1 Does NOT exists " exit $STATE_OK fi



Add a Review

You must be logged in to submit a review.

Thank you for your review!

Your review has been submitted and is pending approval.

Recommend

To:


From:


Thank you for your recommendation!

Your recommendation has been sent.

Project Stats
Rating
4.5 (4)
Favorites
1
Views
128,037