Page 1 of 2

Different result in web interface and command line

Posted: Sun Jun 22, 2014 11:02 am
by hafthanhf
Hi all,
I've write a plugin to check the bgp session, but it might be get in troubles. The output when running check command directly from command line is expectable (service status is CRITICAL), but the result from web interface is not ( service status is OK). Pls help me solve this, I've tried for a few days but get nothing?

Here is the output when I run the check command:
  • [root@monitor-network plugins]# check_bgp_devel_expect -H 10.255.0.221 -d PE
    PE########
    '1.1.1.1' #### BGP infomation ####
    OOPs :-( Something in the way....
    This infomation is on PE router
    group_BGP peer_BGP name_BGP Email
    Internal 1.1.1.1 test_BGP [email protected]
And the following is the result receive from web Interface:
  • Current Status:
    OK
    (for 0d 6h 15m 34s)
    Status Information: PE########
    #### BGP infomation ####
    OK :--------)
    0
    \n----hafthanhf----

Re: Different result in web interface and command line

Posted: Mon Jun 23, 2014 3:28 pm
by lmiltchev
What happens when you run your check from the CLI as nagios user? Do you get "Critical" or "OK"?

Re: Different result in web interface and command line

Posted: Tue Sep 09, 2014 11:15 pm
by hafthanhf
Thanks lmiltchev
In CLI, I get Critical result, but in Web, I get OK result

Re: Different result in web interface and command line

Posted: Wed Sep 10, 2014 4:00 am
by hafthanhf
Can anyone help me plz?

Re: Different result in web interface and command line

Posted: Wed Sep 10, 2014 4:38 pm
by slansing
Please attach your plugin, you also need to execute it like this:

Code: Select all

./pluginname
If you are currently residing in it's directory. Also, the reason nobody had replied yet is you responded will outside of our business hours, 9:00AM-5:00PM CDT, just for future reference :).

Re: Different result in web interface and command line

Posted: Wed Sep 10, 2014 11:50 pm
by hafthanhf
slansing wrote:Please attach your plugin, you also need to execute it like this:

Code: Select all

./pluginname
If you are currently residing in it's directory. Also, the reason nobody had replied yet is you responded will outside of our business hours, 9:00AM-5:00PM CDT, just for future reference :).
Thanks slansing. Herre is my plugin. I think the reason is when nagios executes this script on web interfaces, it remove the pf2 variable's content, the pf2 variable is used to make the notification (Critical or OK) so the result always is ok.

Code: Select all

#!/bin/bash
OK=0
CRITICAL=2
STATE=0
PLUGIN_PATH="/usr/local/nagios/libexec/plugins"
outputfile=/var/log/bgp.log
OID_sysDescr=SNMPv2-MIB::sysDescr.0
COMMUNITY=******
DBname=********
DBtable=*******
VERSION="1.0"
print_version() {
  echo "$SCRIPTNAME" version "$VERSION"
  echo "This nagios plugins comes with ABSOLUTELY NO WARRANTY."
  echo "You may redistribute copies of the plugins under the terms of the GNU General Public License v2."
}

print_help() {
  print_version
  echo ""
  print_usage
  echo ""
  echo "**** Check BGP connection on a router  ****"
  echo ""
  echo -e "-H ADDRESS      - Hostname to query (default: 127.0.0.1)\n -l logical-system for Junos router \n -V Print version"
  echo  "-h Print this help"
  echo "Befor using this plugin, you can create a mysql database that have a 4 field table:  id | group_BGP | name_BGP | peer_BGP"
}
# Arguments
while getopts H:l:v:hV OPT
do
  case "$OPT" in
    H) HOSTNAME="$OPTARG" ;;
    l) LS="$OPTARG";;
    h) print_help
       exit $STATE ;;
    V) print_version
       exit $STATE ;;
   esac
done
############################
######---Main----###########
############################

#Get system infomation
sysinfo=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME $OID_sysDescr | awk 'NR==1 {print $4}'`
#################  INFO   ############

if [ "$LS" == "physical" ]; then
# Get BGP status
      [color=#FF0040]  pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep "Active" | awk '{print  "\47" $1 "\47, "}') | sed 's/,*$//'`[/color]

