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