简体   繁体   English

AWS Lambda function 与 DynamoDB 给出架构错误

[英]AWS Lambda function with DynamoDB giving schema Error

I have a below aws lambda code which is basically for ONTAP FileSystem monitoring and works if I do not integrate that to Dynamodb, while using this for now its giving me an error element does not match the schema .我有一个下面的aws lambda代码,它基本上用于 ONTAP 文件系统监控,如果我不将它集成到 Dynamodb 中,它就可以工作,而现在使用它它给我一个错误element does not match the schema

Being a First time user of DynamoDB, i would love you seek some guidance on this.作为 DynamoDB 的初次用户,我希望您能就此寻求一些指导。

Code:代码:

import json
import os
import boto3
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError

def lambda_handler(event, context):
    fsx = boto3.client('fsx')
    cloudwatch = boto3.client('cloudwatch') 
    ses = boto3.client('ses')
    region_name = os.environ['AWS_REGION']
    dynamodb = boto3.resource('dynamodb', region_name=region_name)
    dbtable = dynamodb.Table('FsxNMonitorFsx')
    now = datetime.utcnow()
    start_time = (now - timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ')
    end_time = now.strftime('%Y-%m-%dT%H:%M:%SZ')
    table =  []
    result = []
    next_token = None
    while True:
        if next_token:
            response = fsx.describe_file_systems(NextToken=next_token)
        else:
            response = fsx.describe_file_systems()
        for filesystem in response.get('FileSystems'):
            filesystem_id = filesystem.get('FileSystemId')
            table.append(filesystem_id)
        next_token = response.get('NextToken')
        if not next_token:
            break
    try:
        # Create the DynamoDB table if it does not exist
        dbtable = dynamodb.create_table(
            TableName='FsxNMonitorFsx',
            KeySchema=[
                {
                    'AttributeName': filesystem_id,
                    'KeyType': 'HASH' 
                },
                {
                    'AttributeName': 'alert_sent',
                    'KeyType': 'RANGE'
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': filesystem_id,
                    'AttributeType': 'S'
                },
                {
                    'AttributeName': 'alert_sent',
                    'AttributeType': 'B'
                }
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 10,
                'WriteCapacityUnits': 10
            }
        )
        # Wait for the table to be created
        dbtable.meta.client.get_waiter('table_exists').wait(TableName='FsxNMonitorFsx')
    except ClientError as e:
        if e.response['Error']['Code'] != 'ResourceInUseException':
            raise
    # Code to retrieve metric data and check if alert needs to be sent
    for filesystem_id in table:
        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=start_time,
            EndTime=end_time
        )
        storage_capacity = response['MetricDataResults'][0]['Values']
        storage_used = response['MetricDataResults'][1]['Values']
        if storage_capacity:
            storage_capacity = storage_capacity[0]
        else:
            storage_capacity = None
        if storage_used:
            storage_used = storage_used[0]
        else:
            storage_used = None
        if storage_capacity and storage_used:
            percent_used = (storage_used / storage_capacity) * 100
        else:
            percent_used = None
        ######################################################################
        ### Check if an alert has already been sent for this filesystem_id ###
        ######################################################################
        response = dbtable.get_item(
            Key={'filesystem_id': filesystem_id}
                )
        if 'Item' in response:
            alert_sent = response['Item']['alert_sent']
        else:
            alert_sent = False
        # Send alert if storage usage exceeds threshold and no alert has been sent yet
        if percent_used > 80 and not alert_sent:
            email_body = "Dear Team,<br><br> Please Find the FSx ONTAP FileSystem Alert Report Below for the {} region.".format(region)
            email_body += "<br></br>"
            email_body += "<table>"
            email_body += "<tr>"
            email_body += "<th style='text-align: left'>FileSystemId</th>"
            email_body += "<th style='text-align: right'>Used %</th>"
            email_body += "</tr>"
            for fs in result:
                if fs['percent_used'] > 80:
                    email_body += "<tr>"
                    email_body += "<td style='text-align: left'>" + fs['filesystem_id'] + "</td>"
                    email_body += "<td style='text-align: right; color:red;'>" + str(round(fs['percent_used'], 2)) + "%</td>"
                    email_body += "</tr>"
            email_body += "</table>"
            email_body += "<br></br>"
            email_body += "Sincerely,<br>AWS FSx Alert Team"
            email_subject = "FSx ONTAP FileSystem Alert Report - {}".format(region) 
            ses.send_email(
                Source='test@example.com',
                Destination={
                    'ToAddresses': ['test@example.com'],
                },
                Message={
                    'Subject': {
                        'Data': email_subject
                    },
                    'Body': {
                        'Html': {
                            'Data': email_body
                        }
                    }
                }
            )
            dbtable.update_item(
                TableName='FsxNMonitorFsx',
                Key={'filesystem_id': {'S': filesystem_id}},
                UpdateExpression='SET alert_sent = :val',
                ExpressionAttributeValues={':val': {'BOOL': True}}
            )
    return {
        'statusCode': 200,
        'body': json.dumps('Email sent!')
    }
    

