~bloodearnest/charms/trusty/postgresql/trunk

« back to all changes in this revision

Viewing changes to hooks/nagios.py

  • Committer: Matthew Bruzek
  • Date: 2015-11-11 19:37:09 UTC
  • mfrom: (121.4.130 rewrite)
  • Revision ID: matthew.bruzek@canonical.com-20151111193709-fn9vb1jd865frl9t
[stub] A rewrite to make use of modern Juju and improve the reliability of the postgresql charm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2015 Canonical Ltd.
 
2
#
 
3
# This file is part of the PostgreSQL Charm for Juju.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License version 3, as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
import os.path
 
18
 
 
19
from charmhelpers import context
 
20
from charmhelpers.contrib.charmsupport.nrpe import NRPE
 
21
from charmhelpers.core import host
 
22
 
 
23
from decorators import data_ready_action, leader_only, master_only
 
24
import helpers
 
25
import postgresql
 
26
 
 
27
 
 
28
def nagios_username():
 
29
    return '_juju_nagios'
 
30
 
 
31
 
 
32
@leader_only
 
33
@data_ready_action
 
34
def ensure_nagios_credentials():
 
35
    leader = context.Leader()
 
36
    if 'nagios_password' not in leader:
 
37
        leader['nagios_password'] = host.pwgen()
 
38
 
 
39
 
 
40
@master_only
 
41
@data_ready_action
 
42
def ensure_nagios_user():
 
43
    leader = context.Leader()
 
44
    con = postgresql.connect()
 
45
    postgresql.ensure_user(con, nagios_username(), leader['nagios_password'])
 
46
    con.commit()
 
47
 
 
48
 
 
49
def nagios_pgpass_path():
 
50
    return os.path.expanduser('~nagios/.pgpass')
 
51
 
 
52
 
 
53
@data_ready_action
 
54
def update_nagios_pgpass():
 
55
    leader = context.Leader()
 
56
    nagios_password = leader['nagios_password']
 
57
    content = '*:*:*:{}:{}'.format(nagios_username(), nagios_password)
 
58
    helpers.write(nagios_pgpass_path(), content,
 
59
                  mode=0o600, user='nagios', group='nagios')
 
60
 
 
61
 
 
62
@data_ready_action
 
63
def update_nrpe_config():
 
64
    nrpe = NRPE()
 
65
 
 
66
    user = nagios_username()
 
67
    port = postgresql.port()
 
68
    nrpe.add_check(shortname='pgsql',
 
69
                   description='Check pgsql',
 
70
                   check_cmd='check_pgsql -P {} -l {}'.format(port, user))
 
71
 
 
72
    # TODO: These should be calcualted from the backup schedule,
 
73
    # which is difficult since that is specified in crontab format.
 
74
    warn_age = 172800
 
75
    crit_age = 194400
 
76
    backups_log = helpers.backups_log_path()
 
77
    nrpe.add_check(shortname='pgsql_backups',
 
78
                   description='Check pgsql backups',
 
79
                   check_cmd=('check_file_age -w {} -c {} -f {}'
 
80
                              ''.format(warn_age, crit_age, backups_log)))
 
81
    nrpe.write()