Update scripts and example config.

Refs #6107
This commit is contained in:
Gunnar Beutner 2014-05-08 12:17:21 +02:00
parent ae8137d7c8
commit 656d6ff13a
6 changed files with 37 additions and 62 deletions

View File

@ -16,50 +16,35 @@
# along with this program; if not, write to the Free Software Foundation
# 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
inventory_json = subprocess.check_output(["icinga2-list-agents", "--batch"])
inventory = json.loads(inventory_json)
for agent, agent_info in inventory.items():
for host, host_info in agent_info["hosts"].items():
if host == "localhost":
host_name = agent
else:
host_name = host
print "object Host \"%s\" {" % (host_name)
print " import \"agent-host\""
print " vars.agent_identity = \"%s\"" % (agent)
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"])
print "object Endpoint \"%s\" {" % (agent)
print " host = \"%s\"" % (agent)
print "}"
print ""
print "object Zone \"%s\" {" % (agent_info["zone"])
print " parent = \"%s\"" % (agent_info["parent_zone"])
print " endpoints = [ \"%s\" ]" % (agent)
print "}"
print ""
print "zone \"%s\" {" % (agent_info["zone"])
for host, services in agent_info["repository"].items():
print "object Host \"%s\" {" % (host)
print " check_command = \"dummy\""
print "}"
print ""
for service in host_info["services"]:
for service in services:
print "object Service \"%s\" {" % (service)
print " import \"agent-service\""
print " host_name = \"%s\"" % (host_name)
print " check_command = \"dummy\""
print " host_name = \"%s\"" % (host)
print "}"
print ""
print "}"

View File

@ -10,4 +10,4 @@ const PluginDir = "/usr/lib/nagios/plugins"
const NodeName = "localhost"
/* Our local zone name. */
const ZoneName = "master"
const ZoneName = NodeName

View File

@ -295,6 +295,3 @@ object CheckCommand "cluster" {
import "cluster-check-command"
}
object CheckCommand "agent" {
import "agent-check-command"
}

View File

@ -31,10 +31,6 @@ template CheckCommand "plugin-check-command" {
methods.execute = "PluginCheck"
}
template CheckCommand "agent-check-command" {
methods.execute = "AgentCheck"
}
template CheckCommand "clr-check-command" {
methods.execute = "ClrCheck"
}

View File

@ -891,7 +891,7 @@ void ApiEvents::RepositoryTimerHandler(void)
Array::Ptr services = make_shared<Array>();
BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
services->Add(service->GetName());
services->Add(service->GetShortName());
}
repository->Set(host->GetName(), services);
@ -901,6 +901,7 @@ void ApiEvents::RepositoryTimerHandler(void)
Zone::Ptr my_zone = my_endpoint->GetZone();
Dictionary::Ptr params = make_shared<Dictionary>();
params->Set("seen", Utility::GetTime());
params->Set("endpoint", my_endpoint->GetName());
Zone::Ptr parent_zone = my_zone->GetParent();

View File

@ -19,57 +19,53 @@
import sys, os, json
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:
if len(file) != 64:
continue
fp = open(root + file, "r")
inventory_info = json.load(fp)
repository_info = json.load(fp)
fp.close()
if not "params" in inventory_info:
if not "endpoint" in repository_info:
continue
inventory[inventory_info["identity"]] = {}
inventory[inventory_info["identity"]]["seen"] = inventory_info["params"]["seen"]
inventory[inventory_info["identity"]]["hosts"] = {}
if not "seen" in repository_info:
repository_info["seen"] = 0
if not "hosts" in inventory_info["params"]:
continue
for host, host_info in inventory_info["params"]["hosts"].items():
inventory[inventory_info["identity"]]["hosts"][host] = { "services": host_info["services"].keys() }
repository[repository_info["endpoint"]] = repository_info
try:
fp = open(root + file + ".peer", "r")
peer_info = json.load(fp)
fp.close()
inventory[inventory_info["identity"]]["peer"] = peer_info
repository[repository_info["endpoint"]]["peer"] = peer_info
except:
pass
if len(sys.argv) > 1 and sys.argv[1] == "--batch":
json.dump(inventory, sys.stdout)
json.dump(repository, sys.stdout)
else:
for agent, agent_info in inventory.items():
for agent, agent_info in repository.items():
if "peer" in agent_info:
peer_info = agent_info["peer"]
peer_addr = "peer address: %s:%s" % (peer_info["agent_host"], peer_info["agent_port"])
else:
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)
for service in host_info["services"]:
for service in services:
print " * %s" % (service)
sys.exit(0)