Result without using DB:不使用数据库的结果:

FileSystemId    Used %
fs-0c700005a823f755c    87.95%
fs-074999ef7111b8315    84.51%

Execution Error:执行错误:

[ERROR] ClientError: An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema

Code edit based on the feedback:根据反馈编辑代码:

import os
import boto3, json
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError

fsx = boto3.client('fsx')
cloudwatch = boto3.client('cloudwatch')
ses = boto3.client('ses')
region_name = os.environ['AWS_REGION']
dynamodb = boto3.resource('dynamodb', region_name=region_name)


def lambda_handler(event, context):

    now = datetime.utcnow()
    start_time = (now - timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ')
    end_time = now.strftime('%Y-%m-%dT%H:%M:%SZ')
    table = []
    result = []
    next_token = None
    while True:
        if next_token:
            response = fsx.describe_file_systems(NextToken=next_token)
        else:
            response = fsx.describe_file_systems()
        for filesystem in response.get('FileSystems'):
            filesystem_id = filesystem.get('FileSystemId')
            table.append(filesystem_id)
        next_token = response.get('NextToken')
        if not next_token:
            break
    try:
        # Create the DynamoDB table if it does not exist
        dbtable = dynamodb.Table('FsxNMonitorFsx')
        dbtable = dynamodb.create_table(
            TableName='FsxNMonitorFsx',
            KeySchema=[
                {
                    'AttributeName': 'filesystem_id',
                    'KeyType': 'HASH'
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'filesystem_id',
                    'AttributeType': 'S'
                }
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 10,
                'WriteCapacityUnits': 10
            }
        )
        # Wait for the table to be created
        dbtable.meta.client.get_waiter(
            'table_exists').wait(TableName='FsxNMonitorFsx')
    except ClientError as e:
        if e.response['Error']['Code'] != 'ResourceInUseException':
            raise
    # Code to retrieve metric data and check if alert needs to be sent
    for filesystem_id in table:
        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=start_time,
            EndTime=end_time
        )
        storage_capacity = response['MetricDataResults'][0]['Values']
        storage_used = response['MetricDataResults'][1]['Values']
        if storage_capacity:
            storage_capacity = storage_capacity[0]
        else:
            storage_capacity = None
        if storage_used:
            storage_used = storage_used[0]
        else:
            storage_used = None
        if storage_capacity and storage_used:
            percent_used = (storage_used / storage_capacity) * 100
        else:
            percent_used = None
        ######################################################################
        ### Check if an alert has already been sent for this filesystem_id ###
        ######################################################################
        response = dbtable.get_item(
            Key={'filesystem_id': filesystem_id}
                )
        if 'Item' in response:
            alert_sent = response['Item']['alert_sent']
        else:
            alert_sent = False
        # Send alert if storage usage exceeds threshold and no alert has been sent yet
        if percent_used > 80 and not alert_sent:
            email_body = "Dear Team,<br><br> Please Find the FSx ONTAP FileSystem Alert Report Below for the {} region.".format(
                region_name)
            email_body += "<br></br>"
            email_body += "<table>"
            email_body += "<tr>"
            email_body += "<th style='text-align: left'>FileSystemId</th>"
            email_body += "<th style='text-align: right'>Used %</th>"
            email_body += "</tr>"
            for fs in result:
                if fs['percent_used'] > 80:
                    email_body += "<tr>"
                    email_body += "<td style='text-align: left'>" + \
                        fs['filesystem_id'] + "</td>"
                    email_body += "<td style='text-align: right; color:red;'>" + \
                        str(round(fs['percent_used'], 2)) + "%</td>"
                    email_body += "</tr>"
            email_body += "</table>"
            email_body += "<br></br>"
            email_body += "Sincerely,<br>AWS FSx Alert Team"
            email_subject = "FSx ONTAP FileSystem Alert Report - {}".format(
                region_name)
            ses.send_email(
                Source='test@example.com',
                Destination={
                    'ToAddresses': ['test@example.com'],
                },
                Message={
                    'Subject': {
                        'Data': email_subject
                    },
                    'Body': {
                        'Html': {
                            'Data': email_body
                        }
                    }
                }
            )
            dbtable.put_item(
                Item={
                    'filesystem_id': filesystem_id,
                    'alert_sent': now.strftime('%Y-%m-%d %H:%M:%S')
                }
            )

    return {
        'statusCode': 200,
        'body': json.dumps('Email sent!')
    }

