Wrong status displayed when using custom bash/shell script.

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
George0027
Posts: 4
Joined: Fri Jan 04, 2019 7:42 pm

Wrong status displayed when using custom bash/shell script.

Post by George0027 »

Hello!

I wrote my own little script called "check_files". Basically it checks whether or not a file exist within a specified directory on ubuntu 18.04.

I've spent quite some time researching both shell/bash scripting and nagios integration, but things aren't going as I want.
My code is using exit codes 0(OK) and 1(WARNING), however, on my nagios services, it displays WARNING when in actuality it should be OK.
Not sure how or why it's getting the wrong exit code.

When I run my code locally and then type in "echo $?" the exit code I get is 0.

Please let me know what I'm doing wrong, if you need more info simply request it.

Cheers,
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Wrong status displayed when using custom bash/shell scri

Post by scottwilkerson »

Can you share the plugin so we can look at it?
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
George0027
Posts: 4
Joined: Fri Jan 04, 2019 7:42 pm

Re: Wrong status displayed when using custom bash/shell scri

Post by George0027 »

Thanks for the reply, here's the script.

#!/bin/bash

echo =========================
#Files to be checked
fileName="Test"
file="File"
#Today's date
date=`date +_%Y.%m.%d`
#Today's date with the time
now=`date +_%Y.%m.%d_%H:%M:%S`
#Path location of the files to be checked for
path=/home/xxxx/TEST/Script_learning/Stuff/Files
#Exit code to trigger Nagios checks
exitCode=0

declare -a fileNameArray=($fileName $file)

for i in "${fileNameArray[@]}"
do
if [ -e $i$date ];
then
echo "$i found"
echo "Backup for $i$now was successful" >> ./Files/Backup.log
let exitCode=0
else
echo "$i not found!"
let exitCode=1
echo "Backup for $i$now has failed" >> ./Files/Backup.log
echo "Creating $i."
echo "File created on $now" >> ./Files/$i$date
fi
done
#
echo =========================

exit $exitCode;

hope it's clear enough to understand. It's my first "real" shell script, so it may be unclear.

Thanks!
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Wrong status displayed when using custom bash/shell scri

Post by scottwilkerson »

What text does it display when you run it?
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
George0027
Posts: 4
Joined: Fri Jan 04, 2019 7:42 pm

Re: Wrong status displayed when using custom bash/shell scri

Post by George0027 »

The only message I see on Nagios is this;

=========================
Test not found!
Creating Test.
File not found!
Creating File.
=========================

and the status for this service is always Warning, However, when I run the script manually, exit code is 0.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Wrong status displayed when using custom bash/shell scri

Post by ssax »

If you are seeing those messages it means that you're gonna get a warning because that code block contains the exitCode=1 options (which will output a WARNING status).

More than likely this is a permissions issue, please run this command first before testing:

Code: Select all

su - nagios
Likely when you run it as root it has the proper permissions but because the backend nagios processes run as the nagios user, you will need to "su - nagios" before testing for an apples-to-apples comparison.

Let us know the results.
George0027
Posts: 4
Joined: Fri Jan 04, 2019 7:42 pm

Re: Wrong status displayed when using custom bash/shell scri

Post by George0027 »

HEY!

So ya, there were two issues with my code.
The first was that the code nagios was trying to run was written from a different user, so I gave it the correct permissions.
Second, I added absolute path for the files I'm looking for, turns out I was running the script from a different directory.

Long story short, it works!!

THANKS so much.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Wrong status displayed when using custom bash/shell scri

Post by ssax »

Congratulations, you're now a developer! :ugeek:

Are we okay to lock the topic and mark it as resolved?
Locked