Page 1 of 1

Check Peers script: wrong output nagios

Posted: Mon Aug 17, 2015 3:56 am
by m0le121
output in nagios is diffrent.
Nagios output is always:

Code: Select all

ok.
when i run the script manualy:

Code: Select all

Problems
vogels: 0/ 10
tijhuis: 0/ 3
sh: 0/ 19
below the script:

Code: Select all

#!/bin/bash
#
# Description: Check peers
#
melding=0;
COUNT_NOT_OK=$(asterisk -rx "sip show peers" | grep "Lagged\|UNREACHABLE\|UNKNOWN" | awk -F '/' '{print $1}' | sed 's/[^a-Z]*//g'| uniq | sort -n | \sort -rn);
arr=$(echo $COUNT_NOT_OK | tr " " "\n");

for x in $arr
do
COUNT_UN=$(asterisk -rx "sip show peers" | grep $x | grep "Lagged\|UNREACHABLE\|UNKNOWN" | awk '{print ""}' | uniq -c | sort -n);
COUNT_OK=$(asterisk -rx "sip show peers" | grep $x | grep "OK" | awk '{print ""}' | uniq -c | sort -n);

if [ "$COUNT_UN" = "" ]; then
        COUNT_UN=0;
fi

if [ "$COUNT_OK" = "" ]; then
        COUNT_OK=0;
fi

total=$((COUNT_UN+COUNT_OK));
#echo "totaal: $total"

procent=$(($total*50/100))

if [ $total == 1 ] && [ $COUNT_UN == 1 ] || [ $total == 2 ] && [ $COUNT_UN == 2 ] ; then
        melding=2;
elif [ $total -gt 2 ] && [ $COUNT_UN -gt $procent ]; then
        melding=2;
elif [ $total -gt 2 ] && [ $COUNT_UN -eq $total ]; then
        melding=2;
fi
done


if [ $melding = 0 ]; then
echo "ok.";
if [ "$arr" != "" ]; then
for y in $arr
do
COUNT_UN=$(asterisk -rx "sip show peers" | grep $y | grep "Lagged\|UNREACHABLE|\UNKNOWN" | awk '{print ""}' | uniq -c | sort -n);
COUNT_OK=$(asterisk -rx "sip show peers" | grep $y | grep "OK" | awk '{print ""}' | uniq -c | sort -n);

if [ "$COUNT_UN" = "" ]; then
        COUNT_UN=0;
fi

if [ "$COUNT_OK" = "" ]; then
        COUNT_OK=0;
fi

total=$((COUNT_UN+COUNT_OK));
echo $y: $COUNT_UN"/" $total;
done
fi
exit $melding;

elif [[ ${melding} == "2" ]]; then
echo "Problems";
if [ "$arr" != "" ]; then
for y in $arr
do
COUNT_UN=$(asterisk -rx "sip show peers" | grep $y | grep "Lagged\|UNREACHABLE|\UNKNOWN" | awk '{print ""}' | uniq -c | sort -n);
COUNT_OK=$(asterisk -rx "sip show peers" | grep $y | grep "OK" | awk '{print ""}' | uniq -c | sort -n);

if [ "$COUNT_UN" = "" ]; then
        COUNT_UN=0;
fi

if [ "$COUNT_OK" = "" ]; then
        COUNT_OK=0;
fi

total=$((COUNT_UN+COUNT_OK));
echo $y: $COUNT_UN"/" $total;
done
fi
exit $melding;
else
echo "Cant read output.";
exit 3;
fi

Re: Check Peers script: wrong output nagios

Posted: Mon Aug 17, 2015 3:58 pm
by tmcdonald
When you run the script manually, are you running it as root or some other non-nagios user? Make sure you test your plugins as nagios.

Also, this sort of thing is generally out of scope for the forum. We can try to answer specific questions, but debugging an unknown script would be heading for a consultation job.

Re: Check Peers script: wrong output nagios

Posted: Tue Aug 18, 2015 3:34 am
by m0le121
Root user:

Code: Select all

[root@... ~]
melding: 2
Problems
Nagios user:

Code: Select all

[nagios@... ~]
melding: 0
ok.
the problem is somewhere in melding.

Re: Check Peers script: wrong output nagios

Posted: Tue Aug 18, 2015 6:20 am
by m0le121
it is fixed now.
Thanks for your answer.

the most imporant:

Code: Select all

sudo /usr/sbin/asterisk -rx "sip show peers"
instead of

Code: Select all

asterisk -rx "sip show peers"
code (in dutch):

Code: Select all

#!/bin/bash
#
# Date: 18-08-2015
# Description: Check peers
#
melding=0;
users="";
COUNT_NOT_OK=$(sudo /usr/sbin/asterisk -rx "sip show peers" | grep "Lagged\|UNREACHABLE\|UNKNOWN" | awk -F '/' '{print $1}' | sed 's/[^a-Z]*//g'| uniq | sort -n | \sort -rn);
arr=$(echo $COUNT_NOT_OK | tr " " "\n");

for x in $arr
do
COUNT_UN=$(sudo /usr/sbin/asterisk -rx "sip show peers" | grep $x | grep "Lagged\|UNREACHABLE\|UNKNOWN" | awk '{print ""}' | uniq -c | sort -n);
COUNT_OK=$(sudo /usr/sbin/asterisk -rx "sip show peers" | grep $x | grep "OK" | awk '{print ""}' | uniq -c | sort -n);

total=$((COUNT_UN+COUNT_OK));
procent=$(($total*50/100));
if [ "$COUNT_UN" = "" ]; then
        COUNT_UN=0;
fi


if [ "$COUNT_OK" = "" ]; then
        COUNT_OK=0;
fi

if [ $total -lt 3 ] && [ $COUNT_UN == $total ] || [ $total -gt 2 ] && [ $COUNT_UN -gt $procent ] || [ $total -gt 2 ] && [ $COUNT_UN -eq $total ]; then
melding=2;
users=$users" ""$x($COUNT_UN"/" $total )";
fi

done

if [ $melding = 0 ]; then
echo "Toestellen zijn ok.";
exit $melding;

elif [[ ${melding} == "2" ]]; then
echo "Er zijn toestellen offline/niet geregistreerd: $users";
exit $melding;
else
echo "Kan de toestellen niet uitlezen.";
exit 3;
fi

Re: Check Peers script: wrong output nagios

Posted: Tue Aug 18, 2015 9:15 am
by hsmith
Good to hear that this is resolved!

Is there anything else we can help with or are we all right to close this thread?