From 7f2be8a4112d9d99a782fa37abe4c0ff1e8c2535 Mon Sep 17 00:00:00 2001 From: Chris LaPointe Date: Tue, 27 Aug 2019 22:56:59 -0400 Subject: [PATCH] Prometheus (#11) * Begin adding prometheus metric support * Deeply navigate all metrics for prometheus * Readme blurb --- README.md | 8 ++++++++ sshsysmon/templates/__init__.py | 29 ++++++++++++++++++++++++++++- sshsysmon/templates/prometheus.hb | 10 ++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 sshsysmon/templates/prometheus.hb diff --git a/README.md b/README.md index 64153c6..0631603 100644 --- a/README.md +++ b/README.md @@ -437,6 +437,14 @@ The following types are supported: SshSysMon uses handlebars to template its summary output. See the [templating](/templates) for more information. +#### Prometheus + +SshSysMon supports writing to a format supported by prometheus, which can in-turn be pushed to a pushgateway via a pipe. + +```sh +sshmon -f prometheus myconfig.yml | curl --data-binary @- http://prometheus.example.com/metrics/job/sshmon +``` + ### Writing Your Own Component To learn how to write a specific type of component, visit its readme in the appropriate subfolder. diff --git a/sshsysmon/templates/__init__.py b/sshsysmon/templates/__init__.py index a2dc10c..c8b22e6 100644 --- a/sshsysmon/templates/__init__.py +++ b/sshsysmon/templates/__init__.py @@ -29,12 +29,39 @@ def __coalesce(this, *args): def __alphanum(this, val, replaceWith='_'): return sanitize(val, replaceWith) +def __numeric(this, val): + if hasattr(val, '__float__'): + return float(val) + if hasattr(val, '__int__'): + return int(val) + return 1 if val else 0 + +def __deepEach(this, options, obj): + results = [] + def deepWalk(obj, address): + if isinstance(obj, list): + for i in range(len(obj)): + deepWalk(obj[i], '{}[{}]'.format(address, i)) + elif isinstance(obj, dict): + for k, v in obj.items(): + deepWalk(v, k if not address else '{}.{}'.format(address, k)) + else: + kwargs = { + 'key': address, + } + scope = pybars.Scope(obj, this, options['root'], **kwargs) + results.extend(options['fn'](scope)) + deepWalk(obj, '') + return results + __helpers = { 'ifEq' : __ifEq, 'replace' : __replace, 'coalesce' : __coalesce, 'alphanum': __alphanum, - 'format': __format + 'format': __format, + 'numeric': __numeric, + 'deepEach': __deepEach, } def __template(src, data): diff --git a/sshsysmon/templates/prometheus.hb b/sshsysmon/templates/prometheus.hb new file mode 100644 index 0000000..ab6cb44 --- /dev/null +++ b/sshsysmon/templates/prometheus.hb @@ -0,0 +1,10 @@ +# TYPE sshmon_alert gauge +{{#servers}}{{#inspectors}}{{#alarms}} +sshmon_alert{server="{{alphanum ../../name}}",inspector="{{alphanum ../type}}",alarm="{{alphanum name}}"} {{#if fired}}1{{else}}0{{/if}} +{{! new line intentional }} +{{/alarms}}{{/inspectors}}{{/servers}} +# TYPE sshmon_metric gauge +{{#servers}}{{#inspectors}}{{#deepEach metrics}} +sshmon_metric{server="{{alphanum ../../name}}",inspector="{{alphanum ../type}}",name="{{alphanum @key}}"} {{numeric this}} +{{! intentionally blank}} +{{/deepEach}}{{/inspectors}}{{/servers}}