| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the Ansible project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
module: cloudwatch_metric_alarm |
| 9 |
short_description: Create/update or delete AWS CloudWatch 'metric alarms' |
| 10 |
version_added: 5.0.0 |
| 11 |
description: |
| 12 |
- Can create or delete AWS CloudWatch metric alarms. |
| 13 |
- Metrics you wish to alarm on must already exist. |
| 14 |
- Prior to release 5.0.0 this module was called M(community.aws.ec2_metric_alarm). |
| 15 |
The usage did not change. |
| 16 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 17 |
author: |
| 18 |
- "Zacharie Eakin (@Zeekin)" |
| 19 |
options: |
| 20 |
state: |
| 21 |
description: |
| 22 |
- Register or deregister the alarm. |
| 23 |
choices: ['present', 'absent'] |
| 24 |
default: 'present' |
| 25 |
type: str |
| 26 |
name: |
| 27 |
description: |
| 28 |
- Unique name for the alarm. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
metric_name: |
| 32 |
description: |
| 33 |
- Name of the monitored metric (e.g. V(CPUUtilization)). |
| 34 |
- Metric must already exist. |
| 35 |
required: false |
| 36 |
type: str |
| 37 |
aliases: ['metric'] |
| 38 |
metrics: |
| 39 |
description: |
| 40 |
- An array of MetricDataQuery structures that enable |
| 41 |
you to create an alarm based on the result of a metric math expression. |
| 42 |
type: list |
| 43 |
required: false |
| 44 |
version_added: "5.5.0" |
| 45 |
elements: dict |
| 46 |
default: [] |
| 47 |
suboptions: |
| 48 |
id: |
| 49 |
description: |
| 50 |
- A short name used to tie this object to the results in the response. |
| 51 |
type: str |
| 52 |
required: true |
| 53 |
metric_stat: |
| 54 |
description: The metric to be returned, along with statistics, period, and units. |
| 55 |
type: dict |
| 56 |
required: false |
| 57 |
suboptions: |
| 58 |
metric: |
| 59 |
description: The metric to return, including the metric name, namespace, and dimensions. |
| 60 |
type: dict |
| 61 |
required: false |
| 62 |
suboptions: |
| 63 |
namespace: |
| 64 |
description: The namespace of the metric. |
| 65 |
type: str |
| 66 |
required: false |
| 67 |
metric_name: |
| 68 |
description: The name of the metric. |
| 69 |
type: str |
| 70 |
required: True |
| 71 |
dimensions: |
| 72 |
description: A name/value pair that is part of the identity of a metric. |
| 73 |
type: list |
| 74 |
elements: dict |
| 75 |
required: false |
| 76 |
suboptions: |
| 77 |
name: |
| 78 |
description: The name of the dimension. |
| 79 |
type: str |
| 80 |
required: True |
| 81 |
value: |
| 82 |
description: The value of the dimension. |
| 83 |
type: str |
| 84 |
required: True |
| 85 |
period: |
| 86 |
description: The granularity, in seconds, of the returned data points. |
| 87 |
type: int |
| 88 |
required: True |
| 89 |
stat: |
| 90 |
description: The statistic to return. It can include any CloudWatch statistic or extended statistic. |
| 91 |
type: str |
| 92 |
required: True |
| 93 |
unit: |
| 94 |
description: Unit to use when storing the metric. |
| 95 |
type: str |
| 96 |
required: false |
| 97 |
expression: |
| 98 |
description: |
| 99 |
- This field can contain either a Metrics Insights query, |
| 100 |
or a metric math expression to be performed on the returned data. |
| 101 |
type: str |
| 102 |
required: false |
| 103 |
label: |
| 104 |
description: A human-readable label for this metric or expression. |
| 105 |
type: str |
| 106 |
required: false |
| 107 |
return_data: |
| 108 |
description: This option indicates whether to return the timestamps and raw data values of this metric. |
| 109 |
type: bool |
| 110 |
required: false |
| 111 |
period: |
| 112 |
description: The granularity, in seconds, of the returned data points. |
| 113 |
type: int |
| 114 |
required: false |
| 115 |
account_id: |
| 116 |
description: The ID of the account where the metrics are located, if this is a cross-account alarm. |
| 117 |
type: str |
| 118 |
required: false |
| 119 |
namespace: |
| 120 |
description: |
| 121 |
- Name of the appropriate namespace (V(AWS/EC2), V(System/Linux), etc.), which determines the category it will appear under in CloudWatch. |
| 122 |
required: false |
| 123 |
type: str |
| 124 |
statistic: |
| 125 |
description: |
| 126 |
- Operation applied to the metric. |
| 127 |
- Works in conjunction with O(period) and O(evaluation_periods) to determine the comparison value. |
| 128 |
required: false |
| 129 |
choices: ['SampleCount','Average','Sum','Minimum','Maximum'] |
| 130 |
type: str |
| 131 |
extended_statistic: |
| 132 |
description: The percentile statistic for the metric specified in the metric name. |
| 133 |
type: str |
| 134 |
required: false |
| 135 |
version_added: "5.5.0" |
| 136 |
comparison: |
| 137 |
description: |
| 138 |
- Determines how the threshold value is compared |
| 139 |
required: false |
| 140 |
type: str |
| 141 |
choices: |
| 142 |
- 'GreaterThanOrEqualToThreshold' |
| 143 |
- 'GreaterThanThreshold' |
| 144 |
- 'LessThanThreshold' |
| 145 |
- 'LessThanOrEqualToThreshold' |
| 146 |
threshold: |
| 147 |
description: |
| 148 |
- Sets the min/max bound for triggering the alarm. |
| 149 |
required: false |
| 150 |
type: float |
| 151 |
period: |
| 152 |
description: |
| 153 |
- The time (in seconds) between metric evaluations. |
| 154 |
required: false |
| 155 |
type: int |
| 156 |
evaluate_low_sample_count_percentile: |
| 157 |
description: |
| 158 |
- Used only for alarms based on percentiles. |
| 159 |
- If you specify V(ignore), the alarm state does not change during periods with too few data points to be statistically significant. |
| 160 |
- If you specify V(evaluate) or omit this parameter, alarm is evaluated and possibly changes state no matter how many data points are available. |
| 161 |
type: str |
| 162 |
choices: ['ignore', 'evaluate'] |
| 163 |
version_added: 8.2.0 |
| 164 |
datapoints_to_alarm: |
| 165 |
description: |
| 166 |
- The number of data points that must be breaching to trigger the alarm. |
| 167 |
required: false |
| 168 |
type: int |
| 169 |
version_added: 8.2.0 |
| 170 |
evaluation_periods: |
| 171 |
description: |
| 172 |
- The number of times in which the metric is evaluated before final calculation. |
| 173 |
required: false |
| 174 |
type: int |
| 175 |
unit: |
| 176 |
description: |
| 177 |
- The threshold's unit of measurement. |
| 178 |
required: false |
| 179 |
type: str |
| 180 |
choices: |
| 181 |
- 'Seconds' |
| 182 |
- 'Microseconds' |
| 183 |
- 'Milliseconds' |
| 184 |
- 'Bytes' |
| 185 |
- 'Kilobytes' |
| 186 |
- 'Megabytes' |
| 187 |
- 'Gigabytes' |
| 188 |
- 'Terabytes' |
| 189 |
- 'Bits' |
| 190 |
- 'Kilobits' |
| 191 |
- 'Megabits' |
| 192 |
- 'Gigabits' |
| 193 |
- 'Terabits' |
| 194 |
- 'Percent' |
| 195 |
- 'Count' |
| 196 |
- 'Bytes/Second' |
| 197 |
- 'Kilobytes/Second' |
| 198 |
- 'Megabytes/Second' |
| 199 |
- 'Gigabytes/Second' |
| 200 |
- 'Terabytes/Second' |
| 201 |
- 'Bits/Second' |
| 202 |
- 'Kilobits/Second' |
| 203 |
- 'Megabits/Second' |
| 204 |
- 'Gigabits/Second' |
| 205 |
- 'Terabits/Second' |
| 206 |
- 'Count/Second' |
| 207 |
- 'None' |
| 208 |
description: |
| 209 |
description: |
| 210 |
- A longer description of the alarm. |
| 211 |
required: false |
| 212 |
type: str |
| 213 |
dimensions: |
| 214 |
description: |
| 215 |
- A dictionary describing which metric the alarm is applied to. |
| 216 |
- 'For more information see the AWS documentation:' |
| 217 |
- U(https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html#Dimension) |
| 218 |
required: false |
| 219 |
type: dict |
| 220 |
alarm_actions: |
| 221 |
description: |
| 222 |
- A list of the names action(s) taken when the alarm is in the V(alarm) status, denoted as Amazon Resource Name(s). |
| 223 |
required: false |
| 224 |
type: list |
| 225 |
elements: str |
| 226 |
default: [] |
| 227 |
insufficient_data_actions: |
| 228 |
description: |
| 229 |
- A list of the names of action(s) to take when the alarm is in the V(insufficient_data) status. |
| 230 |
required: false |
| 231 |
type: list |
| 232 |
elements: str |
| 233 |
default: [] |
| 234 |
ok_actions: |
| 235 |
description: |
| 236 |
- A list of the names of action(s) to take when the alarm is in the V(ok) status, denoted as Amazon Resource Name(s). |
| 237 |
required: false |
| 238 |
type: list |
| 239 |
elements: str |
| 240 |
default: [] |
| 241 |
treat_missing_data: |
| 242 |
description: |
| 243 |
- Sets how the alarm handles missing data points. |
| 244 |
required: false |
| 245 |
type: str |
| 246 |
choices: |
| 247 |
- 'breaching' |
| 248 |
- 'notBreaching' |
| 249 |
- 'ignore' |
| 250 |
- 'missing' |
| 251 |
default: 'missing' |
| 252 |
extends_documentation_fragment: |
| 253 |
- amazon.aws.common.modules |
| 254 |
- amazon.aws.region.modules |
| 255 |
- amazon.aws.boto3 |
| 256 |
""" |
| 257 |
|
| 258 |
EXAMPLES = r""" |
| 259 |
- name: create alarm |
| 260 |
amazon.aws.cloudwatch_metric_alarm: |
| 261 |
state: present |
| 262 |
region: ap-southeast-2 |
| 263 |
name: "cpu-low" |
| 264 |
metric_name: "CPUUtilization" |
| 265 |
namespace: "AWS/EC2" |
| 266 |
statistic: Average |
| 267 |
comparison: "LessThanOrEqualToThreshold" |
| 268 |
threshold: 5.0 |
| 269 |
period: 300 |
| 270 |
evaluate_low_sample_count_percentile: "ignore" |
| 271 |
evaluation_periods: 3 |
| 272 |
unit: "Percent" |
| 273 |
description: "This will alarm when a instance's CPU usage average is lower than 5% for 15 minutes" |
| 274 |
dimensions: {'InstanceId': 'i-XXX'} |
| 275 |
alarm_actions: ["action1", "action2"] |
| 276 |
|
| 277 |
- name: create alarm with metrics |
| 278 |
amazon.aws.cloudwatch_metric_alarm: |
| 279 |
state: present |
| 280 |
region: ap-southeast-2 |
| 281 |
name: "cpu-low" |
| 282 |
metrics: |
| 283 |
- id: 'CPU' |
| 284 |
metric_stat: |
| 285 |
metric: |
| 286 |
dimensions: |
| 287 |
name: "InstanceId" |
| 288 |
value: "i-xx" |
| 289 |
metric_name: "CPUUtilization" |
| 290 |
namespace: "AWS/EC2" |
| 291 |
period: "300" |
| 292 |
stat: "Average" |
| 293 |
unit: "Percent" |
| 294 |
return_data: false |
| 295 |
alarm_actions: ["action1", "action2"] |
| 296 |
|
| 297 |
- name: Create an alarm to recover a failed instance |
| 298 |
amazon.aws.cloudwatch_metric_alarm: |
| 299 |
state: present |
| 300 |
region: us-west-1 |
| 301 |
name: "recover-instance" |
| 302 |
metric: "StatusCheckFailed_System" |
| 303 |
namespace: "AWS/EC2" |
| 304 |
statistic: "Minimum" |
| 305 |
comparison: "GreaterThanOrEqualToThreshold" |
| 306 |
threshold: 1.0 |
| 307 |
period: 60 |
| 308 |
evaluation_periods: 2 |
| 309 |
unit: "Count" |
| 310 |
description: "This will recover an instance when it fails" |
| 311 |
dimensions: {"InstanceId":'i-XXX'} |
| 312 |
alarm_actions: ["arn:aws:automate:us-west-1:ec2:recover"] |
| 313 |
""" |
| 314 |
|
| 315 |
RETURN = r""" |
| 316 |
actions_enabled: |
| 317 |
description: Indicates whether actions should be executed during any changes to the alarm state. |
| 318 |
returned: always |
| 319 |
type: bool |
| 320 |
name: |
| 321 |
description: Unique name for the alarm. |
| 322 |
returned: always |
| 323 |
type: str |
| 324 |
alarm_arn: |
| 325 |
description: The Amazon Resource Name (ARN) of the alarm. |
| 326 |
returned: always |
| 327 |
type: str |
| 328 |
description: |
| 329 |
description: The description of the alarm. |
| 330 |
returned: always |
| 331 |
type: str |
| 332 |
last_updated: |
| 333 |
description: The time stamp of the last update to the alarm configuration. |
| 334 |
returned: always |
| 335 |
type: str |
| 336 |
ok_actions: |
| 337 |
description: The actions to execute when this alarm transitions to an OK state from any other state. |
| 338 |
returned: always |
| 339 |
type: list |
| 340 |
elements: str |
| 341 |
alarm_actions: |
| 342 |
description: The actions to execute when this alarm transitions to an ALARM state from any other state. |
| 343 |
returned: always |
| 344 |
type: list |
| 345 |
elements: str |
| 346 |
insufficient_data_actions: |
| 347 |
description: The actions to execute when this alarm transitions to an INSUFFICIENT_DATA state from any other state. |
| 348 |
returned: always |
| 349 |
type: list |
| 350 |
elements: str |
| 351 |
state_value: |
| 352 |
description: The state value for the alarm. |
| 353 |
returned: always |
| 354 |
type: str |
| 355 |
state_reason: |
| 356 |
description: An explanation for the alarm state, in text format. |
| 357 |
returned: always |
| 358 |
type: str |
| 359 |
metric: |
| 360 |
description: Name of the monitored metric (e.g. C(CPUUtilization)). |
| 361 |
returned: always |
| 362 |
type: str |
| 363 |
metric_name: |
| 364 |
description: Name of the monitored metric (e.g. C(CPUUtilization)). |
| 365 |
returned: always |
| 366 |
type: str |
| 367 |
namespace: |
| 368 |
description: |
| 369 |
- Name of the appropriate namespace (C(AWS/EC2), C(System/Linux), etc.). |
| 370 |
- Determines the category it will appear under in CloudWatch. |
| 371 |
returned: always |
| 372 |
type: str |
| 373 |
statistic: |
| 374 |
description: The statistic for the metric associated with the alarm, other than percentile. |
| 375 |
returned: always |
| 376 |
type: str |
| 377 |
extended_statistic: |
| 378 |
description: The percentile statistic for the metric associated with the alarm. |
| 379 |
returned: always |
| 380 |
type: str |
| 381 |
dimensions: |
| 382 |
description: The dimensions for the metric. |
| 383 |
returned: always |
| 384 |
type: list |
| 385 |
elements: dict |
| 386 |
contains: |
| 387 |
name: |
| 388 |
description: The name of the dimension. |
| 389 |
returned: always |
| 390 |
type: str |
| 391 |
value: |
| 392 |
description: The value of the dimension. |
| 393 |
returned: always |
| 394 |
type: str |
| 395 |
period: |
| 396 |
description: |
| 397 |
- The length, in seconds, used each time the metric specified in MetricName is evaluated. |
| 398 |
- Valid values are 10, 30, and any multiple of 60. |
| 399 |
returned: always |
| 400 |
type: int |
| 401 |
unit: |
| 402 |
description: Unit used when storing the metric |
| 403 |
returned: always |
| 404 |
type: str |
| 405 |
evaluation_periods: |
| 406 |
description: The number of periods over which data is compared to the specified threshold. |
| 407 |
returned: always |
| 408 |
type: int |
| 409 |
threshold: |
| 410 |
description: The value to compare with the specified statistic. |
| 411 |
returned: always |
| 412 |
type: float |
| 413 |
comparison: |
| 414 |
description: The arithmetic operation to use when comparing the specified statistic and threshold. |
| 415 |
returned: always |
| 416 |
type: str |
| 417 |
treat_missing_data: |
| 418 |
description: Sets how alarm is to handle missing data points. |
| 419 |
returned: always |
| 420 |
type: str |
| 421 |
evaluate_low_sample_count_percentile: |
| 422 |
description: |
| 423 |
- Used only for alarms based on percentiles. |
| 424 |
- If I(ignore), the alarm state does not change during periods with too few data points to be statistically significant. |
| 425 |
- If I(evaluate) or this parameter is not used, the alarm is always evaluated and possibly changes state. |
| 426 |
returned: always |
| 427 |
type: str |
| 428 |
metrics: |
| 429 |
description: An array of MetricDataQuery structures, used in an alarm based on a metric math expression. |
| 430 |
returned: always |
| 431 |
type: list |
| 432 |
elements: dict |
| 433 |
""" |
| 434 |
|
| 435 |
try: |
| 436 |
from botocore.exceptions import ClientError |
| 437 |
except ImportError: |
| 438 |
pass # protected by AnsibleAWSModule |
| 439 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 440 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 441 |
|
| 442 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 443 |
|
| 444 |
|
| 445 |
def create_metric_alarm(connection, module, params): |
| 446 |
alarms = connection.describe_alarms(AlarmNames=[params["AlarmName"]]) |
| 447 |
if params.get("Dimensions"): |
| 448 |
if not isinstance(params["Dimensions"], list): |
| 449 |
fixed_dimensions = [] |
| 450 |
for key, value in params["Dimensions"].items(): |
| 451 |
fixed_dimensions.append({"Name": key, "Value": value}) |
| 452 |
params["Dimensions"] = fixed_dimensions |
| 453 |
|
| 454 |
if not alarms["MetricAlarms"]: |
| 455 |
try: |
| 456 |
if not module.check_mode: |
| 457 |
connection.put_metric_alarm(**params) |
| 458 |
changed = True |
| 459 |
except ClientError as e: |
| 460 |
module.fail_json_aws(e) |
| 461 |
|
| 462 |
else: |
| 463 |
changed = False |
| 464 |
alarm = alarms["MetricAlarms"][0] |
| 465 |
|
| 466 |
# Workaround for alarms created before TreatMissingData was introduced |
| 467 |
if "TreatMissingData" not in alarm.keys(): |
| 468 |
alarm["TreatMissingData"] = "missing" |
| 469 |
|
| 470 |
# Prevent alarm without dimensions to always return changed |
| 471 |
if not alarm["Dimensions"]: |
| 472 |
alarm.pop("Dimensions", None) |
| 473 |
|
| 474 |
# Exclude certain props from change detection |
| 475 |
for key in [ |
| 476 |
"ActionsEnabled", |
| 477 |
"StateValue", |
| 478 |
"StateReason", |
| 479 |
"StateReasonData", |
| 480 |
"StateUpdatedTimestamp", |
| 481 |
"StateTransitionedTimestamp", |
| 482 |
"AlarmArn", |
| 483 |
"AlarmConfigurationUpdatedTimestamp", |
| 484 |
"Metrics", |
| 485 |
]: |
| 486 |
alarm.pop(key, None) |
| 487 |
if alarm != params: |
| 488 |
changed = True |
| 489 |
alarm = params |
| 490 |
|
| 491 |
try: |
| 492 |
if changed: |
| 493 |
if not module.check_mode: |
| 494 |
connection.put_metric_alarm(**alarm) |
| 495 |
except ClientError as e: |
| 496 |
module.fail_json_aws(e) |
| 497 |
|
| 498 |
try: |
| 499 |
alarms = connection.describe_alarms(AlarmNames=[params["AlarmName"]]) |
| 500 |
except ClientError as e: |
| 501 |
module.fail_json_aws(e) |
| 502 |
|
| 503 |
result = {} |
| 504 |
if alarms["MetricAlarms"]: |
| 505 |
if alarms["MetricAlarms"][0].get("Metrics"): |
| 506 |
metric_list = [] |
| 507 |
for metric_element in alarms["MetricAlarms"][0]["Metrics"]: |
| 508 |
metric_list.append(camel_dict_to_snake_dict(metric_element)) |
| 509 |
alarms["MetricAlarms"][0]["Metrics"] = metric_list |
| 510 |
result = alarms["MetricAlarms"][0] |
| 511 |
|
| 512 |
module.exit_json( |
| 513 |
changed=changed, |
| 514 |
name=result.get("AlarmName"), |
| 515 |
actions_enabled=result.get("ActionsEnabled"), |
| 516 |
alarm_actions=result.get("AlarmActions"), |
| 517 |
alarm_arn=result.get("AlarmArn"), |
| 518 |
comparison=result.get("ComparisonOperator"), |
| 519 |
description=result.get("AlarmDescription"), |
| 520 |
dimensions=result.get("Dimensions"), |
| 521 |
extended_statistic=result.get("ExtendedStatistic"), |
| 522 |
evaluate_low_sample_count_percentile=result.get("EvaluateLowSampleCountPercentile"), |
| 523 |
datapoints_to_alarm=result.get("DatapointsToAlarm"), |
| 524 |
evaluation_periods=result.get("EvaluationPeriods"), |
| 525 |
insufficient_data_actions=result.get("InsufficientDataActions"), |
| 526 |
last_updated=result.get("AlarmConfigurationUpdatedTimestamp"), |
| 527 |
metric=result.get("MetricName"), |
| 528 |
metric_name=result.get("MetricName"), |
| 529 |
metrics=result.get("Metrics"), |
| 530 |
namespace=result.get("Namespace"), |
| 531 |
ok_actions=result.get("OKActions"), |
| 532 |
period=result.get("Period"), |
| 533 |
state_reason=result.get("StateReason"), |
| 534 |
state_value=result.get("StateValue"), |
| 535 |
statistic=result.get("Statistic"), |
| 536 |
threshold=result.get("Threshold"), |
| 537 |
treat_missing_data=result.get("TreatMissingData"), |
| 538 |
unit=result.get("Unit"), |
| 539 |
) |
| 540 |
|
| 541 |
|
| 542 |
def delete_metric_alarm(connection, module, params): |
| 543 |
alarms = connection.describe_alarms(AlarmNames=[params["AlarmName"]]) |
| 544 |
|
| 545 |
if alarms["MetricAlarms"]: |
| 546 |
try: |
| 547 |
if not module.check_mode: |
| 548 |
connection.delete_alarms(AlarmNames=[params["AlarmName"]]) |
| 549 |
module.exit_json(changed=True) |
| 550 |
except ClientError as e: |
| 551 |
module.fail_json_aws(e) |
| 552 |
else: |
| 553 |
module.exit_json(changed=False) |
| 554 |
|
| 555 |
|
| 556 |
def main(): |
| 557 |
argument_spec = dict( |
| 558 |
name=dict(required=True, type="str"), |
| 559 |
metric_name=dict(type="str", aliases=["metric"]), |
| 560 |
namespace=dict(type="str"), |
| 561 |
statistic=dict(type="str", choices=["SampleCount", "Average", "Sum", "Minimum", "Maximum"]), |
| 562 |
comparison=dict( |
| 563 |
type="str", |
| 564 |
choices=[ |
| 565 |
"LessThanOrEqualToThreshold", |
| 566 |
"LessThanThreshold", |
| 567 |
"GreaterThanThreshold", |
| 568 |
"GreaterThanOrEqualToThreshold", |
| 569 |
], |
| 570 |
), |
| 571 |
threshold=dict(type="float"), |
| 572 |
period=dict(type="int"), |
| 573 |
unit=dict( |
| 574 |
type="str", |
| 575 |
choices=[ |
| 576 |
"Seconds", |
| 577 |
"Microseconds", |
| 578 |
"Milliseconds", |
| 579 |
"Bytes", |
| 580 |
"Kilobytes", |
| 581 |
"Megabytes", |
| 582 |
"Gigabytes", |
| 583 |
"Terabytes", |
| 584 |
"Bits", |
| 585 |
"Kilobits", |
| 586 |
"Megabits", |
| 587 |
"Gigabits", |
| 588 |
"Terabits", |
| 589 |
"Percent", |
| 590 |
"Count", |
| 591 |
"Bytes/Second", |
| 592 |
"Kilobytes/Second", |
| 593 |
"Megabytes/Second", |
| 594 |
"Gigabytes/Second", |
| 595 |
"Terabytes/Second", |
| 596 |
"Bits/Second", |
| 597 |
"Kilobits/Second", |
| 598 |
"Megabits/Second", |
| 599 |
"Gigabits/Second", |
| 600 |
"Terabits/Second", |
| 601 |
"Count/Second", |
| 602 |
"None", |
| 603 |
], |
| 604 |
), |
| 605 |
evaluate_low_sample_count_percentile=dict(type="str", choices=["ignore", "evaluate"]), |
| 606 |
datapoints_to_alarm=dict(type="int"), |
| 607 |
evaluation_periods=dict(type="int"), |
| 608 |
extended_statistic=dict(type="str"), |
| 609 |
description=dict(type="str"), |
| 610 |
dimensions=dict(type="dict"), |
| 611 |
alarm_actions=dict(type="list", default=[], elements="str"), |
| 612 |
insufficient_data_actions=dict(type="list", default=[], elements="str"), |
| 613 |
ok_actions=dict(type="list", default=[], elements="str"), |
| 614 |
treat_missing_data=dict( |
| 615 |
type="str", choices=["breaching", "notBreaching", "ignore", "missing"], default="missing" |
| 616 |
), |
| 617 |
state=dict(default="present", choices=["present", "absent"]), |
| 618 |
metrics=dict(type="list", elements="dict", default=[]), |
| 619 |
) |
| 620 |
|
| 621 |
mutually_exclusive = [ |
| 622 |
["metric_name", "metrics"], |
| 623 |
["dimensions", "metrics"], |
| 624 |
["period", "metrics"], |
| 625 |
["namespace", "metrics"], |
| 626 |
["statistic", "metrics"], |
| 627 |
["extended_statistic", "metrics"], |
| 628 |
["unit", "metrics"], |
| 629 |
["statistic", "extended_statistic"], |
| 630 |
] |
| 631 |
|
| 632 |
module = AnsibleAWSModule( |
| 633 |
argument_spec=argument_spec, |
| 634 |
mutually_exclusive=mutually_exclusive, |
| 635 |
supports_check_mode=True, |
| 636 |
) |
| 637 |
|
| 638 |
state = module.params.get("state") |
| 639 |
|
| 640 |
params = dict() |
| 641 |
params["AlarmName"] = module.params.get("name") |
| 642 |
params["MetricName"] = module.params.get("metric_name") |
| 643 |
params["Namespace"] = module.params.get("namespace") |
| 644 |
params["Statistic"] = module.params.get("statistic") |
| 645 |
params["ComparisonOperator"] = module.params.get("comparison") |
| 646 |
params["Threshold"] = module.params.get("threshold") |
| 647 |
params["Period"] = module.params.get("period") |
| 648 |
params["EvaluateLowSampleCountPercentile"] = module.params.get("evaluate_low_sample_count_percentile") |
| 649 |
params["DatapointsToAlarm"] = module.params.get("datapoints_to_alarm") |
| 650 |
params["EvaluationPeriods"] = module.params.get("evaluation_periods") |
| 651 |
if module.params.get("unit"): |
| 652 |
params["Unit"] = module.params.get("unit") |
| 653 |
params["AlarmDescription"] = module.params.get("description") |
| 654 |
params["Dimensions"] = module.params.get("dimensions") |
| 655 |
params["AlarmActions"] = module.params.get("alarm_actions", []) |
| 656 |
params["InsufficientDataActions"] = module.params.get("insufficient_data_actions", []) |
| 657 |
params["OKActions"] = module.params.get("ok_actions", []) |
| 658 |
params["TreatMissingData"] = module.params.get("treat_missing_data") |
| 659 |
if module.params.get("metrics"): |
| 660 |
params["Metrics"] = snake_dict_to_camel_dict(module.params["metrics"], capitalize_first=True) |
| 661 |
if module.params.get("extended_statistic"): |
| 662 |
params["ExtendedStatistic"] = module.params.get("extended_statistic") |
| 663 |
|
| 664 |
for key, value in list(params.items()): |
| 665 |
if value is None: |
| 666 |
del params[key] |
| 667 |
|
| 668 |
connection = module.client("cloudwatch") |
| 669 |
|
| 670 |
if state == "present": |
| 671 |
create_metric_alarm(connection, module, params) |
| 672 |
elif state == "absent": |
| 673 |
delete_metric_alarm(connection, module, params) |
| 674 |
|
| 675 |
|
| 676 |
if __name__ == "__main__": |
| 677 |
main() |