Python datadog.api() Examples
The following are 30
code examples of datadog.api().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
datadog
, or try the search function
.
Example #1
Source File: datadog.py From airflow with Apache License 2.0 | 8 votes |
def poke(self, context): # This instantiates the hook, but doesn't need it further, # because the API authenticates globally (unfortunately), # but for airflow this shouldn't matter too much, because each # task instance runs in its own process anyway. DatadogHook(datadog_conn_id=self.datadog_conn_id) response = api.Event.query( start=self.from_seconds_ago, end=self.up_to_seconds_from_now, priority=self.priority, sources=self.sources, tags=self.tags) if isinstance(response, dict) and response.get('status', 'ok') != 'ok': self.log.error("Unexpected Datadog result: %s", response) raise AirflowException("Datadog returned unexpected result") if self.response_check: # run content check on response return self.response_check(response) # If no check was inserted, assume any event that matched yields true. return len(response) > 0
Example #2
Source File: dogpush.py From DogPush with Apache License 2.0 | 7 votes |
def get_datadog_monitors(): monitors = datadog.api.Monitor.get_all(with_downtimes="true") if CONFIG['dogpush']['ignore_prefix'] is not None: monitors = [ m for m in monitors if not m['name'].startswith(CONFIG['dogpush']['ignore_prefix']) ] if not _check_monitor_names_unique(monitors): raise DogPushException( 'Duplicate names found in remote datadog monitors.') result = {} for m in monitors: m = _canonical_monitor(m) result[m['name']] = m return result
Example #3
Source File: empty_dash.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 7 votes |
def updateEmptyDashboard(id): print "Updating dashboard %s" % (id) result = api.Screenboard.update( id, board_title='Datadog Empty Dash Test', description='Testing an empty dashboard', # worth noting that 'graphs' isn't actually a valid parameter # should use widgets; see https://docs.datadoghq.com/api/screenboards/#creating-boards # however this is how the customer was trying to create/update a board graphs=None ) print result
Example #4
Source File: datadog.py From airflow with Apache License 2.0 | 6 votes |
def __init__(self, datadog_conn_id='datadog_default'): super().__init__() conn = self.get_connection(datadog_conn_id) self.api_key = conn.extra_dejson.get('api_key', None) self.app_key = conn.extra_dejson.get('app_key', None) self.source_type_name = conn.extra_dejson.get('source_type_name', None) # If the host is populated, it will use that hostname instead. # for all metric submissions. self.host = conn.host if self.api_key is None: raise AirflowException("api_key must be specified in the " "Datadog connection details") self.log.info("Setting up api keys for Datadog") initialize(api_key=self.api_key, app_key=self.app_key)
Example #5
Source File: datadog_service.py From spinnaker-monitoring with Apache License 2.0 | 6 votes |
def publish_metrics(self, service_metrics): """Writes time series data to Datadog for a metric snapshot.""" points = [] spectator_client.foreach_metric_in_service_map( service_metrics, self.__append_timeseries_point, points) offset = 0 while offset < len(points): last = min(offset + self.MAX_BATCH, len(points)) chunk = points[offset:last] try: self.api.Metric.send(chunk) except IOError as ioerr: logging.error('Error sending to datadog: %s', ioerr) offset = last return len(points)
Example #6
Source File: datadog_service.py From spinnaker-monitoring with Apache License 2.0 | 6 votes |
def api(self): """The Datadog API stub for interacting with Datadog.""" if self.__api is None: datadog.initialize(api_key=self.__arguments['api_key'], app_key=self.__arguments['app_key'], host_name=self.__arguments['host']) self.__api = datadog.api return self.__api
Example #7
Source File: api_init.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def init(api_key, app_key): options = { 'api_key' : api_key, 'app_key' : app_key } initialize(**options) # test_init confirms that initialization of the datadog api wrapper client has been successful
Example #8
Source File: get.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def metric_report(ids_list, metrics_to_eval, resource): title = '' resp = {} query = '' getter = '' for id in ids_list: if resource == "dash": resp = api.Dashboard.get(str(id)) query = str(resp.get("widgets")) getter = "title" elif resource == "monitor": resp = api.Monitor.get(str(id)) query = str(resp.get("query")) getter = "name" else: print(resource + " is an invalid resource name, exiting.") quit() for metric in metrics_to_eval: if query.find(metric) != -1: if title != resp[getter]: title = resp[getter] print('\n\n\tTitle: ' + resp[getter]) print('\n\t\t Metric: ' + metric)
Example #9
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def push_logpipelines(options): count = 0 fJSON = _files_to_json("logpipelines") if not fJSON: exit("No logpipelines are locally available. Consider pulling logpipelines first.") for item in fJSON: with open(item) as f: data = json.load(f) count = count + 1 print("Pushing {}".format(data["id"].encode('utf8'))) itemId = data['id'] del data['id'] del data['is_read_only'] del data['type'] headers = {'content-type': 'application/json'} if not arguments["--dry-run"]: r = requests.post('{}api/v1/logs/config/pipelines?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), headers=headers, json=data) json_data = json.loads(r.text) json_data["id"] = itemId path = _json_to_file('logpipelines.out', itemId, json_data) print("Pushed '{}' log pipelines.".format(count))
Example #10
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def push_awsaccounts(options): count = 0 awsaccounts = _files_to_json("awsaccounts") if not awsaccounts: exit("No awsaccounts are locally available. Consider pulling awsaccounts first.") for awsaccount in awsaccounts: with open(awsaccount) as f: data = json.load(f) count = count + 1 print("Pushing {}".format(data["account_id"].encode('utf8'))) if not arguments["--dry-run"]: r = requests.post('{}api/v1/integration/aws?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), json=data) json_data = json.loads(r.text) json_data["account_id"] = data["account_id"] print(json.dumps(json_data)) path = _json_to_file('awsaccounts.out', data["account_id"], json_data) print("Pushed '{}' AWS accounts.".format(count)) print("You can now use the json files in the awsaccounts.out folder to automate the AWS External ID onboarding using AWS APIs.")
Example #11
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def push_synthetics_browser_tests(options): count = 0 synthetics = _files_to_json("synthetics_browser_tests") if not synthetics: exit("No synthetic tests are locally available. Consider synthetics first.") for synthetic in synthetics: with open(synthetic) as f: data = json.load(f) count = count + 1 invalid_keys = ["public_id", "monitor_id"] list(map(data.pop, invalid_keys)) print("Pushing {}".format(data["name"].encode('utf8'))) if not arguments["--dry-run"]: r = requests.post('{}api/v1/synthetics/tests?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), json=data) print("Pushed '{}' synthetic tests.".format(count))
Example #12
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def push_synthetics_api_tests(options): count = 0 synthetics = _files_to_json("synthetics_api_tests") if not synthetics: exit("No synthetic tests are locally available. Consider synthetics first.") for synthetic in synthetics: with open(synthetic) as f: data = json.load(f) count = count + 1 invalid_keys = ["public_id", "monitor_id"] list(map(data.pop, invalid_keys)) print("Pushing {}".format(data["name"].encode('utf8'))) if not arguments["--dry-run"]: r = requests.post('{}api/v1/synthetics/tests?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), json=data) print("Pushed '{}' synthetic tests.".format(count))
Example #13
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def push_dashboards(): count = 0 dashboards = _files_to_json("dashboards") if not dashboards: exit("No dashboards are locally available. Consider pulling dashboards first.") for dashboard in dashboards: with open(dashboard) as f: data = json.load(f) count = count + 1 print("Pushing {}".format(data["title"].encode('utf8'))) if not arguments["--dry-run"]: api.Dashboard.create( title=data["title"], description=data["description"], widgets=data["widgets"], template_variables=data["template_variables"], layout_type=data["layout_type"], notify_list=data["notify_list"], is_read_only=data["is_read_only"] ) print("Pushed '{}' dashboards".format(count))
Example #14
Source File: issue10.py From isitfit with Apache License 2.0 | 6 votes |
def test_metric_query_cpuIdle(self, datadog_api): # set start/end import datetime as dt from datetime import timedelta dt_now = dt.datetime.now() dt_1w = dt_now - timedelta(days=7) # convert to seconds since unix epoch # https://stackoverflow.com/a/6999787/4126114 import time conv2sec = lambda x: time.mktime(x.timetuple()) ue_now = conv2sec(dt_now) ue_1w = conv2sec(dt_1w) # build query from isitfit.utils import SECONDS_IN_ONE_DAY query = 'system.cpu.idle{host:%s}.rollup(min,%i)'%(self.datadog_hostname, SECONDS_IN_ONE_DAY) # query datadog # https://docs.datadoghq.com/api/?lang=python#query-timeseries-points m = datadog_api.Metric.query(start=ue_1w, end=ue_now, query=query) if 'errors' in m: print(m) raise Exception(m['errors']) if m['status'] != 'ok': raise Exception(m['status']) assert len(m['series'])>0
Example #15
Source File: datadog.py From airflow with Apache License 2.0 | 6 votes |
def send_metric(self, metric_name, datapoint, tags=None, type_=None, interval=None): """ Sends a single datapoint metric to DataDog :param metric_name: The name of the metric :type metric_name: str :param datapoint: A single integer or float related to the metric :type datapoint: int or float :param tags: A list of tags associated with the metric :type tags: list :param type_: Type of your metric: gauge, rate, or count :type type_: str :param interval: If the type of the metric is rate or count, define the corresponding interval :type interval: int """ response = api.Metric.send( metric=metric_name, points=datapoint, host=self.host, tags=tags, type=type_, interval=interval) self.validate_response(response) return response
Example #16
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def pull_monitors(): path = False count = 0 good_keys = ['tags', 'deleted', 'query', 'message', 'matching_downtimes', 'multi', 'name', 'type', 'options', 'id'] new_monitors = [] monitors = api.Monitor.get_all() for monitor in monitors: if monitor["type"] == "synthetics alert": print("Skipping {} as this is a monitor belonging to a synthetic test. Synthetic monitors will be automatically re-created when you push synthetic tests.".format(monitor["name"])) continue count = count + 1 new_monitor = {} for k, v in monitor.items(): if k in good_keys: new_monitor[k] = v if not arguments["--dry-run"]: path = _json_to_file('monitors', str(new_monitor["id"]), new_monitor) print("Pulling monitor: {} with id: {}, writing to file: {}".format(new_monitor["name"].encode('utf8'), new_monitor["id"], path)) print("Retrieved '{}' monitors.".format(count))
Example #17
Source File: update_host_tags_using_metadata_example.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_hosts(filter_string): host_count = api.Hosts.search(filter=initial_filter_string)['total_matching'] print('%r hosts matching initial_filter_string' % host_count) num_req = host_count // 100 + 1 print('%r number of api requests to query all matching hosts' % num_req) matching_hosts = [] start_index = 0 for i in range(1, num_req+1): print('api request %r of %r' % (i, num_req)) host_list = api.Hosts.search(filter=initial_filter_string, sort_field='apps', count=100, start=start_index)['host_list'] start_index += 100 for host in host_list: matching_hosts.append(host) return matching_hosts # filter list returned by API by searchin on host[key]
Example #18
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 6 votes |
def pull_synthetics_api_tests(options, tag): path = False count = 0 tags = [] if not tag else tag r = requests.get('{}api/v1/synthetics/tests?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"])) synthetics = r.json() for synthetic in synthetics["tests"]: if synthetic["type"] == "api": for tag in tags: if tag in synthetic["tags"]: print("Tag: {} found in synthetic test: {}".format(tag, synthetic["name"])) count = count + 1 json_data = requests.get('{}api/v1/synthetics/tests/{}?api_key={}&application_key={}'.format( options["api_host"], synthetic["public_id"], options["api_key"], options["app_key"] )).json() path = _json_to_file('synthetics_api_tests', synthetic["public_id"], json_data) print("Pulling: {} and writing to file: {}".format(synthetic["name"].encode('utf8'), path)) print("Retrieved '{}' synthetic tests.".format(count))
Example #19
Source File: dogpush.py From DogPush with Apache License 2.0 | 5 votes |
def command_mute(args): local_monitors = get_local_monitors() remote_monitors = get_datadog_monitors() mute_tags = {} now = datetime.datetime.now(pytz.UTC) for tag_key, tag_value in CONFIG.get('mute_tags', {}).items(): tz = pytz.timezone(tag_value['timezone']) if _should_mute(tag_value['expr'], tz, now): next_active_time = _mute_until(tag_value['expr'], tz, now) mute_tags[tag_key] = { 'datetime': next_active_time.astimezone(tz), 'timestamp': calendar.timegm(next_active_time.timetuple()) } else: mute_tags[tag_key] = None for monitor in local_monitors.values(): 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'] 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'])
Example #20
Source File: datadog_service.py From spinnaker-monitoring with Apache License 2.0 | 5 votes |
def generate_arguments(self): required_options = { 'api_key': self.__resolve_value(identifier='Datadog API key', key='api_key'), } nonessential_options = { # we only need Datadog write access, for which an api key is # sufficient, hence this is not required 'app_key': self.__resolve_value(identifier='Datadog app key', key='app_key', required=False), 'tags': self.__convert_to_list_of_strings( self.__resolve_value(identifier='Datadog static tags', key='tags', required=False ) ), 'host': socket.getfqdn( self.__resolve_value(identifier='host', key='host', required=False) or self.__lookup_in_agent_config('hostname') or '' ), } required_options.update(nonessential_options) return required_options
Example #21
Source File: api_init.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_init(): test_resp = api.DashboardList.get_all() if test_resp.get('errors') is None: print('API Intialized!\n\n') else: print('There was a problem Initialiizing the API. Please restart and check your API and App Keys for validity.') quit()
Example #22
Source File: datadog.py From airflow with Apache License 2.0 | 5 votes |
def query_metric(self, query, from_seconds_ago, to_seconds_ago): """ Queries datadog for a specific metric, potentially with some function applied to it and returns the results. :param query: The datadog query to execute (see datadog docs) :type query: str :param from_seconds_ago: How many seconds ago to start querying for. :type from_seconds_ago: int :param to_seconds_ago: Up to how many seconds ago to query for. :type to_seconds_ago: int """ now = int(time.time()) response = api.Metric.query( start=now - from_seconds_ago, end=now - to_seconds_ago, query=query) self.validate_response(response) return response # pylint: disable=too-many-arguments
Example #23
Source File: dogpush.py From DogPush with Apache License 2.0 | 5 votes |
def command_push(args): local_monitors = get_local_monitors() remote_monitors = get_datadog_monitors() only_local = set(local_monitors.keys()) - set(remote_monitors.keys()) if 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) for name in changed: datadog.api.Monitor.update( remote_monitors[name]['id'], **_prepare_monitor(local_monitors[name])) if args.delete_untracked: 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'])
Example #24
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 5 votes |
def push_notebooks(options): count = 0 notebooks = _files_to_json("notebooks") if not notebooks: exit("No notebooks are locally available. Consider pulling notebooks first.") for notebook in notebooks: with open(notebook) as f: data = json.load(f) count = count + 1 print("Pushing: {}".format(data["name"].encode('utf8'))) if not arguments["--dry-run"]: r = requests.post('{}api/v1/notebook?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), json=data) print("Pushed '{}' notebooks".format(count))
Example #25
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 5 votes |
def pull_dashboards(): path = False count = 0 dashboards = api.Dashboard.get_all() for dashboard in dashboards["dashboards"]: count = count + 1 json_data = api.Dashboard.get(dashboard["id"]) if not arguments["--dry-run"]: path = _json_to_file('dashboards', dashboard["id"], json_data) print("Pulling dashboard: {} with id: {}, writing to file: {}".format(dashboard["title"].encode('utf8'), dashboard["id"], path)) print("Retrieved '{}' dashboards.".format(count))
Example #26
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 5 votes |
def push_monitors(): count = 0 monitors = _files_to_json("monitors") if not monitors: exit("No monitors are locally available. Consider pulling monitors first.") for monitor in monitors: with open(monitor) as f: data = json.load(f) print("Pushing monitors:", data["id"], data["name"].encode('utf8')) if not arguments["--dry-run"]: result = api.Monitor.create(type=data['type'], query=data['query'], name=data['name'], message=data['message'], tags=data['tags'], options=data['options']) if 'errors' in result: print('Error pushing monitor:',data["id"],json.dumps(result, indent=4, sort_keys=True)) err_count=err_count+1 else: count = count + 1 mon_id= result['id'] api.Monitor.mute(mon_id) if count > 0: print("Pushed '{}' monitors in muted status, navigate to Monitors -> Manage downtime to unmute.".format(count)) if err_count > 0: print("Error pushing '{}' monitors, please check !".format(err_count))
Example #27
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 5 votes |
def pull_users(): path = False count = 0 users = api.User.get_all() for user in users["users"]: if not user["disabled"]: # don't pull disabled users count = count + 1 json_data = api.User.get(user["handle"]) if not arguments["--dry-run"]: path = _json_to_file('users', user["handle"], json_data["user"]) print("Pulling user: {} with role: {}, writing to file: {}".format(user["handle"].encode('utf8'), user["access_role"], path)) print("Retrieved '{}' users.".format(count))
Example #28
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 5 votes |
def pull_notebooks(options): path = False count = 0 r = requests.get('{}api/v1/notebook?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"])) notebooks = r.json() if 'errors' in notebooks: # check if feature flag is enabled in this organisation if 'You do not have permission' in notebooks["errors"][0]: exit("Notebooks API (notebooks_api) feature flag is not enabled on this Datadog organisation. help@datadoghq.com for more information.") for notebook in notebooks["notebooks"]: count = count + 1 path = _json_to_file('notebooks', str(notebook["id"]), notebook) print("Retrieved '{}' notebooks.".format(count))
Example #29
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 5 votes |
def pull_logpipelines(options): path = False count = 0 r = requests.get('{}api/v1/logs/config/pipelines?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"])) rJSON = r.json() for item in rJSON: count = count + 1 path = _json_to_file('logpipelines', item["id"], item) print("Retrieved '{}' log pipelines.".format(count))
Example #30
Source File: dogmover.py From Miscellany with BSD 3-Clause "New" or "Revised" License | 5 votes |
def pull_awsaccounts(options): path = False count = 0 r = requests.get('{}api/v1/integration/aws?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"])) awsaccounts = r.json() for awsaccount in awsaccounts["accounts"]: count = count + 1 if not arguments["--dry-run"]: path = _json_to_file('awsaccounts', awsaccount["account_id"], awsaccount) print("Retrieved '{}' AWS accounts.".format(count))