]> granicus.if.org Git - icinga2/blob - contrib/discover-api.py
Merge pull request #7353 from Icinga/bugfix/influxdbwriter-skip-tags-7341
[icinga2] / contrib / discover-api.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 import urlparse
8 import requests
9 import json
10 from xml.dom.minidom import parse
11
12 api_url = "https://localhost:5665/"
13 api_user = "root"
14 api_password = "root"
15
16 if len(sys.argv) < 2:
17     print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
18     sys.exit(1)
19
20 tcp_service_commands = {
21   'ssh': 'ssh',
22   'http': 'http',
23   'smtp': 'smtp',
24   'ssmtp': 'ssmtp'
25 }
26
27 udp_service_commands = {
28   'ntp': 'ntp_time',
29   'snmp': 'snmp-uptime'
30 }
31
32 hosts = {}
33
34 def process_host(host_element):
35     global hosts
36
37     status = "down"
38
39     for status_element in host_element.getElementsByTagName("status"):
40         status = status_element.getAttribute("state")
41
42     if status != "up":
43         return
44
45     for address_element in host_element.getElementsByTagName("address"):
46         if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
47             continue
48
49         address = address_element.getAttribute("addr")
50         break
51
52     name = address
53
54     for hostname_element in host_element.getElementsByTagName("hostname"):
55         name = hostname_element.getAttribute("name")
56
57     try:
58         services = hosts[name]["services"]
59     except:
60         services = {}
61
62     for port_element in host_element.getElementsByTagName("port"):
63         state = "closed"
64
65         for state_element in port_element.getElementsByTagName("state"):
66             state = state_element.getAttribute("state")
67
68         if state != "open":
69             continue
70
71         port = int(port_element.getAttribute("portid"))
72         protocol = port_element.getAttribute("protocol")
73
74         try:
75             serv = socket.getservbyport(port, protocol)
76         except:
77             serv = str(port)
78
79         try:
80             if protocol == "tcp":
81                 command = tcp_service_commands[serv]
82             elif protocol == "udp":
83                 command = udp_service_commands[serv]
84             else:
85                 raise "Unknown protocol."
86         except:
87             command = protocol
88
89         if command == "udp":
90             continue
91
92         services[serv] = { "command": command, "port": port }
93
94     hosts[name] = { "name": name, "address": address, "services": services }
95
96 def create_host(host):
97     global api_url, api_user, api_password
98
99     req = {
100       "templates": [ "discovered-host" ],
101       "attrs": {
102         "address": host["address"]
103       }
104     }
105
106     headers = {"Accept": "application/json"}
107     url = urlparse.urljoin(api_url, "v1/objects/hosts/%s" % (host["name"]))
108     requests.put(url, headers=headers, auth=(api_user, api_password), data=json.dumps(req), verify=False)
109
110     for serv, service in host["services"].iteritems():
111         req = {
112           "templates": [ "discovered-service" ],
113           "attrs": {
114             "vars.%s_port" % (service["command"]): service["port"],
115             "check_command": service["command"],
116           }
117         }
118     
119         headers = {"Accept": "application/json"}
120         url = urlparse.urljoin(api_url, "v1/objects/services/%s!%s" % (host["name"], serv))
121         requests.put(url, headers=headers, auth=(api_user, api_password), data=json.dumps(req), verify=False)
122
123 for arg in sys.argv[1:]:
124     # Expects XML output from 'nmap -oX'
125     dom = parse(arg)
126
127     for host in dom.getElementsByTagName("host"):
128         process_host(host)
129
130 for host in hosts.values():
131     create_host(host)