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

Add --delete option to push command #9

Merged
merged 2 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ Pushes the local monitors to datadog:
- will create new monitors
- will update existing monitors (so it could override what you were doing if
you edit an existing monitor in datadog)
- will *never* remove or touch untracked monitors (that is, datadog monitors
that are not any of the yaml files).
- will not remove or touch untracked monitors (that is, datadog monitors
that are not in any of the yaml files) unless the `--delete` flag is
passed in.

This command can run from a cronjob to ensure the monitors on DataDog are
synchronized with the local monitors.
Expand Down
30 changes: 20 additions & 10 deletions dogpush/dogpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ def _is_changed(local, remote):
return local['obj'] != remote['obj']


def command_init():
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)


def command_push():
def command_push(args):
local_monitors = get_local_monitors()
remote_monitors = get_datadog_monitors()

Expand All @@ -233,12 +233,20 @@ def command_push():
changed = [name for name in common_names
if _is_changed(local_monitors[name], remote_monitors[name])]
if changed:
print "Updating %d modified alerts" % len(changed)
print "Updating %d modified monitors." % len(changed)
for name in changed:
datadog.api.Monitor.update(
remote_monitors[name]['id'],
**_prepare_monitor(local_monitors[name]))

if args.delete:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

remote_monitors = get_datadog_monitors()
untracked = set(remote_monitors.keys()) - set(local_monitors.keys())
if untracked:
print "Deleting %d untracked monitors." % len(untracked)
for monitor in untracked:
datadog.api.Monitor.delete(remote_monitors[monitor]['id'])


def _should_mute(expr, tz, now):
return eval(expr, {}, {'now': now.astimezone(tz)})
Expand All @@ -261,7 +269,7 @@ def _mute_until(expr, tz, now):
return now


def command_mute():
def command_mute(args):
local_monitors = get_local_monitors()
remote_monitors = get_datadog_monitors()
mute_tags = {}
Expand Down Expand Up @@ -292,7 +300,7 @@ def command_mute():
mute_until['datetime'])


def command_diff():
def command_diff(args):
local_monitors = get_local_monitors()
remote_monitors = get_datadog_monitors()

Expand Down Expand Up @@ -358,26 +366,28 @@ def command_diff():
formatter_class=argparse.ArgumentDefaultsHelpFormatter)


parser.add_argument('--config', '-c',
parser.add_argument('-c', '--config',
default=os.path.join('.', 'config.yaml'),
help='configuration file to load')

subparsers = parser.add_subparsers(help='sub-command help')


parser_push = subparsers.add_parser(
'init', help='init new alerts file')
'init', help='Init new alerts file')
parser_push.set_defaults(command=command_init)


parser_push = subparsers.add_parser(
'push', help='push monitors to datadog')
'push', help='Push monitors to DataDog.')
parser_push.add_argument('-d', '--delete', action='store_true',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete_untracked

help='Delete untracked monitors.')
parser_push.set_defaults(command=command_push)


parser_diff = subparsers.add_parser(
'diff',
help='show diff between local monitors and datadog')
help='Show diff between local monitors and DataDog')
parser_diff.set_defaults(command=command_diff)


Expand All @@ -394,7 +404,7 @@ def command_diff():

def main():
datadog.initialize(**CONFIG['datadog'])
args.command()
args.command(args)


if __name__ == '__main__':
Expand Down