[Python] How to monitor AWS Fsx ONTAP Filesystem Usage using lambda function and cloudwatch...

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I am first time trying to work on the aws lambda function to get the monitoring for FSxN IE FSx for ONTAP Storage in AWS.

    Here I want to get the StorageCapacity and StorageTier in order to achieve total filesystemUsed percentage for monitoring.

    I have tried the code below after a lot of search and trial but got an error.

    Code tried:


    import json
    import boto3
    from datetime import datetime

    def lambda_handler(event, context):
    fsx = boto3.client('fsx')
    filesystems = fsx.describe_file_systems()
    table = []
    for filesystem in filesystems.get('FileSystems'):
    status = filesystem.get('Lifecycle')
    filesystem_id = filesystem.get('FileSystemId')
    table.append(filesystem_id)
    cloudwatch = boto3.client('cloudwatch')
    result = []
    for filesystem_id in table:
    current_time = datetime.utcnow().isoformat()
    response = cloudwatch.get_metric_data(
    MetricDataQueries=[
    {
    'Id': 'm1',
    'MetricStat': {
    'Metric': {
    'Namespace': 'AWS/FSx',
    'MetricName': 'StorageCapacity',
    'Dimensions': [
    {
    'Name': 'FileSystemId',
    'Value': filesystem_id
    },
    {
    'Name': 'StorageTier',
    'Value': 'SSD'
    },
    {
    'Name': 'DataType',
    'Value': 'All'
    }
    ]
    },
    'Period': 60,
    'Stat': 'Sum'
    },
    'ReturnData': True
    },
    {
    'Id': 'm2',
    'MetricStat': {
    'Metric': {
    'Namespace': 'AWS/FSx',
    'MetricName': 'StorageUsed',
    'Dimensions': [
    {
    'Name': 'FileSystemId',
    'Value': filesystem_id
    },
    {
    'Name': 'StorageTier',
    'Value': 'SSD'
    },
    {
    'Name': 'DataType',
    'Value': 'All'
    }
    ]
    },
    'Period': 60,
    'Stat': 'Sum'
    },
    'ReturnData': True
    }
    ],
    StartTime='2023-01-20T00:01:00Z',
    EndTime='2023-01-20T00:02:00Z'
    )
    storage_capacity = response['MetricDataResults'][0]['Values'][0]
    storage_used = response['MetricDataResults'][1]['Values'][0]
    result.append({'filesystem_id': filesystem_id,'storage_capacity': storage_capacity, 'storage_used': storage_used})
    return result

    Error after execution


    Response
    {
    "errorMessage": "list index out of range",
    "errorType": "IndexError",
    "requestId": "a09573f2-87ea-4464-afc0-8b196f669415",
    "stackTrace": [
    " File \"/var/task/lambda_function.py\", line 75, in lambda_handler\n storage_capacity = response['MetricDataResults'][0]['Values'][0]\n"
    ]
    }

    MetricDataResults for metric(m1) and metric(m2)


    {'MetricDataResults': [
    {'Id': 'm1', 'Label': 'StorageCapacity', 'Timestamps': [datetime.datetime(2023, 1, 20, 0, 1, tzinfo=tzlocal())], 'Values': [925308932096.0], 'StatusCode': 'Complete'},
    {'Id': 'm2', 'Label': 'StorageUsed', 'Timestamps': [datetime.datetime(2023, 1, 20, 0, 1, tzinfo=tzlocal())], 'Values': [2439143424.0], 'StatusCode': 'Complete'}], 'Messages': [], 'ResponseMetadata': {'RequestId': '479a53b2-b5f5-46c0-b79d-278d803df94b', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '479a53b2-b5f5-46c0-b79d-278d803df94b', 'content-type': 'text/xml', 'content-length': '923', 'date': 'Sat, 21 Jan 2023 18:06:35 GMT'}, 'RetryAttempts': 0}}
    {'MetricDataResults': [
    {'Id': 'm1', 'Label': 'StorageCapacity', 'Timestamps': [datetime.datetime(2023, 1, 20, 0, 1, tzinfo=tzlocal())], 'Values': [925308932096.0], 'StatusCode': 'Complete'},
    {'Id': 'm2', 'Label': 'StorageUsed', 'Timestamps': [datetime.datetime(2023, 1, 20, 0, 1, tzinfo=tzlocal())], 'Values': [2593112064.0], 'StatusCode': 'Complete'}], 'Messages': [], 'ResponseMetadata': {'RequestId': 'db9ad0a4-0a24-4f1d-be60-55bde63fd49b', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'db9ad0a4-0a24-4f1d-be60-55bde63fd49b', 'content-type': 'text/xml', 'content-length': '923', 'date': 'Sat, 21 Jan 2023 18:06:35 GMT'}, 'RetryAttempts': 0}}


    Please help/hint something to get into the solution.

    Continue reading...

Compartilhe esta Página