Send_nsca not working in cron

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
SimplySimple
Posts: 4
Joined: Thu May 14, 2015 5:17 am

Send_nsca not working in cron

Post by SimplySimple »

Hello All,

I am setting up passive checks for the first time.
I have gotten to the point where my script, to check bandwidth, will run
and successfully execute this command
send_nsca -H 172.16.0.0 -p 5667 -d ";" -c /etc/nagios/send_nsca.cfg <<< "Host;Service;$exitcode;$nagiosmsg"
or
echo -e "Host;Service;$exitcode;$nagiosmsg" | send_nsca -H 172.16.0.0 -p 5667 -d ";" -c /etc/nagios/send_nsca.cfg

Now for whatever reason when I run my script using cron it never sends the data packet to the nagios host.
I have it currently setup so the nsca daemon outputs to a text file so I can see when requests are received.(This is on nagios machine)
I also used logger to verify the script in cron would output the success message when send_nsca finishes and exits.

Any help would be much appreciated because I'm very stuck with this one.
Thanks.
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: Send_nsca not working in cron

Post by jdalrymple »

Is it in the right user's crontab?
Do you see the process getting executed in /var/log/cron?
Is selinux maybe hampering things a bit?
SimplySimple
Posts: 4
Joined: Thu May 14, 2015 5:17 am

Re: Send_nsca not working in cron

Post by SimplySimple »

It is in roots crontab.
I do see the process getting executed in the log file.
Selinux is disabled.

A coworker suggested something along the lines of adding tty to cron.
Since cron isn't run in the same environment as a terminal.

I am still looking into this and will post if I find a solution.
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: Send_nsca not working in cron

Post by jdalrymple »

Look for environmental variables that aren't going to exist in your cron shell environment and also make sure that all of your path references in and out of the script are absolute not relative. That's where I'd start before trying to build up a whole shell environment that mimics your interactive one.
SimplySimple
Posts: 4
Joined: Thu May 14, 2015 5:17 am

Re: Send_nsca not working in cron

Post by SimplySimple »

Thank you for reply's btw.

Ok I found my solution for this.
My script was exiting prematurely in an if statement.
Statement is to check for valid network interfaces.

I'm guessing because cron isn't executed in the same environment it was taking 0 for the value
of $foundface. So then it would just jump straight to the if statement and exit.
Not really sure how exactly that was happening, just a hypothesis.


[BEFORE]
faces=(`ifconfig|grep 'Link encap'|awk '{print $1}'`)

foundface=0

for i in "${faces[@]}"
do
if [[ $i == $interface || $interface == "" ]]; then
foundface=1
fi
done

if [ $foundface == 0 ]; then
echo "Sorry that is not an interface on this machine"
echo;
echo "Availble Interfaces:"
printf '%s\n' "${faces[@]}"
exit
fi


[AFTER]
faces=(`ifconfig|grep 'Link encap'|awk '{print $1}'`)

foundface=0

for i in "${faces[@]}"
do
if [[ $i == $interface || $interface == "" ]]; then
foundface=1
fi
done

if [ $foundface == 0 ]; then
echo "Sorry that is not an interface on this machine"
echo;
echo "Availble Interfaces:"
printf '%s\n' "${faces[@]}"
fi
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: Send_nsca not working in cron

Post by jdalrymple »

I assume that there is more to the script? If not it seems like you should indeed exit with an issue.

It's working to your liking and we can mark it solved?
SimplySimple
Posts: 4
Joined: Thu May 14, 2015 5:17 am

Re: Send_nsca not working in cron

Post by SimplySimple »

Yes there is more to my script.
I have attached it because I find it quite useful.
It will monitor your bandwidth with vnstat and output in a format nagios understands.
Also to note, this is my first proper script.

Yes you can mark this as solved.
Thank you.
Attachments
band.sh
Bandwidth Monitoring Tool
(5.6 KiB) Downloaded 285 times
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: Send_nsca not working in cron

Post by jdalrymple »

Very cool - consider uploading to the exchange!
Locked