Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Refactor severity to use monitor thresholds #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,28 @@ default_rule_options:

### The teams section

Teams in DogPush are just a way to append some text to the message body of a
monitor so it will grab the attention of the right people. By defining your
Teams in DogPush are a way to append notification methods in a conditional fashion
to so it will grab the attention of the right people. By defining your
teams in the global config, it is super easy to add these @-mentions to all
your monitors. For example,
```yaml
teams:
eng:
notifications:
CRITICAL: '@hipchat-Engineering @victorops-eng'
WARNING: '@[email protected]'
alert: '@hipchat-Engineering @victorops-eng'
warning: '@[email protected]'
ops:
notifications:
CRITICAL: '@hipchat-Ops'
alert: '@hipchat-Ops'
```

means that any monitor of severity `CRITICAL` to the `eng` team will have the
'@hipchat-Engineering @victorops-eng' appending to its message body.
means that when an monitor metric reaches the alert threshold the `eng` team
will be notfied via '@hipchat-Engineering @victorops-eng'.

Then, in a rules file, you can have a top level `team` setting (making all
the alerts automatically go to that team), or specify 'team' at the alert
level.

The default severity is `CRITICAL`, and you can override the severity in each
monitor by using the `severity` key.

### Muting alerts based on time windows

DogPush supports automatically muting alerts. A common use case is to mute
Expand Down Expand Up @@ -180,9 +177,9 @@ dogpush:
ignore_prefix: 'string'
```

The `yaml_width` option sets the line width of the generated yaml output.
The `yaml_width` option sets the line width of the generated yaml output.

Using `ignore_prefix` one can define a set of monitor names that are
Using `ignore_prefix` one can define a set of monitor names that are
simply ignored by DogPush when fetching the remote monitors.

## Rule files
Expand Down
10 changes: 7 additions & 3 deletions config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ datadog:
teams:
eng:
notifications:
CRITICAL: '@hipchat-Engineering @victorops-eng'
WARNING: '@[email protected]'
alert:
- '@hipchat-Engineering'
- '@victorops-eng'
warning:
- '@[email protected]'
ops:
notifications:
CRITICAL: '@hipchat-Ops'
alert:
- '@hipchat-Ops'

mute_tags:
not_business_hours:
Expand Down
34 changes: 27 additions & 7 deletions dogpush/dogpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ def _load_config(config_file):
return config


SEVERITIES = ['CRITICAL', 'WARNING', 'INFO']


SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))


Expand Down Expand Up @@ -118,14 +115,12 @@ def _canonical_monitor(original, default_team=None, **kwargs):
m['name'] = m['name'].strip()
original_team = original.get('team')
team = original_team if original_team is not None else default_team
severity = original.get('severity') or 'CRITICAL'
if team:
if isinstance(team, basestring):
team = [team]
m['message'] = m.get('message', '')
for t in team:
dogpush_line = CONFIG['teams'][t]['notifications'][severity]
m['message'] += ('\n' if m['message'] else '') + dogpush_line
dogpush_line = get_notify_string(team)
m['message'] += ('\n' if m['message'] else '') + dogpush_line

result = dict(
name = m['name'],
Expand All @@ -138,6 +133,31 @@ def _canonical_monitor(original, default_team=None, **kwargs):
return result


def get_notify_string(teams):
"""Build notification string."""
is_warning = ''
is_alert = ''
is_recovery = ''
notification_items = {'warning': [], 'alert': [], 'recovery': []}
for team in teams:
for notification in ['warning', 'alert']:
try:
for item in CONFIG['teams'][team]['notifications'][notification]:
if item not in notification_items[notification]:
notification_items[notification].append(item)
if item not in notification_items['recovery']:
notification_items['recovery'].append(item)
except KeyError:
pass
for warning in notification_items['warning']:
is_warning += " {}\n".format(warning)
for alert in notification_items['alert']:
is_alert += " {}\n".format(alert)
for recovery in notification_items['recovery']:
is_recovery += " {}\n".format(recovery)
return "{{#is_warning}}\n{}{{/is_warning}}\n{{#is_alert}}\n{}{{/is_alert}}\n{{#is_recovery}}\n{}{{/is_recovery}}".format(is_warning, is_alert, is_recovery)


def get_datadog_monitors():
monitors = datadog.api.Monitor.get_all(with_downtimes="true")
if CONFIG['dogpush']['ignore_prefix'] is not None:
Expand Down