From a2f21f9462e618d532fd724cd6167112e70b7d92 Mon Sep 17 00:00:00 2001 From: Buckingham <XC01857@lumen.com> Date: Fri, 13 Dec 2024 14:13:35 +0000 Subject: [PATCH 1/2] Update_131224_1 --- terraform/environments/ppud/eventbridge.tf | 25 +++++++++++++++++ .../lambda_scripts/disk_info_report_prod.py | 27 ++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/terraform/environments/ppud/eventbridge.tf b/terraform/environments/ppud/eventbridge.tf index c20d4843d31..c49939d1d3d 100644 --- a/terraform/environments/ppud/eventbridge.tf +++ b/terraform/environments/ppud/eventbridge.tf @@ -102,6 +102,31 @@ resource "aws_cloudwatch_event_target" "trigger_lambda_target_ppud_email_report_ arn = aws_lambda_function.terraform_lambda_func_ppud_email_report_prod[0].arn } +# Eventbridge rule to invoke the PPUD Disk Information Report lambda function every Monday at 07:00 + +resource "aws_lambda_permission" "allow_eventbridge_invoke_ppud_disk_info_report_prod" { + count = local.is-production == true ? 1 : 0 + statement_id = "AllowEventBridgeInvoke" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.terraform_lambda_func_ppud_disk_info_report_prod[0].function_name + principal = "events.amazonaws.com" + source_arn = aws_cloudwatch_event_rule.weekly_schedule_ppud_disk_info_report_prod[0].arn +} + +resource "aws_cloudwatch_event_rule" "weekly_schedule_ppud_disk_info_report_prod" { + count = local.is-production == true ? 1 : 0 + name = "ppud-disk_info-report-weekly-schedule" + description = "Trigger Lambda at 07:10 UTC each Monday" + schedule_expression = "cron(0 7 ? * MON *)" +} + +resource "aws_cloudwatch_event_target" "trigger_lambda_target_ppud_disk_info_report_prod" { + count = local.is-production == true ? 1 : 0 + rule = aws_cloudwatch_event_rule.weekly_schedule_ppud_disk_info_report_prod[0].name + target_id = "disk_info_report" + arn = aws_lambda_function.terraform_lambda_func_ppud_disk_info_report_prod[0].arn +} + # Eventbridge Rule to Disable CPU Alarms each Friday at 23:00 resource "aws_cloudwatch_event_rule" "disable_cpu_alarm" { diff --git a/terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py b/terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py index 3ac99af7c9e..182827688b3 100644 --- a/terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py +++ b/terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py @@ -19,7 +19,7 @@ # Configuration CURRENT_DATE = datetime.now().strftime('%a %d %b %Y') SENDER = "donotreply@cjsm.secure-email.ppud.justice.gov.uk" -RECIPIENTS = ['nick.buckingham@colt.net'] +RECIPIENTS = ["nick.buckingham@colt.net", "gabriella.browning@colt.net", "pankaj.pant@colt.net", "david.savage@colt.net"] SUBJECT = f'AWS PPUD Disk Information Report - {CURRENT_DATE}' AWS_REGION = 'eu-west-2' bucket_name = 'moj-lambda-layers-prod' @@ -62,6 +62,7 @@ def format_disk_info(disk_info): formatted_info = """<table border="1" style="border-collapse: collapse; width: 100%;"> <tr style="background-color: #f2f2f2;"> <th style="padding: 8px; text-align: left;">Server</th> + <th style="padding: 8px; text-align: left;">Date</th> <th style="padding: 8px; text-align: left;">Drive</th> <th style="padding: 8px; text-align: left;">Drive Label</th> <th style="padding: 8px; text-align: left;">File System</th> @@ -77,19 +78,28 @@ def format_disk_info(disk_info): if current_hostname != info[0]: if current_hostname is not None: # formatted_info += f"""<tr><td colspan="9" style="border-bottom: 10px solid black;"></td></tr>""" - formatted_info += f"""<tr><td colspan="9" style="height: 20px;"></td></tr>""" + formatted_info += f"""<tr><td colspan="10" style="height: 20px;"></td></tr>""" current_hostname = info[0] + + status = info[9].strip().capitalize() + status_color = { + 'Good': 'green', + 'Low': 'teal', + 'Warning': 'orange', + 'Critical': 'red' + }.get(status, 'black') formatted_info += f"""<tr> <td style="padding: 8px; text-align: left;">{info[0]}</td> + <td style="padding: 8px; text-align: left;">{info[1]}</td> <td style="padding: 8px; text-align: left;">{info[2]}</td> <td style="padding: 8px; text-align: left;">{info[3]}</td> <td style="padding: 8px; text-align: left;">{info[4]}</td> <td style="padding: 8px; text-align: left;">{info[5]}</td> <td style="padding: 8px; text-align: left;">{info[6]}</td> <td style="padding: 8px; text-align: left;">{info[7]}</td> - <td style="padding: 8px; text-align: left;">{info[8]}%</td> - <td style="padding: 8px; text-align: left;">{info[9]}</td> + <td style="padding: 8px; text-align: left;">{info[8]}</td> + <td style="padding: 8px; text-align: left; background-color: {status_color};">{info[9]}</td> </tr>""" formatted_info += "</table>" return formatted_info @@ -98,7 +108,7 @@ def send_email(subject, body_html): msg = MIMEMultipart() msg['From'] = SENDER msg['To'] = ', '.join(RECIPIENTS) - msg['Subject'] = subject + msg['Subject'] = SUBJECT msg.attach(MIMEText(body_html, 'html')) @@ -116,9 +126,12 @@ def lambda_handler(event, context): # Format disk information formatted_info = format_disk_info(disk_info) - + + # Get current date + CURRENT_DATE = datetime.now().strftime('%a %d %b %Y') + # Email formatted disk information - subject = 'AWS PPUD Disk Information Report' + subject = 'AWS PPUD Disk Information Report - {CURRENT_DATE}' body_html = f"""<html> <head></head> <body> From 84ff656132e95e7bb8e85c1a10d6299807eab462 Mon Sep 17 00:00:00 2001 From: Buckingham <XC01857@lumen.com> Date: Fri, 13 Dec 2024 14:17:05 +0000 Subject: [PATCH 2/2] Update_131224_2 --- terraform/environments/ppud/eventbridge.tf | 14 +++++++------- .../ppud/lambda_scripts/disk_info_report_prod.py | 1 - 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/terraform/environments/ppud/eventbridge.tf b/terraform/environments/ppud/eventbridge.tf index c49939d1d3d..46587c996f7 100644 --- a/terraform/environments/ppud/eventbridge.tf +++ b/terraform/environments/ppud/eventbridge.tf @@ -104,27 +104,27 @@ resource "aws_cloudwatch_event_target" "trigger_lambda_target_ppud_email_report_ # Eventbridge rule to invoke the PPUD Disk Information Report lambda function every Monday at 07:00 -resource "aws_lambda_permission" "allow_eventbridge_invoke_ppud_disk_info_report_prod" { +resource "aws_lambda_permission" "allow_eventbridge_invoke_disk_info_report_prod" { count = local.is-production == true ? 1 : 0 statement_id = "AllowEventBridgeInvoke" action = "lambda:InvokeFunction" - function_name = aws_lambda_function.terraform_lambda_func_ppud_disk_info_report_prod[0].function_name + function_name = aws_lambda_function.terraform_lambda_func_disk_info_report_prod[0].function_name principal = "events.amazonaws.com" - source_arn = aws_cloudwatch_event_rule.weekly_schedule_ppud_disk_info_report_prod[0].arn + source_arn = aws_cloudwatch_event_rule.weekly_schedule_disk_info_report_prod[0].arn } -resource "aws_cloudwatch_event_rule" "weekly_schedule_ppud_disk_info_report_prod" { +resource "aws_cloudwatch_event_rule" "weekly_schedule_disk_info_report_prod" { count = local.is-production == true ? 1 : 0 name = "ppud-disk_info-report-weekly-schedule" description = "Trigger Lambda at 07:10 UTC each Monday" schedule_expression = "cron(0 7 ? * MON *)" } -resource "aws_cloudwatch_event_target" "trigger_lambda_target_ppud_disk_info_report_prod" { +resource "aws_cloudwatch_event_target" "trigger_lambda_target_disk_info_report_prod" { count = local.is-production == true ? 1 : 0 - rule = aws_cloudwatch_event_rule.weekly_schedule_ppud_disk_info_report_prod[0].name + rule = aws_cloudwatch_event_rule.weekly_schedule_disk_info_report_prod[0].name target_id = "disk_info_report" - arn = aws_lambda_function.terraform_lambda_func_ppud_disk_info_report_prod[0].arn + arn = aws_lambda_function.terraform_lambda_func_disk_info_report_prod[0].arn } # Eventbridge Rule to Disable CPU Alarms each Friday at 23:00 diff --git a/terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py b/terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py index 182827688b3..b1641c469b4 100644 --- a/terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py +++ b/terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py @@ -77,7 +77,6 @@ def format_disk_info(disk_info): for info in sorted_disk_info: if current_hostname != info[0]: if current_hostname is not None: - # formatted_info += f"""<tr><td colspan="9" style="border-bottom: 10px solid black;"></td></tr>""" formatted_info += f"""<tr><td colspan="10" style="height: 20px;"></td></tr>""" current_hostname = info[0]