Last active
January 23, 2023 08:30
-
-
Save DaisukeMiyamoto/4cfcd9a44608b84785f8aa590ba16cfb to your computer and use it in GitHub Desktop.
Lambda function for subscribe slurmctld logs from Slurm on AWS ParallelCluster and submit Job messages to Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
import base64 | |
import gzip | |
import urllib.request | |
LOG_STREAM = 'ip-10-0-1-xxx.i-08f68c623be216d6e.slurmctld' | |
SLACK_WEB_HOOK = 'https://hooks.slack.com/services/XXXX' | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
def lambda_handler(event, context): | |
# TODO implement | |
decoded_data = base64.b64decode(event['awslogs']['data']) | |
json_data = json.loads(gzip.decompress(decoded_data)) | |
logger.info("EVENT: " + json.dumps(json_data)) | |
if json_data['logStream'] == LOG_STREAM: | |
for i in range(len(json_data['logEvents'])): | |
message = json_data['logEvents'][i]['message'] | |
print(message) | |
send_data = { | |
"text": message | |
} | |
send_text = json.dumps(send_data) | |
request = urllib.request.Request( | |
SLACK_WEB_HOOK, | |
data=send_text.encode('utf-8'), | |
method="POST" | |
) | |
with urllib.request.urlopen(request) as response: | |
response_body = response.read().decode('utf-8') | |
return { | |
'statusCode': 200, | |
'body': json.dumps('Hello from Lambda!') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment