1 回答
TA贡献1786条经验 获得超11个赞
获取所有警报是一个棘手的部分,因为除非您预先了解它们,否则很难找到所有警报。尽管如此,我还是以开发演示代码为例CPUUtilization:
import boto3
regions = [
'ap-southeast-1',
'us-west-1',
'us-east-1',
]
for region in regions:
ec2 = boto3.resource('ec2', region_name=region)
running_instances = ec2.instances.filter(Filters=[{
'Name': 'instance-state-name',
'Values': ['running']}])
cw = boto3.client('cloudwatch', region_name=region)
for instance in running_instances:
instance_profile = 'N/A';
if instance.iam_instance_profile:
instance_profile = instance.iam_instance_profile['Arn']
print(region,
instance.id,
instance.instance_type,
instance_profile)
alarms = cw.describe_alarms_for_metric(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
Dimensions=[
{
'Name': 'InstanceId',
'Value': instance.id
}]
)
if 'MetricAlarms' in alarms:
for alarm in alarms['MetricAlarms']:
print(f" - {alarm['AlarmName']}, {alarm['StateValue']}")
示例输出:
us-west-1 i-0d45a86caf2ee0e71 t2.micro arn:aws:iam::353693526480:instance-profile/InstanceRole
- awsec2-i-0d45a86caf2ee0e71-CPU-Utilization, OK
us-east-1 i-0f7cd6d6123e6c47e t2.micro N/A
添加回答
举报