Page 1 of 1

Wrong status displayed when using custom bash/shell script.

Posted: Fri Jan 04, 2019 7:48 pm
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,

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

Posted: Mon Jan 07, 2019 10:55 am
by scottwilkerson
Can you share the plugin so we can look at it?

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

Posted: Tue Jan 08, 2019 2:40 pm
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!

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

Posted: Tue Jan 08, 2019 2:51 pm
by scottwilkerson
What text does it display when you run it?

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

Posted: Tue Jan 08, 2019 3:51 pm
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.

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

Posted: Tue Jan 08, 2019 5:33 pm
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.

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

Posted: Wed Jan 09, 2019 12:49 pm
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.

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

Posted: Wed Jan 09, 2019 4:40 pm
by ssax
Congratulations, you're now a developer! :ugeek:

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