2 #/******************************************************************************
4 # * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
6 # * This program is free software; you can redistribute it and/or *
7 # * modify it under the terms of the GNU General Public License *
8 # * as published by the Free Software Foundation; either version 2 *
9 # * of the License, or (at your option) any later version. *
11 # * This program is distributed in the hope that it will be useful, *
12 # * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 # * GNU General Public License for more details. *
16 # * You should have received a copy of the GNU General Public License *
17 # * along with this program; if not, write to the Free Software Foundation *
18 # * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 # ******************************************************************************/
24 from xml.dom.minidom import parse
27 print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
30 tcp_service_commands = {
38 udp_service_commands = {
45 def process_host(host_element):
50 for status_element in host_element.getElementsByTagName("status"):
51 status = status_element.getAttribute("state")
56 for address_element in host_element.getElementsByTagName("address"):
57 if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
60 address = address_element.getAttribute("addr")
65 for hostname_element in host_element.getElementsByTagName("hostname"):
66 name = hostname_element.getAttribute("name")
69 services = hosts[name]["services"]
73 for port_element in host_element.getElementsByTagName("port"):
76 for state_element in port_element.getElementsByTagName("state"):
77 state = state_element.getAttribute("state")
82 port = int(port_element.getAttribute("portid"))
83 protocol = port_element.getAttribute("protocol")
86 serv = socket.getservbyport(port, protocol)
92 command = tcp_service_commands[serv]
93 elif protocol == "udp":
94 command = udp_service_commands[serv]
96 raise "Unknown protocol."
103 services[serv] = { "command": command, "port": port }
105 hosts[name] = { "name": name, "address": address, "services": services }
107 def print_host(host):
108 print "object Host \"%s\" {" % (host["name"])
109 print "\timport \"discovered-host\","
111 print "\taddress = \"%s\"," % (host["address"])
115 for serv, service in host["services"].iteritems():
116 print "object Service \"%s\" {" % (serv)
117 print "\timport \"discovered-service\","
119 print "\thost_name = \"%s\"" % (host["name"])
120 print "\tcheck_command = \"%s\"," % (service["command"])
122 print "\tvars.port = %s" % (service["port"])
126 for arg in sys.argv[1:]:
127 # Expects XML output from 'nmap -oX'
130 for host in dom.getElementsByTagName("host"):
133 for host in hosts.values():