Merge ~jacekn/charms/+source/telegraf:master into ~ubuntuone-hackers/charms/+source/telegraf:master

Proposed by Jacek Nykis
Status: Merged
Merged at revision: 97c41642bfc873bbe505e44a1076f87085c24006
Proposed branch: ~jacekn/charms/+source/telegraf:master
Merge into: ~ubuntuone-hackers/charms/+source/telegraf:master
Diff against target: 91 lines (+36/-3)
3 files modified
config.yaml (+6/-0)
reactive/telegraf.py (+25/-3)
templates/telegraf.conf.tmpl (+5/-0)
Reviewer Review Type Date Requested Status
Guillermo Gonzalez Approve
Review via email: mp+299326@code.launchpad.net

Description of the change

Add prometheus_output_port config option so that users can enable prometheus output without need to understand configuration format for telegraf. A few pep8/pyflakes fixes

To post a comment you must log in.
Revision history for this message
Guillermo Gonzalez (verterok) wrote :

Thanks, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/config.yaml b/config.yaml
2index 5ebfca3..e4ff9d5 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -57,6 +57,12 @@ options:
6 Override default hostname, if empty use os.Hostname()
7 Supports using UNIT_NAME as the value, and the charm will use a sanitized unit
8 name, e.g: service_name-0
9+ prometheus_output_port:
10+ type: string
11+ default: ""
12+ description: |
13+ If set prometheus output plugin will be configured to listen on the provided port.
14+ If set to string "default" the charm will use default port (9103)
15 inputs_config:
16 type: string
17 default: ""
18diff --git a/reactive/telegraf.py b/reactive/telegraf.py
19index 218e61c..c1d1730 100644
20--- a/reactive/telegraf.py
21+++ b/reactive/telegraf.py
22@@ -10,14 +10,12 @@ from charms.reactive import (
23 helpers,
24 when,
25 when_not,
26- when_file_changed,
27 set_state,
28 remove_state,
29- helpers,
30 )
31 from charms.reactive.bus import get_states
32
33-from charmhelpers.core import hookenv, host
34+from charmhelpers.core import hookenv, host, unitdata
35 from charmhelpers.core.templating import render
36 from charmhelpers.fetch import apt_install, apt_update, add_source
37
38@@ -149,6 +147,26 @@ def render_template(template, context):
39 return tmpl.render(**context)
40
41
42+def check_port(key, new_port):
43+ unitdata_key = '{}.port'.format(key)
44+ kv = unitdata.kv()
45+ if kv.get(unitdata_key) != new_port:
46+ hookenv.open_port(new_port)
47+ if kv.get(unitdata_key): # Dont try to close non existing ports
48+ hookenv.close_port(kv.get(unitdata_key))
49+ kv.set(unitdata_key, new_port)
50+
51+
52+def get_prometheus_port():
53+ config = hookenv.config()
54+ if not config.get('prometheus_output_port', False):
55+ return False
56+ if config.get('prometheus_output_port') == 'default':
57+ return 9103
58+ else:
59+ return int(config.get('prometheus_output_port'))
60+
61+
62 # States
63
64
65@@ -222,6 +240,10 @@ def configure_telegraf():
66 if os.path.exists(config_path):
67 os.unlink(config_path)
68 return
69+ if get_prometheus_port():
70+ context["prometheus_output_port"] = get_prometheus_port()
71+ check_port("prometheus_output", get_prometheus_port())
72+
73 hookenv.log("Updating main config file")
74 render(source='telegraf.conf.tmpl', templates_dir=get_templates_dir(),
75 target=config_path, context=context)
76diff --git a/templates/telegraf.conf.tmpl b/templates/telegraf.conf.tmpl
77index 31ab16f..31af98e 100644
78--- a/templates/telegraf.conf.tmpl
79+++ b/templates/telegraf.conf.tmpl
80@@ -59,6 +59,11 @@
81
82 {{ outputs }}
83
84+{% if prometheus_output_port %}
85+[[outputs.prometheus_client]]
86+ listen = ":{{ prometheus_output_port }}"
87+{%- endif %}
88+
89 ###############################################################################
90 # INPUTS #
91 ###############################################################################

Subscribers

People subscribed via source and target branches