mirror of https://github.com/Icinga/icinga2.git
Implemented auto-discovery script.
This commit is contained in:
parent
0f471683cd
commit
1c1e1ff5e2
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python
|
||||
import sys
|
||||
import subprocess
|
||||
import socket
|
||||
from xml.dom.minidom import parseString
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print "Syntax: %s <host>" % (sys.argv[0])
|
||||
sys.exit(1)
|
||||
|
||||
proc = subprocess.Popen(["nmap", "-oX", "-", sys.argv[1]], stdout=subprocess.PIPE)
|
||||
output = proc.communicate()
|
||||
|
||||
dom = parseString(output[0])
|
||||
|
||||
hostname = sys.argv[1]
|
||||
|
||||
def processHost(host):
|
||||
for element in host.getElementsByTagName("status"):
|
||||
if element.getAttribute("state") != "up":
|
||||
return
|
||||
|
||||
for element in host.getElementsByTagName("address"):
|
||||
if not element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
|
||||
continue
|
||||
|
||||
address = element.getAttribute("addr")
|
||||
break
|
||||
|
||||
hostname = address
|
||||
|
||||
for element in host.getElementsByTagName("hostname"):
|
||||
hostname = element.getAttribute("name")
|
||||
|
||||
print "object host \"%s\" {" % (hostname)
|
||||
print "\tmacros = {"
|
||||
print "\t\taddress = \"%s\"" % (address)
|
||||
print "\t},"
|
||||
print ""
|
||||
print "\tservices += { \"ping\" },"
|
||||
|
||||
for element in host.getElementsByTagName("port"):
|
||||
port = int(element.getAttribute("portid"))
|
||||
|
||||
try:
|
||||
serv = socket.getservbyport(port, "tcp")
|
||||
except:
|
||||
serv = str(port)
|
||||
|
||||
print ""
|
||||
print "\tservices[\"%s\"] = {" % (serv)
|
||||
print "\t\tservice = \"tcp\","
|
||||
print ""
|
||||
print "\t\tmacros = {"
|
||||
print "\t\t\tport = %s" % (port)
|
||||
print "\t\t}"
|
||||
print "\t},"
|
||||
|
||||
print "}"
|
||||
print ""
|
||||
|
||||
for host in dom.getElementsByTagName("host"):
|
||||
processHost(host)
|
Loading…
Reference in New Issue