Created
February 2, 2022 14:42
-
-
Save vjayajv/049fbd8fa8b27d0874d4d872fa9d3099 to your computer and use it in GitHub Desktop.
Python script to fetch mongo metrics from atlas and store them to influx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from datetime import datetime | |
import json | |
import requests | |
from requests.auth import HTTPDigestAuth | |
from influxdb import InfluxDBClient | |
client = InfluxDBClient(host='localhost', port=8085) | |
client.create_database('writetest') | |
data_to_influx = [] | |
# env, cluster are custom tags for reference | |
# host is the shard-host, ex: xxxxx-shard-00-00.xxxxx.mongodb.net:27017 , you can loop through this method for different shards | |
# start and end are timestamps in format : YYYY-MM-DDTHH:MM:SSZ | |
# don't forget to replace the api key credentials in line 20 | |
def fetch_metrics(env, cluster, group_id, host, start, end): | |
api_url = "https://cloud.mongodb.com/api/atlas/v1.0/groups/" + group_id + "/processes/" + host + "/measurements?granularity=PT30S&start=" + start + "&end=" + end | |
data_from_atlas = requests.get(api_url, auth=HTTPDigestAuth('username', 'access_key')) | |
data_from_atlas = json.loads(str(data_from_atlas.text)) | |
for measurement in data_from_atlas['measurements']: | |
for data_point in measurement['dataPoints']: | |
ts = datetime.strptime(str(data_point['timestamp']), '%Y-%m-%dT%H:%M:%SZ') | |
ts = str(int(time.mktime(ts.timetuple()) * 1000)) | |
val = (data_point['value'] if str(data_point['value']) != "None" else 0.0) | |
data_line = measurement['name']+",env="+env+",cluster="+cluster+",host="+host+",units="+str(measurement['units'])+" value="+str(val)+" "+ts | |
if data_line not in data_to_influx: | |
data_to_influx.append(data_line) | |
client.write_points(data_to_influx, database='writetest', time_precision='ms', batch_size=12000, protocol='line') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment