Page 1 of 1

Opposite results in GUI than in command line

Posted: Thu Aug 16, 2018 4:25 am
by pelicanmedia
Hi,

I am using the check_file_exists plugin from Github linked from Nagios Exchange.

BUT... I am getting the opposite results on the web console than I am from the command line eg:

My check_file_exists file =

Code: Select all

#!/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 (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 commands.cfg =

Code: Select all

# 'check_file_exists' command definition
define command {
    command_name    check_file_exists
    command_line    $USER1$/check_file_exists $ARG1$
}
In my_server_name.cfg =

Code: Select all

define service{
    use    generic-service
    host_name    my_server_name
    service_description    Check Backup - Nagios
    check_command    check_file_exists!/root/backups/nagios/nagios.tar.gz
}
When I run from the command line...

Code: Select all

[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 | 16-08-2018 10:22:41 | 0d 0h 58m 5s | 3/3 | CRITICAL - /root/backups/nagios/nagios.tar.gz Does NOT exist

Any ideas as to why I would be getting the opposite result ??

Thanks

Re: Opposite results in GUI than in command line

Posted: Thu Aug 16, 2018 9:23 am
by scottwilkerson
It is likely permissions

When you are running it from the console you are running as root and it has permissions to see the file, but when Nagios runs it it will use the nagios user

you can verify this by running the command as the nagios user

Code: Select all

su nagios -c './check_file_exists /root/backups/nagios/nagios.tar.gz'
and look at the file and directory permissions

Code: Select all

ls -al /root/backups/nagios/
ls -dl /root/backups
ls -dl /root