def search(obj, search_query, team, limit, output, pretty): """ Search dashboards, alerts, checks and grafana dashboards. Example: $ zmon search "search query" -t team-1 -t team-2 """ client = get_client(obj.config) with Output('Searching ...', nl=True, output=output, pretty_json=pretty, printer=render_search) as act: try: data = client.search(search_query, limit=limit, teams=team) for check in data['checks']: check['link'] = client.check_definition_url(check) for alert in data['alerts']: alert['link'] = client.alert_details_url(alert) for dashboard in data['dashboards']: dashboard['link'] = client.dashboard_url(dashboard['id']) for dashboard in data['grafana_dashboards']: dashboard['link'] = client.grafana_dashboard_url(dashboard) act.echo(data) except ZmonArgumentError as e: act.error(str(e))
def dashboard_get(obj, dashboard_id, output, pretty): """Get ZMON dashboard""" client = get_client(obj.config) with Output('Retrieving dashboard ...', nl=True, output=output, pretty_json=pretty) as act: dashboard = client.get_dashboard(dashboard_id) act.echo(dashboard)
def status(obj, output, pretty): """Check ZMON system status""" client = get_client(obj.config) with Output('Retrieving status ...', printer=render_status, output=output, pretty_json=pretty) as act: status = client.status() act.echo(status)
def get_entity(obj, entity_id, output, pretty): """Get a single entity by ID""" client = get_client(obj.config) with Output('Retrieving entity {} ...'.format(entity_id), nl=True, output=output, pretty_json=pretty) as act: entity = client.get_entity(entity_id) act.echo(entity)
def filter_entities(obj, key, value, output, pretty): """List entities filtered by a certain key""" client = get_client(obj.config) with Output('Retrieving and filtering entities ...', nl=True, output=output, printer=render_entities, pretty_json=pretty) as act: entities = client.get_entities(query={key: value}) entities = sorted(entities, key=entity_last_modified) act.echo(entities)
def entities(ctx, output, pretty): """Manage entities""" if not ctx.invoked_subcommand: client = get_client(ctx.obj.config) with Output('Retrieving all entities ...', output=output, printer=render_entities, pretty_json=pretty) as act: entities = client.get_entities() act.echo(entities)
def list_alert_definitions(obj, output, pretty): """List all active alert definitions""" client = get_client(obj.config) with Output('Retrieving active alert definitions ...', nl=True, output=output, pretty_json=pretty, printer=render_alerts) as act: alerts = client.get_alert_definitions() for alert in alerts: alert['link'] = client.alert_details_url(alert) act.echo(alerts)
def list_check_definitions(obj, output, pretty): """List all active check definitions""" client = get_client(obj.config) with Output('Retrieving active check definitions ...', nl=True, output=output, pretty_json=pretty, printer=render_checks) as act: checks = client.get_check_definitions() for check in checks: check['link'] = client.check_definition_url(check) act.echo(checks)
def list_tv_token(obj, output, pretty): """List onetime tokens for your user""" client = get_client(obj.config) with Output('Retrieving onetime tokens ...', nl=True, output=output, pretty_json=pretty) as act: tokens = client.list_onetime_tokens() for t in tokens: t['created'] = datetime.fromtimestamp(t['created'] / 1000) if t['bound_at'] is not None: t['bound_at'] = datetime.fromtimestamp(t['bound_at'] / 1000) act.echo(tokens)
def get_alert_definition(obj, alert_id, output, pretty): """Get a single alert definition""" client = get_client(obj.config) with Output('Retrieving alert definition ...', nl=True, output=output, pretty_json=pretty) as act: alert = client.get_alert_definition(alert_id) keys = list(alert.keys()) for k in keys: if alert[k] is None: del alert[k] act.echo(alert)
def get_check_definition(obj, check_id, output, pretty): """Get a single check definition""" client = get_client(obj.config) with Output('Retrieving check definition ...', nl=True, output=output, pretty_json=pretty) as act: check = client.get_check_definition(check_id) keys = list(check.keys()) for k in keys: if check[k] is None: del check[k] act.echo(check)
def filter_check_definitions(obj, field, value, output, pretty): """Filter active check definitions""" client = get_client(obj.config) with Output('Retrieving and filtering check definitions ...', nl=True, output=output, pretty_json=pretty, printer=render_checks) as act: checks = client.get_check_definitions() filtered = [check for check in checks if check.get(field) == value] for check in filtered: check['link'] = client.check_definition_url(check) act.echo(filtered)
def filter_alert_definitions(obj, field, value, output, pretty): """Filter active alert definitions""" client = get_client(obj.config) with Output('Retrieving and filtering alert definitions ...', nl=True, output=output, pretty_json=pretty, printer=render_alerts) as act: alerts = client.get_alert_definitions() if field == 'check_definition_id': value = int(value) filtered = [alert for alert in alerts if alert.get(field) == value] for alert in filtered: alert['link'] = client.alert_details_url(alert) act.echo(filtered)
def create_downtime(obj, entity_ids, duration, comment, output, pretty): """Create downtime for specified entities""" client = get_client(obj.config) start_ts = time.time() end_ts = time.time() + (duration * 60) downtime = { 'entities': entity_ids, 'comment': comment or 'downtime by ZMON CLI', 'start_time': start_ts, 'end_time': end_ts } with Output('Creating downtime ...', nl=True, output=output, pretty_json=pretty) as act: try: new_downtime = client.create_downtime(downtime) act.echo(new_downtime) except ZmonArgumentError as e: act.error(str(e))
def data(obj, alert_id, entity_ids, output, pretty): """Get check data for alert and entities""" client = get_client(obj.config) with Output('Retrieving alert data ...', nl=True, output=output, pretty_json=pretty) as act: data = client.get_alert_data(alert_id) if not entity_ids: result = data else: result = [d for d in data if d['entity'] in entity_ids] values = { v['entity']: v['results'][0]['value'] for v in result if len(v['results']) } act.echo(values)
def filter_entities(obj, filters, output, pretty): """ List entities filtered by key values pairs E.g.: zmon entities filter type instance application_id my-app """ client = get_client(obj.config) if len(filters) % 2: fatal_error('Invalid filters count: expected even number of args!') with Output('Retrieving and filtering entities ...', nl=True, output=output, printer=render_entities, pretty_json=pretty) as act: query = dict(zip(filters[0::2], filters[1::2])) entities = client.get_entities(query=query) entities = sorted(entities, key=entity_last_modified) act.echo(entities)