Does someone have a boto3 example of how to get the same results as this AWS CLI command:
aws cloudwatch list-metrics --namespace "CWAgent" --metric-name "LogicalDisk % Free Space" --query Metrics[*]
I am trying to get the value for these Dimensions:
instance, InstanceId, ImageId, objectname, InstanceType
This is the code I am trying to use:
import boto3
# Create CloudWatch client
client = boto3.client('cloudwatch')
obj = []
response = client.list_metrics(
Namespace='CWagent',
MetricName='LogicalDisk % Free Space',
Dimensions=[
{
'Name': 'instance',
'Value': obj
},
{
'Name': 'InstanceId',
'Value': obj
},
{
'Name': 'ImageId',
'Value': obj
},
{
'Name': 'objectname',
'Value': obj
},
{
'Name': 'InstanceType',
'Value': obj
},
],
NextToken='string'
)
for r in response:
a = instance
b = InstanceId
c = ImageId
d = objectname
e = InstanceType
print(a, b, c, d, e)
This boto3 code will access the CWAgent Metrics Dimensions form an individual instance or all instances on how you set the instances variable: `
import boto3
ec2 = boto3.resource('ec2')
instances = ec2.instances.filter(Filters=[{'Name': 'tag:Name', 'Values': ['MY_INSTANCE_NAME']}])
#instances = ec2.instances.all()
cw = boto3.client('cloudwatch')
for i in instances:
a = i.instance_id
# List metrics through the pagination interface
paginator = cw.get_paginator('list_metrics')
for response in paginator.paginate(
MetricName='LogicalDisk % Free Space',
Namespace='CWAgent',
Dimensions=[
{'Name': 'instance'},
{'Name': 'InstanceId', 'Value': a},
{'Name': 'ImageId'},
{'Name': 'objectname'},
{'Name': 'InstanceType'}
],):
print(response['Metrics'])
`