Skip to content

Commit

Permalink
Merge pull request #9062 from ministryofjustice/Update_121224_1
Browse files Browse the repository at this point in the history
Update_121224_1
  • Loading branch information
nbuckingham72 authored Dec 12, 2024
2 parents 6580c83 + df81eb3 commit 9245646
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 2 deletions.
104 changes: 104 additions & 0 deletions terraform/environments/ppud/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -711,4 +711,108 @@ data "archive_file" "zip_the_send_memory_graph_code_prod" {
type = "zip"
source_dir = "${path.module}/lambda_scripts/"
output_path = "${path.module}/lambda_scripts/send_memory_graph_prod.zip"
}

#################################################
# Lambda Function to send Disk Info Report - PROD
#################################################

resource "aws_lambda_permission" "allow_lambda_to_query_cloudwatch_disk_info_report_prod" {
count = local.is-production == true ? 1 : 0
statement_id = "AllowAccesstoCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.terraform_lambda_func_disk_info_report_prod[0].function_name
principal = "cloudwatch.amazonaws.com"
source_arn = "arn:aws:cloudwatch:eu-west-2:${local.environment_management.account_ids["ppud-production"]}:*"
}

resource "aws_lambda_function" "terraform_lambda_func_disk_info_report_prod" {
# checkov:skip=CKV_AWS_272: "PPUD Lambda code signing temporarily disabled for maintenance purposes"
count = local.is-production == true ? 1 : 0
filename = "${path.module}/lambda_scripts/disk_info_report_prod.zip"
function_name = "disk_info_report"
role = aws_iam_role.lambda_role_cloudwatch_get_metric_data_prod[0].arn
handler = "disk_info_report_prod.lambda_handler"
runtime = "python3.12"
timeout = 300
depends_on = [aws_iam_role_policy_attachment.attach_lambda_policy_cloudwatch_get_metric_data_to_lambda_role_cloudwatch_get_metric_data_prod]
reserved_concurrent_executions = 5
# code_signing_config_arn = "arn:aws:lambda:eu-west-2:${local.environment_management.account_ids["ppud-production"]}:code-signing-config:csc-0bafee04a642a41c1"
dead_letter_config {
target_arn = aws_sqs_queue.lambda_queue_prod[0].arn
}
tracing_config {
mode = "Active"
}
layers = [
"arn:aws:lambda:eu-west-2:${data.aws_ssm_parameter.klayers_account_prod[0].value}:layer:Klayers-p312-numpy:8",
"arn:aws:lambda:eu-west-2:${data.aws_ssm_parameter.klayers_account_prod[0].value}:layer:Klayers-p312-pillow:1",
aws_lambda_layer_version.lambda_layer_matplotlib_prod[0].arn
]
# VPC configuration
vpc_config {
subnet_ids = [data.aws_subnet.private_subnets_b.id]
security_group_ids = [aws_security_group.PPUD-Mail-Server[0].id]
}
}

# Archive the zip file

data "archive_file" "zip_the_disk_info_report_code_prod" {
count = local.is-production == true ? 1 : 0
type = "zip"
source_dir = "${path.module}/lambda_scripts/"
output_path = "${path.module}/lambda_scripts/disk_info_report_prod.zip"
}

#######################################################
# Lambda Function to send Disk Read Write Report - PROD
#######################################################

resource "aws_lambda_permission" "allow_lambda_to_query_cloudwatch_disk_read_write_report_prod" {
count = local.is-production == true ? 1 : 0
statement_id = "AllowAccesstoCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.terraform_lambda_func_disk_read_write_report_prod[0].function_name
principal = "cloudwatch.amazonaws.com"
source_arn = "arn:aws:cloudwatch:eu-west-2:${local.environment_management.account_ids["ppud-production"]}:*"
}

resource "aws_lambda_function" "terraform_lambda_func_disk_read_write_report_prod" {
# checkov:skip=CKV_AWS_272: "PPUD Lambda code signing temporarily disabled for maintenance purposes"
count = local.is-production == true ? 1 : 0
filename = "${path.module}/lambda_scripts/disk_read_write_report_prod.zip"
function_name = "disk_read_write_report"
role = aws_iam_role.lambda_role_cloudwatch_get_metric_data_prod[0].arn
handler = "disk_read_write_report_prod.lambda_handler"
runtime = "python3.12"
timeout = 300
depends_on = [aws_iam_role_policy_attachment.attach_lambda_policy_cloudwatch_get_metric_data_to_lambda_role_cloudwatch_get_metric_data_prod]
reserved_concurrent_executions = 5
# code_signing_config_arn = "arn:aws:lambda:eu-west-2:${local.environment_management.account_ids["ppud-production"]}:code-signing-config:csc-0bafee04a642a41c1"
dead_letter_config {
target_arn = aws_sqs_queue.lambda_queue_prod[0].arn
}
tracing_config {
mode = "Active"
}
layers = [
"arn:aws:lambda:eu-west-2:${data.aws_ssm_parameter.klayers_account_prod[0].value}:layer:Klayers-p312-numpy:8",
"arn:aws:lambda:eu-west-2:${data.aws_ssm_parameter.klayers_account_prod[0].value}:layer:Klayers-p312-pillow:1",
aws_lambda_layer_version.lambda_layer_matplotlib_prod[0].arn
]
# VPC configuration
vpc_config {
subnet_ids = [data.aws_subnet.private_subnets_b.id]
security_group_ids = [aws_security_group.PPUD-Mail-Server[0].id]
}
}

