Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update_131224_1 #9078

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions terraform/environments/ppud/eventbridge.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Configuration
CURRENT_DATE = datetime.now().strftime('%a %d %b %Y')
SENDER = "[email protected]"
RECIPIENTS = ['[email protected]']
RECIPIENTS = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
SUBJECT = f'AWS PPUD Disk Information Report - {CURRENT_DATE}'
AWS_REGION = 'eu-west-2'
bucket_name = 'moj-lambda-layers-prod'
Expand Down Expand Up @@ -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>
Expand All @@ -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
Expand All @@ -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'))

Expand All @@ -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>
Expand Down
Loading