Member-only story

Using DataDog API to pull metrics data for EC2 instances

Vinayak Pandey
2 min readAug 29, 2020

In this short post, we’ll see how we can use Datadog API to pull instance utilization data.

Pre-requisite: DataDog must be setup, with your EC2 instances metric data being available in DataDog. You will also need DataDog API key and Application key

Step 1: Install datadog package using pip.

Step 2: Save the given code on your local system.

#!/usr/bin/python
import requests, json,sys
from datadog import initialize, api
from time import time

output_file="instance_utilization.csv"

api_key= ''
app_key= ''

url = "https://app.datadoghq.com/reports/v2/overview?api_key="+api_key+"&application_key="+app_key
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}

r = requests.get(url,headers=headers)

if r.status_code == 200:
result= r.json()
instance_data={}
for data in result['rows']:
if 'aws_id' in data and 'host_name' in data:
instance_data[data['aws_id']]=data['host_name']


options = {
'api_key': '',
'app_key': ''
}

hosts=['i-0decf5fb9ebfd270e']

initialize(**options)

end = int(time())
start = end - (3600*24*30)

csv_file = open(output_file,'w+')
csv_file.write("Instance ID,Hostname,Minimum Avaialble CPU(%),Total Memory(MB),Minimum Usable Memory Available(MB)\n\n")

for host in hosts:
if host in instance_data.keys():
hostname=instance_data[host]

#Fetch toal memory on a server
query='min:system.mem.total{host:'+hostname+'}'
results =…

--

--

Vinayak Pandey
Vinayak Pandey

Written by Vinayak Pandey

Experienced Cloud Engineer with a knack of automation. Linkedin profile: https://www.linkedin.com/in/vinayakpandeyit/

No responses yet