Check Peers script: wrong output nagios

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
m0le121
Posts: 9
Joined: Fri Jun 05, 2015 6:50 am

Check Peers script: wrong output nagios

Post 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
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Check Peers script: wrong output nagios

Post 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.
Former Nagios employee
m0le121
Posts: 9
Joined: Fri Jun 05, 2015 6:50 am

Re: Check Peers script: wrong output nagios

Post 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.
m0le121
Posts: 9
Joined: Fri Jun 05, 2015 6:50 am

Re: Check Peers script: wrong output nagios

Post 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
User avatar
hsmith
Agent Smith
Posts: 3539
Joined: Thu Jul 30, 2015 11:09 am
Location: 127.0.0.1
Contact:

Re: Check Peers script: wrong output nagios

Post 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?
Former Nagios Employee.
me.
Locked