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,
Wrong status displayed when using custom bash/shell script.
-
- Posts: 4
- Joined: Fri Jan 04, 2019 7:42 pm
-
- 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
Can you share the plugin so we can look at it?
-
- Posts: 4
- Joined: Fri Jan 04, 2019 7:42 pm
Re: Wrong status displayed when using custom bash/shell scri
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!
#!/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!
-
- 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
What text does it display when you run it?
-
- Posts: 4
- Joined: Fri Jan 04, 2019 7:42 pm
Re: Wrong status displayed when using custom bash/shell scri
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.
=========================
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
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:
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.
More than likely this is a permissions issue, please run this command first before testing:
Code: Select all
su - nagios
Let us know the results.
-
- Posts: 4
- Joined: Fri Jan 04, 2019 7:42 pm
Re: Wrong status displayed when using custom bash/shell scri
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.
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
Congratulations, you're now a developer!
Are we okay to lock the topic and mark it as resolved?
Are we okay to lock the topic and mark it as resolved?