From 90d1118cb58012e318f664ef835a32630f736e2d Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 13 Apr 2014 11:10:17 +0200 Subject: [PATCH] Implement --batch parameter for icinga2-list-agents. Refs #6002 --- contrib/make-agent-config.py | 2 +- tools/icinga2-forget-agent.cmake | 2 +- tools/icinga2-list-agents.cmake | 20 +++++++++++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/contrib/make-agent-config.py b/contrib/make-agent-config.py index 8524b8a785d..c61a4e6e95f 100755 --- a/contrib/make-agent-config.py +++ b/contrib/make-agent-config.py @@ -30,7 +30,7 @@ import subprocess, json -inventory_json = subprocess.check_output(["icinga2-list-agents"]) +inventory_json = subprocess.check_output(["icinga2-list-agents", "--batch"]) inventory = json.loads(inventory_json) for host, hostinfo in inventory.items(): diff --git a/tools/icinga2-forget-agent.cmake b/tools/icinga2-forget-agent.cmake index 8cabe8832c4..42d2b74bdd9 100644 --- a/tools/icinga2-forget-agent.cmake +++ b/tools/icinga2-forget-agent.cmake @@ -31,7 +31,7 @@ cn = sys.argv[1] inventory_file = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/agent/inventory/" + hashlib.sha256(cn).hexdigest() if not os.path.isfile(inventory_file): - warning("There's no inventory file for agent '%s'.") + warning("There's no inventory file for agent '%s'." % (cn)) sys.exit(0) os.unlink(inventory_file) diff --git a/tools/icinga2-list-agents.cmake b/tools/icinga2-list-agents.cmake index 8c9c427531a..d1e5f1b98aa 100644 --- a/tools/icinga2-list-agents.cmake +++ b/tools/icinga2-list-agents.cmake @@ -16,12 +16,8 @@ # along with this program; if not, write to the Free Software Foundation # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. -from __future__ import print_function import sys, os, json -def warning(*objs): - print(*objs, file=sys.stderr) - inventory_dir = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/agent/inventory/" inventory = {} @@ -47,5 +43,19 @@ for root, dirs, files in os.walk(inventory_dir): except: pass -json.dump(inventory, sys.stdout) +if len(sys.argv) > 1 and sys.argv[1] == "--batch": + json.dump(inventory, sys.stdout) +else: + for host, host_info in inventory.items(): + if "peer" in host_info: + peer_info = host_info["peer"] + peer_addr = " (%s:%s)" % (peer_info["agent_host"], peer_info["agent_port"]) + else: + peer_addr = "" + + print "* %s%s" % (host, peer_addr) + + for service in host_info["services"]: + print " * %s" % (service) + sys.exit(0)