~charmers/charms/precise/nagios/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import subprocess
import socket
import os
import os.path
import re
import sqlite3
import shutil
import tempfile

from pynag import Model

INPROGRESS_DIR = '/etc/nagios3-inprogress'
INPROGRESS_CFG = '/etc/nagios3-inprogress/nagios.cfg'
INPROGRESS_CONF_D = '/etc/nagios3-inprogress/conf.d'
CHARM_CFG = '/etc/nagios3-inprogress/conf.d/charm.cfg'
MAIN_NAGIOS_BAK = '/etc/nagios3.bak'
MAIN_NAGIOS_DIR = '/etc/nagios3'
MAIN_NAGIOS_CFG = '/etc/nagios3/nagios.cfg'
PLUGIN_PATH = '/usr/lib/nagios/plugins'

Model.cfg_file = INPROGRESS_CFG
Model.pynag_directory = INPROGRESS_CONF_D

reduce_RE = re.compile('[\W_]')


def check_ip(n):
    try:
        socket.inet_pton(socket.AF_INET, n)
        return True
    except socket.error:
        try:
            socket.inet_pton(socket.AF_INET6, n)
            return True
        except socket.error:
            return False


def get_ip_and_hostname(remote_unit, relation_id=None):
    args = ["relation-get", "private-address", remote_unit]
    if relation_id is not None:
        args.extend(['-r', relation_id])
    hostname = subprocess.check_output(args).strip()

    if hostname is None or not len(hostname):
        print "relation-get failed"
        return 2
    if check_ip(hostname):
        # Some providers don't provide hostnames, so use the remote unit name.
        ip_address = hostname
    else:
        ip_address = socket.getaddrinfo(hostname, None)[0][4][0]
    return (ip_address, remote_unit.replace('/', '-'))


def refresh_hostgroups():
    """ Not the most efficient thing but since we're only
        parsing what is already on disk here its not too bad """
    hosts = [x['host_name'] for x in Model.Host.objects.all if x['host_name']]

    hgroups = {}
    for host in hosts:
        try:
            (service, unit_id) = host.rsplit('-', 1)
        except ValueError:
            continue
        if service in hgroups:
            hgroups[service].append(host)
        else:
            hgroups[service] = [host]

    # Find existing autogenerated
    auto_hgroups = Model.Hostgroup.objects.filter(notes__contains='#autogenerated#')
    auto_hgroups = [x.get_attribute('hostgroup_name') for x in auto_hgroups]

    # Delete the ones not in hgroups
    to_delete = set(auto_hgroups).difference(set(hgroups.keys()))
    for hgroup_name in to_delete:
        try:
            hgroup = Model.Hostgroup.objects.get_by_shortname(hgroup_name)
            hgroup.delete()
        except ValueError:
            pass

    for hgroup_name, members in hgroups.iteritems():
        try:
            hgroup = Model.Hostgroup.objects.get_by_shortname(hgroup_name)
        except ValueError:
            hgroup = Model.Hostgroup()
            hgroup.set_filename(CHARM_CFG)
            hgroup.set_attribute('hostgroup_name', hgroup_name)
            hgroup.set_attribute('notes', '#autogenerated#')

        hgroup.set_attribute('members', ','.join(members))
        hgroup.save()


def _make_check_command(args):
    args = [str(arg) for arg in args]
    # There is some worry of collision, but the uniqueness of the initial
    # command should be enough.
    signature = reduce_RE.sub('_', ''.join(
                [os.path.basename(arg) for arg in args]))
    Model.Command.objects.reload_cache()
    try:
        cmd = Model.Command.objects.get_by_shortname(signature)
    except ValueError:
        cmd = Model.Command()
        cmd.set_attribute('command_name', signature)
        cmd.set_attribute('command_line', ' '.join(args))
        cmd.save()
    return signature


def _extend_args(args, cmd_args, switch, value):
    args.append(value)
    cmd_args.extend((switch, '"$ARG%d$"' % len(args)))


def customize_http(service, name, extra):
    args = []
    cmd_args = []
    plugin = os.path.join(PLUGIN_PATH, 'check_http')
    port = extra.get('port', 80)
    path = extra.get('path', '/')
    args = [port, path]
    cmd_args = [plugin, '-p', '"$ARG1$"', '-u', '"$ARG2$"']
    if 'status' in extra:
        _extend_args(args, cmd_args, '-e', extra['status'])
    if 'host' in extra:
        _extend_args(args, cmd_args, '-H', extra['host'])
        cmd_args.extend(('-I', '$HOSTADDRESS$'))
    else:
        cmd_args.extend(('-H', '$HOSTADDRESS$'))
    check_command = _make_check_command(cmd_args)
    cmd = '%s!%s' % (check_command, '!'.join([str(x) for x in args]))
    service.set_attribute('check_command', cmd)
    return True


