]> granicus.if.org Git - icinga2/blob - tools/icinga2-discover-agent.cmake
Fix null ptr exception in Zone::GetLocalZone()
[icinga2] / tools / icinga2-discover-agent.cmake
1 #!/usr/bin/env python
2 # Icinga 2
3 # Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
4 #
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.
9 #
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.
14 #
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.
18
19 from __future__ import print_function
20 import socket, ssl, sys, json, os, hashlib, time
21
22 def warning(*objs):
23     print(*objs, file=sys.stderr)
24
25 if len(sys.argv) < 2:
26     warning("Syntax: %s <host> [<port>]" % (sys.argv[0]))
27     sys.exit(1)
28
29 host = sys.argv[1]
30 if len(sys.argv) > 2:
31     port = int(sys.argv[2])
32 else:
33     port = 5665
34
35 agentpki = "@CMAKE_INSTALL_FULL_SYSCONFDIR@/icinga2/pki/agent"
36 keyfile = agentpki + "/agent.key"
37 certfile = agentpki + "/agent.crt"
38 cafile = agentpki + "/ca.crt"
39
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.")
43     sys.exit(1)
44
45 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46
47 # require a certificate from the server
48 ssl_sock = ssl.wrap_socket(s,
49                            keyfile=keyfile,
50                            certfile=certfile,
51                            ca_certs=cafile,
52                            cert_reqs=ssl.CERT_REQUIRED)
53
54 ssl_sock.connect((host, port))
55
56 cn = None
57
58 subject = ssl_sock.getpeercert()["subject"]
59
60 for prdn in subject:
61     rdn = prdn[0]
62     if rdn[0] == "commonName":
63         cn = rdn[1]
64
65 if cn == None:
66     warning("Agent certificate does not have a commonName:", repr(subject))
67     sys.exit(1)
68
69 ssl_sock.close()
70
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)
75 fp.close()
76
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)
81 fp.close()
82
83 print("Inventory information has been updated for agent '%s'." % (cn))
84 sys.exit(0)