# Copyright 2015 Canonical Ltd.
#
# This file is part of the PostgreSQL Charm for Juju.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import glob
import os.path
from charmhelpers import context
from charmhelpers.core import hookenv, host, templating
from charms.reactive.decorators import when
from charms.reactive.helpers import data_changed
from charms.reactive.bus import set_state, remove_state
@when('syslog.available', 'postgresql.cluster.configured')
def configure_syslog(syslog, pgsql):
loggers = syslog.get_loggers()
log_line_prefix = pgsql.get_log_line_prefix()
state = {'loggers': loggers, 'llp': log_line_prefix}
if not data_changed('syslog.state', state):
return
programname = hookenv.local_unit().replace('/', '_')
for remote_unit, remote_addr in loggers.items():
conf_path = _rsyslog_conf_path(remote_unit)
templating.render('rsyslog_forward.conf', conf_path,
dict(local_unit=hookenv.local_unit(),
remote_unit=remote_unit,
remote_addr=remote_addr,
programname=programname))
syslog.send_config(remote_unit, programname, log_line_prefix)
set_state('syslog.needs_restart')
def _rsyslog_conf_path(self, remote_unit):
# Use both the local unit and remote unit in the config file
# path to avoid conflicts with subordinates.
rsyslog_conf_dir = '/etc/rsyslog.d'
local = hookenv.local_unit().replace('/', '_')
remote = remote_unit.replace('/', '_')
return os.path.join(rsyslog_conf_dir,
'juju-{}-{}.conf'.format(local, remote))
@when('syslog.needs_restart')
def restart_rsyslog():
host.service_restart('rsyslog')
remove_state('syslog.needs_restart')