Skip to content
This repository was archived by the owner on May 5, 2022. It is now read-only.
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
91 changes: 47 additions & 44 deletions dogpush/dogpush.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env python

from __future__ import print_function
from six import string_types

import argparse
import calendar
import copy
Expand All @@ -15,7 +18,7 @@
import pytz
import yaml

import bcolors
from .bcolors import *


PROGNAME = 'dogpush'
Expand Down Expand Up @@ -97,8 +100,8 @@ def _canonical_monitor(original, default_team=None, **kwargs):
m.pop(field, None)
for field in IGNORE_OPTIONS:
m.get('options', {}).pop(field, None)
option_defaults = (DATADOG_DEFAULT_OPTIONS.items() +
CONFIG['default_rule_options'].items())
option_defaults = (list(DATADOG_DEFAULT_OPTIONS.items()) +
list(CONFIG['default_rule_options'].items()))
for (field, value) in option_defaults:
if m.get('options', {}).get(field) == value:
del m['options'][field]
Expand All @@ -114,7 +117,7 @@ def _canonical_monitor(original, default_team=None, **kwargs):
original_team = original.get('team')
team = original_team if original_team is not None else default_team
if team:
if isinstance(team, basestring):
if isinstance(team, string_types):
team = [team]
m['message'] = m.get('message', '')
dogpush_line = get_notify_string(team)
Expand Down Expand Up @@ -181,14 +184,14 @@ def _check_monitor_names_unique(monitors):
for name in names:
counts[name] = counts.get(name, 0) + 1
if counts[name] > 1:
print >> sys.stderr, "Duplicate name: %s" % name
print("Duplicate name: %s" % name, file=sys.stderr)
return False
return True


def _check_monitor(monitor, location):
name = monitor.get('name', '')
if isinstance(name, basestring):
if isinstance(name, string_types):
name = name.strip()
if not name:
raise DogPushException('%s: found monitor without a name' % location)
Expand Down Expand Up @@ -244,9 +247,9 @@ def _is_changed(local, remote):
def command_init(args):
remote_monitors = [m['obj'] for m in get_datadog_monitors().values()]
monitors = {'alerts': remote_monitors}
print '# team: TEAMNAME'
print
print _pretty_yaml(monitors)
print('# team: TEAMNAME')
print('')
print(_pretty_yaml(monitors))


def command_push(args):
Expand All @@ -255,15 +258,15 @@ def command_push(args):

only_local = set(local_monitors.keys()) - set(remote_monitors.keys())
if only_local:
print "Pushing %d new monitors." % len(only_local)
print("Pushing %d new monitors." % len(only_local))
for name in only_local:
datadog.api.Monitor.create(**_prepare_monitor(local_monitors[name]))

common_names = set(local_monitors.keys()) & set(remote_monitors.keys())
changed = [name for name in common_names
if _is_changed(local_monitors[name], remote_monitors[name])]
if changed:
print "Updating %d modified monitors." % len(changed)
print("Updating %d modified monitors." % len(changed))
for name in changed:
datadog.api.Monitor.update(
remote_monitors[name]['id'],
Expand All @@ -273,7 +276,7 @@ def command_push(args):
remote_monitors = get_datadog_monitors()
untracked = set(remote_monitors.keys()) - set(local_monitors.keys())
if untracked:
print "Deleting %d untracked monitors." % len(untracked)
print("Deleting %d untracked monitors." % len(untracked))
for monitor in untracked:
datadog.api.Monitor.delete(remote_monitors[monitor]['id'])

Expand Down Expand Up @@ -320,14 +323,14 @@ def command_mute(args):
if monitor['mute_when'] and remote_monitors.has_key(monitor['name']):
remote = remote_monitors[monitor['name']]
if remote['is_silenced']:
print "Alert '%s' is already muted. Skipping." % monitor['name']
print("Alert '%s' is already muted. Skipping." % monitor['name'])
continue
mute_until = mute_tags[monitor['mute_when']]
if mute_until:
id = remote['id']
datadog.api.Monitor.mute(id, end=mute_until['timestamp'])
print "Muting alert '%s' until %s" % (monitor['name'],
mute_until['datetime'])
print("Muting alert '%s' until %s" % (monitor['name'],
mute_until['datetime']))


def command_diff(args):
Expand All @@ -342,23 +345,23 @@ def command_diff(args):
remote_monitors[name])]

