22
33import json
44import logging
5- from datetime import datetime , timezone
65
76import boto3
87import botocore
98from botocore .client import BaseClient
109
11- from .ialirt_realtime import query_filenames
12-
1310logger = logging .getLogger (__name__ )
1411logger .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
7571def 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