2 # Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+
7 from xml.dom.minidom import parse
10 print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
13 tcp_service_commands = {
21 udp_service_commands = {
28 def process_host(host_element):
33 for status_element in host_element.getElementsByTagName("status"):
34 status = status_element.getAttribute("state")
39 for address_element in host_element.getElementsByTagName("address"):
40 if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
43 address = address_element.getAttribute("addr")
48 for hostname_element in host_element.getElementsByTagName("hostname"):
49 name = hostname_element.getAttribute("name")
52 services = hosts[name]["services"]
56 for port_element in host_element.getElementsByTagName("port"):
59 for state_element in port_element.getElementsByTagName("state"):
60 state = state_element.getAttribute("state")
65 port = int(port_element.getAttribute("portid"))
66 protocol = port_element.getAttribute("protocol")
69 serv = socket.getservbyport(port, protocol)
75 command = tcp_service_commands[serv]
76 elif protocol == "udp":
77 command = udp_service_commands[serv]
79 raise "Unknown protocol."
86 services[serv] = { "command": command, "port": port }
88 hosts[name] = { "name": name, "address": address, "services": services }
91 print "object Host \"%s\" {" % (host["name"])
92 print "\timport \"discovered-host\","
94 print "\taddress = \"%s\"," % (host["address"])
98 for serv, service in host["services"].iteritems():
99 print "object Service \"%s\" {" % (serv)
100 print "\timport \"discovered-service\","
102 print "\thost_name = \"%s\"" % (host["name"])
103 print "\tcheck_command = \"%s\"," % (service["command"])
105 print "\tvars.port = %s" % (service["port"])
109 for arg in sys.argv[1:]:
110 # Expects XML output from 'nmap -oX'
113 for host in dom.getElementsByTagName("host"):
116 for host in hosts.values():