python boto 脚本中 EC2 实例的 DiskSpaceUtilization 指标显示空响应
DiskSpaceUtilization metrics for EC2 instance in python boto script showing empty response
我想获取 DiskSpaceUtilization 指标的数据点,但 get_metrics_statitics 的响应为空。
get_metrics_statitics 函数完美适用于其他指标,即 CPUUtilization、MemoryUtilization。但是相同的代码不适用于 DiskSpaceUtilization。
我尝试了以下代码:
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
提前致谢。
那是因为除了InstanceId,还需要指定两个额外的维度:Filesystem和MountPath。
AWS Developer Forums 中报告了类似的问题。如果您不想对这两个维度进行硬编码,请查看注释:
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.
编辑:如果您想使用 Python 获得这两个维度,您可以执行类似的操作:
#!/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
参考:https://askubuntu.com/questions/189987/get-device-with-mount-point
我修改了 get_metric_statistics
函数,它运行良好。对于磁盘 Space 利用率,我们需要添加额外的两个维度,即 FileSystem
和 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')
我想获取 DiskSpaceUtilization 指标的数据点,但 get_metrics_statitics 的响应为空。
get_metrics_statitics 函数完美适用于其他指标,即 CPUUtilization、MemoryUtilization。但是相同的代码不适用于 DiskSpaceUtilization。
我尝试了以下代码:
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
提前致谢。
那是因为除了InstanceId,还需要指定两个额外的维度:Filesystem和MountPath。
AWS Developer Forums 中报告了类似的问题。如果您不想对这两个维度进行硬编码,请查看注释:
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.
编辑:如果您想使用 Python 获得这两个维度,您可以执行类似的操作:
#!/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
参考:https://askubuntu.com/questions/189987/get-device-with-mount-point
我修改了 get_metric_statistics
函数,它运行良好。对于磁盘 Space 利用率,我们需要添加额外的两个维度,即 FileSystem
和 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')