2012-07-08 11:37:31 +02:00
|
|
|
#!/usr/bin/env python
|
2019-02-25 15:09:36 +01:00
|
|
|
# Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+
|
2013-10-03 20:36:44 +02:00
|
|
|
|
2012-07-08 11:37:31 +02:00
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import socket
|
2013-03-08 16:02:33 +01:00
|
|
|
from xml.dom.minidom import parse
|
2012-07-08 11:37:31 +02:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-10-07 09:35:35 +02:00
|
|
|
tcp_service_commands = {
|
2013-03-08 14:41:19 +01:00
|
|
|
'ssh': 'ssh',
|
|
|
|
'http': 'http_ip',
|
|
|
|
'https': 'https_ip',
|
|
|
|
'smtp': 'smtp',
|
|
|
|
'ssmtp': 'ssmtp'
|
|
|
|
}
|
2012-07-08 11:37:31 +02:00
|
|
|
|
2013-10-07 09:35:35 +02:00
|
|
|
udp_service_commands = {
|
2013-03-08 16:02:33 +01:00
|
|
|
'ntp': 'ntp_time',
|
|
|
|
'snmp': 'snmp-uptime'
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts = {}
|
|
|
|
|
|
|
|
def process_host(host_element):
|
|
|
|
global hosts
|
|
|
|
|
|
|
|
status = "down"
|
|
|
|
|
|
|
|
for status_element in host_element.getElementsByTagName("status"):
|
|
|
|
status = status_element.getAttribute("state")
|
2012-07-08 11:37:31 +02:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
if status != "up":
|
|
|
|
return
|
2012-07-08 11:37:31 +02:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
for address_element in host_element.getElementsByTagName("address"):
|
|
|
|
if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
|
2012-07-08 11:37:31 +02:00
|
|
|
continue
|
2013-03-08 16:02:33 +01:00
|
|
|
|
|
|
|
address = address_element.getAttribute("addr")
|
2012-07-08 11:37:31 +02:00
|
|
|
break
|
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
name = address
|
2012-07-08 11:37:31 +02:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
for hostname_element in host_element.getElementsByTagName("hostname"):
|
|
|
|
name = hostname_element.getAttribute("name")
|
2012-07-08 11:37:31 +02:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
try:
|
|
|
|
services = hosts[name]["services"]
|
|
|
|
except:
|
|
|
|
services = {}
|
2012-07-08 11:37:31 +02:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
for port_element in host_element.getElementsByTagName("port"):
|
|
|
|
state = "closed"
|
|
|
|
|
|
|
|
for state_element in port_element.getElementsByTagName("state"):
|
|
|
|
state = state_element.getAttribute("state")
|
|
|
|
|
|
|
|
if state != "open":
|
|
|
|
continue
|
|
|
|
|
|
|
|
port = int(port_element.getAttribute("portid"))
|
|
|
|
protocol = port_element.getAttribute("protocol")
|
2012-07-08 11:37:31 +02:00
|
|
|
|
|
|
|
try:
|
2012-07-08 21:24:20 +02:00
|
|
|
serv = socket.getservbyport(port, protocol)
|
2012-07-08 11:37:31 +02:00
|
|
|
except:
|
|
|
|
serv = str(port)
|
|
|
|
|
2013-03-08 14:41:19 +01:00
|
|
|
try:
|
2013-03-08 16:02:33 +01:00
|
|
|
if protocol == "tcp":
|
2013-10-07 09:35:35 +02:00
|
|
|
command = tcp_service_commands[serv]
|
2013-03-08 16:02:33 +01:00
|
|
|
elif protocol == "udp":
|
2013-10-07 09:35:35 +02:00
|
|
|
command = udp_service_commands[serv]
|
2013-03-08 16:02:33 +01:00
|
|
|
else:
|
|
|
|
raise "Unknown protocol."
|
2013-03-08 14:41:19 +01:00
|
|
|
except:
|
2013-10-07 09:35:35 +02:00
|
|
|
command = protocol
|
2013-03-08 14:41:19 +01:00
|
|
|
|
2013-10-07 09:35:35 +02:00
|
|
|
if command == "udp":
|
2013-03-08 16:02:33 +01:00
|
|
|
continue
|
|
|
|
|
2013-10-07 09:35:35 +02:00
|
|
|
services[serv] = { "command": command, "port": port }
|
2013-03-08 16:02:33 +01:00
|
|
|
|
|
|
|
hosts[name] = { "name": name, "address": address, "services": services }
|
|
|
|
|
|
|
|
def print_host(host):
|
2014-03-28 23:14:37 +01:00
|
|
|
print "object Host \"%s\" {" % (host["name"])
|
|
|
|
print "\timport \"discovered-host\","
|
|
|
|
print ""
|
2014-04-08 17:20:10 +02:00
|
|
|
print "\taddress = \"%s\"," % (host["address"])
|
2014-03-28 23:14:37 +01:00
|
|
|
print "}"
|
|
|
|
print ""
|
2013-03-08 16:02:33 +01:00
|
|
|
|
|
|
|
for serv, service in host["services"].iteritems():
|
2015-11-09 12:16:02 +01:00
|
|
|
print "object Service \"%s\" {" % (serv)
|
2014-03-28 23:14:37 +01:00
|
|
|
print "\timport \"discovered-service\","
|
2012-07-08 11:37:31 +02:00
|
|
|
print ""
|
2015-11-09 12:16:02 +01:00
|
|
|
print "\thost_name = \"%s\"" % (host["name"])
|
2014-03-28 23:14:37 +01:00
|
|
|
print "\tcheck_command = \"%s\"," % (service["command"])
|
2012-07-08 11:37:31 +02:00
|
|
|
print ""
|
2014-04-05 13:55:29 +02:00
|
|
|
print "\tvars.port = %s" % (service["port"])
|
2014-03-28 23:14:37 +01:00
|
|
|
print "}"
|
2013-10-07 09:35:35 +02:00
|
|
|
print ""
|
2012-07-08 11:37:31 +02:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
for arg in sys.argv[1:]:
|
|
|
|
# Expects XML output from 'nmap -oX'
|
|
|
|
dom = parse(arg)
|
|
|
|
|
|
|
|
for host in dom.getElementsByTagName("host"):
|
|
|
|
process_host(host)
|
2013-03-08 14:41:19 +01:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
for host in hosts.values():
|
|
|
|
print_host(host)
|