From 656d6ff13a8f09f856407834fa3fd10f2172b371 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 8 May 2014 12:17:21 +0200 Subject: [PATCH] Update scripts and example config. Refs #6107 --- contrib/make-agent-config.py | 55 ++++++++++++--------------------- etc/icinga2/constants.conf | 2 +- itl/command-common.conf | 3 -- itl/command.conf | 4 --- lib/icinga/apievents.cpp | 3 +- tools/icinga2-list-agents.cmake | 34 +++++++++----------- 6 files changed, 38 insertions(+), 63 deletions(-) diff --git a/contrib/make-agent-config.py b/contrib/make-agent-config.py index e1156726c..a22d85ab6 100755 --- a/contrib/make-agent-config.py +++ b/contrib/make-agent-config.py @@ -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 "}" + diff --git a/etc/icinga2/constants.conf b/etc/icinga2/constants.conf index 7f9c9146f..b15b00250 100644 --- a/etc/icinga2/constants.conf +++ b/etc/icinga2/constants.conf @@ -10,4 +10,4 @@ const PluginDir = "/usr/lib/nagios/plugins" const NodeName = "localhost" /* Our local zone name. */ -const ZoneName = "master" +const ZoneName = NodeName diff --git a/itl/command-common.conf b/itl/command-common.conf index 6869014fc..d019d869c 100644 --- a/itl/command-common.conf +++ b/itl/command-common.conf @@ -295,6 +295,3 @@ object CheckCommand "cluster" { import "cluster-check-command" } -object CheckCommand "agent" { - import "agent-check-command" -} diff --git a/itl/command.conf b/itl/command.conf index c234b9881..a2b272b74 100644 --- a/itl/command.conf +++ b/itl/command.conf @@ -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" } diff --git a/lib/icinga/apievents.cpp b/lib/icinga/apievents.cpp index 3063193ab..f3c431f0a 100644 --- a/lib/icinga/apievents.cpp +++ b/lib/icinga/apievents.cpp @@ -891,7 +891,7 @@ void ApiEvents::RepositoryTimerHandler(void) Array::Ptr services = make_shared(); 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(); + params->Set("seen", Utility::GetTime()); params->Set("endpoint", my_endpoint->GetName()); Zone::Ptr parent_zone = my_zone->GetParent(); diff --git a/tools/icinga2-list-agents.cmake b/tools/icinga2-list-agents.cmake index 639779b76..4dbcdd309 100644 --- a/tools/icinga2-list-agents.cmake +++ b/tools/icinga2-list-agents.cmake @@ -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) + -- 2.40.0