Different result in web interface and command line

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.
hafthanhf
Posts: 10
Joined: Sun Jun 22, 2014 10:42 am
Location: Hanoi
Contact:

Different result in web interface and command line

Post 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 admin@xxx.com
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----
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: Different result in web interface and command line

Post by lmiltchev »

What happens when you run your check from the CLI as nagios user? Do you get "Critical" or "OK"?
Be sure to check out our Knowledgebase for helpful articles and solutions!
hafthanhf
Posts: 10
Joined: Sun Jun 22, 2014 10:42 am
Location: Hanoi
Contact:

Re: Different result in web interface and command line

Post by hafthanhf »

Thanks lmiltchev
In CLI, I get Critical result, but in Web, I get OK result
hafthanhf
Posts: 10
Joined: Sun Jun 22, 2014 10:42 am
Location: Hanoi
Contact:

Re: Different result in web interface and command line

Post by hafthanhf »

Can anyone help me plz?
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Different result in web interface and command line

Post 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 :).
hafthanhf
Posts: 10
Joined: Sun Jun 22, 2014 10:42 am
Location: Hanoi
Contact:

Re: Different result in web interface and command line

Post 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
                                
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Different result in web interface and command line

Post 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.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
hafthanhf
Posts: 10
Joined: Sun Jun 22, 2014 10:42 am
Location: Hanoi
Contact:

Re: Different result in web interface and command line

Post 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
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Different result in web interface and command line

Post 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.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
hafthanhf
Posts: 10
Joined: Sun Jun 22, 2014 10:42 am
Location: Hanoi
Contact:

Re: Different result in web interface and command line

Post 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 digicom_noc@119.18.185.2
--- 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 digicom_noc@119.18.185.2
--- 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?
Locked