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

Commit

Permalink
replacing obsolte alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerpen80 committed May 2, 2024
1 parent b1d33b5 commit 646198d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions alarm_creator/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ def get_alarm_params():

# Alarm creator
def AWS_Alarms():
thresholds = []
alarms = get_alarm_params()
for service in alarms:

# Fill instances variable with Running instances per service
if service == "EC2":
instances = GetRunningInstances()
Expand All @@ -34,7 +35,7 @@ def AWS_Alarms():
elif service == "ECS":
instances = GetRunningClusters()
for alarm in alarms[service]:

# Query the namespaces in CloudWatch Metrics
response = CWclient.list_metrics(Namespace=f"{alarms[service][alarm]['Namespace']}", RecentlyActive='PT3H',)
for metrics in response["Metrics"]:
Expand All @@ -44,7 +45,7 @@ def AWS_Alarms():
for dimensions in metrics["Dimensions"]:
if dimensions["Name"] == alarms[service][alarm]['Dimensions']:
for priority, threshold in zip(alarms[service][alarm]['AlarmThresholds']["priority"], alarms[service][alarm]['AlarmThresholds']["alarm_threshold"]):

thresholds.append(threshold)
# To make alarmnames pretty, 'MB/GB' is used instead of 1000000/1000000000 bytes, needs to be in bytes for actual threshold
if alarms[service][alarm]['Description']['ThresholdUnit'] == "GB":
cw_threshold = int(threshold) * 1000000000
Expand All @@ -66,7 +67,7 @@ def AWS_Alarms():
except KeyError: #
dimensionlist = []
dimensionlist.insert(0, instanceDimensions)

for instance in instances:

# Create alarms
Expand All @@ -85,6 +86,7 @@ def AWS_Alarms():
Dimensions=dimensionlist,
Tags=[{"Key": "CreatedbyLambda", "Value": "True"}],
)
return thresholds

def GetRunningInstances():
get_running_instances = ec2client.describe_instances(
Expand Down Expand Up @@ -118,7 +120,7 @@ def GetRunningClusters():

return RunningClusterNames

def DeleteAlarms():
def DeleteAlarms(thresholds):
get_alarm_info = CWclient.describe_alarms()
RunningInstances = GetRunningInstances()
RunningRDSInstances = GetRunningDBInstances()
Expand All @@ -139,3 +141,6 @@ def DeleteAlarms():
elif len(cluster_name) == 1:
if cluster_name[0]["Value"] not in RunningClusters:
CWclient.delete_alarms(AlarmNames=[metricalarm["AlarmName"]])
elif metricalarm["Threshold"] not in thresholds:
CWclient.delete_alarms(AlarmNames=[metricalarm["AlarmName"]])

4 changes: 2 additions & 2 deletions alarm_creator/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def lambda_handler(event, context):

try:
print("{}: AWS_Alarms()".format(datetime.datetime.now()))
AWS_Alarms()
thresholds = AWS_Alarms()
print("{}: DeleteAlarms()".format(datetime.datetime.now()))
DeleteAlarms()
DeleteAlarms(thresholds)
print("{}: Finished()".format(datetime.datetime.now()))
except Exception as exp:
exception_type, exception_value, exception_traceback = sys.exc_info()
Expand Down

0 comments on commit 646198d

Please sign in to comment.