mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-22 13:14:32 +02:00
Added more templates.
This commit is contained in:
parent
c7f9a78f4d
commit
16b0722cbc
@ -2,9 +2,13 @@
|
|||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
import socket
|
import socket
|
||||||
from xml.dom.minidom import parseString
|
from xml.dom.minidom import parse
|
||||||
|
|
||||||
service_templates = {
|
if len(sys.argv) < 2:
|
||||||
|
print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
tcp_service_templates = {
|
||||||
'ssh': 'ssh',
|
'ssh': 'ssh',
|
||||||
'http': 'http_ip',
|
'http': 'http_ip',
|
||||||
'https': 'https_ip',
|
'https': 'https_ip',
|
||||||
@ -12,34 +16,52 @@ service_templates = {
|
|||||||
'ssmtp': 'ssmtp'
|
'ssmtp': 'ssmtp'
|
||||||
}
|
}
|
||||||
|
|
||||||
# Expects XML output from 'nmap -oX'
|
udp_service_templates = {
|
||||||
dom = parseString(sys.stdin.read())
|
'ntp': 'ntp_time',
|
||||||
|
'snmp': 'snmp-uptime'
|
||||||
|
}
|
||||||
|
|
||||||
def processHost(host):
|
hosts = {}
|
||||||
for element in host.getElementsByTagName("status"):
|
|
||||||
if element.getAttribute("state") != "up":
|
|
||||||
return
|
|
||||||
|
|
||||||
for element in host.getElementsByTagName("address"):
|
def process_host(host_element):
|
||||||
if not element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
|
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
|
continue
|
||||||
|
|
||||||
address = element.getAttribute("addr")
|
address = address_element.getAttribute("addr")
|
||||||
break
|
break
|
||||||
|
|
||||||
hostname = address
|
name = address
|
||||||
|
|
||||||
for element in host.getElementsByTagName("hostname"):
|
for hostname_element in host_element.getElementsByTagName("hostname"):
|
||||||
hostname = element.getAttribute("name")
|
name = hostname_element.getAttribute("name")
|
||||||
|
|
||||||
print "object Host \"%s\" inherits \"itl-host\" {" % (hostname)
|
try:
|
||||||
print "\tmacros = {"
|
services = hosts[name]["services"]
|
||||||
print "\t\taddress = \"%s\"" % (address)
|
except:
|
||||||
print "\t},"
|
services = {}
|
||||||
|
|
||||||
for element in host.getElementsByTagName("port"):
|
for port_element in host_element.getElementsByTagName("port"):
|
||||||
port = int(element.getAttribute("portid"))
|
state = "closed"
|
||||||
protocol = element.getAttribute("protocol")
|
|
||||||
|
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:
|
try:
|
||||||
serv = socket.getservbyport(port, protocol)
|
serv = socket.getservbyport(port, protocol)
|
||||||
@ -47,23 +69,51 @@ def processHost(host):
|
|||||||
serv = str(port)
|
serv = str(port)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
template = service_templates[serv]
|
if protocol == "tcp":
|
||||||
|
template = tcp_service_templates[serv]
|
||||||
|
elif protocol == "udp":
|
||||||
|
template = udp_service_templates[serv]
|
||||||
|
else:
|
||||||
|
raise "Unknown protocol."
|
||||||
except:
|
except:
|
||||||
template = protocol
|
template = protocol
|
||||||
|
|
||||||
|
if template == "udp":
|
||||||
|
continue
|
||||||
|
|
||||||
|
services[serv] = { "template": template, "port": port }
|
||||||
|
|
||||||
|
hosts[name] = { "name": name, "address": address, "services": services }
|
||||||
|
|
||||||
|
def print_host(host):
|
||||||
|
print "object Host \"%s\" inherits \"discovered-host\" {" % (host["name"])
|
||||||
|
print "\tmacros = {"
|
||||||
|
print "\t\taddress = \"%s\"" % (host["address"])
|
||||||
|
print "\t},"
|
||||||
|
|
||||||
|
for serv, service in host["services"].iteritems():
|
||||||
print ""
|
print ""
|
||||||
print "\tservices[\"%s\"] = {" % (serv)
|
print "\tservices[\"%s\"] = {" % (serv)
|
||||||
print "\t\ttemplates = { \"%s\" }," % (template)
|
print "\t\ttemplates = { \"%s\" }," % (service["template"])
|
||||||
print ""
|
print ""
|
||||||
print "\t\tmacros = {"
|
print "\t\tmacros = {"
|
||||||
print "\t\t\tport = %s" % (port)
|
print "\t\t\tport = %s" % (service["port"])
|
||||||
print "\t\t}"
|
print "\t\t}"
|
||||||
print "\t},"
|
print "\t},"
|
||||||
|
|
||||||
print "}"
|
print "}"
|
||||||
print ""
|
print ""
|
||||||
|
|
||||||
print "#include <itl/itl.conf>"
|
|
||||||
|
|
||||||
for host in dom.getElementsByTagName("host"):
|
print "#include <itl/itl.conf>"
|
||||||
processHost(host)
|
print ""
|
||||||
|
|
||||||
|
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():
|
||||||
|
print_host(host)
|
||||||
|
@ -210,3 +210,22 @@ template Service "load" inherits "plugin-service" {
|
|||||||
cload15 = 4.0
|
cload15 = 4.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template Service "snmp" inherits "plugin-service" {
|
||||||
|
check_command = {
|
||||||
|
"$plugindir$/check_snmp",
|
||||||
|
"-H", "$address$",
|
||||||
|
"-o", "$oid$",
|
||||||
|
"-C", "$community$"
|
||||||
|
},
|
||||||
|
|
||||||
|
macros = {
|
||||||
|
community = "public"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template Service "snmp-uptime" inherits "snmp" {
|
||||||
|
macros += {
|
||||||
|
oid = "1.3.6.1.2.1.1.3.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -431,6 +431,9 @@ ScriptTask::Ptr DynamicObject::MakeMethodTask(const String& method,
|
|||||||
|
|
||||||
methods = m_Methods;
|
methods = m_Methods;
|
||||||
|
|
||||||
|
if (!methods)
|
||||||
|
return ScriptTask::Ptr();
|
||||||
|
|
||||||
String funcName = methods->Get(method);
|
String funcName = methods->Get(method);
|
||||||
|
|
||||||
if (funcName.IsEmpty())
|
if (funcName.IsEmpty())
|
||||||
|
@ -64,7 +64,7 @@ String MacroProcessor::InternalResolveMacros(const String& str, const Dictionary
|
|||||||
pos_second = result.FindFirstOf("$", pos_first + 1);
|
pos_second = result.FindFirstOf("$", pos_first + 1);
|
||||||
|
|
||||||
if (pos_second == String::NPos)
|
if (pos_second == String::NPos)
|
||||||
BOOST_THROW_EXCEPTION(runtime_error("Closing $ not found in macro format String."));
|
BOOST_THROW_EXCEPTION(runtime_error("Closing $ not found in macro format string."));
|
||||||
|
|
||||||
String name = result.SubStr(pos_first + 1, pos_second - pos_first - 1);
|
String name = result.SubStr(pos_first + 1, pos_second - pos_first - 1);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user