echo "test1 $pf2 ???";
echo -e "log file : \n"
cat /tmp/bgp_$HOSTNAME_$LS.log
else
[color=#FF0000]        pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_ls_$sysinfo $HOSTNAME $LS | grep "Active"| awk '{print  "\47" $1 "\47, "}' )  | sed 's/,*$//'`
[/color]       
fi

if [ "$pf2" != "" ]; then
        STATE=2
        #echo "This alert is not test anymore, Pls check the following connections..."
        error=`mysql -u hafthanhf -e "select group_BGP,peer_BGP,name_BGP,Email from $DBname.$DBtable where peer_BGP in ($pf2)";`
        exit 2;
else
        echo "OK----)"
#       echo $fullinfo
        STATE=0
        exit 0;
fi

        cat /tmp/bgp_$HOSTNAME_$LS.log
        echo -e "\n----hafthanhf----"
        exit $STATE
                                

Re: Different result in web interface and command line

Posted: Thu Sep 11, 2014 5:29 pm
by abrist
Well, lets work backwards through this script. As there is only one set of logic that exits "OK", lets follow that back through any tests/conditional logic and write those values to a file. See the following edits:
############################
######---Main----###########
############################

#Get system infomation
sysinfo=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME $OID_sysDescr | awk 'NR==1 {print $4}'`
################# INFO ############

if [ "$LS" == "physical" ]; then
# Get BGP status
pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep "Active" | awk '{print "\47" $1 "\47, "}') | sed 's/,*$//'`

echo "test1 $pf2 ???";
echo -e "log file : \n"
cat /tmp/bgp_$HOSTNAME_$LS.log
else
pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_ls_$sysinfo $HOSTNAME $LS | grep "Active"| awk '{print "\47" $1 "\47, "}' ) | sed 's/,*$//'`

fi
echo $pf2
if [ "$pf2" != "" ]; then
STATE=2
#echo "This alert is not test anymore, Pls check the following connections..."
error=`mysql -u hafthanhf -e "select group_BGP,peer_BGP,name_BGP,Email from $DBname.$DBtable where peer_BGP in ($pf2)";`
exit 2;
else
echo "OK----)"
# echo $fullinfo
STATE=0
exit 0;
fi

cat /tmp/bgp_$HOSTNAME_$LS.log
echo -e "\n----hafthanhf----"
exit $STATE
Also, you start the script with STATE=0. This means that the script could fail earlier, and as long as it does not end the script, it would exit with OK. You may want to start the script with STATE=3 or something, to see if the value is ever replaced. Also, try running the following as the nagios user from the cli:

Code: Select all

su - nagios -c "echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_ls_$sysinfo $HOSTNAME $LS | grep Active| awk '{print  "\47" $1 "\47, "}' )  | sed 's/,*$//'"
Make sure to replace any macros with the desired values.

Re: Different result in web interface and command line

Posted: Tue Sep 16, 2014 3:17 am
by hafthanhf
Thank abrist,
When I run as root:

Code: Select all

[root@monitor-network ~]#  su - nagios -c "echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_ls_Juniper 119.18.185.2 | grep Active | awk '{print  "\47" $1 "\47, "}' )  | sed 's/,*$//'"
202.40.160.151,
-bash: line 1: 202.40.160.152,: command not found
-bash: line 2: 202.40.160.153,: command not found
-bash: line 3: 202.40.160.154,: command not found
-bash: line 4: 202.40.160.160,: command not found
-bash: line 5: 202.40.161.45,: command not found
-bash: line 6: 202.40.161.59,: command not found
-bash: line 7: 2001:7fa:0:1::ca28:a13b,: command not found
-bash: line 8: 2001:7fa:0:1::ca28:a1dc,: command not found
And when I run as nagios usser:

Code: Select all

[nagios@monitor-network libexec]$ echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_ls_Juniper 119.18.185.2 | grep Active | awk '{print  "\47" $1 "\47, "}' )  | sed 's/,*$//'
'202.40.160.151', '202.40.160.152', '202.40.160.153', '202.40.160.154', '202.40.160.160', '202.40.161.45', '202.40.161.59', '2001:7fa:0:1::ca28:a13b', '2001:7fa:0:1::ca28:a1dc'
btw, here is the code of expect plugin:

Code: Select all

#!/usr/bin/expect
#set user noc
#set pass "password_noc"
set hostname [lindex $argv 0]
#set config
#set timeout 15
spawn ssh -i /home/nagios/.ssh/monitor_rsa digicom_noc@$hostname
# -------------- do not edit below -------------- #
#get data from remote devices
expect ">"
send "show bgp summary | find AS | except inet | except Peer | no-more\n"
expect ">"
send "exit\n"
exit 0

Re: Different result in web interface and command line

Posted: Tue Sep 16, 2014 5:15 pm
by abrist
Well, the first errors were my mistake as I didn't have you escape the double quotes in the awk command. AT this point, you will have to add some echos to your script to check where the plugin is failing from the web ui.

Re: Different result in web interface and command line

Posted: Wed Sep 17, 2014 4:36 am
by hafthanhf
I think the problem comes from this line:

Code: Select all

export pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep "Active" | awk '{print  "\47" $1 "\47, "}') | sed 's/,*$//'`
I've tried this:

