3 #/******************************************************************************
5 # * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
7 # * This program is free software; you can redistribute it and/or *
8 # * modify it under the terms of the GNU General Public License *
9 # * as published by the Free Software Foundation; either version 2 *
10 # * of the License, or (at your option) any later version. *
12 # * This program is distributed in the hope that it will be useful, *
13 # * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 # * GNU General Public License for more details. *
17 # * You should have received a copy of the GNU General Public License *
18 # * along with this program; if not, write to the Free Software Foundation *
19 # * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
20 # ******************************************************************************/
25 from xml.dom.minidom import parse
28 print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
31 tcp_service_commands = {
39 udp_service_commands = {
46 def process_host(host_element):
51 for status_element in host_element.getElementsByTagName("status"):
52 status = status_element.getAttribute("state")
57 for address_element in host_element.getElementsByTagName("address"):
58 if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
61 address = address_element.getAttribute("addr")
66 for hostname_element in host_element.getElementsByTagName("hostname"):
67 name = hostname_element.getAttribute("name")
70 services = hosts[name]["services"]
74 for port_element in host_element.getElementsByTagName("port"):
77 for state_element in port_element.getElementsByTagName("state"):
78 state = state_element.getAttribute("state")
83 port = int(port_element.getAttribute("portid"))
84 protocol = port_element.getAttribute("protocol")
87 serv = socket.getservbyport(port, protocol)
93 command = tcp_service_commands[serv]
94 elif protocol == "udp":
95 command = udp_service_commands[serv]
97 raise "Unknown protocol."
104 services[serv] = { "command": command, "port": port }
106 hosts[name] = { "name": name, "address": address, "services": services }
108 def print_host(host):
109 print "object Host \"%s\" {" % (host["name"])
110 print "\timport \"discovered-host\","
112 print "\taddress = \"%s\"," % (host["address"])
116 for serv, service in host["services"].iteritems():
117 print "apply Service \"%s\" {" % (serv)
118 print "\timport \"discovered-service\","
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():