def import_plugin(url='', org='', account='', key='', plugin_path='', timeout=60, **kwargs): plugin_name = os.path.splitext(os.path.basename(plugin_path))[0] plugin_extension = os.path.splitext(os.path.basename(plugin_path))[1] plugin_content = utils.read_file_content(plugin_path) payload = { "name": plugin_name, "extension": plugin_extension.replace('.', ''), "content": base64.b64encode(plugin_content) } print "restoring plugin %s" % plugin_name resp = requests.post(utils.build_api_url(url, org, account, endpoint='plugins'), headers={'Authorization': "Bearer " + key}, data=payload, timeout=timeout) if resp.status_code == 422: resp = requests.patch(utils.build_api_url(url, org, account, endpoint='plugins/%s' % plugin_name), headers={'Authorization': "Bearer " + key}, data=payload, timeout=timeout) return resp
def import_rule(url='', org='', account='', key='', rule_path='', timeout=60, **kwargs): rule_name = os.path.splitext(os.path.basename(rule_path))[0] rule_content = utils.read_file_content(rule_path) click.echo("restoring rule %s" % rule_name) post(utils.build_api_url(url, org, account, endpoint='rules'), headers={'Authorization': "Bearer " + key}, data={"name": rule_name}, timeout=timeout) return requests.put( utils.build_api_url(url, org, account, endpoint='rules/%s' % rule_name), headers={'Authorization': "Bearer " + key, "Content-Type": "application/yaml"}, data=rule_content, timeout=timeout)
def delete_agent(url='', org='', account='', key='', agent_name='', timeout=60, **kwargs): agent_map = get_agents(url, org, account, key) for agent in agent_map: if agent['name'] == agent_name: return requests.delete(utils.build_api_url(url, org, account, endpoint='agents/%s' % agent['id']), headers={'Authorization': "Bearer " + key}, timeout=timeout)
def update_agents_metric_paths(url='', org='', account='', key='', agents='', metric='', status='', timeout=60, **kwargs): return put(utils.build_api_url(url, org, account, endpoint='metrics/%s/series' % metric), headers={'Authorization': "Bearer " + key}, params={"source": agents}, data={'status': status}, timeout=timeout).json()
def get_agent_name_from_id(url='', org='', account='', key='', agent_id='', timeout=60, **kwargs): agents = get(utils.build_api_url(url, org, account, endpoint='agents'), headers={'Authorization': "Bearer " + key}, timeout=timeout).json() for a in agents: if a['id'] == agent_id: return a['name']
def delete_template(url='', org='', account='', key='', name='', **kwargs): return requests.delete( utils.build_api_url(url, org, account, endpoint='templates/private/%s' % name), headers={'Authorization': "Bearer " + key})
def get_agent_series(url='', org='', account='', key='', agent_id='', metric='', resolution='', period='', **kwargs): return requests.get( utils.build_api_url(url, org, account, endpoint='metrics' + '/' + metric + '/series?source=' + agent_id + '&resolution=' + str(resolution) + '&period=' + str(period)), headers={'Authorization': "Bearer " + key}).json()
def delete_org(url='', org='', account='', key='', org_name='', **kwargs): return requests.delete( utils.build_api_url(url, org, account, orglevel=True) + '/' + org_name, headers={'Authorization': "Bearer " + key})
def get_accounts(url='', org='', key='', **kwargs): return requests.get( utils.build_api_url(url, org, account='', accountlevel=True), headers={'Authorization': "Bearer " + key}).json()
def put_dashboard(url='', org='', account='', key='', path='', template='', timeout=60, **kwargs): dashboard_name = os.path.splitext(os.path.basename(path))[0] dashboard_yaml = utils.read_file_content(path) return put(utils.build_api_url(url, org, account, endpoint='/templates/private/%s/dashboards/%s' % (template, dashboard_name)), headers={'Authorization': "Bearer " + key, "Content-Type": "application/yaml"}, data=dashboard_yaml, timeout=timeout)
def put_plugin(url='', org='', account='', key='', path='', template='', timeout=60, type='INPROCESS', **kwargs): plugin_name = os.path.splitext(os.path.basename(path))[0] plugin_extension = os.path.splitext(os.path.basename(path))[1] plugin_content = utils.read_file_content(path) payload = { "name": plugin_name, "extension": plugin_extension.replace('.', ''), "content": base64.b64encode(plugin_content), "type": type } resp = post(utils.build_api_url(url, org, account, endpoint='/templates/private/%s/plugins' % template), headers={'Authorization': "Bearer " + key}, data=payload, timeout=timeout) return resp
def delete_dashboard(url='', org='', account='', key='', dashboard='', **kwargs): return requests.delete( utils.build_api_url(url, org, account, endpoint='dashboards') + '/' + dashboard, headers={'Authorization': "Bearer " + key})
def put_rule(url='', org='', account='', key='', path='', template='', timeout=60, **kwargs): rule_name = os.path.splitext(os.path.basename(path))[0] rule_content = utils.read_file_content(path) return post(utils.build_api_url(url, org, account, endpoint='/templates/private/%s/rules' % template), headers={'Authorization': "Bearer " + key, "Content-Type": "application/yaml"}, data=rule_content, timeout=timeout)
def export_dashboard(url='', org='', account='', key='', dashboard='', **kwargs): return requests.get( utils.build_api_url(url, org, account, endpoint='dashboards') + '/' + dashboard, headers={'Authorization': "Bearer " + key, "Accept": "application/yaml"}).content
def delete_tag(url='', org='', account='', key='', tag='', **kwargs): return requests.delete( utils.build_api_url(url, org, account, endpoint='tags') + '/' + tag, headers={'Authorization': "Bearer " + key})
def get_tags(url='', org='', account='', key='', **kwargs): return requests.get( utils.build_api_url(url, org, account, endpoint='tags'), headers={'Authorization': "Bearer " + key}).json()
def get_criteria(url='', org='', account='', key='', rule='', **kwargs): return requests.get( utils.build_api_url(url, org, account, endpoint='rules' + '/' + rule + '/criteria'), headers={'Authorization': "Bearer " + key}).json()
def get_agent_series(url='', org='', account='', key='', agent_id='', metric='', resolution='', period='', timeout=60, **kwargs): return get( utils.build_api_url(url, org, account, endpoint='metrics/%s/series?source=%s&resolution=%s&period=%s' % ( metric, agent_id, resolution, period)), headers={'Authorization': "Bearer " + key}, timeout=timeout).json()
def import_rule(url='', org='', account='', key='', rule_path='', **kwargs): rule_name = os.path.splitext(os.path.basename(rule_path))[0] rule_content = utils.read_file_content(rule_path) requests.post( utils.build_api_url(url, org, account, endpoint='rules'), headers={'Authorization': "Bearer " + key}, data={"name": rule_name}) requests.put( utils.build_api_url(url, org, account, endpoint='rules' + '/' + rule_name), headers={'Authorization': "Bearer " + key, "Content-Type": "application/yaml"}, data=rule_content)
def install_template(url='', org='', account='', key='', name='', **kwargs): return requests.post( utils.build_api_url(url, org, account, endpoint='/packs'), headers={'Authorization': "Bearer " + key}, json={"name": name, "force": True, "email": "", "repo": "private"})
def get_agent_metrics(url='', org='', account='', key='', agent_id='', **kwargs): return requests.get( utils.build_api_url(url, org, account, endpoint='metrics'), headers={'Authorization': "Bearer " + key}, params={"source": agent_id}).json()
def get_tag_metrics(url='', org='', account='', key='', tag_name='', **kwargs): return requests.get( utils.build_api_url(url, org, account, endpoint='metrics'), headers={'Authorization': "Bearer " + key}, params={"tag": tag_name}).json()
def export_plugin(url='', org='', account='', key='', plugin='', **kwargs): resp = requests.get( utils.build_api_url(url, org, account, endpoint='plugins') + '/' + plugin, headers={'Authorization': "Bearer " + key}).json() return base64.b64decode(resp['content'])
def create_template(url='', org='', account='', key='', name='', **kwargs): return requests.post( utils.build_api_url(url, org, account, endpoint='templates/private'), data={"name": name}, headers={'Authorization': "Bearer " + key})
def delete_stream(url='', org='', account='', key='', stream='', **kwargs): return requests.delete( utils.build_api_url(url, org, account, endpoint='annotations/%s' % stream ), headers={'Authorization': "Bearer " + key})
def create_annotation(url='', org='', account='', key='', stream='', name='', description='', **kwargs): return requests.post( utils.build_api_url(url, org, account, endpoint='annotations/%s' % stream ), headers={'Authorization': "Bearer " + key}, json={"name": name, "description": description})
def import_dashboard(url='', org='', account='', key='', file_path='', timeout=60, **kwargs): dashboard_name = os.path.splitext(os.path.basename(file_path))[0] dashboard_yaml = utils.read_file_content(file_path) print "restoring dashboard %s" % dashboard_name return put(utils.build_api_url(url, org, account, endpoint='dashboards/%s' % dashboard_name), headers={'Authorization': "Bearer " + key, "Content-Type": "application/yaml", "Accept-Encoding": "identity"}, data=dashboard_yaml, timeout=timeout)
def put_manifest(url='', org='', account='', key='', name='', path='', **kwargs): content = yaml.safe_load(utils.read_file_content(os.path.join(path, 'package.yaml'))) return requests.put( utils.build_api_url(url, org, account, endpoint='templates/private/%s' % name), headers={'Authorization': "Bearer " + key, "Content-Type": "application/json"}, data=json.dumps(content))
def import_dashboard(url='', org='', account='', key='', file_path='', **kwargs): dashboard_name = os.path.splitext(os.path.basename(file_path))[0] dashboard_yaml = utils.read_file_content(file_path) return requests.put( utils.build_api_url(url, org, account, endpoint='dashboards') + '/' + dashboard_name, headers={'Authorization': "Bearer " + key, "Content-Type": "application/yaml"}, data=dashboard_yaml)
def delete_template(url='', org='', account='', key='', name='', timeout=60, **kwargs): return delete(utils.build_api_url(url, org, account, endpoint='templates/private/%s' % name), headers={'Authorization': "Bearer " + key}, timeout=timeout)
def delete_dashboard(url='', org='', account='', key='', dashboard='', timeout=60, **kwargs): return delete(utils.build_api_url(url, org, account, endpoint='dashboards/%s' % dashboard), headers={'Authorization': "Bearer " + key}, timeout=timeout)
def delete_plugin(url='', org='', account='', key='', plugin='', timeout=60, **kwargs): return delete(utils.build_api_url(url, org, account, endpoint='plugins/%s' % plugin), headers={'Authorization': "Bearer " + key}, timeout=timeout)
def get_private_templates(url='', org='', account='', key='', timeout=60, **kwargs): return get(utils.build_api_url(url, org, account, endpoint='templates/private'), headers={ 'Authorization': "Bearer " + key }, timeout=timeout).json()
def create_template(url='', org='', account='', key='', name='', timeout=60, **kwargs): return post(utils.build_api_url(url, org, account, endpoint='templates/private'), data={"name": name}, headers={'Authorization': "Bearer " + key}, timeout=timeout)
def get_agent_name_from_id(url='', org='', account='', key='', agent_id='', timeout=60, **kwargs): agents = get(utils.build_api_url(url, org, account, endpoint='agents'), headers={ 'Authorization': "Bearer " + key }, timeout=timeout).json() for a in agents: if a['id'] == agent_id: return a['name']
def ping_agent(url='', org='', account='', key='', payload='', finger='', timeout=60, **kwargs): return post(utils.build_api_url(url, org, account, endpoint='agents/%s/ping' % finger), headers={'Authorization': "Bearer " + key}, data=payload, timeout=timeout)
def get_agent(url='', org='', account='', key='', agent_name='', timeout=60, **kwargs): agent_map = get_agents(url=url, org=org, account=account, key=key) for agent in agent_map: if agent['name'] == agent_name: return get(utils.build_api_url(url, org, account, endpoint='agents/%s' % agent['id']), headers={'Authorization': "Bearer " + key}, timeout=timeout)
def export_plugin(url='', org='', account='', key='', plugin='', timeout=60, **kwargs): resp = get(utils.build_api_url(url, org, account, endpoint='plugins/%s' % plugin), headers={ 'Authorization': "Bearer " + key }, timeout=timeout).json() return base64.b64decode(resp['content'])
def install_template(url='', org='', account='', key='', name='', timeout=60, **kwargs): return post(utils.build_api_url(url, org, account, endpoint='/packs'), headers={'Authorization': "Bearer " + key}, json={ "name": name, "force": True, "email": "", "repo": "private" }, timeout=timeout)
def export_dashboard(url='', org='', account='', key='', dashboard='', timeout=60, **kwargs): return get(utils.build_api_url(url, org, account, endpoint='dashboards/%s' % dashboard), headers={ 'Authorization': "Bearer " + key, "Accept": "application/yaml" }, timeout=timeout).content
def update_tag_metrics(url='', org='', account='', key='', tag='', status='', timeout=60, **kwargs): return put(utils.build_api_url(url, org, account, endpoint='metrics/series?tag=%s' % tag), headers={ 'Authorization': "Bearer " + key }, data={ 'status': status }, timeout=timeout).json()
def get_agent_series(url='', org='', account='', key='', agent_id='', metric='', resolution='', period='', timeout=60, **kwargs): return get(utils.build_api_url( url, org, account, endpoint='metrics/%s/series?source=%s&resolution=%s&period=%s' % (metric, agent_id, resolution, period)), headers={ 'Authorization': "Bearer " + key }, timeout=timeout).json()
def put_manifest(url='', org='', account='', key='', name='', path='', timeout=60, **kwargs): content = yaml.safe_load( utils.read_file_content(os.path.join(path, 'package.yaml'))) return put(utils.build_api_url(url, org, account, endpoint='templates/private/%s' % name), headers={ 'Authorization': "Bearer " + key, "Content-Type": "application/json" }, data=json.dumps(content), timeout=timeout)
def put_rule(url='', org='', account='', key='', path='', template='', timeout=60, **kwargs): rule_name = os.path.splitext(os.path.basename(path))[0] rule_content = utils.read_file_content(path) return post(utils.build_api_url(url, org, account, endpoint='/templates/private/%s/rules' % template), headers={ 'Authorization': "Bearer " + key, "Content-Type": "application/yaml" }, data=rule_content, timeout=timeout)
def run_command(url='', org='', account='', key='', command='', agent_list='', **kwargs): plugin_content = '#!/usr/bin/env bash \n' + command requests = (grequests.post( utils.build_api_url(url, org, account, endpoint='rpc' + '/run'), data={ 'name': 'temp.sh', 'agent': agent_id, 'content': base64.b64encode(plugin_content), 'encoding': 'base64', 'params': '', 'type': 'SCRIPT' }, callback=set_meta(agent_id), headers={'Authorization': "Bearer " + key}) for agent_id in agent_list) data = [] for resp in grequests.imap(requests, size=10): data.append([resp.meta, resp.json()]) return data
def import_dashboard(url='', org='', account='', key='', file_path='', timeout=60, **kwargs): dashboard_name = os.path.splitext(os.path.basename(file_path))[0] dashboard_yaml = utils.read_file_content(file_path) print "restoring dashboard %s" % dashboard_name return put(utils.build_api_url(url, org, account, endpoint='dashboards/%s' % dashboard_name), headers={ 'Authorization': "Bearer " + key, "Content-Type": "application/yaml", "Accept-Encoding": "identity" }, data=dashboard_yaml, timeout=timeout)
def put_dashboard(url='', org='', account='', key='', path='', template='', timeout=60, **kwargs): dashboard_name = os.path.splitext(os.path.basename(path))[0] dashboard_yaml = utils.read_file_content(path) return put( utils.build_api_url(url, org, account, endpoint='/templates/private/%s/dashboards/%s' % (template, dashboard_name)), headers={ 'Authorization': "Bearer " + key, "Content-Type": "application/yaml" }, data=dashboard_yaml, timeout=timeout)
def update_agents_metric_paths(url='', org='', account='', key='', agents='', metric='', status='', timeout=60, **kwargs): return put(utils.build_api_url(url, org, account, endpoint='metrics/%s/series' % metric), headers={ 'Authorization': "Bearer " + key }, params={ "source": agents }, data={ 'status': status }, timeout=timeout).json()
def run_local(url='', org='', account='', key='', plugin_path='', agent_list='', **kwargs): plugin_name = os.path.splitext(os.path.basename(plugin_path)) plugin_content = utils.read_file_content(plugin_path) requests = (grequests.post( utils.build_api_url(url, org, account, endpoint='rpc' + '/run'), data={ 'name': plugin_name, 'agent': agent_id, 'content': base64.b64encode(plugin_content), 'encoding': 'base64', 'shell': '', 'params': '', 'type': 'INPROCESS' }, callback=set_meta(agent_id), headers={'Authorization': "Bearer " + key}) for agent_id in agent_list) data = [] for resp in grequests.imap(requests, size=10): data.append([resp.meta, resp.json()]) return data
def get_dashboards(url='', org='', account='', key='', timeout=60, **kwargs): return get(utils.build_api_url(url, org, account, endpoint='dashboards'), headers={ 'Authorization': "Bearer " + key }, timeout=timeout).json()
def get_agent_metrics(url='', org='', account='', key='', agent_id='', timeout=60, **kwargs): return get(utils.build_api_url(url, org, account, endpoint='metrics'), headers={'Authorization': "Bearer " + key}, params={"source": agent_id}, timeout=timeout).json()
def get_tag_metrics(url='', org='', account='', key='', tag_name='', timeout=60, **kwargs): return get(utils.build_api_url(url, org, account, endpoint='metrics'), headers={'Authorization': "Bearer " + key}, params={"tag": tag_name}, timeout=timeout).json()
def unmute_rule(url='', org='', account='', key='', rule='', timeout=60, **kwargs): click.echo("unmuting rule %s" % rule) return patch(utils.build_api_url(url, org, account, endpoint='rules/%s' % rule), headers={'Authorization': "Bearer " + key}, data={'mute': False}, timeout=timeout)
def delete_rule(url='', org='', account='', key='', rule='', timeout=60, **kwargs): return delete(utils.build_api_url(url, org, account, endpoint='rules/%s' % rule), headers={'Authorization': "Bearer " + key}, timeout=timeout)
def get_criteria(url='', org='', account='', key='', rule='', timeout=60, **kwargs): return get(utils.build_api_url(url, org, account, endpoint='rules/%s/criteria' % rule), headers={'Authorization': "Bearer " + key}, timeout=timeout).json()
def get_accounts(url='', org='', key='', timeout=60, **kwargs): return get(utils.build_api_url(url, org, account='', account_level=True), headers={ 'Authorization': "Bearer " + key }, timeout=timeout).json()
def delete_account(url='', org='', account='', key='', timeout=60, **kwargs): return delete(utils.build_api_url(url, org, account, account_level=True) + '/' + account, headers={'Authorization': "Bearer " + key}, timeout=timeout)