Page 1 of 1

Send_nsca not working in cron

Posted: Thu May 14, 2015 5:28 am
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.

Re: Send_nsca not working in cron

Posted: Thu May 14, 2015 9:32 am
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?

Re: Send_nsca not working in cron

Posted: Thu May 14, 2015 11:01 am
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.

Re: Send_nsca not working in cron

Posted: Thu May 14, 2015 12:09 pm
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.

Re: Send_nsca not working in cron

Posted: Fri May 15, 2015 7:06 am
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

Re: Send_nsca not working in cron

Posted: Fri May 15, 2015 9:26 am
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?

Re: Send_nsca not working in cron

Posted: Fri May 15, 2015 11:33 am
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.

Re: Send_nsca not working in cron

Posted: Fri May 15, 2015 11:36 am
by jdalrymple
Very cool - consider uploading to the exchange!