if only_local:
sys.stdout.write(bcolors.WARNING)
print '---------------------------------------------------------'
print ' NEW MONITORS. These monitors are currently missing in'
print ' datadog and can be pushed using "%s push"' % PROGNAME
print '---------------------------------------------------------'
sys.stdout.write(bcolors.ENDC)
sys.stdout.write(WARNING)
print('---------------------------------------------------------')
print(' NEW MONITORS. These monitors are currently missing in')
print(' datadog and can be pushed using "%s push"' % PROGNAME)
print('---------------------------------------------------------')
sys.stdout.write(ENDC)
monitors = [local_monitors[name]['obj'] for name in only_local]
print _pretty_yaml(monitors)
print(_pretty_yaml(monitors))
if changed:
sys.stdout.write(bcolors.WARNING)
print '---------------------------------------------------------'
print ' TO BE UPDATED. These monitors exist in datadog, but are'
print ' different than the local version. Use "%s push"' % PROGNAME
print ' to push them to datadog.'
print '---------------------------------------------------------'
print
sys.stdout.write(bcolors.ENDC)
sys.stdout.write(WARNING)
print('---------------------------------------------------------')
print(' TO BE UPDATED. These monitors exist in datadog, but are')
print(' different than the local version. Use "%s push"' % PROGNAME)
print(' to push them to datadog.')
print('---------------------------------------------------------')
print('')
sys.stdout.write(ENDC)
for name in changed:
remote_name = 'datadog:%s' % name
local_name = '%s:%s' % (local_monitors[name]['filename'], name)
Expand All @@ -367,26 +370,26 @@ def command_diff(args):
_pretty_yaml(local_monitors[name]['obj']).splitlines(True),
fromfile=remote_name, tofile=local_name):
if line.startswith('---') or line.startswith('+++'):
sys.stdout.write(bcolors.BOLD + line + bcolors.ENDC)
sys.stdout.write(BOLD + line + ENDC)
elif line.startswith('-'):
sys.stdout.write(bcolors.RED + line + bcolors.ENDC)
sys.stdout.write(RED + line + ENDC)
elif line.startswith('+'):
sys.stdout.write(bcolors.GREEN + line + bcolors.ENDC)
sys.stdout.write(GREEN + line + ENDC)
else:
sys.stdout.write(line)
if only_remote and not args.ignore_untracked:
sys.stdout.write(bcolors.WARNING)
print '------------------------------------------------------------'
print ' UNTRACKED MONITORS. These monitors are only in datadog '
print ' and needed to be MANUALLY added to a local file or removed '
print ' from datadog. '
print '------------------------------------------------------------'
sys.stdout.write(bcolors.ENDC)
sys.stdout.write(WARNING)
print('------------------------------------------------------------')
print(' UNTRACKED MONITORS. These monitors are only in datadog ')
print(' and needed to be MANUALLY added to a local file or removed ')
print(' from datadog. ')
print('------------------------------------------------------------')
sys.stdout.write(ENDC)
monitors = [remote_monitors[name]['obj'] for name in only_remote]
print _pretty_yaml(monitors)
sys.stdout.write(bcolors.FAIL)
print "*** FAILED *** Untracked monitors found."
sys.stdout.write(bcolors.ENDC)
print(_pretty_yaml(monitors))
sys.stdout.write(FAIL)
print("*** FAILED *** Untracked monitors found.")
sys.stdout.write(ENDC)
if args.exit_status and any((only_local, changed, only_remote and not args.ignore_untracked)):
sys.exit(1)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PyYAML==3.11
datadog==0.10.0
pytz==2015.7

six