3 # Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation
17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 from xml.dom.minidom import parse
27 api_url = "https://localhost:5665/"
32 print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
35 tcp_service_commands = {
42 udp_service_commands = {
49 def process_host(host_element):
54 for status_element in host_element.getElementsByTagName("status"):
55 status = status_element.getAttribute("state")
60 for address_element in host_element.getElementsByTagName("address"):
61 if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
64 address = address_element.getAttribute("addr")
69 for hostname_element in host_element.getElementsByTagName("hostname"):
70 name = hostname_element.getAttribute("name")
73 services = hosts[name]["services"]
77 for port_element in host_element.getElementsByTagName("port"):
80 for state_element in port_element.getElementsByTagName("state"):
81 state = state_element.getAttribute("state")
86 port = int(port_element.getAttribute("portid"))
87 protocol = port_element.getAttribute("protocol")
90 serv = socket.getservbyport(port, protocol)
96 command = tcp_service_commands[serv]
97 elif protocol == "udp":
98 command = udp_service_commands[serv]
100 raise "Unknown protocol."
107 services[serv] = { "command": command, "port": port }
109 hosts[name] = { "name": name, "address": address, "services": services }
111 def create_host(host):
112 global api_url, api_user, api_password
115 "templates": [ "discovered-host" ],
117 "address": host["address"]
121 headers = {"Accept": "application/json"}
122 url = urlparse.urljoin(api_url, "v1/objects/hosts/%s" % (host["name"]))
123 requests.put(url, headers=headers, auth=(api_user, api_password), data=json.dumps(req), verify=False)
125 for serv, service in host["services"].iteritems():
127 "templates": [ "discovered-service" ],
129 "vars.%s_port" % (service["command"]): service["port"],
130 "check_command": service["command"],
134 headers = {"Accept": "application/json"}
135 url = urlparse.urljoin(api_url, "v1/objects/services/%s!%s" % (host["name"], serv))
136 requests.put(url, headers=headers, auth=(api_user, api_password), data=json.dumps(req), verify=False)
138 for arg in sys.argv[1:]:
139 # Expects XML output from 'nmap -oX'
142 for host in dom.getElementsByTagName("host"):
145 for host in hosts.values():