'DiskSpaceUtilization metrics for EC2 instance in python boto script showing empty response

I want to get the datapoints for DiskSpaceUtilization metrics but the response for get_metrics_statitics is empty.

The get_metrics_statitics function works perfectly for other metrics i.e. CPUUtilization, MemoryUtilization. But the same code doesn't work for DiskSpaceUtilization.

I tried the below code:

import sys
from datetime import datetime as dt, timedelta
import boto3

metricdictionary = {}
metricdictionary['DiskSpaceUtilization'] = 'System/Linux,Percent'

ec2_resource = boto3.resource("ec2")
cloudwatch = boto3.client("cloudwatch")

date = dt.today() - timedelta(days=1)
year = date.year
month = date.month
day = date.day

response = cloudwatch.get_metric_statistics(Namespace='System/Linux', 
                            MetricName='DiskSpaceUtilization', 
                            Dimensions=[{'Name': 'InstanceId', 
                            'Value': 'i-0a22a230c4dae4195', }],                                
                            StartTime=dt(year, month, day, 00, 00, 00),
                            EndTime=dt(year, month, day, 23, 59, 59),
                            Period=3600, 
                            Statistics=['Average', 'Minimum', 'Maximum'],
                            Unit='Percent')

print response

Thanks in advance.



Solution 1:[1]

I modified the get_metric_statistics function and it works perfectly. For Disk Space utilization we need to add extra two dimensions, i.e. FileSystem and Mountpath.

response = cloudwatch.get_metric_statistics(
    Namespace=namesp, MetricName='DiskSpaceUtilization',
    Dimensions=[
        {'Name': 'InstanceId', 'Value': instance['InstanceId'], },
        {'Name': 'Filesystem', 'Value': name_of_file_system},
        {'Name': 'MountPath', 'Value': MountPath_for_filesystem}
    ],
    StartTime=dt(year, month, day, 00, 00, 00),
    EndTime=dt(year, month, day, 23, 59, 59),
    Period=3600, Statistics=['Average', 'Minimum', 'Maximum'],
    Unit='Percent')

Solution 2:[2]

That's because you need to specify two extra dimensions, in addition to InstanceId: Filesystem and MountPath.

There's a similar issue reported in AWS Developer Forums. Check the note in case you don't want to hardcode these two dimensions:

One way to avoid using instance-specific filesystem dimensions is to modify the mon-put-instance-data.pl script and just comment out the part that adds Filesystem and MountPath dimensions, leaving only InstanceId dimension for the reported disk metrics. When making these modifications, try running mon-put-instance-data.pl script from the command line with --verify --verbose options to see what data will be actually sent over to Amazon CloudWatch service.

EDIT: In case you want to get these two dimensions using Python, you can do something similar to this:

#!/usr/bin/env python

import subprocess

mounts = {}

for line in subprocess.check_output(['mount', '-l']).split('\n'):
    parts = line.split(' ')
    if len(parts) > 2:
        mounts[parts[2]] = parts[0]

print mounts

Reference: https://askubuntu.com/questions/189987/get-device-with-mount-point

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Buck
Solution 2