~jamesj/charms/trusty/haproxy/xenial-support

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/charmsupport/nrpe.py

  • Committer: Christopher Glass
  • Date: 2015-02-20 09:42:24 UTC
  • mfrom: (86.2.15 ssl-crt-support)
  • Revision ID: christopher.glass@canonical.com-20150220094224-az770sf2ny2jnkax
Merge lp:~free.ekanayaka/charms/trusty/haproxy/ssl-crt-support [a=free.ekanayaka, r=tribaal]

This branch adds support for SSL termination if the installed HAproxy version
supports it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
1
17
"""Compatibility with the nrpe-external-master charm"""
2
18
# Copyright 2012 Canonical Ltd.
3
19
#
18
34
    log,
19
35
    relation_ids,
20
36
    relation_set,
 
37
    relations_of_type,
21
38
)
22
39
 
23
40
from charmhelpers.core.host import service
54
71
#            juju-myservice-0
55
72
#        If you're running multiple environments with the same services in them
56
73
#        this allows you to differentiate between them.
 
74
#    nagios_servicegroups:
 
75
#      default: ""
 
76
#      type: string
 
77
#      description: |
 
78
#        A comma-separated list of nagios servicegroups.
 
79
#        If left empty, the nagios_context will be used as the servicegroup
57
80
#
58
81
# 3. Add custom checks (Nagios plugins) to files/nrpe-external-master
59
82
#
125
148
 
126
149
    def _locate_cmd(self, check_cmd):
127
150
        search_path = (
128
 
            '/',
129
 
            os.path.join(os.environ['CHARM_DIR'],
130
 
                         'files/nrpe-external-master'),
131
151
            '/usr/lib/nagios/plugins',
 
152
            '/usr/local/lib/nagios/plugins',
132
153
        )
133
154
        parts = shlex.split(check_cmd)
134
155
        for path in search_path:
140
161
        log('Check command not found: {}'.format(parts[0]))
141
162
        return ''
142
163
 
143
 
    def write(self, nagios_context, hostname):
 
164
    def write(self, nagios_context, hostname, nagios_servicegroups=None):
144
165
        nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
145
166
            self.command)
146
167
        with open(nrpe_check_file, 'w') as nrpe_check_config:
152
173
            log('Not writing service config as {} is not accessible'.format(
153
174
                NRPE.nagios_exportdir))
154
175
        else:
155
 
            self.write_service_config(nagios_context, hostname)
 
176
            self.write_service_config(nagios_context, hostname,
 
177
                                      nagios_servicegroups)
156
178
 
157
 
    def write_service_config(self, nagios_context, hostname):
 
179
    def write_service_config(self, nagios_context, hostname,
 
180
                             nagios_servicegroups=None):
158
181
        for f in os.listdir(NRPE.nagios_exportdir):
159
182
            if re.search('.*{}.cfg'.format(self.command), f):
160
183
                os.remove(os.path.join(NRPE.nagios_exportdir, f))
161
184
 
 
185
        if not nagios_servicegroups:
 
186
            nagios_servicegroups = nagios_context
 
187
 
162
188
        templ_vars = {
163
189
            'nagios_hostname': hostname,
164
 
            'nagios_servicegroup': nagios_context,
 
190
            'nagios_servicegroup': nagios_servicegroups,
165
191
            'description': self.description,
166
192
            'shortname': self.shortname,
167
193
            'command': self.command,
181
207
    nagios_exportdir = '/var/lib/nagios/export'
182
208
    nrpe_confdir = '/etc/nagios/nrpe.d'
183
209
 
184
 
    def __init__(self):
 
210
    def __init__(self, hostname=None):
185
211
        super(NRPE, self).__init__()
186
212
        self.config = config()
187
213
        self.nagios_context = self.config['nagios_context']
 
214
        if 'nagios_servicegroups' in self.config:
 
215
            self.nagios_servicegroups = self.config['nagios_servicegroups']
 
216
        else:
 
217
            self.nagios_servicegroups = 'juju'
188
218
        self.unit_name = local_unit().replace('/', '-')
189
 
        self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
 
219
        if hostname:
 
220
            self.hostname = hostname
 
221
        else:
 
222
            self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
190
223
        self.checks = []
191
224
 
192
225
    def add_check(self, *args, **kwargs):
207
240
        nrpe_monitors = {}
208
241
        monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}}
