Skip to content

Commit

Permalink
Merge pull request NagiosEnterprises#45 from xavierba/py3
Browse files Browse the repository at this point in the history
Python3 support
  • Loading branch information
jomann09 authored Feb 11, 2021
2 parents 1c0b024 + 17d79ab commit 3c39a6b
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions clients/send_nrdp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env python3

##############################################################################
#############################################################################
#
#
# send_nrdp.py - Send host/service checkresults to NRDP with XML
Expand Down Expand Up @@ -45,14 +45,18 @@
#
#############################################################################


import argparse, sys, urllib, cgi
import argparse, sys
from html import escape
from future.standard_library import install_aliases
install_aliases()
from urllib import request as UrlRequest
from urllib import parse as UrlParse
from xml.dom.minidom import parseString

class send_nrdp:
def run(self):
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('-u', '--url', action="store",
dest="url", help="\
** REQUIRED ** The URL used to access the remote NRDP agent.")
Expand Down Expand Up @@ -106,19 +110,19 @@ def run(self):
printf \"<hostname>\\t<service>\\t<state>\\t<output>\\n\"\n")
parser.add_argument('-c', '--checktype', action="store",
dest="checktype", help="1 for passive 0 for active")

options = parser.parse_args()

if not options.url:
parser.error('You must specify a url.')
if not options.token:
parser.error('You must specify a token.')
try:
self.setup(options)
sys.exit()
except Exception, e:
except Exception as e:
sys.exit(e)

def getText(self, nodelist):
rc = []
for node in nodelist:
Expand All @@ -130,76 +134,74 @@ def post_data(self, url, token, xml):
# Make sure URL ends with a /
if not url.endswith('/'):
url += '/'

params = urllib.urlencode({'token': token.strip(), 'cmd': 'submitcheck', 'xml': xml});
opener = urllib.FancyURLopener()
params = UrlParse.urlencode({'token': token.strip(), 'cmd': 'submitcheck', 'XMLDATA': xml});
url_with_data = "{0}?{1}".format(url, params)
try:
f = opener.open(url, params)
result = parseString(f.read())
except Exception, e:
print "Cannot connect to url."
# TODO add directory option
sys.exit(e)
with UrlRequest.urlopen(url_with_data) as f:
result = parseString(f.read().decode('utf-8'))
except Exception as e:
print("ERROR - Cannot connect to url: {0}".format(str(e)))
return -3
if self.getText(result.getElementsByTagName("status")[0].childNodes) == "0":
sys.exit()
return 1
else:
print "ERROR - NRDP Returned: "+self.getText(result.getElementsByTagName("message")[0].childNodes)
sys.exit(1)
print("ERROR - NRDP Returned: " + self.getText(result.getElementsByTagName("message")[0].childNodes))
return -1

def setup(self, options):
if not options.delim:
options.delim = "\t"
if not options.checktype:
options.checktype = "1"

# If only url and token have been provided then it is assumed that data is being piped
if not options.hostname and not options.state and not options.file:
xml="<?xml version='1.0'?>\n<checkresults>\n";
for line in sys.stdin.readlines():
parts = line.split(options.delim)
if len(parts) == 4:
xml += "<checkresult type='service' checktype='"+options.checktype+"'>"
xml += "<hostname>"+cgi.escape(parts[0],True)+"</hostname>"
xml += "<servicename>"+cgi.escape(parts[1],True)+"</servicename>"
xml += "<hostname>"+html.escape(parts[0],True)+"</hostname>"
xml += "<servicename>"+html.escape(parts[1],True)+"</servicename>"
xml += "<state>"+parts[2]+"</state>"
xml += "<output>"+cgi.escape(parts[3],True)+"</output>"
xml += "<output>"+html.escape(parts[3],True)+"</output>"
xml += "</checkresult>"
if len(parts) == 3:
xml += "<checkresult type='host' checktype='"+options.checktype+"'>"
xml += "<hostname>"+cgi.escape(parts[0],True)+"</hostname>"
xml += "<hostname>"+html.escape(parts[0],True)+"</hostname>"
xml += "<state>"+parts[1]+"</state>"
xml += "<output>"+cgi.escape(parts[2],True)+"</output>"
xml += "<output>"+html.escape(parts[2],True)+"</output>"
xml += "</checkresult>"
xml += "</checkresults>"

elif options.hostname and options.state:
xml="<?xml version='1.0'?>\n<checkresults>\n";
if options.service:
xml += "<checkresult type='service' checktype='"+options.checktype+"'>"
xml += "<hostname>"+cgi.escape(options.hostname,True)+"</hostname>"
xml += "<servicename>"+cgi.escape(options.service,True)+"</servicename>"
xml += "<hostname>"+html.escape(options.hostname,True)+"</hostname>"
xml += "<servicename>"+html.escape(options.service,True)+"</servicename>"
xml += "<state>"+options.state+"</state>"
xml += "<output>"+cgi.escape(options.output,True)+"</output>"
xml += "<output>"+html.escape(options.output,True)+"</output>"
xml += "</checkresult>"
else:
xml += "<checkresult type='host' checktype='"+options.checktype+"'>"
xml += "<hostname>"+cgi.escape(options.hostname,True)+"</hostname>"
xml += "<hostname>"+html.escape(options.hostname,True)+"</hostname>"
xml += "<state>"+options.state+"</state>"
xml += "<output>"+cgi.escape(options.output,True)+"</output>"
xml += "<output>"+html.escape(options.output,True)+"</output>"
xml += "</checkresult>"
xml += "</checkresults>"

elif options.file:
try:
file_handle = open(options.file, 'r')
except Exception, e:
print "Error opening file '"+options.file+"'"
sys.exit(e)
except Exception as e:
print("ERROR - in opening file: " + options.file)
return -4
else:
xml = file_handle.read()
file_handle.close()

self.post_data(options.url, options.token, xml)
return self.post_data(options.url, options.token, xml)

if __name__ == "__main__":
send_nrdp().run()

0 comments on commit 3c39a6b

Please sign in to comment.