0

I need to capture variable value for Linux EC2 instances.

'Name': 'device',
'Value': "xvdg"

The value Disk Name(device name) changes for EC2 Linux instances e.g sdh,xvdg, etc..

I am using boto3 SDK

Any help is much appreciated.

Current Code (Hard coded the Device Name):

cloudwatch.put_metric_alarm(
  AlarmName=prefix +  ' -  Elastic Search Disc % Availabilty is less than the configured threshold value - Please increase size of EBS Volume',
  ComparisonOperator='LowerThanThreshold',
  EvaluationPeriods=3,
  MetricName='disk_used_percent',
  Namespace='CWAgent',
  Period=300,
  Statistic='Average',
  Threshold=75,
  AlarmActions=[snstopicarn],
  AlarmDescription='Standard Disc Alert - Elastic Search Disc % Availabilty is less than the configured threshold value - Please increase size of EBS Volume',
  Dimensions=[
                            {
                              'Name': 'InstanceId',
                              'Value': instance["InstanceId"]
                            },

                            {
                              'Name': 'path',
                              'Value': "/mnt/elasticsearch"
                            },
                            {
                              'Name': 'device',
                              'Value': "xvdi"
                            },
                            {
                              'Name': 'fstype',
                              'Value': "ext4"
                            },
  ]
)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

0

You can use describe_instances and extract DeviceName. (boto3 doc)

import boto3

instance_id = <instance_id>

client = boto3.client('ec2')

response = client.describe_instances(
    InstanceIds=[
        instance_id,
    ],
)

device_name = response['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['DeviceName']

#/dev/xvda
print(device_name)

When using describe_alarms_for_metric, the code would be like this.

response = client.describe_alarms_for_metric(
    MetricName='string',
    Namespace='string',
    Statistic='SampleCount'|'Average'|'Sum'|'Minimum'|'Maximum',
    ExtendedStatistic='string',
    Dimensions=[
        {
            'Name': 'string',
            'Value': 'string'
        },
    ],
    Period=123,
    Unit='Seconds'|'Microseconds'|'Milliseconds'|'Bytes'|'Kilobytes'|'Megabytes'|'Gigabytes'|'Terabytes'|'Bits'|'Kilobits'|'Megabits'|'Gigabits'|'Terabits'|'Percent'|'Count'|'Bytes/Second'|'Kilobytes/Second'|'Megabytes/Second'|'Gigabytes/Second'|'Terabytes/Second'|'Bits/Second'|'Kilobits/Second'|'Megabits/Second'|'Gigabits/Second'|'Terabits/Second'|'Count/Second'|'None'
)
response = cloudwatch.put_metric_alarm(....)

for i in response['MetricAlarms'][0]['Dimensions'][0]:
    if i['Name'] == 'device':
        print(i['Value'])  # xvdi
shimo
  • 2,156
  • 4
  • 17
  • 21
  • hi @shimo thank you for the details. Well what I actually need is to get the "Device" Variable for "cloudwatch.put_metric_alarm" Dimensions=[ {'Name': 'Device','Value': "xvdi"}] how i get 'device' with boto3. describe_instances can extract DeviceName however it does not work for cloudwatch.put.metric. Thank you in advance for your help. – Damian Chelverajan Nov 06 '22 at 14:20
  • cloudwatch.put_metric_alarm returns None. Using describe_alarms_for_metric might work. Updated but not confirmed myself. – shimo Nov 06 '22 at 21:28
  • `code` response = cloudwatch.describe_alarms_for_metric(MetricName='Device', Namespace='CWAgent',Statistic='Average',Period=300 ,Unit='Megabytes',Dimensions=[{ 'Name': 'Device', 'Value': "Device" },], ) for i in response['MetricAlarms'][0]['Dimensions'][0]: if i['Name'] == 'Device': print(i['Value']) – Damian Chelverajan Nov 20 '22 at 14:16
  • `code`cloudwatch.put_metric_alarm(,ComparisonOperator='LessThanThreshold',EvaluationPeriods=3,MetricName='disk_used_percent',Namespace='CWAgent',Period=300,Statistic='Average',Threshold=1,AlarmActions=[snstopicarn],Dimensions=[ {'Name': 'InstanceId','Value': instance["InstanceId"]}, {'Name': 'path','Value': "/mnt/elasticsearch"}, { 'Name': 'Device', 'Value': 'Device' }, {'Name': 'fstype', 'Value': "ext4"},]) – Damian Chelverajan Nov 20 '22 at 14:18
  • I am getting following error "Dynamic Creation of CloudWatch Alerts for AWS Servers is Failed . Exception : list index out of range" any pointers is much appreciated, thanks. – Damian Chelverajan Nov 20 '22 at 14:19