This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Add monitor and downtime APIs with integration tests. #115
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a3c2587
Add monitor and downtime APIs with integration tests.
conorbranagan 533d273
Add API function for submitting service checks.
conorbranagan 7cb7040
Clean up of the monitor API, better error message.
conorbranagan c93e883
Add current_only param to list_downtime.
conorbranagan 071c06e
Pass the options in the monitor update in tests so they aren't clobbe…
conorbranagan 3b7f874
Don't include name by default in create monitor API.
conorbranagan a4716f9
Tweak some API function names to match existing pattern.
conorbranagan 23f3997
Add optional parameter to filter returned group states, default to no…
conorbranagan b23af16
Use a default value of `now` for service check timestamp.
conorbranagan e7c6aba
Fix host parameter name for the service check API
conorbranagan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
__all__ = [ | ||
'DowntimeApi', | ||
'MonitorApi', | ||
'MonitorType', | ||
] | ||
|
||
from dogapi.constants import MonitorType | ||
from dogapi.exceptions import ApiError | ||
|
||
class MonitorApi(object): | ||
|
||
def monitor(self, mtype, query, name=None, message=None, options=None): | ||
""" | ||
Create a new monitor of type *mtype* with the given *name* and *query*. | ||
The *message* will accompany any notifications sent for the alert and | ||
can contain the same '@' notation as events to alert individual users. | ||
The *options* argument is a dictionary of settings for the monitor. | ||
See the Datadog API documentation for a break down of available options. | ||
|
||
>>> dog_http_api.monitor("metric alert", "sum(last_1d):sum:system.net.bytes_rcvd{host:host0} > 100") | ||
""" | ||
mtype = mtype.lower() | ||
if mtype not in MonitorType.ALL: | ||
raise ApiError('Invalid monitor type, expected one of: %s' \ | ||
% ', '.join(MonitorType.ALL)) | ||
|
||
body = { | ||
'type': mtype, | ||
'query': query, | ||
} | ||
|
||
if name: | ||
body['name'] = name | ||
if message: | ||
body['message'] = message | ||
if options: | ||
if not isinstance(options, dict): | ||
raise ApiError('Invalid type for `options`, expected `dict`.') | ||
body['options'] = options | ||
|
||
return self.http_request('POST', '/monitor', body, | ||
response_formatter=lambda x: x['id'], | ||
) | ||
|
||
def update_monitor(self, monitor_id, query, name=None, message=None, | ||
options=None): | ||
""" | ||
Update the monitor identified by *monitor_id* with the given *query*. | ||
The *message* will accompany any notifications sent for the alert and | ||
can contain the same '@' notation as events to alert individual users. | ||
The *options* argument is a dictionary of settings for the monitor. | ||
See the Datadog API documentation for a break down of available options. | ||
|
||
>>> dog_http_api.update_monitor(1234, "sum(last_1d):sum:system.net.bytes_rcvd{host:host0} > 200") | ||
""" | ||
body = { | ||
'query': query | ||
} | ||
if name: | ||
body['name'] = name | ||
if message: | ||
body['message'] = message | ||
if options: | ||
body['options'] = options | ||
|
||
return self.http_request('PUT', '/monitor/%s' % monitor_id, body, | ||
response_formatter=lambda x: x['id'], | ||
) | ||
|
||
def get_monitor(self, monitor_id, group_states=None): | ||
""" | ||
Get the details for the monitor identified by *monitor_id*. | ||
|
||
*group_states* is optionally a list of statuses chosen from "all", "ok", | ||
"warn", "alert", "no data". For example, if you want only the failing | ||
groups then you would set it to ['alert', 'warn']. If no value is given | ||
then no group states will be returned. | ||
|
||
>>> dog_http_api.get_monitor(1234, group_states=['all']) | ||
""" | ||
params = {} | ||
|
||
if group_states: | ||
params['group_states'] = ','.join(group_states) | ||
|
||
return self.http_request('GET', '/monitor/%s' % monitor_id, **params) | ||
|
||
def delete_monitor(self, monitor_id): | ||
""" | ||
Delete the monitor identified by *monitor_id*. | ||
|
||
>>> dog_http_api.delete_monitor(1234) | ||
""" | ||
|
||
return self.http_request('DELETE', '/monitor/%s' % monitor_id) | ||
|
||
def get_all_monitors(self, group_states=None): | ||
""" | ||
Get the details for all monitors. If *include_state* is set to True then | ||
the response will include the state of each active group in the alert. | ||
|
||
*group_states* is optionally a list of statuses chosen from "all", "ok", | ||
"warn", "alert", "no data". For example, if you want only the failing | ||
groups then you would set it to ['alert', 'warn']. If no value is given | ||
then no group states will be returned. | ||
|
||
>>> dog_http_api.get_all_monitors(group_states=['alert']) | ||
""" | ||
params = {} | ||
|
||
if group_states: | ||
params['group_states'] = ','.join(group_states) | ||
|
||
return self.http_request('GET', '/monitor', **params) | ||
|
||
def mute_monitors(self): | ||
""" | ||
Mute all monitors. | ||
|
||
>>> dog_http_api.mute_monitors() | ||
""" | ||
|
||
return self.http_request('POST', '/monitor/mute_all') | ||
|
||
def unmute_monitors(self): | ||
""" | ||
Unmute all monitors. | ||
|
||
>>> dog_http_api.unmute_monitors() | ||
""" | ||
|
||
return self.http_request('POST', '/monitor/unmute_all') | ||
|
||
def mute_monitor(self, monitor_id, scope=None, end=None): | ||
""" | ||
Mute the monitor identified by *monitor_id*. If a *scope* is given your | ||
mute will just apply to that scope. You can give an *end* argument that | ||
is a POSIX timestamp of when the mute should stop. | ||
|
||
>>> dog_http_api.mute_monitor(1234, scope='env:staging') | ||
""" | ||
body = {} | ||
if scope: | ||
body['scope'] = scope | ||
if end: | ||
body['end'] = end | ||
return self.http_request('POST', '/monitor/%s/mute' % monitor_id, body) | ||
|
||
def unmute_monitor(self, monitor_id, scope=None): | ||
""" | ||
Unmute the monitor identified by *monitor_id*. If a *scope* is given | ||
your unmute will just apply to that scope. | ||
|
||
>>> dog_http_api.unmute_monitors(1234, scope='env:staging') | ||
""" | ||
body = {} | ||
if scope: | ||
body['scope'] = scope | ||
return self.http_request('POST', '/monitor/%s/unmute' % monitor_id, body) | ||
|
||
|
||
class DowntimeApi(object): | ||
|
||
def schedule_downtime(self, scope, start, end=None): | ||
""" | ||
Schedule downtime over *scope* from *start* to *end*, where *start* and | ||
*end* are POSIX timestamps. If *end* is omitted then the downtime will | ||
continue until cancelled. | ||
""" | ||
body = { | ||
'scope': scope, | ||
'start': start, | ||
} | ||
if end: | ||
body['end'] = end | ||
return self.http_request('POST', '/downtime', body, | ||
response_formatter=lambda x: x['id'], | ||
) | ||
|
||
def get_downtime(self, downtime_id): | ||
""" | ||
Get the downtime identified by *downtime_id* | ||
""" | ||
return self.http_request('GET', '/downtime/%s' % downtime_id) | ||
|
||
def cancel_downtime(self, downtime_id): | ||
""" | ||
Cancel the downtime identified by *downtime_id* | ||
""" | ||
return self.http_request('DELETE', '/downtime/%s' % downtime_id) | ||
|
||
def get_all_downtimes(self, current_only=False): | ||
""" | ||
List all scheduled downtimes. | ||
""" | ||
params = {} | ||
if current_only: | ||
params['current_only'] = True | ||
return self.http_request('GET', '/downtime', **params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
__all__ = [ | ||
'ServiceCheckApi', | ||
] | ||
|
||
import logging | ||
import time | ||
from dogapi.constants import CheckStatus | ||
from dogapi.exceptions import ApiError | ||
|
||
logger = logging.getLogger('dd.dogapi') | ||
|
||
|
||
class ServiceCheckApi(object): | ||
def service_check(self, check, host, status, timestamp=None, message=None, tags=None): | ||
if status not in CheckStatus.ALL: | ||
raise ApiError('Invalid status, expected one of: %s' \ | ||
% ', '.join(CheckStatus.ALL)) | ||
|
||
body = { | ||
'check': check, | ||
'host_name': host, | ||
'timestamp': timestamp or time.time(), | ||
'status': status | ||
} | ||
if message: | ||
body['message'] = message | ||
if tags: | ||
body['tags'] = tags | ||
return self.http_request('POST', '/check_run', body) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we require
name
andmessage
here because they're required in the backend?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are not actually required in the backend anymore. That was a bug initially but I have changed the backend now so it has the same logic as the alert API (choosing the query as the name if no name is given, same for message).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! good to know