Extract metrics through the REST API
You can extract metrics from an ExtraHop system through the REST API to visualize metrics in a third-party tool or compare ExtraHop data with other data you have collected. To extract a metric, you must first get identifiers for both the metrics you want to extract and the objects you want to extract metrics for. You can then build and test a metric query in the REST API Explorer before incorporating your request into a script that can read the metrics into a format that can be imported into applications.
Before you begin
- You must log in to the ExtraHop system with an account that has unlimited privileges to generate an API key.
- You must have a valid API key to make changes through the REST API and complete the procedures below. (See Generate an API key.)
- Familiarize yourself with the ExtraHop REST API Guide to learn how to navigate the ExtraHop REST API Explorer.
Retrieve metric IDs
Metrics are identified in the ExtraHop REST API through a combination of the metric_category, the name, and the object_type. You can retrieve all three identifiers through the Metric Explorer.
Retrieve object IDs
Next, you must find the unique identifier for the object that you want to extract metrics for in the REST API. You can retrieve this ID through the REST API Explorer.
Query for metrics
You can query metrics through the REST API Explorer to make sure you have configured the right request body before adding the request to a script.
Python script example
The following example Python script extracts the total count of HTTP responses a server with an ID of 1298 sent over five minute time intervals and then writes the values to a csv file:
#!/usr/bin/python3 import http.client import json import csv import time HOST = 'extrahop.example.com' APIKEY = '123456789abcdefghijklmnop' headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'ExtraHop apikey=%s' % APIKEY} body = r"""{ "metric_category": "http_server", "metric_specs": [ { "name": "rsp" } ], "object_type": "device", "object_ids": [ 14853 ], "cycle": "1hr" }""" conn = http.client.HTTPSConnection(HOST) conn.request('POST', '/api/v1/metrics', headers=headers, body=body) resp = conn.getresponse() parsed_resp = json.loads(resp.read()) output_file = 'output.csv' with open(output_file, 'w') as csvfile: csvwriter = csv.writer(csvfile,dialect='excel') header = [] v = 0 for metric in parsed_resp['stats'][0]: header.append(metric) csvwriter.writerow(header) for metric in parsed_resp['stats']: metric['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(metric['time']/1000)) metric['values'] = str(metric['values'][0]) v += 1 csvwriter.writerow(list(metric.values())) print ("Extracted %s metrics from %s to %s" % (str(v),parsed_resp['stats'][0]['time'],parsed_resp['stats'][-1]['time']))
Note: | If the script returns an error message that the SSL
certificate verification failed, make sure that a trusted certificate has
been added to your ExtraHop system. Alternatively, you can add the context
option and send the request over TLSv1.2 to bypass certificate verification. However,
this method is not secure and is not recommended. The following code creates an HTTP
connection over
TLSv1.2:conn = httplib.HTTPSConnection(HOST, context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)) |
Thank you for your feedback. Can we contact you to ask follow up questions?