~openstack-charmers-archive/charms/precise/swift-storage/trunk

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2016-01-28 09:03:51 UTC
  • Revision ID: liam.young@canonical.com-20160128090351-q8rze2d8a20fd287
16.01 Charm release

Show diffs side-by-side

added added

removed removed

Lines of Context:
148
148
        self.description = description
149
149
        self.check_cmd = self._locate_cmd(check_cmd)
150
150
 
 
151
    def _get_check_filename(self):
 
152
        return os.path.join(NRPE.nrpe_confdir, '{}.cfg'.format(self.command))
 
153
 
 
154
    def _get_service_filename(self, hostname):
 
155
        return os.path.join(NRPE.nagios_exportdir,
 
156
                            'service__{}_{}.cfg'.format(hostname, self.command))
 
157
 
151
158
    def _locate_cmd(self, check_cmd):
152
159
        search_path = (
153
160
            '/usr/lib/nagios/plugins',
163
170
        log('Check command not found: {}'.format(parts[0]))
164
171
        return ''
165
172
 
 
173
    def _remove_service_files(self):
 
174
        if not os.path.exists(NRPE.nagios_exportdir):
 
175
            return
 
176
        for f in os.listdir(NRPE.nagios_exportdir):
 
177
            if f.endswith('_{}.cfg'.format(self.command)):
 
178
                os.remove(os.path.join(NRPE.nagios_exportdir, f))
 
179
 
 
180
    def remove(self, hostname):
 
181
        nrpe_check_file = self._get_check_filename()
 
182
        if os.path.exists(nrpe_check_file):
 
183
            os.remove(nrpe_check_file)
 
184
        self._remove_service_files()
 
185
 
166
186
    def write(self, nagios_context, hostname, nagios_servicegroups):
167
 
        nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
168
 
            self.command)
 
187
        nrpe_check_file = self._get_check_filename()
169
188
        with open(nrpe_check_file, 'w') as nrpe_check_config:
170
189
            nrpe_check_config.write("# check {}\n".format(self.shortname))
171
190
            nrpe_check_config.write("command[{}]={}\n".format(
180
199
 
181
200
    def write_service_config(self, nagios_context, hostname,
182
201
                             nagios_servicegroups):
183
 
        for f in os.listdir(NRPE.nagios_exportdir):
184
 
            if re.search('.*{}.cfg'.format(self.command), f):
185
 
                os.remove(os.path.join(NRPE.nagios_exportdir, f))
 
202
        self._remove_service_files()
186
203
 
187
204
        templ_vars = {
188
205
            'nagios_hostname': hostname,
192
209
            'command': self.command,
193
210
        }
194
211
        nrpe_service_text = Check.service_template.format(**templ_vars)
195
 
        nrpe_service_file = '{}/service__{}_{}.cfg'.format(
196
 
            NRPE.nagios_exportdir, hostname, self.command)
 
212
        nrpe_service_file = self._get_service_filename(hostname)
197
213
        with open(nrpe_service_file, 'w') as nrpe_service_config:
198
214
            nrpe_service_config.write(str(nrpe_service_text))
199
215
 
218
234
        if hostname:
219
235
            self.hostname = hostname
220
236
        else:
221
 
            self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
 
237
            nagios_hostname = get_nagios_hostname()
 
238
            if nagios_hostname:
 
239
                self.hostname = nagios_hostname
 
240
            else:
 
241
                self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
222
242
        self.checks = []
223
243
 
224
244
    def add_check(self, *args, **kwargs):
225
245
        self.checks.append(Check(*args, **kwargs))
226
246
 
 
247
    def remove_check(self, *args, **kwargs):
 
248
        if kwargs.get('shortname') is None:
 
249
            raise ValueError('shortname of check must be specified')
 
250
 
 
251
        # Use sensible defaults if they're not specified - these are not
 
252
        # actually used during removal, but they're required for constructing
 
253
        # the Check object; check_disk is chosen because it's part of the
 
254
        # nagios-plugins-basic package.
 
255
        if kwargs.get('check_cmd') is None:
 
256
            kwargs['check_cmd'] = 'check_disk'
 
257
        if kwargs.get('description') is None:
 
258
            kwargs['description'] = ''
 
259
 
 
260
        check = Check(*args, **kwargs)
 
261
        check.remove(self.hostname)
 
262
 
227
263
    def write(self):
228
264
        try:
229
265
            nagios_uid = pwd.getpwnam('nagios').pw_uid
260
296
    :param str relation_name: Name of relation nrpe sub joined to
261
297
    """
262
298
    for rel in relations_of_type(relation_name):
263
 
        if 'nagios_hostname' in rel:
 
299
        if 'nagios_host_context' in rel:
264
300
            return rel['nagios_host_context']
265
301
 
266
302
 
301
337
        upstart_init = '/etc/init/%s.conf' % svc
302
338
        sysv_init = '/etc/init.d/%s' % svc
303
339
        if os.path.exists(upstart_init):
304
 
            nrpe.add_check(
305
 
                shortname=svc,
306
 
                description='process check {%s}' % unit_name,
307
 
                check_cmd='check_upstart_job %s' % svc
308
 
            )
 
340
            # Don't add a check for these services from neutron-gateway
 
341
            if svc not in ['ext-port', 'os-charm-phy-nic-mtu']:
 
342
                nrpe.add_check(
 
343
                    shortname=svc,
 
344
                    description='process check {%s}' % unit_name,
 
345
                    check_cmd='check_upstart_job %s' % svc
 
346
                )
309
347
        elif os.path.exists(sysv_init):
310
348
            cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
311
349
            cron_file = ('*/5 * * * * root '