3 # Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation
17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 from __future__ import print_function
20 import socket, ssl, sys, json, os, hashlib, time
23 print(*objs, file=sys.stderr)
26 warning("Syntax: %s <host> [<port>]" % (sys.argv[0]))
31 port = int(sys.argv[2])
35 agentpki = "@CMAKE_INSTALL_FULL_SYSCONFDIR@/icinga2/pki/agent"
36 keyfile = agentpki + "/agent.key"
37 certfile = agentpki + "/agent.crt"
38 cafile = agentpki + "/ca.crt"
40 if not os.path.isfile(certfile):
41 warning("Certificate file (" + certfile + ") not found.")
42 warning("Make sure the agent certificates are set up properly.")
45 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
47 # require a certificate from the server
48 ssl_sock = ssl.wrap_socket(s,
52 cert_reqs=ssl.CERT_REQUIRED)
54 ssl_sock.connect((host, port))
58 subject = ssl_sock.getpeercert()["subject"]
62 if rdn[0] == "commonName":
66 warning("Agent certificate does not have a commonName:", repr(subject))
71 repository_file = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/api/repository/" + hashlib.sha256(cn).hexdigest()
72 fp = open(repository_file, "w")
73 repository_info = { "endpoint": cn, "seen": time.time(), "zone": cn, "repository": {} }
74 json.dump(repository_info, fp)
77 peer_file = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/agent/repository/" + hashlib.sha256(cn).hexdigest() + ".peer"
78 fp = open(peer_file, "w")
79 peer_info = { "agent_host": host, "agent_port": port }
80 json.dump(peer_info, fp)
83 print("Inventory information has been updated for agent '%s'." % (cn))