Code: Select all

if [ "$LS" == "physical" ]; then
echo "test123"
# Get BGP status
[color=#FF0000]/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep "Active" | awk '{print  "\47" $1 "\47, "}' | sed 's/,*$//'[/color]
[color=#FF0000]export pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep "Active" | awk '{print  "\47" $1 "\47, "}') | sed 's/,*$//'`[/color]
pf=`/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep "Active" | awk '{print  "\47" $1 "\47, "}' | sed 's/,*$//'`
export test=$pf
echo "$pf " > /tmp/bgp.$HOSTNAME.$LS.log
        #pf2="'202.40.160.151' , '202.40.160.152' , '2001:7fa:0:1::ca28:a13b', '2001:7fa:0:1::ca28:a195'";
#       pf2=`cat /tmp/bgp_$HOSTNAME'_'$LS.log`
#       pf3=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME $LS | grep Active | awk   '{print  "\47" $1 "\47, "}')`
#       pfmy=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep Active | awk   '{print  "\47" $1 "\47, "}') | sed 's/,*$//'`

echo "test1 $pf2 ???";
#cat /tmp/bgp.$HOSTNAME.$LS.log
else
        pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_ls_$sysinfo $HOSTNAME $LS | grep "Active"| awk '{print  "\47" $1 "\47, "}' )  | sed 's/,*$//'`
sudo echo "$pf2 " > /tmp/bgp.$HOSTNAME.$LS.log
#       pfmy=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_ls_$sysinfo $HOSTNAME $LS | grep 'Active' | awk '{print  "\47" $1 "\47, "}') | sed 's/,*$//'`
#echo "456...."
fi
....
The red line is added for debugging, and it give no result. It looks like a permission relative or something wrong in my expect script.
Here is the different ways I ran the script:
The first: gave me unexpected result ( wrong result)

Code: Select all

[root@monitor-network libexec]# su - nagios -c "/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_Juniper 119.18.185.2"
spawn ssh -i /home/nagios/.ssh/monitor_rsa [email protected]
--- JUNOS 11.4R7.5 built 2013-03-01 11:57:40 UTC
{master}
digicom_noc@IGW-HK> ... AS | except inet | except Peer |[root@monitor-network libexec]#
The second:, run the script with nagios account, this gave me the expected result

Code: Select all

[nagios@monitor-network libexec]$ /usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_Juniper 119.18.185.2
spawn ssh -i /home/nagios/.ssh/monitor_rsa [email protected]
--- JUNOS 11.4R7.5 built 2013-03-01 11:57:40 UTC
{master}
digicom_noc@IGW-HK> show bgp summary | find AS | except inet | except Peer | no-more
72.14.223.30          15169     500528     511527       0       2     23w6d5h 184/303/303/0        0/0/0/0
119.18.185.1          38726      22340    3147989       0      12      1w0d0h 184/277/277/0        0/0/0/0
119.18.185.61         38726     195139     235335       0       2     6w4d10h 94/94/94/0           0/0/0/0
and the third, run the script with root account, the same result with the second way.

Code: Select all

[root@monitor-network libexec]# /usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_Juniper 119.18.185.2
So, do you have any suggestions for this?