Skip to content

Commit

Permalink
Merge pull request #19 from votdev/improve_notification_parsing
Browse files Browse the repository at this point in the history
Various improvements
  • Loading branch information
votdev authored Mar 25, 2020
2 parents 4ca279a + dbaa977 commit 773a379
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v1.3

* Add 'trap-default-severity' configuration option.
* Harden the notification parser.

v1.2

* Show used configuration settings in debug mode.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ The OID prefix for trap variable bindings. Defaults to ``1.3.6.1.4.1.50495.15``.
### --trap-default-oid
The trap OID if none is found in the Prometheus alert labels. Defaults to ``1.3.6.1.4.1.50495.15.1.2.1``.
### --trap-default-severity
The trap severity if none is found in the Prometheus alert labels. Defaults to an empty string.
## Command ``run``
### --host
Expand Down
8 changes: 6 additions & 2 deletions prometheus-webhook-snmp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import click
from prometheus_webhook_snmp import utils


__version__ = "1.2"
__version__ = "1.3"


pass_context = click.make_pass_decorator(utils.Context, ensure=True)
Expand All @@ -35,10 +35,13 @@ pass_context = click.make_pass_decorator(utils.Context, ensure=True)
help='The OID prefix for trap variable bindings.')
@click.option('--trap-default-oid',
help='The trap OID if none is found in the Prometheus alert labels.')
@click.option('--trap-default-severity',
help='The trap severity if none is found in the Prometheus alert labels.')
@click.version_option(__version__, message="%(version)s")
@pass_context
def cli(ctx, debug, snmp_host, snmp_port, snmp_community, snmp_retries,
snmp_timeout, alert_oid_label, trap_oid_prefix, trap_default_oid):
snmp_timeout, alert_oid_label, trap_oid_prefix, trap_default_oid,
trap_default_severity):
ctx.config.load(click.get_current_context().info_name)
ctx.config['debug'] = True if debug else None
ctx.config['snmp_host'] = snmp_host
Expand All @@ -48,6 +51,7 @@ def cli(ctx, debug, snmp_host, snmp_port, snmp_community, snmp_retries,
ctx.config['snmp_timeout'] = snmp_timeout
ctx.config['alert_oid_label'] = alert_oid_label
ctx.config['trap_default_oid'] = trap_default_oid
ctx.config['trap_default_severity'] = trap_default_severity
ctx.config['trap_oid_prefix'] = trap_oid_prefix

if ctx.config['debug']:
Expand Down
4 changes: 2 additions & 2 deletions prometheus-webhook-snmp.spec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# spec file for package prometheus-webhook-snmp
#
# Copyright (c) 2019 SUSE LINUX GmbH, Nuernberg, Germany.
# Copyright (c) 2019-2020 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
Expand All @@ -15,7 +15,7 @@
# Please submit bugfixes or comments via http://bugs.opensuse.org/

Name: prometheus-webhook-snmp
Version: 1.2
Version: 1.3
Release: 0
Summary: Prometheus Alertmanager receiver for SNMP traps
License: GPL-3.0
Expand Down
35 changes: 33 additions & 2 deletions prometheus_webhook_snmp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@

def parse_notification(config, notification):
"""
Parse the Alertmanager notification.
Example:
{
"receiver": "storage",
"status": "firing",
"alerts": [
{
"status": "firing",
"labels": {
"alertname": "OSD down",
"monitor": "ses",
"oid": "1.3.6.1.4.1.50495.15.1.2.4.2",
"severity": "warning",
"type": "ceph_default"
},
"annotations": {
"description": "One or more OSDs down for more than 15 minutes."
},
"startsAt": "2020-03-24T10:25:29.689078708-04:00",
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "http://xxxx:9090/graph?g0.expr=..."
}
],
...
}
See https://prometheus.io/docs/alerting/configuration/#webhook_config
:param config: The configuration data.
:type config: dict
:param notification:
Expand Down Expand Up @@ -54,9 +83,10 @@ def parse_notification(config, notification):
config['trap_default_oid']),
'alertname': alertname,
'status': alert['status'],
'severity': labels.pop('severity'),
'severity': labels.pop('severity',
config['trap_default_severity']),
'instance': labels.pop('instance', None),
'job': labels.pop('job'),
'job': labels.pop('job', None),

This comment has been minimized.

Copy link
@harshalk91

harshalk91 Apr 1, 2020

I faced an issue where alertmanager json didn't contain "job" in one of alert for a kubernetes container. So ultimately it i received error like Key error. So i would like to understand use of 'None' parameter over here

This comment has been minimized.

Copy link
@votdev

votdev Apr 1, 2020

Author Member

If the attribute job is not in the labels dictinary, then None will become the default value which results in an empty sting in https://github.com/SUSE/prometheus-webhook-snmp/blob/master/prometheus_webhook_snmp/utils.py#L116.

'description': summary or description,
'labels': labels,
'timestamp': timestamp,
Expand Down Expand Up @@ -184,6 +214,7 @@ def defaults():
'alert_oid_label': 'oid',
'trap_oid_prefix': '1.3.6.1.4.1.50495.15',
'trap_default_oid': '1.3.6.1.4.1.50495.15.1.2.1',
'trap_default_severity': '',
'host': '0.0.0.0',
'port': 9099,
'metrics': False
Expand Down

0 comments on commit 773a379

Please sign in to comment.