]> granicus.if.org Git - icinga2/blob - contrib/discover.py
DB IDO: Fix upgrade script for 2.11.0 (drop index only if existing)
[icinga2] / contrib / discover.py
1 #!/usr/bin/env python
2 # Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+
3
4 import sys
5 import subprocess
6 import socket
7 from xml.dom.minidom import parse
8
9 if len(sys.argv) < 2:
10     print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
11     sys.exit(1)
12
13 tcp_service_commands = {
14   'ssh': 'ssh',
15   'http': 'http_ip',
16   'https': 'https_ip',
17   'smtp': 'smtp',
18   'ssmtp': 'ssmtp'
19 }
20
21 udp_service_commands = {
22   'ntp': 'ntp_time',
23   'snmp': 'snmp-uptime'
24 }
25
26 hosts = {}
27
28 def process_host(host_element):
29     global hosts
30
31     status = "down"
32
33     for status_element in host_element.getElementsByTagName("status"):
34         status = status_element.getAttribute("state")
35
36     if status != "up":
37         return
38
39     for address_element in host_element.getElementsByTagName("address"):
40         if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
41             continue
42
43         address = address_element.getAttribute("addr")
44         break
45
46     name = address
47
48     for hostname_element in host_element.getElementsByTagName("hostname"):
49         name = hostname_element.getAttribute("name")
50
51     try:
52         services = hosts[name]["services"]
53     except:
54         services = {}
55
56     for port_element in host_element.getElementsByTagName("port"):
57         state = "closed"
58
59         for state_element in port_element.getElementsByTagName("state"):
60             state = state_element.getAttribute("state")
61
62         if state != "open":
63             continue
64
65         port = int(port_element.getAttribute("portid"))
66         protocol = port_element.getAttribute("protocol")
67
68         try:
69             serv = socket.getservbyport(port, protocol)
70         except:
71             serv = str(port)
72
73         try:
74             if protocol == "tcp":
75                 command = tcp_service_commands[serv]
76             elif protocol == "udp":
77                 command = udp_service_commands[serv]
78             else:
79                 raise "Unknown protocol."
80         except:
81             command = protocol
82
83         if command == "udp":
84             continue
85
86         services[serv] = { "command": command, "port": port }
87
88     hosts[name] = { "name": name, "address": address, "services": services }
89
90 def print_host(host):
91     print "object Host \"%s\" {" % (host["name"])
92     print "\timport \"discovered-host\","
93     print ""
94     print "\taddress = \"%s\"," % (host["address"])
95     print "}"
96     print ""
97
98     for serv, service in host["services"].iteritems():
99         print "object Service \"%s\" {" % (serv)
100         print "\timport \"discovered-service\","
101         print ""
102         print "\thost_name = \"%s\"" % (host["name"])
103         print "\tcheck_command = \"%s\"," % (service["command"])
104         print ""
105         print "\tvars.port = %s" % (service["port"])
106         print "}"
107         print ""
108
109 for arg in sys.argv[1:]:
110     # Expects XML output from 'nmap -oX'
111     dom = parse(arg)
112
113     for host in dom.getElementsByTagName("host"):
114         process_host(host)
115
116 for host in hosts.values():
117     print_host(host)