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