2014-04-13 09:22:27 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Icinga 2
|
|
|
|
# Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation
|
|
|
|
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
import sys, os, json
|
2014-04-15 13:11:14 +02:00
|
|
|
from datetime import datetime
|
2014-04-13 09:22:27 +02:00
|
|
|
|
|
|
|
inventory_dir = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/agent/inventory/"
|
|
|
|
|
|
|
|
inventory = {}
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(inventory_dir):
|
|
|
|
for file in files:
|
2014-04-13 09:45:14 +02:00
|
|
|
if len(file) != 64:
|
|
|
|
continue
|
|
|
|
|
2014-04-13 09:22:27 +02:00
|
|
|
fp = open(root + file, "r")
|
|
|
|
inventory_info = json.load(fp)
|
2014-04-13 09:45:14 +02:00
|
|
|
fp.close()
|
|
|
|
|
2014-04-15 14:33:19 +02:00
|
|
|
if not "params" in inventory_info:
|
|
|
|
continue
|
|
|
|
|
2014-04-13 09:45:14 +02:00
|
|
|
inventory[inventory_info["identity"]] = {}
|
2014-04-15 13:11:14 +02:00
|
|
|
inventory[inventory_info["identity"]]["seen"] = inventory_info["params"]["seen"]
|
|
|
|
inventory[inventory_info["identity"]]["hosts"] = {}
|
|
|
|
|
2014-04-15 15:03:30 +02:00
|
|
|
if not "hosts" in inventory_info["params"]:
|
2014-04-15 14:33:19 +02:00
|
|
|
continue
|
|
|
|
|
2014-04-15 13:11:14 +02:00
|
|
|
for host, host_info in inventory_info["params"]["hosts"].items():
|
|
|
|
inventory[inventory_info["identity"]]["hosts"][host] = { "services": host_info["services"].keys() }
|
2014-04-13 09:45:14 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
fp = open(root + file + ".peer", "r")
|
|
|
|
peer_info = json.load(fp)
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
inventory[inventory_info["identity"]]["peer"] = peer_info
|
|
|
|
except:
|
|
|
|
pass
|
2014-04-13 09:22:27 +02:00
|
|
|
|
2014-04-13 11:10:17 +02:00
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == "--batch":
|
|
|
|
json.dump(inventory, sys.stdout)
|
|
|
|
else:
|
2014-04-15 13:11:14 +02:00
|
|
|
for agent, agent_info in inventory.items():
|
|
|
|
if "peer" in agent_info:
|
|
|
|
peer_info = agent_info["peer"]
|
2014-04-15 14:33:19 +02:00
|
|
|
peer_addr = "peer address: %s:%s" % (peer_info["agent_host"], peer_info["agent_port"])
|
2014-04-13 11:10:17 +02:00
|
|
|
else:
|
2014-04-15 13:11:14 +02:00
|
|
|
peer_addr = "no peer address"
|
|
|
|
|
2014-04-15 14:33:19 +02:00
|
|
|
print "* %s (%s, last seen: %s)" % (agent, peer_addr, datetime.fromtimestamp(agent_info["seen"]))
|
2014-04-13 11:10:17 +02:00
|
|
|
|
2014-04-15 13:11:14 +02:00
|
|
|
for host, host_info in agent_info["hosts"].items():
|
|
|
|
print " * %s" % (host)
|
2014-04-13 11:10:17 +02:00
|
|
|
|
2014-04-15 13:11:14 +02:00
|
|
|
for service in host_info["services"]:
|
|
|
|
print " * %s" % (service)
|
2014-04-13 11:10:17 +02:00
|
|
|
|
2014-04-13 09:22:27 +02:00
|
|
|
sys.exit(0)
|