209
242
        for nrpecheck in self.checks:
210
 
            nrpecheck.write(self.nagios_context, self.hostname)
 
243
            nrpecheck.write(self.nagios_context, self.hostname,
 
244
                            self.nagios_servicegroups)
211
245
            nrpe_monitors[nrpecheck.shortname] = {
212
246
                "command": nrpecheck.command,
213
247
            }
216
250
 
217
251
        for rid in relation_ids("local-monitors"):
218
252
            relation_set(relation_id=rid, monitors=yaml.dump(monitors))
 
253
 
 
254
 
 
255
def get_nagios_hostcontext(relation_name='nrpe-external-master'):
 
256
    """
 
257
    Query relation with nrpe subordinate, return the nagios_host_context
 
258
 
 
259
    :param str relation_name: Name of relation nrpe sub joined to
 
260
    """
 
261
    for rel in relations_of_type(relation_name):
 
262
        if 'nagios_hostname' in rel:
 
263
            return rel['nagios_host_context']
 
264
 
 
265
 
 
266
def get_nagios_hostname(relation_name='nrpe-external-master'):
 
267
    """
 
268
    Query relation with nrpe subordinate, return the nagios_hostname
 
269
 
 
270
    :param str relation_name: Name of relation nrpe sub joined to
 
271
    """
 
272
    for rel in relations_of_type(relation_name):
 
273
        if 'nagios_hostname' in rel:
 
274
            return rel['nagios_hostname']
 
275
 
 
276
 
 
277
def get_nagios_unit_name(relation_name='nrpe-external-master'):
 
278
    """
 
279
    Return the nagios unit name prepended with host_context if needed
 
280
 
 
281
    :param str relation_name: Name of relation nrpe sub joined to
 
282
    """
 
283
    host_context = get_nagios_hostcontext(relation_name)
 
284
    if host_context:
 
285
        unit = "%s:%s" % (host_context, local_unit())
 
286
    else:
 
287
        unit = local_unit()
 
288
    return unit
 
289
 
 
290
 
 
291
def add_init_service_checks(nrpe, services, unit_name):
 
292
    """
 
293
    Add checks for each service in list
 
294
 
 
295
    :param NRPE nrpe: NRPE object to add check to
 
296
    :param list services: List of services to check
 
297
    :param str unit_name: Unit name to use in check description
 
298
    """
 
299
    for svc in services:
 
300
        upstart_init = '/etc/init/%s.conf' % svc
 
301
        sysv_init = '/etc/init.d/%s' % svc
 
302
        if os.path.exists(upstart_init):
 
303
            nrpe.add_check(
 
304
                shortname=svc,
 
305
                description='process check {%s}' % unit_name,
 
306
                check_cmd='check_upstart_job %s' % svc
 
307
            )
 
308
        elif os.path.exists(sysv_init):
 
309
            cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
 
310
            cron_file = ('*/5 * * * * root '
 
311
                         '/usr/local/lib/nagios/plugins/check_exit_status.pl '
 
312
                         '-s /etc/init.d/%s status > '
 
313
                         '/var/lib/nagios/service-check-%s.txt\n' % (svc,
 
314
                                                                     svc)
 
315
                         )
 
316
            f = open(cronpath, 'w')
 
317
            f.write(cron_file)
 
318
            f.close()
 
319
            nrpe.add_check(
 
320
                shortname=svc,
 
321
                description='process check {%s}' % unit_name,
 
322
                check_cmd='check_status_file.py -f '
 
323
                          '/var/lib/nagios/service-check-%s.txt' % svc,
 
324
            )