Above doesnt through any error but send empty e-mail and keep Db also empty, i'm lost a bit以上没有通过任何错误,但发送空电子邮件并保持 Db 也为空,我有点迷路

在此处输入图像描述

You have another problem on your lambda function as well.您的 lambda function 也有另一个问题。 You are creating table with variable of filesystem_id .您正在使用filesystem_id变量创建表。 I think you want to create table partition key as filesystem_id not with variable value of filesystem_id我认为您想将表分区键创建为filesystem_id而不是filesystem_id的变量值

dbtable = dynamodb.create_table(
            TableName='FsxNMonitorFsx',
            KeySchema=[
                {
                    'AttributeName': 'filesystem_id',
                    'KeyType': 'HASH' 
                },
                {
                    'AttributeName': 'alert_sent',
                    'KeyType': 'RANGE'
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'filesystem_id',
                    'AttributeType': 'S'
                },
                {
                    'AttributeName': 'alert_sent',
                    'AttributeType': 'B'
                }
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 10,
                'WriteCapacityUnits': 10
            }
        )

And you can not use get_item with only Hash_key you need you use query if you want to fetch data only with filesystem_id .而且你不能只使用get_itemHash_key你需要你使用query如果你只想用filesystem_id获取数据。

UPDATE LAMBDA CODE更新 LAMBDA 代码

import os
import boto3
import json
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError

fsx = boto3.client('fsx')
cloudwatch = boto3.client('cloudwatch')
ses = boto3.client('ses')
region_name = os.environ['AWS_REGION']
dynamodb = boto3.resource('dynamodb', region_name=region_name)
dbtable = dynamodb.Table('FsxNMonitorFsx')


