Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #18 from TechNative-B-V/ecs_alarms
Browse files Browse the repository at this point in the history
Added ecs alarms to alarm creator
  • Loading branch information
AndrNgg authored Jan 31, 2024
2 parents 9d9ce23 + 7020445 commit 2475d6a
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
144 changes: 144 additions & 0 deletions alarm_creator/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ec2 = boto3.resource("ec2")
rds = boto3.client("rds")
ec2client = boto3.client("ec2")
ecsclient = boto3.client("ecs")

# Create alarms for Memory and disk usage received by CloudWatch Agent.
def Cwagent_alarms():
Expand Down Expand Up @@ -239,6 +240,8 @@ def RDS_Alarms():
rds_threshold = {
"alarm_threshold": ["2", "5", "10"],
"priority": ["P1", "P2", "P3"],
"alarm_threshold_swap": ["256", "200", "128"],
"alarm_threshold_freemem": ["200", "250", "350"],
}

response = CWclient.list_metrics(
Expand Down Expand Up @@ -276,7 +279,148 @@ def RDS_Alarms():
],
Tags=[{"Key": "CreatedbyLambda", "Value": "True"}],
)
elif metrics["MetricName"] == "SwapUsage":
for dimensions in metrics["Dimensions"]:
if dimensions["Name"] == "DBInstanceIdentifier":
for priority, threshold_swap in zip(
rds_threshold["priority"],
rds_threshold["alarm_threshold_swap"]
):
threshold_swap_bytes= int(threshold_swap) * 1000000

# Create alarm to notify when RDS database is running high on swap usage.
CWclient.put_metric_alarm(
AlarmName=f"{dimensions['Value']}-SwapUsage > {threshold_swap} MB",
ComparisonOperator="GreaterThanThreshold",
EvaluationPeriods=2,
MetricName="SwapUsage",
Namespace="AWS/RDS",
Period=300,
Statistic="Maximum",
Threshold=threshold_swap_bytes,
ActionsEnabled=True,
TreatMissingData="breaching",
AlarmDescription=f"{priority}",
Dimensions=[
{
"Name": "DBInstanceIdentifier",
"Value": f"{dimensions['Value']}",
},
],
Tags=[{"Key": "CreatedbyLambda", "Value": "True"}],
)

elif metrics["MetricName"] == "FreeableMemory":
for dimensions in metrics["Dimensions"]:
if dimensions["Name"] == "DBInstanceIdentifier":
for priority, threshold_freemem in zip(
rds_threshold["priority"],
rds_threshold["alarm_threshold_freemem"]
):
threshold_mem_bytes= int(threshold_freemem) * 1000000

# Create alarm to notify when RDS database is running low on freeable memory.
CWclient.put_metric_alarm(
AlarmName=f"{dimensions['Value']}-FreeableMemory < {threshold_freemem} MB",
ComparisonOperator="LessThanThreshold",
EvaluationPeriods=2,
MetricName="FreeableMemory",
Namespace="AWS/RDS",
Period=300,
Statistic="Maximum",
Threshold=threshold_mem_bytes,
ActionsEnabled=True,
TreatMissingData="breaching",
AlarmDescription=f"{priority}",
Dimensions=[
{
"Name": "DBInstanceIdentifier",
"Value": f"{dimensions['Value']}",
},
],
Tags=[{"Key": "CreatedbyLambda", "Value": "True"}],
)

def ECS_Alarms():

taskcount_threshold = {
"alarm_threshold": ["1", "2", "3"],
"priority": ["P1", "P2", "P3"],
}

response_insights = CWclient.list_metrics(
Namespace="ECS/ContainerInsights"
, RecentlyActive='PT3H',
)
response = CWclient.list_metrics(
Namespace="AWS/ECS"
, RecentlyActive='PT3H',
)

for metrics in response_insights["Metrics"]:
if metrics["MetricName"] == "TaskCount":
for dimensions in metrics["Dimensions"]:
if dimensions["Name"] == "ClusterName":
for priority, threshold in zip(
taskcount_threshold["priority"], taskcount_threshold["alarm_threshold"]
):
threshold = int(threshold)
# Create alarm to notify when ECS tasks number is below threshold.
CWclient.put_metric_alarm(
AlarmName=f"{dimensions['Value']}-TaskCount < {threshold}",
ComparisonOperator="LessThanThreshold",
EvaluationPeriods=2,
MetricName="TaskCount",
Namespace="ECS/ContainerInsights",
Period=300,
Statistic="Minimum",
Threshold=threshold,
ActionsEnabled=True,
TreatMissingData="breaching",
AlarmDescription=f"{priority}",
Dimensions=[
{
"Name": "ClusterName",
"Value": f"{dimensions['Value']}",
},
],
Tags=[{"Key": "CreatedbyLambda", "Value": "True"}],
)
taskcount_threshold = {
"alarm_threshold": ["1"],
"priority": ["P1"],
}
for metrics in response_insights["Metrics"]:
if metrics["MetricName"] == "CpuUtilized":
for dimensions in metrics["Dimensions"]:
if dimensions["Name"] == "ServiceName":
for priority, threshold in zip(
taskcount_threshold["priority"], taskcount_threshold["alarm_threshold"]
):
threshold = int(threshold)
# Create alarm to notify when ECS tasks number is below threshold.
CWclient.put_metric_alarm(
AlarmName=f"{dimensions['Value']}-TaskCount < {threshold}",
ComparisonOperator="LessThanThreshold",
EvaluationPeriods=2,
MetricName="CpuUtilized",
Namespace="ECS/ContainerInsights",
Period=300,
Statistic="Minimum",
Threshold=threshold,
ActionsEnabled=True,
TreatMissingData="breaching",
AlarmDescription=f"{priority}",
Dimensions=[
{
"Name": "ClusterName",
"Value": f"{dimensions['Value']}",
},
],
Tags=[{"Key": "CreatedbyLambda", "Value": "True"}],
)


def GetRunningInstances():
get_running_instances = ec2client.describe_instances(
Filters=[{"Name": "instance-state-name", "Values": ["running"]}]
Expand Down
4 changes: 3 additions & 1 deletion alarm_creator/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import traceback
import sys
import datetime
from actions import Cwagent_alarms, AWS_EC2_Alarms, RDS_Alarms, DeleteAlarms
from actions import Cwagent_alarms, AWS_EC2_Alarms, RDS_Alarms, ECS_Alarms, DeleteAlarms

logger = logging.getLogger()
logger.setLevel(logging.INFO)
Expand All @@ -24,6 +24,8 @@ def lambda_handler(event, context):
AWS_EC2_Alarms()
print("{}: RDS_Alarms()".format(datetime.datetime.now()))
RDS_Alarms()
print("{}: ECS_Alarms()".format(datetime.datetime.now()))
ECS_Alarms()
print("{}: DeleteAlarms()".format(datetime.datetime.now()))
DeleteAlarms()
print("{}: Finished()".format(datetime.datetime.now()))
Expand Down

0 comments on commit 2475d6a

Please sign in to comment.