Problem with check_peer_status pluggin

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
jlccbcn
Posts: 2
Joined: Mon Apr 28, 2014 10:19 am

Problem with check_peer_status pluggin

Post by jlccbcn »

Hello,

I've been trying to install this pluggin on my nagios but i'm not able to make it works. I'm monitoring an asterisk server from my Nagios server and now i want to add this pluggin to monitorize a trunk SIP state. My goal is to set up an alert for this to be informed in case the SIP trunk is down. We have better deal with this provider so in case that the SIP trunk is down we can adjust our calling policy til the SIP trunk recovers and start using it again.

I think i finally install the pluggin but is not working as expected. I'm gonna write down all the process that i followed and if you can take a look and tell me where i'm wrong and how to fix it i'll grateful.

- First of all I add the pluggin .sh into my nagios pluggin folder.
- Then I went to commands.cfg and add the following command.

Code: Select all

           define command{
                                              command_name    check_peer_status
                                              command_line    /usr/lib64/nagios/plugins/check_peer_status.sh -H 192.168.253.60 -u root - s PASSWORD -t sip -p T-partner
          }
Where PASSWORD is my remote asterisk password, same as the ip, root user and Trunk SIP name to monitorize

- After that I went to my file newhost.cfg where I have defined my monitorized hosts and the services and add this.

Code: Select all

                  define service {
                                                use                             generic-service
                                              host_name                       asterisk_voip
                                              service_description             Custom SIP monitor
                                              check_command                   check_peer_status
        }
Asterisk_voip is the name that I defined for my asterisk, for other pluggin and services is working.
- I restart the service and everything seems to work fine, but when I go to the Nagios web interface I find this warning for the service.

Code: Select all

                                         Current Status:	  WARNING  
                                                    (for 0d 0h 31m 8s)
                                            Status Information:	(No output on stdout) stderr: Traceback (most recent call last):
                                            File "/usr/lib64/nagios/plugins/check_peer_status.sh", line 52, in ?
                                           login = """Action: login\r\nUsername: """ + options.user + """\r\nSecret: """ + options.secret + """\r\nEvents: off\r\n\r\n"""
                                           TypeError: cannot concatenate 'str' and 'NoneType' objects
I tried also changing on the newhost.cfg the check_command and instead of check_peer_status I put check_peer_status.sh, because I copied the pluggin on to this file. If I do this the nagios service is not restarting and is replying me with this error.

Code: Select all

[color=#40BF40]Nagios Core 4.0.5
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 04-11-2014
License: GPL

Website: http://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
Error: Service check command 'check_peer_status.sh' specified in service 'Custom SIP monitor' for host 'asterisk_voip' not defined anywhere!
        Checked 29 services.
        Checked 6 hosts.
        Checked 2 host groups.
        Checked 0 service groups.
        Checked 1 contacts.
        Checked 1 contact groups.
        Checked 26 commands.
        Checked 5 time periods.
        Checked 0 host escalations.
        Checked 0 service escalations.
Checking for circular paths...
        Checked 6 hosts
        Checked 0 service dependencies
        Checked 0 host dependencies
        Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   1

***> One or more problems was encountered while running the pre-flight check...

     Check your configuration file(s) to ensure that they contain valid
     directives and data defintions.  If you are upgrading from a previous
     version of Nagios, you should be aware that some variables/definitions
     may have been removed or modified in this version.  Make sure to read
     the HTML documentation regarding the config files, as well as the
     'Whats New' section to find out what has changed.[/color]
Honestly, I think that this last option is kind of stupid but I tried. As long as I know I shouldn’t add the .sh because the command name defined on commands.cfg is not with the .sh and the .sh is already on the command definition, but I had to try ;)


I get the pluggin on http://exchange.nagios.org/directory/Pl ... us/details and this is the script if you want to check it

Code: Select all

[color=#FF0000]#!/usr/bin/python

import os, sys, socket, string
from optparse import *

"""
Check_peer_status plugin for Nagios Copyright (c) 2013 Andrea Zorzetto
Version 0.2.2 updated at 21/11/2013

This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

"""

# Process the command line...
parser = OptionParser(usage="Check_peer_status [options]", version="%prog 0.1.0")
parser.set_defaults(hostname='127.0.0.1')
parser.set_defaults(port=5038)
parser.set_defaults(peer="")


parser.add_option("-u", "--username", action="store", dest="user",
        help="username for AMI.")
parser.add_option("-s", "--secret", action="store", dest="secret",
        help="password for AMI.")
parser.add_option("-H", "--host", action="store", dest="hostname",
        help="the host to connect to. The default is localhost.")
parser.add_option("-P", "--port", action="store", dest="port",
        help="the port to contact. Default is 5038.")

parser.add_option("-t", "--type", action="store", dest="type",
        help="sip or iax are allowed values.")
parser.add_option("-p", "--peer", action="store", dest="peer",
        help="the peer name to check.")
parser.add_option("-a", "--all", action="store_true", dest="all",
        help="print the whole output.")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
        help="print the whole output.")

