mirror of https://github.com/Icinga/icinga2.git
parent
ae8137d7c8
commit
656d6ff13a
|
@ -16,50 +16,35 @@
|
||||||
# along with this program; if not, write to the Free Software Foundation
|
# along with this program; if not, write to the Free Software Foundation
|
||||||
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
|
||||||
# This script assumes that you have the following templates:
|
|
||||||
#
|
|
||||||
# template Host "agent-host" {
|
|
||||||
# check_command = "agent"
|
|
||||||
# vars.agent_host = "$host.name$"
|
|
||||||
# vars.agent_service = ""
|
|
||||||
# vars.agent_peer_host = "$address$"
|
|
||||||
# vars.agent_peer_port = 7000
|
|
||||||
# }
|
|
||||||
#
|
|
||||||
# template Service "agent-service" {
|
|
||||||
# check_command = "agent"
|
|
||||||
# vars.agent_service = "$service.name$"
|
|
||||||
#}
|
|
||||||
|
|
||||||
import subprocess, json
|
import subprocess, json
|
||||||
|
|
||||||
inventory_json = subprocess.check_output(["icinga2-list-agents", "--batch"])
|
inventory_json = subprocess.check_output(["icinga2-list-agents", "--batch"])
|
||||||
inventory = json.loads(inventory_json)
|
inventory = json.loads(inventory_json)
|
||||||
|
|
||||||
for agent, agent_info in inventory.items():
|
for agent, agent_info in inventory.items():
|
||||||
for host, host_info in agent_info["hosts"].items():
|
print "object Endpoint \"%s\" {" % (agent)
|
||||||
if host == "localhost":
|
print " host = \"%s\"" % (agent)
|
||||||
host_name = agent
|
print "}"
|
||||||
else:
|
print ""
|
||||||
host_name = host
|
print "object Zone \"%s\" {" % (agent_info["zone"])
|
||||||
|
print " parent = \"%s\"" % (agent_info["parent_zone"])
|
||||||
print "object Host \"%s\" {" % (host_name)
|
print " endpoints = [ \"%s\" ]" % (agent)
|
||||||
print " import \"agent-host\""
|
print "}"
|
||||||
print " vars.agent_identity = \"%s\"" % (agent)
|
print ""
|
||||||
|
print "zone \"%s\" {" % (agent_info["zone"])
|
||||||
if host != host_name:
|
|
||||||
print " vars.agent_host = \"%s\"" % (host)
|
|
||||||
|
|
||||||
if "peer" in agent_info:
|
|
||||||
print " vars.agent_peer_host = \"%s\"" % (agent_info["peer"]["agent_host"])
|
|
||||||
print " vars.agent_peer_port = \"%s\"" % (agent_info["peer"]["agent_port"])
|
|
||||||
|
|
||||||
|
for host, services in agent_info["repository"].items():
|
||||||
|
print "object Host \"%s\" {" % (host)
|
||||||
|
print " check_command = \"dummy\""
|
||||||
print "}"
|
print "}"
|
||||||
print ""
|
print ""
|
||||||
|
|
||||||
for service in host_info["services"]:
|
for service in services:
|
||||||
print "object Service \"%s\" {" % (service)
|
print "object Service \"%s\" {" % (service)
|
||||||
print " import \"agent-service\""
|
print " check_command = \"dummy\""
|
||||||
print " host_name = \"%s\"" % (host_name)
|
print " host_name = \"%s\"" % (host)
|
||||||
print "}"
|
print "}"
|
||||||
print ""
|
print ""
|
||||||
|
|
||||||
|
print "}"
|
||||||
|
|
||||||
|
|
|
@ -10,4 +10,4 @@ const PluginDir = "/usr/lib/nagios/plugins"
|
||||||
const NodeName = "localhost"
|
const NodeName = "localhost"
|
||||||
|
|
||||||
/* Our local zone name. */
|
/* Our local zone name. */
|
||||||
const ZoneName = "master"
|
const ZoneName = NodeName
|
||||||
|
|
|
@ -295,6 +295,3 @@ object CheckCommand "cluster" {
|
||||||
import "cluster-check-command"
|
import "cluster-check-command"
|
||||||
}
|
}
|
||||||
|
|
||||||
object CheckCommand "agent" {
|
|
||||||
import "agent-check-command"
|
|
||||||
}
|
|
||||||
|
|
|
@ -31,10 +31,6 @@ template CheckCommand "plugin-check-command" {
|
||||||
methods.execute = "PluginCheck"
|
methods.execute = "PluginCheck"
|
||||||
}
|
}
|
||||||
|
|
||||||
template CheckCommand "agent-check-command" {
|
|
||||||
methods.execute = "AgentCheck"
|
|
||||||
}
|
|
||||||
|
|
||||||
template CheckCommand "clr-check-command" {
|
template CheckCommand "clr-check-command" {
|
||||||
methods.execute = "ClrCheck"
|
methods.execute = "ClrCheck"
|
||||||
}
|
}
|
||||||
|
|
|
@ -891,7 +891,7 @@ void ApiEvents::RepositoryTimerHandler(void)
|
||||||
Array::Ptr services = make_shared<Array>();
|
Array::Ptr services = make_shared<Array>();
|
||||||
|
|
||||||
BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
|
BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
|
||||||
services->Add(service->GetName());
|
services->Add(service->GetShortName());
|
||||||
}
|
}
|
||||||
|
|
||||||
repository->Set(host->GetName(), services);
|
repository->Set(host->GetName(), services);
|
||||||
|
@ -901,6 +901,7 @@ void ApiEvents::RepositoryTimerHandler(void)
|
||||||
Zone::Ptr my_zone = my_endpoint->GetZone();
|
Zone::Ptr my_zone = my_endpoint->GetZone();
|
||||||
|
|
||||||
Dictionary::Ptr params = make_shared<Dictionary>();
|
Dictionary::Ptr params = make_shared<Dictionary>();
|
||||||
|
params->Set("seen", Utility::GetTime());
|
||||||
params->Set("endpoint", my_endpoint->GetName());
|
params->Set("endpoint", my_endpoint->GetName());
|
||||||
|
|
||||||
Zone::Ptr parent_zone = my_zone->GetParent();
|
Zone::Ptr parent_zone = my_zone->GetParent();
|
||||||
|
|
|
@ -19,57 +19,53 @@
|
||||||
import sys, os, json
|
import sys, os, json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
inventory_dir = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/agent/inventory/"
|
repository_dir = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/api/repository/"
|
||||||
|
|
||||||
inventory = {}
|
repository = {}
|
||||||
|
|
||||||
for root, dirs, files in os.walk(inventory_dir):
|
for root, dirs, files in os.walk(repository_dir):
|
||||||
for file in files:
|
for file in files:
|
||||||
if len(file) != 64:
|
if len(file) != 64:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
fp = open(root + file, "r")
|
fp = open(root + file, "r")
|
||||||
inventory_info = json.load(fp)
|
repository_info = json.load(fp)
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
if not "params" in inventory_info:
|
if not "endpoint" in repository_info:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
inventory[inventory_info["identity"]] = {}
|
if not "seen" in repository_info:
|
||||||
inventory[inventory_info["identity"]]["seen"] = inventory_info["params"]["seen"]
|
repository_info["seen"] = 0
|
||||||
inventory[inventory_info["identity"]]["hosts"] = {}
|
|
||||||
|
|
||||||
if not "hosts" in inventory_info["params"]:
|
repository[repository_info["endpoint"]] = repository_info
|
||||||
continue
|
|
||||||
|
|
||||||
for host, host_info in inventory_info["params"]["hosts"].items():
|
|
||||||
inventory[inventory_info["identity"]]["hosts"][host] = { "services": host_info["services"].keys() }
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fp = open(root + file + ".peer", "r")
|
fp = open(root + file + ".peer", "r")
|
||||||
peer_info = json.load(fp)
|
peer_info = json.load(fp)
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
inventory[inventory_info["identity"]]["peer"] = peer_info
|
repository[repository_info["endpoint"]]["peer"] = peer_info
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if len(sys.argv) > 1 and sys.argv[1] == "--batch":
|
if len(sys.argv) > 1 and sys.argv[1] == "--batch":
|
||||||
json.dump(inventory, sys.stdout)
|
json.dump(repository, sys.stdout)
|
||||||
else:
|
else:
|
||||||
for agent, agent_info in inventory.items():
|
for agent, agent_info in repository.items():
|
||||||
if "peer" in agent_info:
|
if "peer" in agent_info:
|
||||||
peer_info = agent_info["peer"]
|
peer_info = agent_info["peer"]
|
||||||
peer_addr = "peer address: %s:%s" % (peer_info["agent_host"], peer_info["agent_port"])
|
peer_addr = "peer address: %s:%s" % (peer_info["agent_host"], peer_info["agent_port"])
|
||||||
else:
|
else:
|
||||||
peer_addr = "no peer address"
|
peer_addr = "no peer address"
|
||||||
|
|
||||||
print "* %s (%s, last seen: %s)" % (agent, peer_addr, datetime.fromtimestamp(agent_info["seen"]))
|
print "* %s (zone: %s, parent zone: %s, %s, last seen: %s)" % (agent, agent_info["zone"], agent_info["parent_zone"], peer_addr, datetime.fromtimestamp(agent_info["seen"]))
|
||||||
|
|
||||||
for host, host_info in agent_info["hosts"].items():
|
for host, services in agent_info["repository"].items():
|
||||||
print " * %s" % (host)
|
print " * %s" % (host)
|
||||||
|
|
||||||
for service in host_info["services"]:
|
for service in services:
|
||||||
print " * %s" % (service)
|
print " * %s" % (service)
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue