2 # Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+
10 from xml.dom.minidom import parse
12 api_url = "https://localhost:5665/"
17 print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
20 tcp_service_commands = {
27 udp_service_commands = {
34 def process_host(host_element):
39 for status_element in host_element.getElementsByTagName("status"):
40 status = status_element.getAttribute("state")
45 for address_element in host_element.getElementsByTagName("address"):
46 if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
49 address = address_element.getAttribute("addr")
54 for hostname_element in host_element.getElementsByTagName("hostname"):
55 name = hostname_element.getAttribute("name")
58 services = hosts[name]["services"]
62 for port_element in host_element.getElementsByTagName("port"):
65 for state_element in port_element.getElementsByTagName("state"):
66 state = state_element.getAttribute("state")
71 port = int(port_element.getAttribute("portid"))
72 protocol = port_element.getAttribute("protocol")
75 serv = socket.getservbyport(port, protocol)
81 command = tcp_service_commands[serv]
82 elif protocol == "udp":
83 command = udp_service_commands[serv]
85 raise "Unknown protocol."
92 services[serv] = { "command": command, "port": port }
94 hosts[name] = { "name": name, "address": address, "services": services }
96 def create_host(host):
97 global api_url, api_user, api_password
100 "templates": [ "discovered-host" ],
102 "address": host["address"]
106 headers = {"Accept": "application/json"}
107 url = urlparse.urljoin(api_url, "v1/objects/hosts/%s" % (host["name"]))
108 requests.put(url, headers=headers, auth=(api_user, api_password), data=json.dumps(req), verify=False)
110 for serv, service in host["services"].iteritems():
112 "templates": [ "discovered-service" ],
114 "vars.%s_port" % (service["command"]): service["port"],
115 "check_command": service["command"],
119 headers = {"Accept": "application/json"}
120 url = urlparse.urljoin(api_url, "v1/objects/services/%s!%s" % (host["name"], serv))
121 requests.put(url, headers=headers, auth=(api_user, api_password), data=json.dumps(req), verify=False)
123 for arg in sys.argv[1:]:
124 # Expects XML output from 'nmap -oX'
127 for host in dom.getElementsByTagName("host"):
130 for host in hosts.values():