-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for suppressing notifications (#3)
Users can specify MSK cluster states that should not trigger a notification. - Updated pre-commit hooks to use newer versions: - antonbabenko/[email protected] - pre-commit/[email protected]
- Loading branch information
1 parent
5026a20
commit 45af487
Showing
8 changed files
with
73 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,6 @@ terraform.rc | |
|
||
# Python virtual environment | ||
.venv | ||
|
||
# Lambda zip directory | ||
out/ |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ No inputs. | |
## Outputs | ||
|
||
No outputs. | ||
<!-- END_TF_DOCS --> | ||
<!-- END_TF_DOCS --> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,47 @@ | ||
import boto3 | ||
import os | ||
|
||
|
||
def lambda_handler(event, context): | ||
LAMBDASNSTOPIC = os.environ['SNS_TOPIC_ARN'] | ||
region = 'eu-central-1' | ||
LAMBDASNSTOPIC = os.environ["SNS_TOPIC_ARN"] | ||
SUPPRESS_STATES = os.environ["SUPPRESS_STATES"].split(",") | ||
region = "eu-central-1" | ||
# Create an MSK client | ||
client = boto3.client('kafka', region_name=region) | ||
client = boto3.client("kafka", region_name=region) | ||
# Retrieve a list of clusters | ||
response = client.list_clusters() | ||
# Extract the cluster ARNs from the response | ||
cluster_arns = response['ClusterInfoList'] | ||
cluster_arns = response["ClusterInfoList"] | ||
|
||
valid_states = ["ACTIVE"] + SUPPRESS_STATES | ||
print( | ||
"Notifications suppressed for these MSK states: {}".format( | ||
", ".join(valid_states) | ||
) | ||
) | ||
|
||
for cluster in cluster_arns: | ||
arn = cluster['ClusterArn'] | ||
arn = cluster["ClusterArn"] | ||
response = client.describe_cluster(ClusterArn=arn) | ||
status = response['ClusterInfo']['State'] | ||
sns_client = boto3.client('sns') | ||
|
||
if status != 'ACTIVE': | ||
print("The MSK cluster: {} needs attention.".format(arn)) | ||
sns_client.publish(TopicArn=LAMBDASNSTOPIC, | ||
Message="MSK cluster: " + arn + " needs attention. The status is: " + status, | ||
Subject="MSK Health Warning!") | ||
status = response["ClusterInfo"]["State"] | ||
print("The cluster is in state {}.".format(status)) | ||
sns_client = boto3.client("sns") | ||
if status not in valid_states: | ||
print("The MSK cluster: {} needs attention.".format(arn)) | ||
sns_client.publish( | ||
TopicArn=LAMBDASNSTOPIC, | ||
Message="MSK cluster: " | ||
+ arn | ||
+ " needs attention. The status is: " | ||
+ status, | ||
Subject="MSK Health Warning!", | ||
) | ||
else: | ||
print( | ||
"The MSK cluster: {} is in a healthy state, and is reachable and available for use.".format( | ||
arn)) | ||
print( | ||
"The MSK cluster: {} is in a healthy state, and is reachable and available for use.".format( | ||
arn | ||
) | ||
) | ||
|
||
# Return the status | ||
return { | ||
'statusCode': 200, | ||
'body': 'OK' | ||
} | ||
|
||
if __name__ == '__main__': | ||
lambda_handler(None, None) | ||
return {"statusCode": 200, "body": "OK"} |
This file contains 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
This file contains 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