def customize_mysql(service, name, extra):
    plugin = os.path.join(PLUGIN_PATH, 'check_mysql')
    args = []
    cmd_args = [plugin, '-H', '$HOSTADDRESS$']
    if 'user' in extra:
        _extend_args(args, cmd_args, '-u', extra['user'])
    if 'password' in extra:
        _extend_args(args, cmd_args, '-p', extra['password'])
    check_command = _make_check_command(cmd_args)
    cmd = '%s!%s' % (check_command, '!'.join([str(x) for x in args]))
    service.set_attribute('check_command', cmd)
    return True


def customize_nrpe(service, name, extra):
    plugin = os.path.join(PLUGIN_PATH, 'check_nrpe')
    args = []
    cmd_args = [plugin, '-H', '$HOSTADDRESS$']
    if name in ('mem', 'swap'):
        cmd_args.extend(('-c', 'check_%s' % name))
    elif 'command' in extra:
        cmd_args.extend(('-c', extra['command']))
    else:
        return False
    check_command = _make_check_command(cmd_args)
    cmd = '%s!%s' % (check_command, '!'.join([str(x) for x in args]))
    service.set_attribute('check_command', cmd)
    return True


def customize_service(service, family, name, extra):
    customs = {'http': customize_http,
                'mysql': customize_mysql,
                'nrpe': customize_nrpe}
    if family in customs:
        return customs[family](service, name, extra)
    return False


def get_pynag_host(target_id, owner_unit=None, owner_relation=None):
    try:
        host = Model.Host.objects.get_by_shortname(target_id)
    except ValueError:
        host = Model.Host()
        host.set_filename(CHARM_CFG)
        host.set_attribute('host_name', target_id)
        host.set_attribute('use', 'generic-host')
        host.save()
        host = Model.Host.objects.get_by_shortname(target_id)
    apply_host_policy(target_id, owner_unit, owner_relation)
    return host


def get_pynag_service(target_id, service_name):
    services = Model.Service.objects.filter(host_name=target_id,
                    service_description=service_name)
    if len(services) == 0:
        service = Model.Service()
        service.set_filename(CHARM_CFG)
        service.set_attribute('service_description', service_name)
        service.set_attribute('host_name', target_id)
        service.set_attribute('use', 'generic-service')
    else:
        service = services[0]
    return service


def apply_host_policy(target_id, owner_unit, owner_relation):
    ssh_service = get_pynag_service(target_id, 'SSH')
    ssh_service.set_attribute('check_command', 'check_ssh')
    ssh_service.save()


def get_valid_relations():
    for x in subprocess.Popen(['relation-ids', 'monitors'],
        stdout=subprocess.PIPE).stdout:
        yield x.strip()
    for x in subprocess.Popen(['relation-ids', 'nagios'],
        stdout=subprocess.PIPE).stdout:
        yield x.strip()


def get_valid_units(relation_id):
    for x in subprocess.Popen(['relation-list', '-r', relation_id],
                              stdout=subprocess.PIPE).stdout:
        yield x.strip()


def _replace_in_config(find_me, replacement):
    with open(INPROGRESS_CFG) as cf:
        with tempfile.NamedTemporaryFile(dir=INPROGRESS_DIR, delete=False) as new_cf:
            for line in cf:
                new_cf.write(line.replace(find_me, replacement))
            new_cf.flush()
            os.chmod(new_cf.name, 0644)
            os.unlink(INPROGRESS_CFG)
            os.rename(new_cf.name, INPROGRESS_CFG)


def _commit_in_config(find_me, replacement):
    with open(MAIN_NAGIOS_CFG) as cf:
        with tempfile.NamedTemporaryFile(dir=MAIN_NAGIOS_DIR, delete=False) as new_cf:
            for line in cf:
                new_cf.write(line.replace(find_me, replacement))
            new_cf.flush()
            os.chmod(new_cf.name, 0644)
            os.unlink(MAIN_NAGIOS_CFG)
            os.rename(new_cf.name, MAIN_NAGIOS_CFG)


def initialize_inprogress_config():
    if os.path.exists(INPROGRESS_DIR):
        shutil.rmtree(INPROGRESS_DIR)
    shutil.copytree(MAIN_NAGIOS_DIR, INPROGRESS_DIR)
    _replace_in_config(MAIN_NAGIOS_DIR, INPROGRESS_DIR)
    if os.path.exists(CHARM_CFG):
        os.unlink(CHARM_CFG)


def flush_inprogress_config():
    if not os.path.exists(INPROGRESS_DIR):
        return
    if os.path.exists(MAIN_NAGIOS_BAK):
        shutil.rmtree(MAIN_NAGIOS_BAK)
    if os.path.exists(MAIN_NAGIOS_DIR):
        shutil.move(MAIN_NAGIOS_DIR, MAIN_NAGIOS_BAK)
    shutil.move(INPROGRESS_DIR, MAIN_NAGIOS_DIR)
    # now that directory has been changed need to update the config file to reflect the real stuff..
    _commit_in_config(INPROGRESS_DIR, MAIN_NAGIOS_DIR)