(options, args) = parser.parse_args()

# Define the socket connection

mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
login = """Action: login\r\nUsername: """ + options.user + """\r\nSecret: """ + options.secret + """\r\nEvents: off\r\n\r\n"""

if (options.type=="sip"):
	command="sip show peer"
	commandall="sip show peers"
elif (options.type=="iax"):
	command="iax2 show peer"
	commandall="iax2 show peers"
else:
	print "Type peer error"+options.type
	sys.exit(1)

if (options.all):
	action = """Action: command\r\nCommand: """ + commandall + """\r\n\r\n"""
else:
	action = """Action: command\r\nCommand: """ + command + """ """+ options.peer +"""\r\n\r\n"""

logout = """Action: logoff\r\n\r\n"""



#global port
host = options.hostname
port = int(options.port)
user = options.user
password = options.secret

def connect(host, user, password):
	mysocket.connect((host, port))
	mysocket.send(login)
	
def disconnect(logout):
	send_command(logout)
	mysocket.send(logout)
	mysocket.close()
	

def send_command(action):
	mysocket.send(action)
	global myrcvd
	myrcvd = ""
	while 1:
		data = mysocket.recv(4096)    #The output bytes from the socket connection. You can adjust size to taste.
		myrcvd = myrcvd + data
		#print "$"+ data +"_"
		#print len(data)
		
		if (len(data)==0) or (string.find(data,'END COMMAND')>0):
			break
	return myrcvd

def get_peer_status(myrcvd):
	#Search peer status
	pos1= string.find(myrcvd,'Status')
	pos2= string.find(myrcvd[pos1:],'\n')

	#get peer status
	status=myrcvd[pos1:]
	status=status[:pos2]
	return status

# Perform the operation...
			
try:
	connect(host, user, password)
	result=send_command(action)
	disconnect(logout)
	
	#check auth
	if string.find(result,'accepted') != -1:
		
		if (options.all):
			print result

		elif (options.verbose):
			print result
			
		else:
			status=get_peer_status(result)
				
			if (string.find(status,'OK') >0):
				
				print status
			
			elif (string.find(status,'LAGGED') >0):
				print status
				sys.exit(1)
			
                        elif (string.find(status,'UNKNOWN') >0):
                                print status
                                sys.exit(3)

                        elif (string.find(status,'unmonitored') >0):
                                print status
                                sys.exit(1)


			else:
				print status
				sys.exit(2)

	else:
		print "Critical - Authentication failed"
		sys.exit(2)

	
	sys.exit(0)

except socket.error:
	print "Critical - Cannot contact Asterisk!"
	sys.exit(2)


[/color]
I wish you can give some light on this, thanks in advance for your time

Kind regards

Please use [ code ] [/code] wraps for all large text amounts
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: Problem with check_peer_status pluggin

Post by sreinhardt »

I feel I should point out, that despite the extension, this plugin is most definitely python not bash\shell scripting. Aside from that, I think your issue lies in the command definition.

Code: Select all

/usr/lib64/nagios/plugins/check_peer_status.sh -H 192.168.253.60 -u root - s PASSWORD -t sip -p T-partner
Specifically the space between - and s, the NoneType error you are getting, is because it cannot properly accept the PASSWORD as your secret password. Hopefully the change below will do the trick:

Code: Select all

/usr/lib64/nagios/plugins/check_peer_status.sh -H 192.168.253.60 -u root -s PASSWORD -t sip -p T-partner
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
jlccbcn
Posts: 2
Joined: Mon Apr 28, 2014 10:19 am

Re: Problem with check_peer_status pluggin

Post by jlccbcn »

Thanks sreinhardt.

I have almost fixed the problem. Despites of the extra space before the s there was a problem with the password and user, instead of the root user and password, the plugin was asking for the AMI user and password.

Now I have another problem, from the monitoring service i can execute the following command:

Code: Select all

 /usr/lib64/nagios/plugins/check_peer_status.sh -H 192.168.253.60 -u userAMI -s passAMI -t sip -p TPartner
It gives as a result an OK and the Ping for the service.

The only problem is on the Nagios Interface, when I'm going there the Service State Information is printing the following message

Code: Select all

Current Status:	  CRITICAL   (for 0d 13h 46m 48s)
Status Information:	(No output returned from plugin)
I included on the commands.cfg exactly the same command that I'm executing out of the interface so I don't why is working out of the interface but not inside

I would like to be able to check it form the interface to set up an alarm for this service in case that the trunk is down

Kind regards!
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: Problem with check_peer_status pluggin

Post by sreinhardt »

What output do you get from the plugin if run via command line? Also immediately after executing the plugin, run:

Code: Select all

echo $?
and send us both outputs please. While we are at it, what permissions does that file have?
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
Locked