downtime.py (4480B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\datadog\dogshell\downtime.py 2 from __future__ import print_function 3 from datadog.util.format import pretty_json 4 from datadog import api 5 from datadog.dogshell.common import report_errors, report_warnings 6 from datadog.util.compat import json 7 8 class DowntimeClient(object): 9 10 @classmethod 11 def setup_parser(cls, subparsers): 12 parser = subparsers.add_parser('downtime', help='Create, edit, and delete downtimes') 13 parser.add_argument('--string_ids', action='store_true', dest='string_ids', help='Represent downtime IDs as strings instead of ints in JSON') 14 verb_parsers = parser.add_subparsers(title='Verbs', dest='verb') 15 verb_parsers.required = True 16 post_parser = verb_parsers.add_parser('post', help='Create a downtime') 17 post_parser.add_argument('scope', help='scope to apply downtime to') 18 post_parser.add_argument('start', help='POSIX timestamp to start the downtime', default=None) 19 post_parser.add_argument('--end', help='POSIX timestamp to end the downtime', default=None) 20 post_parser.add_argument('--message', help='message to include with notifications for this downtime', default=None) 21 post_parser.set_defaults(func=cls._schedule_downtime) 22 update_parser = verb_parsers.add_parser('update', help='Update existing downtime') 23 update_parser.add_argument('downtime_id', help='downtime to replace with the new definition') 24 update_parser.add_argument('--scope', help='scope to apply downtime to') 25 update_parser.add_argument('--start', help='POSIX timestamp to start the downtime', default=None) 26 update_parser.add_argument('--end', help='POSIX timestamp to end the downtime', default=None) 27 update_parser.add_argument('--message', help='message to include with notifications for this downtime', default=None) 28 update_parser.set_defaults(func=cls._update_downtime) 29 show_parser = verb_parsers.add_parser('show', help='Show a downtime definition') 30 show_parser.add_argument('downtime_id', help='downtime to show') 31 show_parser.set_defaults(func=cls._show_downtime) 32 show_all_parser = verb_parsers.add_parser('show_all', help='Show a list of all downtimes') 33 show_all_parser.add_argument('--current_only', help='only return downtimes that are active when the request is made', default=None) 34 show_all_parser.set_defaults(func=cls._show_all_downtime) 35 delete_parser = verb_parsers.add_parser('delete', help='Delete a downtime') 36 delete_parser.add_argument('downtime_id', help='downtime to delete') 37 delete_parser.set_defaults(func=cls._cancel_downtime) 38 39 @classmethod 40 def _schedule_downtime(cls, args): 41 api._timeout = args.timeout 42 format = args.format 43 res = api.Downtime.create(scope=args.scope, start=args.start, end=args.end, message=args.message) 44 report_warnings(res) 45 report_errors(res) 46 if format == 'pretty': 47 print(pretty_json(res)) 48 else: 49 print(json.dumps(res)) 50 51 @classmethod 52 def _update_downtime(cls, args): 53 api._timeout = args.timeout 54 format = args.format 55 res = api.Downtime.update(args.downtime_id, scope=args.scope, start=args.start, end=args.end, message=args.message) 56 report_warnings(res) 57 report_errors(res) 58 if format == 'pretty': 59 print(pretty_json(res)) 60 else: 61 print(json.dumps(res)) 62 63 @classmethod 64 def _cancel_downtime(cls, args): 65 api._timeout = args.timeout 66 res = api.Downtime.delete(args.downtime_id) 67 if res is not None: 68 report_warnings(res) 69 report_errors(res) 70 71 @classmethod 72 def _show_downtime(cls, args): 73 api._timeout = args.timeout 74 format = args.format 75 res = api.Downtime.get(args.downtime_id) 76 report_warnings(res) 77 report_errors(res) 78 if format == 'pretty': 79 print(pretty_json(res)) 80 else: 81 print(json.dumps(res)) 82 83 @classmethod 84 def _show_all_downtime(cls, args): 85 api._timeout = args.timeout 86 format = args.format 87 res = api.Downtime.get_all(current_only=args.current_only) 88 report_warnings(res) 89 report_errors(res) 90 if format == 'pretty': 91 print(pretty_json(res)) 92 else: 93 print(json.dumps(res))