~evarlast/charms/precise/juju-gui/hacking-note

« back to all changes in this revision

Viewing changes to scripts/charmsupport/nrpe.py

  • Committer: Rick Harding
  • Date: 2014-04-09 19:04:29 UTC
  • mfrom: (60.13.103 juju-gui-trunk)
  • Revision ID: rick.harding@canonical.com-20140409190429-m4k6jw7ev7jddfrv
New charm release

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, 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):
117
127
            'shortname': self.shortname,
118
128
        }
119
129
        nrpe_service_text = Check.service_template.format(**templ_vars)
120
 
        nrpe_service_file = '{}/service__{}_check_{}.cfg'.format(
121
 
            NRPE.nagios_exportdir, hostname, self.shortname)
 
130
        nrpe_service_file = self.service_file_name(hostname)
122
131
        with open(nrpe_service_file, 'w') as nrpe_service_config:
123
132
            nrpe_service_config.write(str(nrpe_service_text))
124
133
 
128
137
            nrpe_check_config.write("command[check_{}]={}\n".format(
129
138
                self.shortname, self.check_cmd))
130
139
 
 
140
    def remove(self, hostname):
 
141
        """Remove the configuration file for this check."""
 
142
        try:
 
143
            os.unlink(self.service_file_name(hostname))
 
144
        except OSError, e:
 
145
            if e.errno == errno.ENOENT:
 
146
                # Ignore the fact that the file didn't exist.
 
147
                pass
 
148
            else:
 
149
                raise
 
150
 
131
151
    def run(self):
132
152
        subprocess.call(self.check_cmd)
133
153
 
 
154
 
134
155
class NRPE(object):
135
156
    nagios_logdir = '/var/log/nagios'
136
157
    nagios_exportdir = '/var/lib/nagios/export'
167
188
 
168
189
        if os.path.isfile('/etc/init.d/nagios-nrpe-server'):
169
190
            subprocess.call(['service', 'nagios-nrpe-server', 'reload'])
 
191
 
 
192
    def remove_checks(self):
 
193
        for check in self.checks:
 
194
            check.remove(self.hostname)