Merge ~paulgear/influxdb-charm/+git/influxdb-charm:master into influxdb-charm:master

Proposed by Paul Gear
Status: Merged
Approved by: Haw Loeung
Approved revision: 5d7f64f53bf5fdaeed9b2183cb69004df1251b43
Merged at revision: ebd439eaeaf8a42422a9626a869aa2d6e1a18b69
Proposed branch: ~paulgear/influxdb-charm/+git/influxdb-charm:master
Merge into: influxdb-charm:master
Diff against target: 95 lines (+51/-1)
3 files modified
files/check_influxdb.py (+24/-0)
layer.yaml (+1/-0)
reactive/influxdb.py (+26/-1)
Reviewer Review Type Date Requested Status
Haw Loeung Approve
Review via email: mp+332691@code.launchpad.net

Description of the change

Add a basic Nagios check for InfluxDB RT#106501

To post a comment you must log in.
Revision history for this message
Haw Loeung (hloeung) wrote :

LGTM, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/files/check_influxdb.py b/files/check_influxdb.py
2new file mode 100755
3index 0000000..5ab579b
4--- /dev/null
5+++ b/files/check_influxdb.py
6@@ -0,0 +1,24 @@
7+#!/usr/bin/python3
8+
9+# Copyright (c) 2017 Canonical Ltd
10+# Author: Paul Gear
11+# Description: Run influxdb 'show diagnostics' command to ensure influxdb is running and operational
12+
13+import json
14+import subprocess
15+import sys
16+import traceback
17+
18+try:
19+ output = subprocess.check_output(['influx', '-execute', 'show diagnostics', '-format', 'json'])
20+ j = json.loads(output.decode('utf-8'))
21+ for o in j['results'][0]['series']:
22+ if o['name'] == 'system' and o['columns'][-1] == 'uptime':
23+ print('OK: InfluxDB uptime is {}'.format(o['values'][0][-1]))
24+ sys.exit(0)
25+ print('CRITICAL: Unable to determine InfluxDB uptime')
26+ sys.exit(2)
27+except Exception as e:
28+ print('CRITICAL: Exception while attempting to check InfluxDB')
29+ traceback.print_exc()
30+ sys.exit(2)
31diff --git a/layer.yaml b/layer.yaml
32index 37d19f1..7a1dd2b 100644
33--- a/layer.yaml
34+++ b/layer.yaml
35@@ -2,6 +2,7 @@ repo: https://git.launchpad.net/influxdb-charm
36 includes:
37 - layer:basic
38 - layer:apt
39+ - layer:nagios
40 - interface:http
41 - interface:grafana-source
42 - interface:influxdb-api
43diff --git a/reactive/influxdb.py b/reactive/influxdb.py
44index 8895dd0..d7698cf 100644
45--- a/reactive/influxdb.py
46+++ b/reactive/influxdb.py
47@@ -1,11 +1,16 @@
48-from charmhelpers.core import hookenv
49+from charmhelpers.contrib.charmsupport import nrpe
50 from charmhelpers.core.host import service_start, service_stop
51+from charmhelpers.core import hookenv
52 from charmhelpers.core.templating import render
53 from charms.reactive import hook, set_state, when, when_any, when_not
54
55 import os
56+import shutil
57
58 CRONFILE = '/etc/cron.daily/influxdb-charm-backup'
59+CHECK_SRC = 'files/check_influxdb.py'
60+CHECK_DIR = '/usr/local/lib/nagios/plugins'
61+CHECK_CMD = CHECK_DIR + '/check_influxdb.py'
62
63
64 @when('admin.available')
65@@ -74,6 +79,9 @@ def upgrade_charm():
66 set_state('config.changed.backup_dir')
67 set_state('config.changed.bind_port')
68 set_state('config.changed.ip_address')
69+ # If we have an NRPE relation, force run of that as well
70+ if hookenv.relation_ids('nrpe-exteral-master'):
71+ set_state('nrpe-external-master.available')
72
73
74 @when('query.api.available')
75@@ -88,3 +96,20 @@ def configure_grafana(grafana):
76 config = hookenv.config()
77 port = config.get('bind_port')
78 grafana.provide('influxdb', port, 'Juju generated source')
79+
80+
81+@when('nrpe-external-master.available')
82+def update_nrpe_config(svc):
83+ # copy the check into place
84+ os.makedirs(CHECK_DIR, exist_ok=True)
85+ shutil.copy(CHECK_SRC, CHECK_CMD)
86+ os.chmod(CHECK_CMD, 0o755)
87+ # install the nrpe configuration
88+ hostname = nrpe.get_nagios_hostname()
89+ nrpe_setup = nrpe.NRPE(hostname=hostname)
90+ nrpe_setup.add_check(
91+ shortname='influxdb',
92+ description='Check InfluxDB',
93+ check_cmd=CHECK_CMD,
94+ )
95+ nrpe_setup.write()

Subscribers

People subscribed via source and target branches

to all changes: