Merge lp:~michael.nelson/charms/trusty/logstash-forwarder/merge-tanuki-trunk into lp:~canonical-is-sa/charms/trusty/logstash-forwarder/trunk

Proposed by Michael Nelson
Status: Merged
Merged at revision: 14
Proposed branch: lp:~michael.nelson/charms/trusty/logstash-forwarder/merge-tanuki-trunk
Merge into: lp:~canonical-is-sa/charms/trusty/logstash-forwarder/trunk
Diff against target: 139 lines (+66/-5)
3 files modified
config.yaml (+11/-1)
hooks/Config.py (+9/-0)
hooks/hooks.py (+46/-4)
To merge this branch: bzr merge lp:~michael.nelson/charms/trusty/logstash-forwarder/merge-tanuki-trunk
Reviewer Review Type Date Requested Status
Paul Collins Approve
Review via email: mp+275786@code.launchpad.net

Commit message

Set nagios_hostname from nrpe-external-master relation.
Add optional server map so logs can be forwarded to a logstash server without DNS. (thanks tanuki)

Description of the change

This adds two changes (and some pep8 cleanup) so that:
 1) The nagios_hostname is set from the nrpe-external-master relation
 2) An optional server map can be set to send data to a logstash server which does not have a dns entry (helpful not only in devel, but also makes it possible to send data with the correct cert without DNS in staging when needed).

I'm assuming this supersedes Guillermo's MP at:

https://code.launchpad.net/~verterok/charms/trusty/logstash-forwarder/set-nrpe-hostname-from-relation/+merge/272633

(as it just adds another of his commits to the MP :) ).

Revisions 15 and 16 are just formatting/doc issues that I noticed and fixed in the config.yaml, while testing the charm.

To post a comment you must log in.
15. By Michael Nelson

Try fixing juju config formatting.

16. By Michael Nelson

Fix example in servers_name_map to match implementation (dns is the key, value is IP).

Revision history for this message
Paul Collins (pjdc) wrote :

Looks pretty good, except for yaml.load() as noted below; -1 for now.

review: Needs Fixing
17. By Michael Nelson

Use yaml.safe_load