def lambda_handler(event, context):

    now = datetime.utcnow()
    start_time = (now - timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ')
    end_time = now.strftime('%Y-%m-%dT%H:%M:%SZ')
    filesystem_ids = []
    result = []
    next_token = None
    # get all filesystem_ids
    while True:
        if next_token:
            response = fsx.describe_file_systems(NextToken=next_token)
        else:
            response = fsx.describe_file_systems()
        for filesystem in response.get('FileSystems'):
            filesystem_id = filesystem.get('FileSystemId')
            filesystem_ids.append(filesystem_id)
        next_token = response.get('NextToken')
        if not next_token:
            break

    # create table if not exist
    # I think here is not good point to create table. (I prefer you create table outside of this lambda)
    try:
        # Create the DynamoDB table if it does not exist
        dbtable = dynamodb.create_table(
            TableName='FsxNMonitorFsx',
            KeySchema=[
                {
                    'AttributeName': 'filesystem_id',
                    'KeyType': 'HASH'
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'filesystem_id',
                    'AttributeType': 'S'
                }
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 10,
                'WriteCapacityUnits': 10
            }
        )
        # Wait for the table to be created
        dbtable.meta.client.get_waiter(
            'table_exists').wait(TableName='FsxNMonitorFsx')
    except ClientError as e:
        if e.response['Error']['Code'] != 'ResourceInUseException':
            raise

    # Code to retrieve metric data and check if alert needs to be sent
    for filesystem_id in filesystem_ids:
        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=start_time,
            EndTime=end_time
        )
        storage_capacity = response['MetricDataResults'][0]['Values']
        storage_used = response['MetricDataResults'][1]['Values']
        if storage_capacity:
            storage_capacity = storage_capacity[0]
        else:
            storage_capacity = None
        if storage_used:
            storage_used = storage_used[0]
        else:
            storage_used = None
        if storage_capacity and storage_used:
            percent_used = (storage_used / storage_capacity) * 100
        else:
            percent_used = None
        ######################################################################
        ### Check if an alert has already been sent for this filesystem_id ###
        ######################################################################
        response = dbtable.get_item(Key={'filesystem_id': filesystem_id})
        if 'Item' in response:
            alert_sent = response['Item']['alert_sent']
        else:
            alert_sent = False
        # Send alert if storage usage exceeds threshold and no alert has been sent yet
        if percent_used > 80 and not alert_sent:
            result.append({'filesystem_id': filesystem_id, 'percent_used': percent_used})

    header = f"""
                Dear Team,<br><br> Please Find the FSx ONTAP FileSystem Alert Report Below for the {region_name} region.
                <br></br>
                <table>
                <tr>
                <th style='text-align: left'>FileSystemId</th>
                <th style='text-align: right'>Used %</th>
                </tr>
                """
    body = ""
    for fs in result:
        body += f"""
                <tr>
                    <td style='text-align: left'>{fs['filesystem_id']}</td>
                    <td style='text-align: right; color:red;'>{str(round(fs['percent_used'], 2))}%</td>
                </tr>
                """

    footer = f"""</table>
                <br></br>
                Sincerely,<br>AWS FSx Alert Team
                FSx ONTAP FileSystem Alert Report - {region_name}
                """
    email_body = header + body + footer
    ses.send_email(
        Source='test@example.com',
        Destination={
            'ToAddresses': ['test@example.com'],
        },
        Message={
            'Subject': {
                'Data': "Emai Subject"
            },
            'Body': {
                'Html': {
                    'Data': email_body
                }
            }
        }
    )

    for fs in result:
        filesystem_id = fs['filesystem_id']
        dbtable.put_item(
            Item = {
                'filesystem_id': filesystem_id,
                'alert_sent': True
            }
        )
    return {
        'statusCode': 200,
        'body': json.dumps('Email sent!')
    }

You are setting your table with a Partition Key and Sort Key, but your GetItem only indicates the Partition Key.您正在使用分区键和排序键设置表,但您的GetItem仅指示分区键。 You can do one of two things:您可以执行以下两项操作之一:

Supply Sort Key also供应排序键也

 response = dbtable.get_item(
            Key={
                'filesystem_id': filesystem_id,
                'alert_sent': alert_value
                }
             )

Use Query使用查询

Note: This option will return multiple items, if multiple items should exist for a given filesystem_id注意:如果给定的filesystem_id应该存在多个项目,则此选项将返回多个项目

response = dbtable.query(
    KeyConditionExpression='#id=:id',
    ExpressionAttributeValues={':id':filesystem_id},   
    ExpressionAttributeNames={'#id':'filesystem_id'}
)

Table Creation建表

filesystem_id should be a string not your variables value. filesystem_id应该是一个字符串而不是你的变量值。

KeySchema=[
                {
                    'AttributeName': 'filesystem_id',
                    'KeyType': 'HASH' 
                },
                {
                    'AttributeName': 'alert_sent',
                    'KeyType': 'RANGE'
                }
            ],

Lambda clients Lambda 客户

Clients should be created outside of the request handler客户端应该在请求处理程序之外创建

import json
import os
import boto3
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError
fsx = boto3.client('fsx')
cloudwatch = boto3.client('cloudwatch') 
ses = boto3.client('ses')
region_name = os.environ['AWS_REGION']
dynamodb = boto3.resource('dynamodb', region_name=region_name)
dbtable = dynamodb.Table('FsxNMonitorFsx')

def lambda_handler(event, context):


Code代码

import os
import boto3, json
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError

