~benji/charms/precise/juju-gui/bug-1284088

« back to all changes in this revision

Viewing changes to scripts/charmsupport/nrpe.py

  • Committer: Benji York
  • Date: 2014-03-19 16:39:34 UTC
  • Revision ID: benji@benjiyork.com-20140319163934-lis6xcvghcwton9f
handle lost nrpe relations

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
# Authors:
6
6
#  Matthew Wedgwood <matthew.wedgwood@canonical.com>
7
7
 
 
8
# XXX This file has been modified to add the ability to remove NRPE checks.
 
9
# The original project that created this code has been abandoned so there is
 
10
# no upstream to which the modifications can be sent.
 
11
 
8
12
import subprocess
9
13
import pwd
10
14
import grp
11
15
import os
12
16
import re
13
17
import shlex
 
18
import errno
14
19
 
15
20
from hookenv import config, local_unit
16
21
 
105
110
        subprocess.call(['juju-log', 'Check command not found: {}'.format(command[0])])
106
111
        return ''
107
112
 
 
113
    def service_file_name(self, nagios_context, hostname):
 
114
        return '{}/service__{}_check_{}.cfg'.format(
 
115
            NRPE.nagios_exportdir, hostname, self.shortname)
 
116
 
 
117
 
108
118
    def write(self, nagios_context, hostname):
109
119
        for f in os.listdir(NRPE.nagios_exportdir):
110
120
            if re.search('.*check_{}.cfg'.format(self.shortname), f):
119
129
        nrpe_service_text = Check.service_template.format(**templ_vars)
120
130
        nrpe_service_file = '{}/service__{}_check_{}.cfg'.format(
121
131
            NRPE.nagios_exportdir, hostname, self.shortname)
 
132
        nrpe_service_file = self.service_file_name(nagios_context, hostname)
122
133
        with open(nrpe_service_file, 'w') as nrpe_service_config:
123
134
            nrpe_service_config.write(str(nrpe_service_text))
124
135
 
128
139
            nrpe_check_config.write("command[check_{}]={}\n".format(
129
140
                self.shortname, self.check_cmd))
130
141
 
 
142
    def remove(self, nagios_context, hostname):
 
143
        """Remove the configuration file for this check."""
 
144
        try:
 
145
            os.unlink(self.service_file_name(nagios_context, hostname))
 
146
        except OSError, e:
 
147
            if e.errno == errno.ENOENT:
 
148
                # Ignore the fact that the file didn't exist.
 
149
                pass
 
150
            else:
 
151
                raise
 
152
 
131
153
    def run(self):
132
154
        subprocess.call(self.check_cmd)
133
155
 
 
156
 
134
157
class NRPE(object):
135
158
    nagios_logdir = '/var/log/nagios'
136
159
    nagios_exportdir = '/var/lib/nagios/export'
167
190
 
168
191
        if os.path.isfile('/etc/init.d/nagios-nrpe-server'):
169
192
            subprocess.call(['service', 'nagios-nrpe-server', 'reload'])
 
193
 
 
194
    def remove_checks(self):
 
195
        for check in self.checks:
 
196
            check.remove(self.nagios_context, self.hostname)