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