# Archive the zip file

data "archive_file" "zip_the_disk_read_write_report_code_prod" {
count = local.is-production == true ? 1 : 0
type = "zip"
source_dir = "${path.module}/lambda_scripts/"
output_path = "${path.module}/lambda_scripts/disk_read_write_report_prod.zip"
}
138 changes: 138 additions & 0 deletions terraform/environments/ppud/lambda_scripts/disk_info_report_prod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Python script to retrieve cloudwatch metic data (disk information), graph it and email it to end users via the internal mail relay.
# Nick Buckingham
# 12 December 2024

import boto3
import os
os.environ['MPLCONFIGDIR'] = "/tmp/graph"
import re
import io
import base64
from datetime import datetime, timedelta
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from botocore.exceptions import NoCredentialsError, PartialCredentialsError

# Configuration
CURRENT_DATE = datetime.now().strftime('%a %d %b %Y')
SENDER = "[email protected]"
RECIPIENTS = ['[email protected]']
SUBJECT = f'AWS PPUD Disk Information Report - {CURRENT_DATE}'
AWS_REGION = 'eu-west-2'
bucket_name = 'moj-lambda-layers-prod'
file_key = 'all-disks.log'

# SMTP Configuration
SMTP_SERVER = "10.27.9.39"
SMTP_PORT = 25

# Initialize boto3 client
s3 = boto3.client('s3')
cloudwatch = boto3.client("cloudwatch", region_name=AWS_REGION)
ses = boto3.client("ses")

def retrieve_file_from_s3(bucket, key):
response = s3.get_object(Bucket=bucket, Key=key)
content = response['Body'].read().decode('utf-8')
return content

def parse_disk_info(content):
disk_info_pattern = re.compile(
r"Hostname\s+:\s+(.+)\n"
r"Current Date\s+:\s+(.+)\n"
r"Drive Letter\s+:\s+(.+)\n"
r"Drive Label\s+:\s*(.*)\n"
r"File System Type\s+:\s+(.+)\n"
r"Total Capacity \(GB\)\s+:\s+(.+)\n"
r"Used Capacity \(GB\)\s+:\s+(.+)\n"
r"Total Free Space \(GB\)\s+:\s+(.+)\n"
r"% Free Space\s+:\s+(.+)\n"
r"Status\s+:\s+(.+)"
)
matches = disk_info_pattern.findall(content)
return matches

def format_disk_info(disk_info):
# Sort disk_info to place the C drive first
sorted_disk_info = sorted(disk_info, key=lambda x: (x[2] != 'C', x))

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;">Drive</th>
<th style="padding: 8px; text-align: left;">Drive Label</th>
<th style="padding: 8px; text-align: left;">File System</th>
<th style="padding: 8px; text-align: left;">Total Capacity (GB)</th>
<th style="padding: 8px; text-align: left;">Used Capacity (GB)</th>
<th style="padding: 8px; text-align: left;">Free Space (GB)</th>
<th style="padding: 8px; text-align: left;">% Free Space</th>
<th style="padding: 8px; text-align: left;">Status</th>
</tr>"""

current_hostname = None
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="9" style="height: 20px;"></td></tr>"""
current_hostname = info[0]

formatted_info += f"""<tr>
<td style="padding: 8px; text-align: left;">{info[0]}</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>
</tr>"""
formatted_info += "</table>"
return formatted_info

def send_email(subject, body_html):
msg = MIMEMultipart()
msg['From'] = SENDER
msg['To'] = ', '.join(RECIPIENTS)
msg['Subject'] = subject

msg.attach(MIMEText(body_html, 'html'))

with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
# server.starttls()
# server.login(smtp_username, smtp_password)
server.send_message(msg)

def lambda_handler(event, context):
# Retrieve file from S3
content = retrieve_file_from_s3(bucket_name, file_key)

# Parse disk information
disk_info = parse_disk_info(content)

# Format disk information
formatted_info = format_disk_info(disk_info)

# Email formatted disk information
subject = 'AWS PPUD Disk Information Report'
body_html = f"""<html>
<head></head>
<body>
<p>Hi Team</p>
<p></p>
<p>Please find below the PPUD disk information report.</p>
<p>{formatted_info}</p>
<p>This is an automated email</p>
</body>
</html>"""

send_email(subject, body_html)

return {
'statusCode': 200,
'body': 'Email sent successfully!'
}
Loading

0 comments on commit 9245646

Please sign in to comment.