def _show(cls, args): api._timeout = args.timeout format = args.format if args.host == 'all': res = api.Tag.get_all() else: res = api.Tag.get(args.host) report_warnings(res) report_errors(res) if args.host == 'all': if format == 'pretty': for tag, hosts in list(res['tags'].items()): for host in hosts: print(tag) print(' ' + host) print() elif format == 'raw': print(json.dumps(res)) else: for tag, hosts in list(res['tags'].items()): for host in hosts: print(tag + '\t' + host) else: if format == 'pretty': for tag in res['tags']: print(tag) elif format == 'raw': print(json.dumps(res)) else: for tag in res['tags']: print(tag)
def _unmute(cls, args): api._timeout = args.timeout # TODO CHECK res = api.Monitor.unmute(args.monitor_id, scope=args.scope) if res is not None: report_warnings(res) report_errors(res)
def _delete(cls, args): api._timeout = args.timeout # TODO CHECK res = api.Screenboard.delete(args.screenboard_id) if res is not None: report_warnings(res) report_errors(res)
def _show_all(cls, args): api._timeout = args.timeout format = args.format res = api.Monitor.get_all( group_states=args.group_states, name=args.name, tags=args.tags, monitor_tags=args.monitor_tags ) report_warnings(res) report_errors(res) if args.string_ids: for d in res: d["id"] = str(d["id"]) if format == 'pretty': print(pretty_json(res)) elif format == 'raw': print(json.dumps(res)) else: for d in res: print("\t".join([(str(d["id"])), (cls._escape(d["message"])), (cls._escape(d["name"])), (str(d["options"])), (str(d["org_id"])), (d["query"]), (d["type"])]))
def _pull_all(cls, args): api._timeout = args.timeout def _title_to_filename(title): # Get a lowercased version with most punctuation stripped out... no_punct = ''.join([c for c in title.lower() if c.isalnum() or c in [" ", "_", "-"]]) # Now replace all -'s, _'s and spaces with "_", and strip trailing _ return no_punct.replace(" ", "_").replace("-", "_").strip("_") format = args.format res = api.Timeboard.get_all() report_warnings(res) report_errors(res) if not os.path.exists(args.pull_dir): os.mkdir(args.pull_dir, 0o755) used_filenames = set() for dash_summary in res['dashes']: filename = _title_to_filename(dash_summary['title']) if filename in used_filenames: filename = filename + "-" + dash_summary['id'] used_filenames.add(filename) cls._write_dash_to_file( dash_summary['id'], os.path.join(args.pull_dir, filename + ".json"), args.timeout, format, args.string_ids) if format == 'pretty': print(("\n### Total: {0} dashboards to {1} ###" .format(len(used_filenames), os.path.realpath(args.pull_dir))))
def _push(cls, args): api._timeout = args.timeout for f in args.file: try: screen_obj = json.load(f) except Exception as err: raise Exception("Could not parse {0}: {1}".format(f.name, err)) if args.append_auto_text: datetime_str = datetime.now().strftime('%x %X') auto_text = ("<br/>\nUpdated at {0} from {1} ({2}) on {3}" .format(datetime_str, f.name, screen_obj["id"], platform.node())) screen_obj["description"] += auto_text if 'id' in screen_obj: # Always convert to int, in case it was originally a string. screen_obj["id"] = int(screen_obj["id"]) res = api.Screenboard.update(**screen_obj) else: res = api.Screenboard.create(**screen_obj) if 'errors' in res: print_err('Upload of screenboard {0} from file {1} failed.' .format(screen_obj["id"], f.name)) report_warnings(res) report_errors(res) if format == 'pretty': print(pretty_json(res)) else: print(json.dumps(res)) if args.format == 'pretty': print("Uploaded file {0} (screenboard {1})".format(f.name, screen_obj["id"]))
def _stream(cls, args): api._timeout = args.timeout format = args.format if args.sources is not None: sources = [s.strip() for s in args.sources.split(',')] else: sources = None if args.tags is not None: tags = [t.strip() for t in args.tags.split(',')] else: tags = None start = parse_time(args.start) end = parse_time(args.end) # res = api.Event.query(start=start, end=end) # TODO FIXME res = api.Event.query(start=start, end=end, priority=args.priority, sources=sources, tags=tags) report_warnings(res) report_errors(res) if format == 'pretty': for event in res['events']: prettyprint_event(event) print() elif format == 'raw': print(json.dumps(res)) else: for event in res['events']: print_event(event) print()
def _post(cls, args): """ Post a metric. """ # Format parameters api._timeout = args.timeout host = None if args.no_host else args.host if args.tags: tags = sorted(set([t.strip() for t in args.tags.split(',') if t])) else: tags = None # Submit metric res = api.Metric.send( metric=args.name, points=args.value, host=host, device=args.device, tags=tags, metric_type=args.type) # Report res = defaultdict(list, res) if args.localhostname: # Warn about`--localhostname` command line flag deprecation res['warnings'].append( u"`--localhostname` command line flag is deprecated, made default when no `--host` " u"is specified. See the `--host` option for more information." ) report_warnings(res) report_errors(res)
def _post(cls, args): api._timeout = args.timeout format = args.format message = args.message if message is None: message = sys.stdin.read() if args.tags is not None: tags = [t.strip() for t in args.tags.split(',')] else: tags = None res = api.Event.create( title=args.title, text=message, # TODO FXIME # date_happened=args.date_happened, handle=args.handle, priority=args.priority, related_event_id=args.related_event_id, tags=tags, host=args.host, device=args.device, aggregation_key=args.aggregation_key, source_type_name=args.type) report_warnings(res) report_errors(res) if format == 'pretty': prettyprint_event(res['event']) elif format == 'raw': print(json.dumps(res)) else: print_event(res['event'])
def _delete(cls, args): api._timeout = args.timeout id = args.comment_id res = api.Comment.delete(id) if res is not None: report_warnings(res) report_errors(res)
def _update(cls, args): handle = args.handle comment = args.comment id = args.comment_id format = args.format if comment is None: comment = sys.stdin.read() res = api.Comment.update(id, handle=handle, message=comment) report_warnings(res) report_errors(res) if format == 'pretty': message = res['comment']['message'] lines = message.split('\n') message = '\n'.join([' ' + line for line in lines]) print('id\t\t' + str(res['comment']['id'])) print('url\t\t' + res['comment']['url']) print('resource\t' + res['comment']['resource']) print('handle\t\t' + res['comment']['handle']) print('message\n' + message) elif format == 'raw': print(json.dumps(res)) else: print('id\t\t' + str(res['comment']['id'])) print('url\t\t' + res['comment']['url']) print('resource\t' + res['comment']['resource']) print('handle\t\t' + res['comment']['handle']) print('message\t\t' + res['comment']['message'].__repr__())
def _delete(cls, args): api._timeout = args.timeout # TODO CHECK res = api.Monitor.delete(args.monitor_id) if res is not None: report_warnings(res) report_errors(res)
def _unmute(cls, args): api._timeout = args.timeout res = api.Monitor.unmute(args.monitor_id, scope=args.scope, all_scopes=args.all_scopes) report_warnings(res) report_errors(res) if format == 'pretty': print(cls._pretty_json(res)) else: print(json.dumps(res))
def _mute_all(cls, args): api._timeout = args.timeout format = args.format res = api.Monitor.mute_all() report_warnings(res) report_errors(res) if format == 'pretty': print(cls._pretty_json(res)) else: print(json.dumps(res))
def _unmute(cls, args): api._timeout = args.timeout format = args.format res = api.Host.unmute(args.host_name) report_warnings(res) report_errors(res) if format == 'pretty': print(cls._pretty_json(res)) else: print(json.dumps(res))
def _show_downtime(cls, args): api._timeout = args.timeout format = args.format res = api.Downtime.get(args.downtime_id) report_warnings(res) report_errors(res) if format == 'pretty': print(pretty_json(res)) else: print(json.dumps(res))
def _show_all_downtime(cls, args): api._timeout = args.timeout format = args.format res = api.Downtime.get_all(current_only=args.current_only) report_warnings(res) report_errors(res) if format == 'pretty': print(pretty_json(res)) else: print(json.dumps(res))
def _schedule_downtime(cls, args): api._timeout = args.timeout format = args.format res = api.Downtime.create(scope=args.scope, start=args.start, end=args.end, message=args.message) report_warnings(res) report_errors(res) if format == "pretty": print(cls._pretty_json(res)) else: print(json.dumps(res))
def _mute(cls, args): api._timeout = args.timeout format = args.format res = api.Monitor.mute(args.monitor_id, scope=args.scope, end=args.end) report_warnings(res) report_errors(res) if format == 'pretty': print(pretty_json(res)) else: print(json.dumps(res))
def _update_downtime(cls, args): api._timeout = args.timeout format = args.format res = api.Downtime.update(args.downtime_id, scope=args.scope, start=args.start, end=args.end, message=args.message) report_warnings(res) report_errors(res) if format == 'pretty': print(pretty_json(res)) else: print(json.dumps(res))
def _mute(cls, args): api._timeout = args.timeout format = args.format res = api.Host.mute(args.host_name, end=args.end, message=args.message, override=args.override) report_warnings(res) report_errors(res) if format == 'pretty': print(cls._pretty_json(res)) else: print(json.dumps(res))
def _show(cls, args): api._timeout = args.timeout format = args.format res = api.Event.get(args.event_id) report_warnings(res) report_errors(res) if format == 'pretty': prettyprint_event_details(res['event']) elif format == 'raw': print(json.dumps(res)) else: print_event_details(res['event'])
def _check(cls, args): api._timeout = args.timeout format = args.format res = api.ServiceCheck.check( check=args.check, host_name=args.host_name, status=int(args.status), timestamp=args.timestamp, message=args.message, tags=args.tags) report_warnings(res) report_errors(res) if format == 'pretty': print(cls._pretty_json(res)) else: print(json.dumps(res))
def _show(cls, args): api._timeout = args.timeout format = args.format res = api.Monitor.get(args.monitor_id) report_warnings(res) report_errors(res) if args.string_ids: res["id"] = str(res["id"]) if format == 'pretty': print(cls._pretty_json(res)) else: print(json.dumps(res))
def _show(cls, args): api._timeout = args.timeout format = args.format res = api.Timeboard.get(args.timeboard_id) report_warnings(res) report_errors(res) if args.string_ids: res["dash"]["id"] = str(res["dash"]["id"]) if format == 'pretty': print(pretty_json(res)) else: print(json.dumps(res))
def _replace(cls, args): api._timeout = args.timeout format = args.format res = api.Tag.update(args.host, tags=args.tag) report_warnings(res) report_errors(res) if format == 'pretty': print("Tags for '%s':" % res['host']) for c in res['tags']: print(' ' + c) elif format == 'raw': print(json.dumps(res)) else: for c in res['tags']: print(c)
def _post(cls, args): api._timeout = args.timeout if args.localhostname: host = find_localhost() else: host = args.host if args.tags: tags = sorted(set([t.strip() for t in args.tags.split(',') if t])) else: tags = None res = api.Metric.send( metric=args.name, points=args.value, host=host, device=args.device, tags=tags, metric_type=args.type) report_warnings(res) report_errors(res)
def _update(cls, args): api._timeout = args.timeout format = args.format options = None if args.options is not None: try: options = json.loads(args.options) except: raise Exception('bad json parameter') res = api.Monitor.update(args.monitor_id, type=args.type, query=args.query, name=args.name, message=args.message, options=options) report_warnings(res) report_errors(res) if format == 'pretty': print(cls._pretty_json(res)) else: print(json.dumps(res))
def _post(cls, args): api._timeout = args.timeout format = args.format graphs = args.graphs if args.graphs is None: graphs = sys.stdin.read() try: graphs = json.loads(graphs) except: raise Exception('bad json parameter') res = api.Timeboard.create(title=args.title, description=args.description, graphs=[graphs], template_variables=args.template_variables) report_warnings(res) report_errors(res) if format == 'pretty': print(pretty_json(res)) else: print(json.dumps(res))
def _query(cls, args): api._timeout = args.timeout res = api.Infrastructure.search(q=args.query) report_warnings(res) report_errors(res) if format == 'pretty': for facet, results in list(res['results'].items()): for idx, result in enumerate(results): if idx == 0: print('\n') print("%s\t%s" % (facet, result)) else: print("%s\t%s" % (' ' * len(facet), result)) elif format == 'raw': print(json.dumps(res)) else: for facet, results in list(res['results'].items()): for result in results: print("%s\t%s" % (facet, result))
def _detach(cls, args): api._timeout = args.timeout res = api.Tag.delete(args.host) if res is not None: report_warnings(res) report_errors(res)
def _cancel_downtime(cls, args): api._timeout = args.timeout res = api.Downtime.delete(args.downtime_id) if res is not None: report_warnings(res) report_errors(res)
def _unmute_all(cls, args): api._timeout = args.timeout res = api.Monitor.unmute_all() if res is not None: report_warnings(res) report_errors(res)
def _delete(cls, args): api._timeout = args.timeout res = api.Dashboard.delete(args.dashboard_id) if res is not None: report_warnings(res) report_errors(res)