fsx = boto3.client('fsx')
cloudwatch = boto3.client('cloudwatch')
ses = boto3.client('ses')
region_name = os.environ['AWS_REGION']
dynamodb = boto3.resource('dynamodb', region_name=region_name)
dbtable = dynamodb.Table('FsxNMonitorFsx')

def lambda_handler(event, context):

    now = datetime.utcnow()
    start_time = (now - timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ')
    end_time = now.strftime('%Y-%m-%dT%H:%M:%SZ')
    table = []
    result = []
    next_token = None
    while True:
        if next_token:
            response = fsx.describe_file_systems(NextToken=next_token)
        else:
            response = fsx.describe_file_systems()
        for filesystem in response.get('FileSystems'):
            filesystem_id = filesystem.get('FileSystemId')
            table.append(filesystem_id)
        next_token = response.get('NextToken')
        if not next_token:
            break
    try:
        # Create the DynamoDB table if it does not exist
        dbtable = dynamodb.create_table(
            TableName='FsxNMonitorFsx',
            KeySchema=[
                {
                    'AttributeName': 'filesystem_id',
                    'KeyType': 'HASH'
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'filesystem_id',
                    'AttributeType': 'S'
                }
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 10,
                'WriteCapacityUnits': 10
            }
        )
        # Wait for the table to be created
        dbtable.meta.client.get_waiter(
            'table_exists').wait(TableName='FsxNMonitorFsx')
    except ClientError as e:
        if e.response['Error']['Code'] != 'ResourceInUseException':
            raise
    # Code to retrieve metric data and check if alert needs to be sent
    for filesystem_id in table:
        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=start_time,
            EndTime=end_time
        )
        storage_capacity = response['MetricDataResults'][0]['Values']
        storage_used = response['MetricDataResults'][1]['Values']
        if storage_capacity:
            storage_capacity = storage_capacity[0]
        else:
            storage_capacity = None
        if storage_used:
            storage_used = storage_used[0]
        else:
            storage_used = None
        if storage_capacity and storage_used:
            percent_used = (storage_used / storage_capacity) * 100
        else:
            percent_used = None
        ######################################################################
        ### Check if an alert has already been sent for this filesystem_id ###
        ######################################################################
        response = dbtable.get_item(
            Key={'filesystem_id': filesystem_id}
                )
        if 'Item' in response:
            alert_sent = response['Item']['alert_sent']
        else:
            alert_sent = False
        # Send alert if storage usage exceeds threshold and no alert has been sent yet
        if percent_used > 80 and not alert_sent:
            email_body = "Dear Team,<br><br> Please Find the FSx ONTAP FileSystem Alert Report Below for the {} region.".format(
                region_name)
            email_body += "<br></br>"
            email_body += "<table>"
            email_body += "<tr>"
            email_body += "<th style='text-align: left'>FileSystemId</th>"
            email_body += "<th style='text-align: right'>Used %</th>"
            email_body += "</tr>"
            for fs in result:
                if fs['percent_used'] > 80:
                    email_body += "<tr>"
                    email_body += "<td style='text-align: left'>" + \
                        fs['filesystem_id'] + "</td>"
                    email_body += "<td style='text-align: right; color:red;'>" + \
                        str(round(fs['percent_used'], 2)) + "%</td>"
                    email_body += "</tr>"
            email_body += "</table>"
            email_body += "<br></br>"
            email_body += "Sincerely,<br>AWS FSx Alert Team"
            email_subject = "FSx ONTAP FileSystem Alert Report - {}".format(
                region_name)
            ses.send_email(
                Source='test@example.com',
                Destination={
                    'ToAddresses': ['test@example.com'],
                },
                Message={
                    'Subject': {
                        'Data': email_subject
                    },
                    'Body': {
                        'Html': {
                            'Data': email_body
                        }
                    }
                }
            )
            dbtable.update_item(
                TableName='FsxNMonitorFsx',
                Key={'filesystem_id': {'S': filesystem_id}},
                UpdateExpression='SET alert_sent = :val',
                ExpressionAttributeValues={':val': {'BOOL': True}}
            )
    return {
        'statusCode': 200,
        'body': json.dumps('Email sent!')
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM