Wrong status displayed when using custom bash/shell script.

An open discussion forum for obtaining help with Nagios Core. Nagios Core users of all experience levels are welcome here. Subforum have been created for the discussion of Nagios Core and Nagios Plugin development.

NOTE: The SourceForge.net mailing lists have been deprecated in favor of this forum in order to expedite support and provide additional features not available on the old mailing list.

Wrong status displayed when using custom bash/shell script.

Postby George0027 » Fri Jan 04, 2019 7:48 pm

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,
George0027
 
Posts: 4
Joined: Fri Jan 04, 2019 7:42 pm

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

Postby scottwilkerson » Mon Jan 07, 2019 10:55 am

Can you share the plugin so we can look at it?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
User avatar
scottwilkerson
DevOps Engineer
 
Posts: 12916
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises

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

Postby George0027 » Tue Jan 08, 2019 2:40 pm

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!
George0027
 
Posts: 4
Joined: Fri Jan 04, 2019 7:42 pm

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

Postby scottwilkerson » Tue Jan 08, 2019 2:51 pm

What text does it display when you run it?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
User avatar
scottwilkerson
DevOps Engineer
 
Posts: 12916
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises

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

Postby George0027 » Tue Jan 08, 2019 3:51 pm

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.
George0027
 
Posts: 4
Joined: Fri Jan 04, 2019 7:42 pm

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

Postby ssax » Tue Jan 08, 2019 5:33 pm

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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
ssax
Dreams In Code
 
Posts: 3581
Joined: Wed Feb 11, 2015 12:54 pm

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

Postby George0027 » Wed Jan 09, 2019 12:49 pm

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.
George0027
 
Posts: 4
Joined: Fri Jan 04, 2019 7:42 pm

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

Postby ssax » Wed Jan 09, 2019 4:40 pm

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

Are we okay to lock the topic and mark it as resolved?
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
ssax
Dreams In Code
 
Posts: 3581
Joined: Wed Feb 11, 2015 12:54 pm


Return to Nagios Core

Who is online

Users browsing this forum: Google [Bot] and 14 guests