-
Notifications
You must be signed in to change notification settings - Fork 2
/
cloudformation.yaml
772 lines (712 loc) · 40.1 KB
/
cloudformation.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
Description: "Deploy auto-add-cw-alarms."
#
# This just formats the page that prompts for the parameters when using the AWS Console to deploy your stack.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: "Configuration Parameters"
Parameters:
- SNStopic
- accountId
- customerId
- defaultCPUThreshold
- defaultSSDThreshold
- defaultVolumeThreshold
- checkInterval
- alarmPrefixString
- regions
Parameters:
SNStopic:
Description: "The SNS Topic name where CloudWatch will send alerts to. Note that it is assumed that the SNS topic, with the same name, will exist in all the regions where alarms are to be created."
Type: String
accountId:
Description: "The AWS account ID associated with the SNStopic. This is only used to compute the ARN to the SNS Topic."
Type: String
customerId:
Description: "This is really just a comment that will be added to the alarm description."
Type: String
Default: ""
defaultCPUThreshold:
Description: "This will define the default CPU utilization threshold. You can override the default by having a specific tag associated with the file system. See below for more information."
Type: Number
MinValue: 0
MaxValue: 100
Default: 80
defaultSSDThreshold:
Description: "This will define the default SSD (aggregate) utilization threshold. You can override the default by having a specific tag associated with the file system. See below for more information."
Type: Number
MinValue: 0
MaxValue: 100
Default: 80
defaultVolumeThreshold:
Description: "This will define the default Volume utilization threshold. You can override the default by having a specific tag associated with the volume. See below for more information."
Type: Number
MinValue: 0
MaxValue: 100
Default: 80
checkInterval:
Description: "This is how often you want the Lambda function to run to look for new file systems and/or volumes (minutes)."
Type: Number
MinValue: 5
Default: 15
alarmPrefixString:
Description: "This is the string that will be prepended to all CloudWatch alarms created by this script."
Type: String
Default: "FSx-ONTAP-Auto"
regions:
Description: "This is a list of AWS regions that you want the Lambda function to run in. If left blank, it will run in all regions."
Type: CommaDelimitedList
Default: ""
Resources:
LambdaRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: !Sub "Lambda-Role-for-${AWS::StackName}"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "lambda.amazonaws.com"
Action: "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
- PolicyName: "LambdaPolicy_for_Auto_Add_CW_Alarms"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "fsx:DescribeFileSystems"
- "fsx:DescribeVolumes"
- "fsx:ListTagsForResource"
- "ec2:DescribeRegions"
- "cloudwatch:DescribeAlarms"
- "cloudwatch:DescribeAlarmsForMetric"
- "cloudwatch:PutMetricAlarm"
Resource: "*"
- Effect: "Allow"
Action:
- "cloudwatch:DeleteAlarms"
Resource: !Sub "arn:aws:cloudwatch:*:${AWS::AccountId}:alarm:${alarmPrefixString}*"
SchedulerRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: !Sub "SchedulerRole-for-${AWS::StackName}"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "scheduler.amazonaws.com"
Action: "sts:AssumeRole"
Policies:
- PolicyName: "SchedulerPolicy_for_Auto_Add_CW_Alarms"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "lambda:InvokeFunction"
Resource: !GetAtt LambdaFunction.Arn
LambdaScheduler:
Type: "AWS::Scheduler::Schedule"
Properties:
Description: "Schedule the auto_add_cw_alarms Lambda function."
Name: !Sub "Schedule-for-${AWS::StackName}"
FlexibleTimeWindow:
Mode: "OFF"
ScheduleExpression: !Sub "rate(${checkInterval} minutes)"
Target:
Arn: !GetAtt LambdaFunction.Arn
RoleArn: !GetAtt SchedulerRole.Arn
LambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
FunctionName: !Sub "Lambda-for-${AWS::StackName}"
Role: !GetAtt LambdaRole.Arn
PackageType: "Zip"
Runtime: "python3.12"
Handler: "index.lambda_handler"
Timeout: 300
Environment:
Variables:
SNStopic: !Ref SNStopic
accountId: !Ref accountId
customerId: !Ref customerId
defaultCPUThreshold: !Ref defaultCPUThreshold
defaultSSDThreshold: !Ref defaultSSDThreshold
defaultVolumeThreshold: !Ref defaultVolumeThreshold
basePrefix: !Ref alarmPrefixString
regions: !Join [",", !Ref regions]
Code:
ZipFile: |
#!/usr/bin/python3
#
# This script is used to add CloudWatch alarms for all the FSx for NetApp
# ONTAP volumes, that don't already have one, that will trigger when the
# utilization of the volume gets above the threshold defined below. It will
# also create an alarm that will trigger when the file system reach
# an average CPU utilization greater than what is specified below as well
# an alarm that will trigger when the SSD utilization is greater than what
# is specified below.
#
# It can either be run as a standalone script, or uploaded as a Lambda
# function with the thought being that you will create a EventBridge schedule
# to invoke it periodically.
#
# It will scan all regions looking for FSxN volumes and file systems
# and since CloudWatch can't send SNS messages across regions, it assumes
# that the specified SNS topic exist in each region for the specified
# account ID.
#
# Finally, a default volume threshold is defined below. It sets the volume
# utilization threshold that will cause CloudWatch to send the alarm event
# to the SNS topic. It can be overridden on a per volume basis by having a
# tag with the name of "alarm_threshold" set to the desired threshold.
# If the tag is set to 100, then no alarm will be created. You can also
# set an override to the filesystem CPU utilization alarm, but setting
# a tag with the name of 'CPU_Alarm_Threshold' on the file system resouce.
# Lastly, you can create an override for the SSD alarm, by creating a tag
# with the name "SSD_Alarm_Threshold" on the file system resource.
#
# Version: v2.13
# Date: 2024-10-03-12:30:21
#
################################################################################
#
# The following variables effect the behavior of the script. They can be
# either be set here, overridden via the command line options, or
# overridden by environment variables.
#
# Define which SNS topic you want "volume full" message to be sent to.
SNStopic=''
#
# Provide the account id the SNS topic resides under:
# MUST be a string.
accountId=''
#
# Set the customer ID associated with the AWS account. This is used to
# as part of the alarm name prefix so a customer ID can be associated
# with the alarm. If it is left as an empty string, no extra prefix
# will be added.
customerId=''
#
# Define the default CPU utilization threshold before sending the alarm.
# Setting it to 100 will disable the creation of the alarm.
defaultCPUThreshold=80
#
# Define the default SSD utilization threshold before sending the alarm.
# Setting it to 100 will disable the creation of the alarm.
defaultSSDThreshold=90
#
# Define the default volume utilization threshold before sending the alarm.
# Setting it to 100 will disable the creation of the alarm.
defaultVolumeThreshold=80
#
#
################################################################################
# You can't change the following variables from the command line or environment
# variables since changing them after the program has run once would cause
# all existing CloudWatch alarms to be abandoned, and all new alarms to be
# created. So, it is not recommended to change these variables unless you know
# what you are doing.
################################################################################
#
# The following is put in front of all alarms so an IAM policy can be create
# that will allow this script to only be able to delete the alarms it creates.
# If you change this, you must also change the IAM policy. It can be
# set via an environment variable, this is so that the CloudFormation template
# can pass the value to the Lambda function. To change the value, change
# the "FSx-ONTAP-Auto" string to your desired value.
import os
basePrefix = os.environ.get('basePrefix', "FSx-ONTAP-Auto")
#
# Define the prefix for the volume utilization alarm name for the CloudWatch alarms.
alarmPrefixVolume=f"{basePrefix}-Volume_Utilization_for_volume_"
#
# Define the prefix for the CPU utilization alarm name for the CloudWatch alarms.
alarmPrefixCPU=f"{basePrefix}-CPU_Utilization_for_fs_"
#
# Define the prefix for the SSD utilization alarm name for the CloudWatch alarms.
alarmPrefixSSD=f"{basePrefix}-SSD_Utilization_for_fs_"
################################################################################
# You shouldn't have to modify anything below here.
################################################################################
import botocore
from botocore.config import Config
import boto3
import getopt
import sys
import time
import json
################################################################################
# This function adds the SSD Utilization CloudWatch alarm.
################################################################################
def add_ssd_alarm(cw, fsId, alarmName, alarmDescription, threshold, region):
action = 'arn:aws:sns:' + region + ':' + accountId + ':' + SNStopic
if not dryRun:
cw.put_metric_alarm(
AlarmName=alarmName,
ActionsEnabled=True,
AlarmActions=[action],
AlarmDescription=alarmDescription,
EvaluationPeriods=1,
DatapointsToAlarm=1,
Threshold=threshold,
ComparisonOperator='GreaterThanThreshold',
MetricName="StorageCapacityUtilization",
Period=300,
Statistic="Average",
Namespace="AWS/FSx",
Dimensions=[{'Name': 'FileSystemId', 'Value': fsId}, {'Name': 'StorageTier', 'Value': 'SSD'}, {'Name': 'DataType', 'Value': 'All'}]
)
else:
print(f'Would have added SSD alarm for {fsId} with name {alarmName} with thresold of {threshold} in {region} with action {action}')
################################################################################
# This function adds the CPU Utilization CloudWatch alarm.
################################################################################
def add_cpu_alarm(cw, fsId, alarmName, alarmDescription, threshold, region):
action = 'arn:aws:sns:' + region + ':' + accountId + ':' + SNStopic
if not dryRun:
cw.put_metric_alarm(
AlarmName=alarmName,
ActionsEnabled=True,
AlarmActions=[action],
AlarmDescription=alarmDescription,
EvaluationPeriods=1,
DatapointsToAlarm=1,
Threshold=threshold,
ComparisonOperator='GreaterThanThreshold',
MetricName="CPUUtilization",
Period=300,
Statistic="Average",
Namespace="AWS/FSx",
Dimensions=[{'Name': 'FileSystemId', 'Value': fsId}]
)
else:
print(f'Would have added CPU alarm for {fsId} with name {alarmName} with thresold of {threshold} in {region} with action {action}.')
################################################################################
# This function adds the Volume utilization CloudWatch alarm.
################################################################################
def add_volume_alarm(cw, volumeId, alarmName, alarmDescription, fsId, threshold, region):
action = 'arn:aws:sns:' + region + ':' + accountId + ':' + SNStopic
if not dryRun:
cw.put_metric_alarm(
ActionsEnabled=True,
AlarmName=alarmName,
AlarmActions=[action],
AlarmDescription=alarmDescription,
EvaluationPeriods=1,
DatapointsToAlarm=1,
Threshold=threshold,
ComparisonOperator='GreaterThanThreshold',
Metrics=[{"Id":"e1","Label":"Utilization","ReturnData":True,"Expression":"m2/m1*100"},\
{"Id":"m2","ReturnData":False,"MetricStat":{"Metric":{"Namespace":"AWS/FSx","MetricName":"StorageUsed","Dimensions":[{"Name":"VolumeId","Value": volumeId},{"Name":"FileSystemId","Value":fsId}]},"Period":300,"Stat":"Average"}},\
{"Id":"m1","ReturnData":False,"MetricStat":{"Metric":{"Namespace":"AWS/FSx","MetricName":"StorageCapacity","Dimensions":[{"Name":"VolumeId","Value": volumeId},{"Name":"FileSystemId","Value":fsId}]},"Period":300,"Stat":"Average"}}]
)
else:
print(f'Would have added volume alarm for {volumeId} {fsId} with name {alarmName} with thresold of {threshold} in {region} with action {action}.')
################################################################################
# This function deletes a CloudWatch alarm.
################################################################################
def delete_alarm(cw, alarmName):
if not dryRun:
cw.delete_alarms(AlarmNames=[alarmName])
else:
print(f'Would have deleted alarm {alarmName}.')
################################################################################
# This function checks to see if the alarm already exists.
################################################################################
def contains_alarm(alarmName, alarms):
for alarm in alarms:
if(alarm['AlarmName'] == alarmName):
return True
return False
################################################################################
# This function checks to see if a volume exists.
################################################################################
def contains_volume(volumeId, volumes):
for volume in volumes:
if(volume['VolumeId'] == volumeId):
return True
return False
################################################################################
# This function checks to see if a file system exists.
################################################################################
def contains_fs(fsId, fss):
for fs in fss:
if(fs['FileSystemId'] == fsId):
return True
return False
################################################################################
# This function returns the value assigned to the "alarm_threshold" tag
# associated with the arn passed in. If none is found, it returns the default
# threshold set above.
################################################################################
def getAlarmThresholdTagValue(fsx, arn):
#
# If there are a lot of volumes, we could get hit by the AWS rate limit,
# so we will sleep for a short period of time and then retry. We will
# double the sleep time each time we get a rate limit exception until
# we get to 5 seconds, then we will just raise the exception.
sleep=.125
#
# This is put into a try block because it is possible that the volume
# is deleted between the time we get the list of volumes and the time
# we try to get the tags for the volume.
while True:
try:
tags = fsx.list_tags_for_resource(ResourceARN=arn)
for tag in tags['Tags']:
if(tag['Key'].lower() == "alarm_threshold"):
return(tag['Value'])
return(defaultVolumeThreshold)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFound':
return(100) # Return 100 so we don't try to create an alarm.
if e.response['Error']['Code'] == 'TooManyRequestsException' or e.response['Error']['Code'] == 'ThrottlingException':
sleep = sleep * 2
if sleep > 5:
raise e
print(f"Warning: Rate Limit fault while getting tags. Sleeping for {sleep} seconds.")
time.sleep(sleep)
else:
print(f"boto3 client error: {json.dumps(e.response)}")
raise e
################################################################################
# This function returns the value assigned to the "CPU_alarm_threshold" tag
# that is in the array of tags passed in. if it doesn't find that tag it
# returns the default threshold set above.
################################################################################
def getCPUAlarmThresholdTagValue(tags):
for tag in tags:
if(tag['Key'].lower() == "cpu_alarm_threshold"):
return(tag['Value'])
return(defaultCPUThreshold)
################################################################################
# This function returns the value assigned to the "CPU_alarm_threshold" tag
# that is in the array of tags passed in. if it doesn't find that tag it
# returns the default threshold set above.
################################################################################
def getSSDAlarmThresholdTagValue(tags):
for tag in tags:
if(tag['Key'].lower() == "ssd_alarm_threshold"):
return(tag['Value'])
return(defaultSSDThreshold)
################################################################################
# This function returns the file system id that the passed in alarm is
# associated with.
################################################################################
def getFileSystemId(alarm):
for metric in alarm['Metrics']:
if metric["Id"] == "m1":
for dim in metric['MetricStat']['Metric']['Dimensions']:
if dim['Name'] == 'FileSystemId':
return dim['Value']
return None
################################################################################
# This function will return all the file systems in the region. It will handle the
# case where there are more file systms than can be returned in a single call.
# It will also handle the case where we get a rate limit exception.
################################################################################
def getFss(fsx):
# The initial amount of time to sleep if there is a rate limit exception.
sleep=.125
while True:
try:
response = fsx.describe_file_systems()
fss = response['FileSystems']
nextToken = response.get('NextToken')
sleep=.125
break
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'TooManyRequestsException' or e.response['Error']['Code'] == 'ThrottlingException':
sleep = sleep * 2 # Exponential backoff.
if sleep > 5:
raise e
print(f"Warning: Rate Limit fault while getting initial file system list. Sleeping for {sleep} seconds.")
time.sleep(sleep)
else:
print(f"boto3 client error: {json.dumps(e.response)}")
raise e
while nextToken:
try:
response = fsx.describe_file_systems(NextToken=nextToken)
fss += response['FileSystems']
nextToken = response.get('NextToken')
sleep=.125
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'TooManyRequestsException' or e.response['Error']['Code'] == 'ThrottlingException':
sleep = sleep * 2 # Exponential backoff.
if sleep > 5:
raise e
print(f"Warning: Rate Limit fault while getting additional file systems. Sleeping for {sleep} seconds.")
time.sleep(sleep)
else:
print(f"boto3 client error: {json.dumps(e.response)}")
raise e
return fss
################################################################################
# This function will return all the volumes in the region. It will handle the
# case where there are more volumes than can be returned in a single call.
# It will also handle the case where we get a rate limit exception.
################################################################################
def getVolumes(fsx):
#
# The initial amount of time to sleep if there is a rate limit exception.
sleep=.125
while True:
try:
response = fsx.describe_volumes()
volumes = response['Volumes']
nextToken = response.get('NextToken')
sleep=.125
break
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'TooManyRequestsException' or e.response['Error']['Code'] == 'ThrottlingException':
sleep = sleep * 2 # Exponential backoff.
if sleep > 5:
raise e
print(f"Warning: Rate Limit fault while getting the initial list of volumes. Sleeping for {sleep} seconds.")
time.sleep(sleep)
else:
print(f"boto3 client error: {json.dumps(e.response)}")
raise e
while nextToken:
try:
response = fsx.describe_volumes(NextToken=nextToken)
volumes += response['Volumes']
nextToken = response.get('NextToken')
sleep=.125
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'TooManyRequestsException' or e.response['Error']['Code'] == 'ThrottlingException':
sleep = sleep * 2 # Exponential backoff.
if sleep > 5:
raise e
print(f"Warning: Rate Limit fault while getting additional volumes. Sleeping for {sleep} seconds.")
time.sleep(sleep)
else:
print(f"boto3 client error: {json.dumps(e.response)}")
raise e
return volumes
################################################################################
# This function will return all the alarms in the region. It will handle the
# case where there are more alarms than can be returned in a single call.
# It will also handle the case where we get a rate limit exception.
################################################################################
def getAlarms(cw):
# The initial amount of time to sleep if there is a rate limit exception.
sleep=.125
while True:
try:
response = cw.describe_alarms()
alarms = response['MetricAlarms']
nextToken = response.get('NextToken')
sleep=.125
break
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'TooManyRequestsException' or e.response['Error']['Code'] == 'ThrottlingException':
sleep = sleep * 2
if sleep > 5:
raise e
print(f"Warning: Rate Limit fault while getting the initial list of alarms. Sleeping for {sleep} seconds.")
time.sleep(sleep)
else:
print(f"boto3 client error: {json.dumps(e.response)}")
raise e
while nextToken:
try:
response = cw.describe_alarms(NextToken=nextToken)
alarms += response['MetricAlarms']
nextToken = response.get('NextToken')
sleep=.125
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'TooManyRequestsException' or e.response['Error']['Code'] == 'ThrottlingException':
sleep = sleep * 2 # Exponential backoff.
if sleep > 5:
raise e
print(f"Warning: Rate Limit fault while getting additional alarms. Sleeping for {sleep} seconds.")
time.sleep(sleep)
else:
print(f"boto3 client error: {json.dumps(e.response)}")
raise e
return alarms
################################################################################
# This is the main logic of the program. It loops on all the regions then all
# the fsx volumes within the region, checking to see if any of them already
# have a CloudWatch alarm, and if not, add one.
################################################################################
def lambda_handler(event, context):
global customerId, regions, SNStopic, accountId, onlyFilesystemId
#
# If the customer ID is set, reformat it to be used in the alarm description.
if customerId != '':
customerId = f", CustomerID: {customerId}"
if len(SNStopic) == 0:
raise Exception("You must specify a SNS topic to send the alarm messages to.")
if len(accountId) == 0:
raise Exception("You must specify an accountId to run this program.")
#
# Configure boto3 to use the more advanced "adaptive" retry method.
boto3Config = Config(
retries = {
'max_attempts': 5,
'mode': 'adaptive'
}
)
if len(regions) == 0: # pylint: disable=E0601
ec2Client = boto3.client('ec2', config=boto3Config)
ec2Regions = ec2Client.describe_regions()['Regions']
for region in ec2Regions:
regions += [region['RegionName']]
fsxRegions = boto3.Session().get_available_regions('fsx')
for region in regions:
if region in fsxRegions:
print(f'Scanning {region}')
fsx = boto3.client('fsx', region_name=region, config=boto3Config)
cw = boto3.client('cloudwatch', region_name=region, config=boto3Config)
#
# Get all the file systems, volumes and alarm in the region.
fss = getFss(fsx)
volumes = getVolumes(fsx)
alarms = getAlarms(cw)
#
# Scan for filesystems without CPU Utilization Alarm.
for fs in fss:
if(fs['FileSystemType'] == "ONTAP"):
threshold = int(getCPUAlarmThresholdTagValue(fs['Tags']))
if(threshold != 100):
fsId = fs['FileSystemId']
fsName = fsId.replace('fs-', 'FsxId')
alarmName = alarmPrefixCPU + fsId
alarmDescription = f"CPU utilization alarm for file system {fsName}{customerId} in region {region}."
if(not contains_alarm(alarmName, alarms) and onlyFilesystemId == None or
not contains_alarm(alarmName, alarms) and onlyFilesystemId != None and onlyFilesystemId == fsId):
print(f'Adding CPU Alarm for {fs["FileSystemId"]}')
add_cpu_alarm(cw, fsId, alarmName, alarmDescription, threshold, region)
#
# Scan for CPU alarms without a FSxN filesystem.
for alarm in alarms:
alarmName = alarm['AlarmName']
if(alarmName[:len(alarmPrefixCPU)] == alarmPrefixCPU):
fsId = alarmName[len(alarmPrefixCPU):]
if(not contains_fs(fsId, fss) and onlyFilesystemId == None or
not contains_fs(fsId, fss) and onlyFilesystemId != None and onlyFilesystemId == fsId):
print("Deleting alarm: " + alarmName + " in region " + region)
delete_alarm(cw, alarmName)
#
# Scan for filesystems without SSD Utilization Alarm.
for fs in fss:
if(fs['FileSystemType'] == "ONTAP"):
threshold = int(getSSDAlarmThresholdTagValue(fs['Tags']))
if(threshold != 100):
fsId = fs['FileSystemId']
fsName = fsId.replace('fs-', 'FsxId')
alarmName = alarmPrefixSSD + fsId
alarmDescription = f"SSD utilization alarm for file system {fsName}{customerId} in region {region}."
if(not contains_alarm(alarmName, alarms) and onlyFilesystemId == None or
not contains_alarm(alarmName, alarms) and onlyFilesystemId != None and onlyFilesystemId == fsId):
print(f'Adding SSD Alarm for {fsId}')
add_ssd_alarm(cw, fs['FileSystemId'], alarmName, alarmDescription, threshold, region)
#
# Scan for SSD alarms without a FSxN filesystem.
for alarm in alarms:
alarmName = alarm['AlarmName']
if(alarmName[:len(alarmPrefixSSD)] == alarmPrefixSSD):
fsId = alarmName[len(alarmPrefixSSD):]
if(not contains_fs(fsId, fss) and onlyFilesystemId == None or
not contains_fs(fsId, fss) and onlyFilesystemId != None and onlyFilesystemId == fsId):
print("Deleteing alarm: " + alarmName + " in region " + region)
delete_alarm(cw, alarmName)
#
# Scan for volumes without alarms.
for volume in volumes:
if(volume['VolumeType'] == "ONTAP"):
volumeId = volume['VolumeId']
volumeName = volume['Name']
volumeARN = volume['ResourceARN']
fsId = volume['FileSystemId']
threshold = int(getAlarmThresholdTagValue(fsx, volumeARN))
if(threshold != 100): # No alarm if the value is set to 100.
alarmName = alarmPrefixVolume + volumeId
fsName = fsId.replace('fs-', 'FsxId')
alarmDescription = f"Volume utilization alarm for volumeId {volumeId}{customerId}, File System Name: {fsName}, Volume Name: {volumeName} in region {region}."
if(not contains_alarm(alarmName, alarms) and onlyFilesystemId == None or
not contains_alarm(alarmName, alarms) and onlyFilesystemId != None and onlyFilesystemId == fsId):
print(f'Adding volume utilization alarm for {volumeName} in region {region}.')
add_volume_alarm(cw, volumeId, alarmName, alarmDescription, fsId, threshold, region)
#
# Scan for volume alarms without volumes.
for alarm in alarms:
alarmName = alarm['AlarmName']
if(alarmName[:len(alarmPrefixVolume)] == alarmPrefixVolume):
volumeId = alarmName[len(alarmPrefixVolume):]
if(not contains_volume(volumeId, volumes) and onlyFilesystemId == None or
not contains_volume(volumeId, volumes) and onlyFilesystemId != None and onlyFilesystemId == getFileSystemId(alarm)):
print("Deleteing alarm: " + alarmName + " in region " + region)
delete_alarm(cw, alarmName)
return
################################################################################
# This function is used to print out the usage of the script.
################################################################################
def usage():
print('Usage: auto_add_cw_alarms [-h|--help] [-d|--dryRun] [[-c|--customerID customerID] [[-a|--accountID aws_account_id] [[-s|--SNSTopic SNS_Topic_Name] [[-r|--region region] [[-C|--CPUThreshold threshold] [[-S|--SSDThreshold threshold] [[-V|--VolumeThreshold threshold] [-F|--FileSystemID FileSystemID]')
################################################################################
# Main logic starts here.
################################################################################
#
# Set some default values.
regions = []
dryRun = False
#
# Check to see if there any any environment variables set.
customerId = os.environ.get('customerId', '')
accountId = os.environ.get('accountId', '')
SNStopic = os.environ.get('SNStopic', '')
onlyFilesystemId = None
defaultCPUThreshold = int(os.environ.get('defaultCPUThreshold', defaultCPUThreshold))
defaultSSDThreshold = int(os.environ.get('defaultSSDThreshold', defaultSSDThreshold))
defaultVolumeThreshold = int(os.environ.get('defaultVolumeThreshold', defaultVolumeThreshold))
regionsEnv = os.environ.get('regions', '')
if regionsEnv != '':
regions = regionsEnv.split(',')
#
# Check to see if we are bring run from a command line or a Lmabda function.
if os.environ.get('AWS_LAMBDA_FUNCTION_NAME') == None:
argumentList = sys.argv[1:]
options = "hc:a:s:dr:C:S:V:F:"
longOptions = ["help", "customerID=", "accountID=", "SNSTopic=", "dryRun", "region=", "CPUThreshold=", "SSDThreshold=", "VolumeThreshold=", "FileSystemID="]
skip = False
try:
arguments, values = getopt.getopt(argumentList, options, longOptions)
for currentArgument, currentValue in arguments:
if currentArgument in ("-h", "--help"):
usage()
skip = True
elif currentArgument in ("-c", "--customerID"):
customerId = currentValue
elif currentArgument in ("-a", "--accountID"):
accountId = currentValue
elif currentArgument in ("-s", "--SNSTopic"):
SNStopic = currentValue
elif currentArgument in ("-C", "--CPUThreshold"):
defaultCPUThreshold = int(currentValue)
elif currentArgument in ("-S", "--SSDThreshold"):
defaultSSDThreshold = int(currentValue)
elif currentArgument in ("-V", "--VolumeThreshold"):
defaultVolumeThreshold = int(currentValue)
elif currentArgument in ("-d", "--dryRun"):
dryRun = True
elif currentArgument in ("-r", "--region"):
regions += [currentValue]
elif currentArgument in ("-F", "--FileSystemID"):
onlyFilesystemId = currentValue
except getopt.error as err:
print(str(err))
usage()
skip = True
if not skip:
lambda_handler(None, None)