1
# Copyright 2015 Canonical Ltd.
3
# This file is part of the PostgreSQL Charm for Juju.
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.
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.
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/>.
19
from charmhelpers import context
20
from charmhelpers.contrib.charmsupport.nrpe import NRPE
21
from charmhelpers.core import host
23
from decorators import data_ready_action, leader_only, master_only
28
def nagios_username():
34
def ensure_nagios_credentials():
35
leader = context.Leader()
36
if 'nagios_password' not in leader:
37
leader['nagios_password'] = host.pwgen()
42
def ensure_nagios_user():
43
leader = context.Leader()
44
con = postgresql.connect()
45
postgresql.ensure_user(con, nagios_username(), leader['nagios_password'])
49
def nagios_pgpass_path():
50
return os.path.expanduser('~nagios/.pgpass')
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')
63
def update_nrpe_config():
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))
72
# TODO: These should be calcualted from the backup schedule,
73
# which is difficult since that is specified in crontab format.
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)))