~brad-marshall/charms/trusty/postgresql/fix-cron-backups

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Stuart Bishop
  • Date: 2014-11-11 06:08:02 UTC
  • mfrom: (101.4.3 metrics)
  • Revision ID: stuart@stuartbishop.net-20141111060802-jlmlm82tsq0papg9
[james-w] Publish statsd metrics, per https://code.launchpad.net/~james-w/charms/precise/postgresql/metrics/+merge/231322

Show diffs side-by-side

added added

removed removed

Lines of Context:
92
92
    return host.lsb_release()['DISTRIB_CODENAME']
93
93
 
94
94
 
 
95
def render_template(template_name, vars):
 
96
    # deferred import so install hook can install jinja2
 
97
    templates_dir = os.path.join(os.environ['CHARM_DIR'], 'templates')
 
98
    template_env = Environment(loader=FileSystemLoader(templates_dir))
 
99
    template = template_env.get_template(template_name)
 
100
    return template.render(vars)
 
101
 
 
102
 
95
103
class State(dict):
96
104
    """Encapsulate state common to the unit for republishing to relations."""
97
105
    def __init__(self, state_file):
1115
1123
    create_wal_e_envdir()
1116
1124
    update_service_port()
1117
1125
    update_nrpe_checks()
 
1126
    write_metrics_cronjob('/usr/local/bin/postgres_to_statsd.py',
 
1127
        '/etc/cron.d/postgres_metrics')
1118
1128
 
1119
1129
    # If an external mountpoint has caused an old, existing DB to be
1120
1130
    # mounted, we need to ensure that all the users, databases, roles
2393
2403
        units, lambda a, b: cmp(int(a.split('/')[-1]), int(b.split('/')[-1])))
2394
2404
 
2395
2405
 
 
2406
def delete_metrics_cronjob(cron_path):
 
2407
    try:
 
2408
        os.unlink(cron_path)
 
2409
    except OSError:
 
2410
        pass
 
2411
 
 
2412
 
 
2413
def write_metrics_cronjob(script_path, cron_path):
 
2414
    config_data = hookenv.config()
 
2415
 
 
2416
    # need the following two configs to be valid
 
2417
    metrics_target = config_data['metrics_target'].strip()
 
2418
    metrics_sample_interval = config_data['metrics_sample_interval']
 
2419
    if (not metrics_target
 
2420
            or ':' not in metrics_target
 
2421
            or not metrics_sample_interval):
 
2422
        log("Required config not found or invalid "
 
2423
            "(metrics_target, metrics_sample_interval), "
 
2424
            "disabling metrics", WARNING)
 
2425
        delete_metrics_cronjob(cron_path)
 
2426
        return
 
2427
 
 
2428
    charm_dir = os.environ['CHARM_DIR']
 
2429
    statsd_host, statsd_port = metrics_target.split(':', 1)
 
2430
    metrics_prefix = config_data['metrics_prefix'].strip()
 
2431
    metrics_prefix = metrics_prefix.replace(
 
2432
        "$UNIT", hookenv.local_unit().replace('.', '-').replace('/', '-'))
 
2433
 
 
2434
    # ensure script installed
 
2435
    host.write_file(
 
2436
        os.path.join(charm_dir, 'files', 'metrics', 'postgres_to_statsd.py'),
 
2437
        open(script_path, 'rb').read())
 
2438
 
 
2439
    # write the crontab
 
2440
    with open(cron_path, 'w') as cronjob:
 
2441
        cronjob.write(render_template("metrics_cronjob.template", {
 
2442
            'interval': config_data['metrics_sample_interval'],
 
2443
            'script': script_path,
 
2444
            'metrics_prefix': metrics_prefix,
 
2445
            'metrics_sample_interval': metrics_sample_interval,
 
2446
            'statsd_host': statsd_host,
 
2447
            'statsd_port': statsd_port,
 
2448
        }))
 
2449
 
 
2450
 
2396
2451
@hooks.hook('nrpe-external-master-relation-changed')
2397
2452
def update_nrpe_checks():
2398
2453
    config_data = hookenv.config()
2423
2478
            os.remove(os.path.join('/var/lib/nagios/export/', f))
2424
2479
 
2425
2480
    # --- exported service configuration file
2426
 
    template_env = Environment(
2427
 
        loader=FileSystemLoader(
2428
 
            os.path.join(os.environ['CHARM_DIR'], 'templates')))
2429
2481
    templ_vars = {
2430
2482
        'nagios_hostname': nagios_hostname,
2431
2483
        'nagios_servicegroup': config_data['nagios_context'],
2432
2484
    }
2433
 
    template = \
2434
 
        template_env.get_template('nrpe_service.tmpl').render(templ_vars)
 
2485
    template = render_template('nrpe_service.tmpl', templ_vars)
2435
2486
    with open(nrpe_service_file, 'w') as nrpe_service_config:
2436
2487
        nrpe_service_config.write(str(template))
2437
2488