Merge lp:~brad-marshall/charms/trusty/nagios/add-livestatus-support into lp:charms/trusty/nagios
- Trusty Tahr (14.04)
- add-livestatus-support
- Merge into trunk
Status: | Merged |
---|---|
Merged at revision: | 31 |
Proposed branch: | lp:~brad-marshall/charms/trusty/nagios/add-livestatus-support |
Merge into: | lp:charms/trusty/nagios |
Diff against target: |
227 lines (+147/-2) 5 files modified
README.md (+4/-0) config.yaml (+10/-0) hooks/install (+18/-0) hooks/upgrade-charm (+72/-2) tests/23-livestatus-test (+43/-0) |
To merge this branch: | bzr merge lp:~brad-marshall/charms/trusty/nagios/add-livestatus-support |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Charles Butler (community) | Approve | ||
Review via email: mp+257992@code.launchpad.net |
Commit message
Description of the change
Add livestatus support, which can't be done via the extraconfig since the broker_module needs to be in the main nagios.cfg file.
- 21. By Brad Marshall
-
[bradm] Merge from upstream, had started with lp:charms/nagios instead of lp:charms/trusty/nagios
- 22. By Brad Marshall
-
[bradm] Add test for livestatus config variable
Brad Marshall (brad-marshall) wrote : | # |
Adam Israel (aisrael) wrote : | # |
Hi!
I'm doing a triage of reviews in the queue, in the spirit of fail-fast. This is a cursory check of the proposed merge to see if there are any "obvious" things that may prevent the review from being accepted.
The goal is to get you feedback sooner, before an in-depth review has been done. I'm happy to see the addition of a unit test for the new functionality. Could you also add something to the README to explains the usage of this new feature?
Thanks!
- 23. By Brad Marshall
-
[bradm] Add documentation about livestatus config options, tidy up comments
Brad Marshall (brad-marshall) wrote : | # |
I've added some basic documentation about the config variables.
Charles Butler (lazypower) wrote : | # |
Greetings Brad,
I took some time to review this and would like to thank you for the contribution. The test coverage + documentation updates.
In response to your earlier question about running config-get, that is one way of doing it or you can read directly from the config dictionary you supply to the charm in the test. However if you did not supply one - your method was correct. To obtain the data from juju-run.
I found some test failures that seem unrelated to your merge. I filed them under this existing bug about tests failing in CI here: https:/
+1 LGTM. Thank you for taking the time to submit this feature for the nagios charm. We appreciate your work. I've merged this branch and it should be available in the charm store after the next ingestion.
If you have any questions/
Preview Diff
1 | === modified file 'README.md' | |||
2 | --- README.md 2014-04-11 14:06:48 +0000 | |||
3 | +++ README.md 2015-05-07 07:07:58 +0000 | |||
4 | @@ -28,6 +28,10 @@ | |||
5 | 28 | 28 | ||
6 | 29 | # Configuration | 29 | # Configuration |
7 | 30 | 30 | ||
8 | 31 | - `enable_livestatus` - Setting to enable the [livestatus module](https://mathias-kettner.de/checkmk_livestatus.html). This is an easy interface to get data out of Nagios. | ||
9 | 32 | |||
10 | 33 | - `livestatus_path` - Configuration of where the livestatus module is stored - defaults to /var/lib/nagios3/livestatus/socket. | ||
11 | 34 | |||
12 | 31 | ### SSL Configuration | 35 | ### SSL Configuration |
13 | 32 | 36 | ||
14 | 33 | - `ssl` - Determinant configuration for enabling SSL. Valid options are "on", "off", "only". The "only" option disables HTTP traffic on Apache in favor of HTTPS. This setting may cause unexpected behavior with existing nagios charm deployments. | 37 | - `ssl` - Determinant configuration for enabling SSL. Valid options are "on", "off", "only". The "only" option disables HTTP traffic on Apache in favor of HTTPS. This setting may cause unexpected behavior with existing nagios charm deployments. |
15 | 34 | 38 | ||
16 | === modified file 'config.yaml' | |||
17 | --- config.yaml 2014-03-04 21:33:40 +0000 | |||
18 | +++ config.yaml 2015-05-07 07:07:58 +0000 | |||
19 | @@ -34,3 +34,13 @@ | |||
20 | 34 | description: | | 34 | description: | |
21 | 35 | base64 encoded chain certificates file. If ssl_cert is | 35 | base64 encoded chain certificates file. If ssl_cert is |
22 | 36 | blank, this will be ignored. | 36 | blank, this will be ignored. |
23 | 37 | enable_livestatus: | ||
24 | 38 | type: boolean | ||
25 | 39 | default: false | ||
26 | 40 | description: | | ||
27 | 41 | Config variable to enable livestatus module or not. | ||
28 | 42 | livestatus_path: | ||
29 | 43 | type: string | ||
30 | 44 | default: "/var/lib/nagios3/livestatus/socket" | ||
31 | 45 | description: | | ||
32 | 46 | Default path to livestatus socket, if enabled via enable_livestatus | ||
33 | 37 | 47 | ||
34 | === modified file 'hooks/install' | |||
35 | --- hooks/install 2014-10-13 23:19:09 +0000 | |||
36 | +++ hooks/install 2015-05-07 07:07:58 +0000 | |||
37 | @@ -37,6 +37,24 @@ | |||
38 | 37 | rm -v /etc/nagios3/conf.d/extinfo_nagios2.cfg | 37 | rm -v /etc/nagios3/conf.d/extinfo_nagios2.cfg |
39 | 38 | fi | 38 | fi |
40 | 39 | 39 | ||
41 | 40 | enable_livestatus=$(config-get enable_livestatus) | ||
42 | 41 | livestatus_path=$(config-get livestatus_path) | ||
43 | 42 | livestatus_dir=$(dirname $livestatus_path) | ||
44 | 43 | |||
45 | 44 | if [ "$enable_livestatus" ]; then | ||
46 | 45 | # install check-mk-livestatus | ||
47 | 46 | DEBIAN_FRONTEND=noninteractive apt-get -qy install check-mk-livestatus | ||
48 | 47 | # enable livestatus broker | ||
49 | 48 | if ! grep '^broker_module=' /etc/nagios3/nagios.cfg ; then | ||
50 | 49 | echo "broker_module=/usr/lib/check_mk/livestatus.o $livestatus_path" >> /etc/nagios3/nagios.cfg | ||
51 | 50 | fi | ||
52 | 51 | # fix permissions on the livestatus directory | ||
53 | 52 | mkdir -p $livestatus_dir | ||
54 | 53 | chown nagios:www-data $livestatus_dir | ||
55 | 54 | chmod g+w $livestatus_dir | ||
56 | 55 | chmod g+s $livestatus_dir | ||
57 | 56 | fi | ||
58 | 57 | |||
59 | 40 | if [ -f $CHARM_DIR/files/index.html ]; then | 58 | if [ -f $CHARM_DIR/files/index.html ]; then |
60 | 41 | # Replace the default index.html file to redirect to nagios3/ | 59 | # Replace the default index.html file to redirect to nagios3/ |
61 | 42 | cp -v $CHARM_DIR/files/index.html /var/www/html/index.html | 60 | cp -v $CHARM_DIR/files/index.html /var/www/html/index.html |
62 | 43 | 61 | ||
63 | === modified file 'hooks/upgrade-charm' | |||
64 | --- hooks/upgrade-charm 2014-10-14 17:18:28 +0000 | |||
65 | +++ hooks/upgrade-charm 2015-05-07 07:07:58 +0000 | |||
66 | @@ -5,19 +5,28 @@ | |||
67 | 5 | import base64 | 5 | import base64 |
68 | 6 | from jinja2 import Template | 6 | from jinja2 import Template |
69 | 7 | import os | 7 | import os |
71 | 8 | import shutil | 8 | import re |
72 | 9 | import pwd | ||
73 | 10 | import grp | ||
74 | 11 | import stat | ||
75 | 12 | import errno | ||
76 | 13 | # import shutil | ||
77 | 9 | import subprocess | 14 | import subprocess |
78 | 10 | from charmhelpers.contrib import ssl | 15 | from charmhelpers.contrib import ssl |
79 | 11 | from charmhelpers.core import hookenv, host | 16 | from charmhelpers.core import hookenv, host |
80 | 17 | from charmhelpers import fetch | ||
81 | 12 | 18 | ||
82 | 13 | from common import update_localhost | 19 | from common import update_localhost |
83 | 14 | 20 | ||
84 | 15 | # Gather facts | 21 | # Gather facts |
85 | 16 | legacy_relations = hookenv.config('legacy') | 22 | legacy_relations = hookenv.config('legacy') |
86 | 17 | extra_config = hookenv.config('extraconfig') | 23 | extra_config = hookenv.config('extraconfig') |
87 | 24 | enable_livestatus = hookenv.config('enable_livestatus') | ||
88 | 25 | livestatus_path = hookenv.config('livestatus_path') | ||
89 | 18 | ssl_config = hookenv.config('ssl') | 26 | ssl_config = hookenv.config('ssl') |
90 | 19 | charm_dir = os.environ['CHARM_DIR'] | 27 | charm_dir = os.environ['CHARM_DIR'] |
91 | 20 | cert_domain = hookenv.unit_get('public-address') | 28 | cert_domain = hookenv.unit_get('public-address') |
92 | 29 | nagios_cfg = "/etc/nagios3/nagios.cfg" | ||
93 | 21 | 30 | ||
94 | 22 | 31 | ||
95 | 23 | # Checks the charm relations for legacy relations | 32 | # Checks the charm relations for legacy relations |
96 | @@ -44,6 +53,66 @@ | |||
97 | 44 | host.write_file('/etc/nagios3/conf.d/extra.cfg', extra_config) | 53 | host.write_file('/etc/nagios3/conf.d/extra.cfg', extra_config) |
98 | 45 | 54 | ||
99 | 46 | 55 | ||
100 | 56 | # Equivalent of mkdir -p, since we can't rely on | ||
101 | 57 | # python 3.2 os.makedirs exist_ok argument | ||
102 | 58 | def mkdir_p(path): | ||
103 | 59 | try: | ||
104 | 60 | os.makedirs(path) | ||
105 | 61 | except OSError as exc: # Python >2.5 | ||
106 | 62 | if exc.errno == errno.EEXIST and os.path.isdir(path): | ||
107 | 63 | pass | ||
108 | 64 | else: | ||
109 | 65 | raise | ||
110 | 66 | |||
111 | 67 | |||
112 | 68 | # Fix the path to be world executable | ||
113 | 69 | def fixpath(path): | ||
114 | 70 | if os.path.isdir(path): | ||
115 | 71 | st = os.stat(path) | ||
116 | 72 | os.chmod(path, st.st_mode | stat.S_IXOTH) | ||
117 | 73 | if path != "/": | ||
118 | 74 | fixpath(os.path.split(path)[0]) | ||
119 | 75 | |||
120 | 76 | |||
121 | 77 | def enable_livestatus_config(): | ||
122 | 78 | if enable_livestatus: | ||
123 | 79 | hookenv.log("Livestatus is enabled") | ||
124 | 80 | fetch.apt_update() | ||
125 | 81 | fetch.apt_install('check-mk-livestatus') | ||
126 | 82 | broker = re.compile("^broker_module=") | ||
127 | 83 | broker_found = False | ||
128 | 84 | for line in open(nagios_cfg): | ||
129 | 85 | if broker.match(line): | ||
130 | 86 | broker_found = True | ||
131 | 87 | hookenv.log("broker_module line exists, not adding..") | ||
132 | 88 | break | ||
133 | 89 | |||
134 | 90 | if not broker_found: | ||
135 | 91 | with open(nagios_cfg, "a") as nagiosfile: | ||
136 | 92 | broker_str = "broker_module=/usr/lib/check_mk/livestatus.o " . livestatus_path | ||
137 | 93 | nagiosfile.write(broker_str) | ||
138 | 94 | nagiosfile.close() | ||
139 | 95 | |||
140 | 96 | # Make the directory and fix perms on it | ||
141 | 97 | hookenv.log("Fixing perms on livestatus_path") | ||
142 | 98 | livestatus_path = hookenv.config('livestatus_path') | ||
143 | 99 | livestatus_dir = os.path.dirname(livestatus_path) | ||
144 | 100 | if not os.path.isdir(livestatus_dir): | ||
145 | 101 | hookenv.log("Making path for livestatus_dir") | ||
146 | 102 | mkdir_p(livestatus_dir) | ||
147 | 103 | fixpath(livestatus_dir) | ||
148 | 104 | |||
149 | 105 | # Fix the perms on the socket | ||
150 | 106 | hookenv.log("Fixing perms on the socket") | ||
151 | 107 | uid = pwd.getpwnam("nagios").pw_uid | ||
152 | 108 | gid = grp.getgrnam("www-data").gr_gid | ||
153 | 109 | os.chown(livestatus_path, uid, gid) | ||
154 | 110 | os.chown(livestatus_dir, uid, gid) | ||
155 | 111 | st = os.stat(livestatus_path) | ||
156 | 112 | os.chmod(livestatus_path, st.st_mode | stat.S_IRGRP) | ||
157 | 113 | os.chmod(livestatus_dir, st.st_mode | stat.S_IRGRP | stat.S_ISGID) | ||
158 | 114 | |||
159 | 115 | |||
160 | 47 | def ssl_configured(): | 116 | def ssl_configured(): |
161 | 48 | allowed_options = ["on", "only"] | 117 | allowed_options = ["on", "only"] |
162 | 49 | if str(ssl_config).lower() in allowed_options: | 118 | if str(ssl_config).lower() in allowed_options: |
163 | @@ -89,7 +158,7 @@ | |||
164 | 89 | def enable_ssl(): | 158 | def enable_ssl(): |
165 | 90 | # Set the basename of all ssl files | 159 | # Set the basename of all ssl files |
166 | 91 | 160 | ||
168 | 92 | #Validate that we have configs, and generate a self signed certificate. | 161 | # Validate that we have configs, and generate a self signed certificate. |
169 | 93 | if not hookenv.config('ssl_cert'): | 162 | if not hookenv.config('ssl_cert'): |
170 | 94 | # bail if keys already exist | 163 | # bail if keys already exist |
171 | 95 | if os.path.exists(cert_file): | 164 | if os.path.exists(cert_file): |
172 | @@ -143,6 +212,7 @@ | |||
173 | 143 | 212 | ||
174 | 144 | warn_legacy_relations() | 213 | warn_legacy_relations() |
175 | 145 | write_extra_config() | 214 | write_extra_config() |
176 | 215 | enable_livestatus_config() | ||
177 | 146 | if ssl_configured(): | 216 | if ssl_configured(): |
178 | 147 | enable_ssl() | 217 | enable_ssl() |
179 | 148 | update_apache() | 218 | update_apache() |
180 | 149 | 219 | ||
181 | === added file 'tests/23-livestatus-test' | |||
182 | --- tests/23-livestatus-test 1970-01-01 00:00:00 +0000 | |||
183 | +++ tests/23-livestatus-test 2015-05-07 07:07:58 +0000 | |||
184 | @@ -0,0 +1,43 @@ | |||
185 | 1 | #!/usr/bin/python3 | ||
186 | 2 | |||
187 | 3 | import amulet | ||
188 | 4 | # import requests | ||
189 | 5 | |||
190 | 6 | seconds = 20000 | ||
191 | 7 | |||
192 | 8 | d = amulet.Deployment(series='trusty') | ||
193 | 9 | |||
194 | 10 | d.add('nagios') | ||
195 | 11 | |||
196 | 12 | d.expose('nagios') | ||
197 | 13 | |||
198 | 14 | try: | ||
199 | 15 | d.setup(timeout=seconds) | ||
200 | 16 | d.sentry.wait() | ||
201 | 17 | except amulet.helpers.TimeoutError: | ||
202 | 18 | amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time") | ||
203 | 19 | except: | ||
204 | 20 | raise | ||
205 | 21 | |||
206 | 22 | |||
207 | 23 | ## | ||
208 | 24 | # Set relationship aliases | ||
209 | 25 | ## | ||
210 | 26 | nagios_unit = d.sentry.unit['nagios/0'] | ||
211 | 27 | |||
212 | 28 | d.configure('nagios', { | ||
213 | 29 | 'enable_livestatus': True | ||
214 | 30 | }) | ||
215 | 31 | |||
216 | 32 | d.sentry.wait() | ||
217 | 33 | |||
218 | 34 | |||
219 | 35 | def test_livestatus_file_exists(): | ||
220 | 36 | livestatus_path = nagios_unit.run('config-get livestatus_path') | ||
221 | 37 | try: | ||
222 | 38 | livestatus_file = nagios_unit.file(livestatus_path[0]) | ||
223 | 39 | except OSError: | ||
224 | 40 | message = "Can't find livestatus file" | ||
225 | 41 | amulet.raise_status(amulet.FAIL, msg=message) | ||
226 | 42 | |||
227 | 43 | test_livestatus_file_exists() |
Added tests for the livestatus path existing, although I'm not sure I did the config-get correctly - it feels like there should be a better way in amulet than a unit.run( 'config- get <var>') - please let me know if there is.