Skip to content

Commit 711d9d8

Browse files
committed
testing changes
1 parent 22f85a6 commit 711d9d8

2 files changed

Lines changed: 22 additions & 31 deletions

File tree

sds_data_manager/constructs/ialirt_rsync_alarm_construct.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(
5252
self.create_event_rule(ialirt_bucket, ialirt_rsync_lambda)
5353
# Parameter store lookup.
5454
# Note: this must be run once for each account:
55-
# aws ssm put-parameter --name /imap/ialirt/alarm_email
55+
# aws ssm put-parameter --name /imap/ialirt/rsync_alarm_email
5656
# --value ialirt@example.com --type String --overwrite
5757
rsync_alarm_email = ssm.StringParameter.value_for_string_parameter(
5858
self, "/imap/ialirt/rsync_alarm_email"
@@ -76,6 +76,13 @@ def create_realtime_lambda(
7676
],
7777
)
7878

79+
lambda_role.add_to_policy(
80+
iam.PolicyStatement(
81+
actions=["cloudwatch:PutMetricData"],
82+
resources=["*"],
83+
)
84+
)
85+
7986
s3_read_write_policy = iam.PolicyStatement(
8087
effect=iam.Effect.ALLOW,
8188
actions=["s3:ListBucket", "s3:GetObject"],

sds_data_manager/lambda_code/IAlirtCode/ialirt_rsync_alarm.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22

33
import json
44
import logging
5-
from datetime import datetime, timezone
65

76
import boto3
87
import botocore
98
from botocore.client import BaseClient
109

11-
from .ialirt_realtime import query_filenames
12-
1310
logger = logging.getLogger(__name__)
1411
logger.setLevel(logging.INFO)
1512

1613

17-
def check_for_rsync_failure(
18-
s3_client: BaseClient, filenames: list, bucket: str
19-
) -> bool:
14+
def check_for_rsync_failure(s3_client: BaseClient, key: str, bucket: str) -> bool:
2015
"""Scan recent log files in an S3 bucket for 'rsync' command failures.
2116
2217
This function iterates through the specified log files stored in the given
@@ -28,8 +23,8 @@ def check_for_rsync_failure(
2823
----------
2924
s3_client : BaseClient
3025
A boto3 S3 client used to retrieve log objects from the bucket.
31-
filenames : list
32-
List of S3 object keys (filenames) to scan for the failure message.
26+
key : str
27+
The S3 object key (filename) to scan for the failure message.
3328
bucket : str
3429
Name of the S3 bucket containing the log files.
3530
@@ -39,13 +34,12 @@ def check_for_rsync_failure(
3934
True if any log file contains the string 'command failed: rsync';
4035
False otherwise.
4136
"""
42-
for key in filenames:
43-
obj = s3_client.get_object(Bucket=bucket, Key=f"logs/{key}")
44-
body = obj["Body"]
45-
for line in body.iter_lines():
46-
if b"command failed: rsync" in line:
47-
logger.warning(f"Found rsync failure in {key}")
48-
return True
37+
obj = s3_client.get_object(Bucket=bucket, Key=key)
38+
body = obj["Body"]
39+
for line in body.iter_lines():
40+
if b"command failed: rsync" in line:
41+
logger.warning(f"Found rsync failure in {key}")
42+
return True
4943
return False
5044

5145

@@ -69,7 +63,9 @@ def publish_failure_metric(found: bool):
6963
},
7064
],
7165
)
72-
logger.info(f"Published CloudWatch metric: IMAP/Ialirt::RsyncFailures = {value}")
66+
logger.info(
67+
f"Published CloudWatch metric: IMAP/Ialirt::IalirtRsyncFailures = {value}"
68+
)
7369

7470

7571
def lambda_handler(event, context):
@@ -90,27 +86,15 @@ def lambda_handler(event, context):
9086

9187
bucket = event["detail"]["bucket"]["name"]
9288
region = event["region"]
89+
key = event["detail"]["object"]["key"]
9390

9491
s3_client = boto3.client(
9592
"s3",
9693
region_name=region,
9794
config=botocore.client.Config(signature_version="s3v4"),
9895
)
9996

100-
if "now" in event:
101-
now = datetime.fromisoformat(event["now"].replace("Z", "")).replace(
102-
tzinfo=timezone.utc
103-
)
104-
else:
105-
now = datetime.now(timezone.utc)
106-
107-
filenames = query_filenames(s3_client, bucket, now)
108-
filenames = sorted(filenames)
109-
if not filenames:
110-
logger.info("No log files found in the last 48 hours.")
111-
return {"statusCode": 204, "body": ""}
112-
113-
found = check_for_rsync_failure(s3_client, filenames, bucket)
97+
found = check_for_rsync_failure(s3_client, key, bucket)
11498

11599
publish_failure_metric(found)
116100

0 commit comments

Comments
 (0)