#!/usr/bin/env python import argparse import sys import urllib import html from xml.dom.minidom import parseString from urllib.request import FancyURLopener from urllib.parse import urlencode from urllib.request import * 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.") parser.add_argument('-t', '--token', action="store", dest="token", help="\ ** REQUIRED ** The authentication token used to access the\n\ remote NRDP agent.") parser.add_argument('-H', '--hostname', action="store", dest="hostname", help="\ The name of the host associated with the passive host/service\n\ check result.") parser.add_argument('-s', '--service', action="store", dest="service", help="\ For service checks, the name of the service associated with the\n\ passive check result.") parser.add_argument('-S', '--state', action="store", dest="state", help="\ An integer indicating the current state of the host or service.") parser.add_argument('-o', '--output', action="store", dest="output", help="\ Text output to be sent as the passive check result.\n\ Newlines should be encoded with encoded newlines (\\n).") parser.add_argument('-f', '--file', action="store", dest="file", help="\ This file will be sent to the NRDP server specified in -u\n\ The file should be an XML file in the following format:\n\ ##################################################\n\ \n\ \n\ \n\ YOUR_HOSTNAME\n\ 0\n\ OK|perfdata=1.00;5;10;0\n\ \n\ \n\ YOUR_HOSTNAME\n\ YOUR_SERVICENAME\n\ 0\n\ OK|perfdata=1.00;5;10;0\n\ \n\ \n\ ##################################################") parser.add_argument('-d', '--delim', action="store", dest="delim", help="\ With only the required parameters send_nrdp.py is capable of\n\ processing data piped to it either from a file or other process.\n\ By default, we use t (\\t) as the delimiter however this may be\n\ specified with the -d option data should be in the following\n\ formats of one entry per line:\n\ printf \"\\t\\t\\n\"\n\ printf \"\\t\\t\\t\\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 as e: sys.exit(e) def getText(self, nodelist): rc = [] for node in nodelist: if node.nodeType == node.TEXT_NODE: rc.append(node.data) return ''.join(rc) def post_data(self, url, token, xml): # Make sure URL ends with a / if not url.endswith('/'): url += '/' params = urlencode({'token': token.strip(), 'cmd': 'submitcheck', 'xml': xml}) params = params.encode() try: f = urllib.request.urlopen(url, params) result = parseString(f.read()) except Exception as e: print("Cannot connect to url.") # TODO add directory option sys.exit(e) if self.getText(result.getElementsByTagName("status")[0].childNodes) == "0": sys.exit() else: print("ERROR - NRDP Returned: "+self.getText(result.getElementsByTagName("message")[0].childNodes)) sys.exit(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 = "\n\n" for line in sys.stdin.readlines(): parts = line.split(options.delim) if len(parts) == 4: xml += "" xml += "" + \ html.escape(parts[0], True)+"" xml += "" + \ html.escape(parts[1], True)+"" xml += ""+parts[2]+"" xml += ""+html.escape(parts[3], True)+"" xml += "" if len(parts) == 3: xml += "" xml += "" + \ html.escape(parts[0], True)+"" xml += ""+parts[1]+"" xml += ""+html.escape(parts[2], True)+"" xml += "" xml += "" elif options.hostname and options.state: xml = "\n\n" if options.service: xml += "" xml += "" + \ html.escape(options.hostname, True)+"" xml += "" + \ html.escape(options.service, True)+"" xml += ""+options.state+"" xml += ""+html.escape(options.output, True)+"" xml += "" else: xml += "" xml += "" + \ html.escape(options.hostname, True)+"" xml += ""+options.state+"" xml += ""+html.escape(options.output, True)+"" xml += "" xml += "" elif options.file: try: file_handle = open(options.file, 'r') except Exception as e: print("Error opening file '"+options.file+"'") sys.exit(e) else: xml = file_handle.read() file_handle.close() self.post_data(options.url, options.token, xml) if __name__ == "__main__": send_nrdp().run()