Revision history for this message
Paul Collins (pjdc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2015-05-29 04:52:33 +0000
3+++ config.yaml 2015-10-27 02:28:56 +0000
4@@ -38,7 +38,17 @@
5 servers:
6 default: ""
7 type: string
8- description: "Space seperated list of logstash-indexer servers"
9+ description: |
10+ Space seperated list of logstash-indexer servers (hostnames, not IPs).
11+ E.g. logstash.devel.canonical.com
12+ servers_name_map:
13+ default: ""
14+ type: string
15+ description: >
16+ If there is no real DNS, map servers hostname to IPs. The format
17+ is a yaml dict, e.g:
18+ servers_name_map: |
19+ logstash.devel.canonical.com: ip-of-logstash-or-logstash-haproxy
20 ssl_ca_cert:
21 default: ""
22 type: string
23
24=== modified file 'hooks/Config.py'
25--- hooks/Config.py 2014-09-22 12:14:13 +0000
26+++ hooks/Config.py 2015-10-27 02:28:56 +0000
27@@ -9,6 +9,9 @@
28 sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))
29 from charmhelpers.core.hookenv import config, log
30
31+import yaml
32+
33+
34 class Config:
35 def appName(self):
36 return self.getConfig("application_name")
37@@ -48,6 +51,12 @@
38 def servers(self):
39 return self.getConfig("servers").split()
40
41+ def serversNameMap(self):
42+ value = self.getConfig("servers_name_map")
43+ if not value:
44+ return {}
45+ return yaml.safe_load(value)
46+
47 def sslCACert(self):
48 return self.getConfig("ssl_ca_cert")
49
50
51=== modified file 'hooks/hooks.py'
52--- hooks/hooks.py 2015-05-29 04:52:33 +0000
53+++ hooks/hooks.py 2015-10-27 02:28:56 +0000
54@@ -11,7 +11,7 @@
55 import base64
56 import shutil
57 sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))
58-from charmhelpers.core.hookenv import Hooks, log, charm_dir
59+from charmhelpers.core.hookenv import Hooks, log, charm_dir, relations_of_type
60 from charmhelpers.core.host import mkdir, service_start, service_stop, service_restart
61 from charmhelpers.fetch import apt_install, apt_update, add_source
62 from Cheetah.Template import Template
63@@ -107,9 +107,50 @@
64 f.write(json.dumps(config, sort_keys=True, indent=4))
65
66
67+def writeEtcHosts():
68+ log("CHARM: updating /etc/hosts with logstash servers name mapping")
69+ name_map = conf.serversNameMap()
70+ servers = conf.servers()
71+ if not name_map:
72+ return
73+ new_hosts = []
74+ for server in servers:
75+ ipaddr = name_map[server]
76+ new_hosts.append('{} {}'.format(ipaddr.strip(), server.strip()))
77+ # update /etc/hosts
78+ with open('/etc/hosts') as f:
79+ current_hosts = f.readlines()
80+ section_start = "### logstash-forwarder start ###\n"
81+ section_end = "### logstash-forwarder end ###\n"
82+ with open('/tmp/new_hosts', 'w') as f:
83+ ignore_lines = False
84+ for host_line in current_hosts:
85+ if host_line == section_start:
86+ ignore_lines = True
87+ elif host_line == section_end:
88+ ignore_lines = False
89+ elif not ignore_lines:
90+ f.write(host_line)
91+ # now write a new section for our hosts
92+ f.write("### logstash-forwarder start ###\n")
93+ for new_host_line in new_hosts:
94+ f.write("{}\n".format(new_host_line))
95+ f.write("### logstash-forwarder end ###\n")
96+ os.rename('/tmp/new_hosts', '/etc/hosts')
97+
98+
99 @hooks.hook('nrpe-external-master-relation-changed')
100 def update_nrpe_checks():
101- nrpe_compat = nrpe.NRPE()
102+ rels = relations_of_type('nrpe-external-master')
103+ if not rels:
104+ log("No nrpe-external-master relations found, skipping update_nrpe_checks")
105+ return
106+ if 'nagios_hostname' not in rels[0]:
107+ log("No nagios_hostname in nrpe-external-master relations found, "
108+ "skipping update_nrpe_checks")
109+ return
110+ # only use the first relation in the list...we should have a single nrpe
111+ nrpe_compat = nrpe.NRPE(hostname=rels[0]['nagios_hostname'])
112 conf = nrpe_compat.config
113 check_procs_params = conf.get('nagios_check_procs_params')
114 if check_procs_params:
115@@ -121,14 +162,14 @@
116 nrpe_compat.add_check(
117 shortname='logstashforwarder_sending',
118 description='Check logstash-forwarder is sending by tailing logfile',
119- check_cmd = "/usr/local/lib/nagios/plugins/check-logstashforwarder-sending.sh"
120+ check_cmd="/usr/local/lib/nagios/plugins/check-logstashforwarder-sending.sh"
121 )
122 nrpe_compat.write()
123 copy_check_files()
124
125
126 def copy_check_files():
127- script_dir="/usr/local/lib/nagios/plugins"
128+ script_dir = "/usr/local/lib/nagios/plugins"
129 mkdir(script_dir)
130 for script in ["check-logstashforwarder-sending.sh"]:
131 src = os.path.join(charm_dir(), "files", script)
132@@ -156,6 +197,7 @@
133 replaceInitScript()
134 writeEtcDefault()
135 writeSSL()
136+ writeEtcHosts()
137 writeConfig()
138 service_restart("logstash-forwarder")
139 update_nrpe_checks()

Subscribers

People subscribed via source and target branches