Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.
Open
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
50 changes: 50 additions & 0 deletions dogpush/dogpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import re
import sys
import time

import datadog
import datadog.api
Expand All @@ -20,6 +21,19 @@

PROGNAME = 'dogpush'

# Colors in console
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'


class DogPushException(Exception):
pass
Expand Down Expand Up @@ -214,6 +228,15 @@ def _prepare_monitor(m):
return obj


def _prepare_to_validate(m):
obj = {
'type': m['type'],
'query': m['query']
}

return obj


def _is_changed(local, remote):
# For an alert with `mute_when`, we ignore silencing when comparing.
# TODO(nadavsr): rethink how silencing should affect monitors in general.
Expand All @@ -231,6 +254,29 @@ def command_init(args):
print _pretty_yaml(monitors)


def command_validate(args):
local_monitors = get_local_monitors()
errors = False
for name in local_monitors:
# pprint.pprint(_prepare_monitor(local_monitors[name]))
print "Checking monitor %s%s%s...\t\t" % (color.BOLD, name, color.END),
try:
datadog.api.base.HTTPClient.request('POST', '/monitor/validate', _prepare_to_validate(_prepare_monitor(local_monitors[name])))
print color.BOLD + color.GREEN + "OK" + color.END
except datadog.api.exceptions.ApiError as e:
errors = True
print color.BOLD + color.RED + "FAIL" + color.END
# Display error which has been returned
print "Error received: {}\n".format(e)

# Just a little sleep so we don't overload DD API with each call
time.sleep(0.1)

if errors:
sys.exit(1)



def command_push(args):
local_monitors = get_local_monitors()
remote_monitors = get_datadog_monitors()
Expand Down Expand Up @@ -411,6 +457,10 @@ def command_diff(args):
help='Diff will return 0 if there are differences. Default (true), return 1 for differences.')
parser_diff.set_defaults(command=command_diff)

parser_validate = subparsers.add_parser(
'validate',
help='Validate all local monitors agains Datadog API Monitor Validate')
parser_validate.set_defaults(command=command_validate)

parser_mute = subparsers.add_parser(
'mute',
Expand Down