monitor.py (7989B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\datadog\dogshell\monitor.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 MonitorClient(object): 9 10 @classmethod 11 def setup_parser(cls, subparsers): 12 parser = subparsers.add_parser('monitor', help='Create, edit, and delete monitors') 13 parser.add_argument('--string_ids', action='store_true', dest='string_ids', help='Represent monitor 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 monitor') 17 post_parser.add_argument('type', help="type of the monitor, e.g.'metric alert' 'service check'") 18 post_parser.add_argument('query', help='query to notify on with syntax varying depending on what type of monitor you are creating') 19 post_parser.add_argument('--name', help='name of the alert', default=None) 20 post_parser.add_argument('--message', help='message to include with notifications for this monitor', default=None) 21 post_parser.add_argument('--options', help='json options for the monitor', default=None) 22 post_parser.set_defaults(func=cls._post) 23 update_parser = verb_parsers.add_parser('update', help='Update existing monitor') 24 update_parser.add_argument('monitor_id', help='monitor to replace with the new definition') 25 update_parser.add_argument('type', help="type of the monitor, e.g. 'metric alert' 'service check'") 26 update_parser.add_argument('query', help='query to notify on with syntax varying depending on what type of monitor you are creating') 27 update_parser.add_argument('--name', help='name of the alert', default=None) 28 update_parser.add_argument('--message', help='message to include with notifications for this monitor', default=None) 29 update_parser.add_argument('--options', help='json options for the monitor', default=None) 30 update_parser.set_defaults(func=cls._update) 31 show_parser = verb_parsers.add_parser('show', help='Show a monitor definition') 32 show_parser.add_argument('monitor_id', help='monitor to show') 33 show_parser.set_defaults(func=cls._show) 34 show_all_parser = verb_parsers.add_parser('show_all', help='Show a list of all monitors') 35 show_all_parser.set_defaults(func=cls._show_all) 36 delete_parser = verb_parsers.add_parser('delete', help='Delete a monitor') 37 delete_parser.add_argument('monitor_id', help='monitor to delete') 38 delete_parser.set_defaults(func=cls._delete) 39 mute_all_parser = verb_parsers.add_parser('mute_all', help='Globally mute monitors (downtime over *)') 40 mute_all_parser.set_defaults(func=cls._mute_all) 41 unmute_all_parser = verb_parsers.add_parser('unmute_all', help='Globally unmute monitors (cancel downtime over *)') 42 unmute_all_parser.set_defaults(func=cls._unmute_all) 43 mute_parser = verb_parsers.add_parser('mute', help='Mute a monitor') 44 mute_parser.add_argument('monitor_id', help='monitor to mute') 45 mute_parser.add_argument('--scope', help='scope to apply the mute to, e.g. role:db (optional)', default=[]) 46 mute_parser.add_argument('--end', help='POSIX timestamp for when the mute should end (optional)', default=None) 47 mute_parser.set_defaults(func=cls._mute) 48 unmute_parser = verb_parsers.add_parser('unmute', help='Unmute a monitor') 49 unmute_parser.add_argument('monitor_id', help='monitor to unmute') 50 unmute_parser.add_argument('--scope', help='scope to unmute (must be muted), e.g. role:db', default=[]) 51 unmute_parser.add_argument('--all_scopes', help='clear muting across all scopes', action='store_true') 52 unmute_parser.set_defaults(func=cls._unmute) 53 54 @classmethod 55 def _post(cls, args): 56 api._timeout = args.timeout 57 format = args.format 58 options = None 59 if args.options is not None: 60 try: 61 options = json.loads(args.options) 62 except: 63 raise Exception('bad json parameter') 64 65 res = api.Monitor.create(type=args.type, query=args.query, name=args.name, message=args.message, options=options) 66 report_warnings(res) 67 report_errors(res) 68 if format == 'pretty': 69 print(pretty_json(res)) 70 else: 71 print(json.dumps(res)) 72 73 @classmethod 74 def _update(cls, args): 75 api._timeout = args.timeout 76 format = args.format 77 options = None 78 if args.options is not None: 79 try: 80 options = json.loads(args.options) 81 except: 82 raise Exception('bad json parameter') 83 84 res = api.Monitor.update(args.monitor_id, type=args.type, query=args.query, name=args.name, message=args.message, options=options) 85 report_warnings(res) 86 report_errors(res) 87 if format == 'pretty': 88 print(pretty_json(res)) 89 else: 90 print(json.dumps(res)) 91 92 @classmethod 93 def _show(cls, args): 94 api._timeout = args.timeout 95 format = args.format 96 res = api.Monitor.get(args.monitor_id) 97 report_warnings(res) 98 report_errors(res) 99 if args.string_ids: 100 res['id'] = str(res['id']) 101 if format == 'pretty': 102 print(pretty_json(res)) 103 else: 104 print(json.dumps(res)) 105 106 @classmethod 107 def _show_all(cls, args): 108 api._timeout = args.timeout 109 format = args.format 110 res = api.Monitor.get_all() 111 report_warnings(res) 112 report_errors(res) 113 if args.string_ids: 114 for d in res: 115 d['id'] = str(d['id']) 116 117 if format == 'pretty': 118 print(pretty_json(res)) 119 elif format == 'raw': 120 print(json.dumps(res)) 121 else: 122 for d in res: 123 print('\t'.join([str(d['id']), 124 cls._escape(d['message']), 125 cls._escape(d['name']), 126 str(d['options']), 127 str(d['org_id']), 128 d['query'], 129 d['type']])) 130 131 @classmethod 132 def _delete(cls, args): 133 api._timeout = args.timeout 134 res = api.Monitor.delete(args.monitor_id) 135 if res is not None: 136 report_warnings(res) 137 report_errors(res) 138 139 @classmethod 140 def _escape(cls, s): 141 return s.replace('\r', '\\r').replace('\n', '\\n').replace('\t', '\\t') 142 143 @classmethod 144 def _mute_all(cls, args): 145 api._timeout = args.timeout 146 format = args.format 147 res = api.Monitor.mute_all() 148 report_warnings(res) 149 report_errors(res) 150 if format == 'pretty': 151 print(pretty_json(res)) 152 else: 153 print(json.dumps(res)) 154 155 @classmethod 156 def _unmute_all(cls, args): 157 api._timeout = args.timeout 158 res = api.Monitor.unmute_all() 159 if res is not None: 160 report_warnings(res) 161 report_errors(res) 162 163 @classmethod 164 def _mute(cls, args): 165 api._timeout = args.timeout 166 format = args.format 167 res = api.Monitor.mute(args.monitor_id, scope=args.scope, end=args.end) 168 report_warnings(res) 169 report_errors(res) 170 if format == 'pretty': 171 print(pretty_json(res)) 172 else: 173 print(json.dumps(res)) 174 175 @classmethod 176 def _unmute(cls, args): 177 api._timeout = args.timeout 178 res = api.Monitor.unmute(args.monitor_id, scope=args.scope, all_scopes=args.all_scopes) 179 report_warnings(res) 180 report_errors(res) 181 if format == 'pretty': 182 print(pretty_json(res)) 183 else: 184 print(json.dumps(res))