~ionutbalutoiu/charms/trusty/neutron-api/next

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-01-12 12:04:00 UTC
  • mto: This revision was merged to the branch mainline in revision 70.
  • Revision ID: liam.young@canonical.com-20150112120400-a6cfdh24ufkfj5n7
Use rnpe functions from charmhelpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
    log,
19
19
    relation_ids,
20
20
    relation_set,
 
21
    relations_of_type,
21
22
)
22
23
 
23
24
from charmhelpers.core.host import service
54
55
#            juju-myservice-0
55
56
#        If you're running multiple environments with the same services in them
56
57
#        this allows you to differentiate between them.
 
58
#    nagios_servicegroups:
 
59
#      default: ""
 
60
#      type: string
 
61
#      description: |
 
62
#        A comma-separated list of nagios servicegroups.
 
63
#        If left empty, the nagios_context will be used as the servicegroup
57
64
#
58
65
# 3. Add custom checks (Nagios plugins) to files/nrpe-external-master
59
66
#
125
132
 
126
133
    def _locate_cmd(self, check_cmd):
127
134
        search_path = (
128
 
            '/',
129
 
            os.path.join(os.environ['CHARM_DIR'],
130
 
                         'files/nrpe-external-master'),
131
135
            '/usr/lib/nagios/plugins',
132
136
            '/usr/local/lib/nagios/plugins',
133
137
        )
141
145
        log('Check command not found: {}'.format(parts[0]))
142
146
        return ''
143
147
 
144
 
    def write(self, nagios_context, hostname):
 
148
    def write(self, nagios_context, hostname, nagios_servicegroups=None):
145
149
        nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
146
150
            self.command)
147
151
        with open(nrpe_check_file, 'w') as nrpe_check_config:
153
157
            log('Not writing service config as {} is not accessible'.format(
154
158
                NRPE.nagios_exportdir))
155
159
        else:
156
 
            self.write_service_config(nagios_context, hostname)
 
160
            self.write_service_config(nagios_context, hostname,
 
161
                                      nagios_servicegroups)
157
162
 
158
 
    def write_service_config(self, nagios_context, hostname):
 
163
    def write_service_config(self, nagios_context, hostname,
 
164
                             nagios_servicegroups=None):
159
165
        for f in os.listdir(NRPE.nagios_exportdir):
160
166
            if re.search('.*{}.cfg'.format(self.command), f):
161
167
                os.remove(os.path.join(NRPE.nagios_exportdir, f))
162
168
 
 
169
        if not nagios_servicegroups:
 
170
            nagios_servicegroups = nagios_context
 
171
 
163
172
        templ_vars = {
164
173
            'nagios_hostname': hostname,
165
 
            'nagios_servicegroup': nagios_context,
 
174
            'nagios_servicegroup': nagios_servicegroups,
166
175
            'description': self.description,
167
176
            'shortname': self.shortname,
168
177
            'command': self.command,
186
195
        super(NRPE, self).__init__()
187
196
        self.config = config()
188
197
        self.nagios_context = self.config['nagios_context']
 
198
        if 'nagios_servicegroups' in self.config:
 
199
            self.nagios_servicegroups = self.config['nagios_servicegroups']
 
200
        else:
 
201
            self.nagios_servicegroups = 'juju'
189
202
        self.unit_name = local_unit().replace('/', '-')
190
203
        if hostname:
191
204
            self.hostname = hostname
211
224
        nrpe_monitors = {}
212
225
        monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}}
213
226
        for nrpecheck in self.checks:
214
 
            nrpecheck.write(self.nagios_context, self.hostname)
 
227
            nrpecheck.write(self.nagios_context, self.hostname,
 
228
                            self.nagios_servicegroups)
215
229
            nrpe_monitors[nrpecheck.shortname] = {
216
230
                "command": nrpecheck.command,
217
231
            }
220
234
 
221
235
        for rid in relation_ids("local-monitors"):
222
236
            relation_set(relation_id=rid, monitors=yaml.dump(monitors))
 
237
 
 
238
 
 
239
def get_nagios_hostcontext(relation_name='nrpe-external-master'):
 
240
    """
 
241
    Query relation with nrpe subordinate, return the nagios_host_context
 
242
 
 
243
    :param str relation_name: Name of relation nrpe sub joined to
 
244
    """
 
245
    for rel in relations_of_type(relation_name):
 
246
        if 'nagios_hostname' in rel:
 
247
            return rel['nagios_host_context']
 
248
 
 
249
 
 
250
def get_nagios_hostname(relation_name='nrpe-external-master'):
 
251
    """
 
252
    Query relation with nrpe subordinate, return the nagios_hostname
 
253
 
 
254
    :param str relation_name: Name of relation nrpe sub joined to
 
255
    """
 
256
    for rel in relations_of_type(relation_name):
 
257
        if 'nagios_hostname' in rel:
 
258
            return rel['nagios_hostname']
 
259
 
 
260
 
 
261
def get_nagios_unit_name(relation_name='nrpe-external-master'):
 
262
    """
 
263
    Return the nagios unit name prepended with host_context if needed
 
264
 
 
265
    :param str relation_name: Name of relation nrpe sub joined to
 
266
    """
 
267
    host_context = get_nagios_hostcontext(relation_name)
 
268
    if host_context:
 
269
        unit = "%s:%s" % (host_context, local_unit())
 
270
    else:
 
271
        unit = local_unit()
 
272
    return unit
 
273
 
 
274
 
 
275
def add_init_service_checks(nrpe, services, unit_name):
 
276
    """
 
277
    Add checks for each service in list
 
278
 
 
279
    :param NRPE nrpe: NRPE object to add check to
 
280
    :param list services: List of services to check
 
281
    :param str unit_name: Unit name to use in check description
 
282
    """
 
283
    for svc in services:
 
284
        upstart_init = '/etc/init/%s.conf' % svc
 
285
        sysv_init = '/etc/init.d/%s' % svc
 
286
        if os.path.exists(upstart_init):
 
287
            nrpe.add_check(
 
288
                shortname=svc,
 
289
                description='process check {%s}' % unit_name,
 
290
                check_cmd='check_upstart_job %s' % svc
 
291
            )
 
292
        elif os.path.exists(sysv_init):
 
293
            cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
 
294
            cron_file = ('*/5 * * * * root '
 
295
                         '/usr/local/lib/nagios/plugins/check_exit_status.pl '
 
296
                         '-s /etc/init.d/%s status > '
 
297
                         '/var/lib/nagios/service-check-%s.txt\n' % (svc,
 
298
                                                                     svc)
 
299
                         )
 
300
            f = open(cronpath, 'w')
 
301
            f.write(cron_file)
 
302
            f.close()
 
303
            nrpe.add_check(
 
304
                shortname=svc,
 
305
                description='process check {%s}' % unit_name,
 
306
                check_cmd='check_status_file.py -f '
 
307
                          '/var/lib/nagios/service-check-%s.txt' % svc,
 
308
            )