| 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://wwww.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: cloudwatch_metric_alarm_info |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: Gather information about the alarms for the specified metric |
| 12 |
description: |
| 13 |
- Retrieves the alarms for the specified metric. |
| 14 |
author: |
| 15 |
- Mandar Vijay Kulkarni (@mandar242) |
| 16 |
options: |
| 17 |
alarm_names: |
| 18 |
description: |
| 19 |
- The name of the metric. |
| 20 |
required: false |
| 21 |
type: list |
| 22 |
elements: str |
| 23 |
alarm_name_prefix: |
| 24 |
description: |
| 25 |
- An alarm name prefix to retrieve information about alarms that have names that start with this prefix. |
| 26 |
- Can not be used with O(alarm_names). |
| 27 |
required: false |
| 28 |
type: str |
| 29 |
alarm_type: |
| 30 |
description: |
| 31 |
- Specify this to return metric alarms or composite alarms. |
| 32 |
- Module is defaulted to return metric alarms but can return composite alarms if O(alarm_type=CompositeAlarm). |
| 33 |
required: false |
| 34 |
type: str |
| 35 |
default: MetricAlarm |
| 36 |
choices: ['CompositeAlarm', 'MetricAlarm'] |
| 37 |
children_of_alarm_name: |
| 38 |
description: |
| 39 |
- If specified returns information about the "children" alarms of the alarm name specified. |
| 40 |
required: false |
| 41 |
type: str |
| 42 |
parents_of_alarm_name: |
| 43 |
description: |
| 44 |
- If specified returns information about the "parent" alarms of the alarm name specified. |
| 45 |
required: false |
| 46 |
type: str |
| 47 |
state_value: |
| 48 |
description: |
| 49 |
- If specified returns information only about alarms that are currently in the particular state. |
| 50 |
required: false |
| 51 |
type: str |
| 52 |
choices: ['OK', 'ALARM', 'INSUFFICIENT_DATA'] |
| 53 |
action_prefix: |
| 54 |
description: |
| 55 |
- This parameter can be used to filter the results of the operation to only those alarms that use a certain alarm action. |
| 56 |
required: false |
| 57 |
type: str |
| 58 |
|
| 59 |
extends_documentation_fragment: |
| 60 |
- amazon.aws.common.modules |
| 61 |
- amazon.aws.region.modules |
| 62 |
- amazon.aws.boto3 |
| 63 |
""" |
| 64 |
|
| 65 |
EXAMPLES = r""" |
| 66 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 67 |
|
| 68 |
- name: describe the metric alarm based on alarm names |
| 69 |
amazon.aws.cloudwatch_metric_alarm_info: |
| 70 |
alarm_names: |
| 71 |
- my-test-alarm-1 |
| 72 |
- my-test-alarm-2 |
| 73 |
|
| 74 |
- name: describe the metric alarm based alarm names and state value |
| 75 |
amazon.aws.cloudwatch_metric_alarm_info: |
| 76 |
alarm_names: |
| 77 |
- my-test-alarm-1 |
| 78 |
- my-test-alarm-2 |
| 79 |
state_value: OK |
| 80 |
|
| 81 |
- name: describe the metric alarm based alarm names prefix |
| 82 |
amazon.aws.cloudwatch_metric_alarm_info: |
| 83 |
alarm_name_prefix: my-test- |
| 84 |
""" |
| 85 |
|
| 86 |
RETURN = r""" |
| 87 |
metric_alarms: |
| 88 |
description: The gathered information about specified metric alarms. |
| 89 |
returned: when success |
| 90 |
type: list |
| 91 |
elements: dict |
| 92 |
contains: |
| 93 |
alarm_name: |
| 94 |
description: Unique name for the alarm. |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
alarm_arn: |
| 98 |
description: The Amazon Resource Name (ARN) of the alarm. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
alarm_description: |
| 102 |
description: The description of the alarm. |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
alarm_configuration_updated_timestamp: |
| 106 |
description: The time stamp of the last update to the alarm configuration. |
| 107 |
returned: always |
| 108 |
type: str |
| 109 |
actions_enabled: |
| 110 |
description: Indicates whether actions should be executed during any changes to the alarm state. |
| 111 |
returned: always |
| 112 |
type: bool |
| 113 |
ok_actions: |
| 114 |
description: The actions to execute when this alarm transitions to an OK state from any other state. |
| 115 |
returned: always |
| 116 |
type: list |
| 117 |
elements: str |
| 118 |
alarm_actions: |
| 119 |
description: The actions to execute when this alarm transitions to an ALARM state from any other state. |
| 120 |
returned: always |
| 121 |
type: list |
| 122 |
elements: str |
| 123 |
insufficient_data_actions: |
| 124 |
description: The actions to execute when this alarm transitions to an INSUFFICIENT_DATA state from any other state. |
| 125 |
returned: always |
| 126 |
type: list |
| 127 |
elements: str |
| 128 |
state_value: |
| 129 |
description: The state value for the alarm. |
| 130 |
returned: always |
| 131 |
type: str |
| 132 |
state_reason: |
| 133 |
description: An explanation for the alarm state, in text format. |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
state_reason_data: |
| 137 |
description: An explanation for the alarm state, in JSON format. |
| 138 |
returned: always |
| 139 |
type: str |
| 140 |
state_updated_timestamp: |
| 141 |
description: The time stamp of the last update to the alarm state. |
| 142 |
returned: always |
| 143 |
type: str |
| 144 |
metric_name: |
| 145 |
description: Name of the monitored metric (e.g. C(CPUUtilization)). |
| 146 |
returned: always |
| 147 |
type: str |
| 148 |
namespace: |
| 149 |
description: |
| 150 |
- Name of the appropriate namespace (V(AWS/EC2), V(System/Linux), etc.). |
| 151 |
- Determines the category it will appear under in CloudWatch. |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
statistic: |
| 155 |
description: The statistic for the metric associated with the alarm, other than percentile. |
| 156 |
returned: always |
| 157 |
type: str |
| 158 |
extended_statistic: |
| 159 |
description: The percentile statistic for the metric associated with the alarm. |
| 160 |
returned: always |
| 161 |
type: str |
| 162 |
dimensions: |
| 163 |
description: The dimensions for the metric. |
| 164 |
returned: always |
| 165 |
type: list |
| 166 |
elements: dict |
| 167 |
contains: |
| 168 |
name: |
| 169 |
description: The name of the dimension. |
| 170 |
returned: always |
| 171 |
type: str |
| 172 |
value: |
| 173 |
description: The value of the dimension. |
| 174 |
returned: always |
| 175 |
type: str |
| 176 |
period: |
| 177 |
description: |
| 178 |
- The length, in seconds, used each time the metric specified in MetricName is evaluated. |
| 179 |
- Valid values are 10, 30, and any multiple of 60. |
| 180 |
returned: always |
| 181 |
type: int |
| 182 |
unit: |
| 183 |
description: Unit used when storing the metric |
| 184 |
returned: always |
| 185 |
type: str |
| 186 |
evaluation_period: |
| 187 |
description: The number of periods over which data is compared to the specified threshold. |
| 188 |
returned: always |
| 189 |
type: int |
| 190 |
datapoints_to_alarm: |
| 191 |
description: The number of data points that must be breaching to trigger the alarm. |
| 192 |
returned: always |
| 193 |
type: int |
| 194 |
threshold: |
| 195 |
description: The value to compare with the specified statistic. |
| 196 |
returned: always |
| 197 |
type: float |
| 198 |
comparison_operator: |
| 199 |
description: The arithmetic operation to use when comparing the specified statistic and threshold. |
| 200 |
returned: always |
| 201 |
type: str |
| 202 |
treat_missing_data: |
| 203 |
description: Sets how alarm is to handle missing data points. |
| 204 |
returned: always |
| 205 |
type: str |
| 206 |
evaluate_low_sample_count_percentile: |
| 207 |
description: |
| 208 |
- Used only for alarms based on percentiles. |
| 209 |
- If I(ignore), the alarm state does not change during periods with too few data points to be statistically significant. |
| 210 |
- If I(evaluate) or this parameter is not used, the alarm is always evaluated and possibly changes state. |
| 211 |
returned: always |
| 212 |
type: str |
| 213 |
metrics: |
| 214 |
description: An array of MetricDataQuery structures, used in an alarm based on a metric math expression. |
| 215 |
returned: always |
| 216 |
type: list |
| 217 |
elements: dict |
| 218 |
threshold_metric_id: |
| 219 |
description: This is the ID of the ANOMALY_DETECTION_BAND function used as the threshold for the alarm. |
| 220 |
returned: always |
| 221 |
type: str |
| 222 |
""" |
| 223 |
|
| 224 |
|
| 225 |
try: |
| 226 |
import botocore |
| 227 |
except ImportError: |
| 228 |
pass # Handled by AnsibleAWSModule |
| 229 |
|
| 230 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 231 |
|
| 232 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 233 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 234 |
|
| 235 |
|
| 236 |
@AWSRetry.jittered_backoff(retries=10) |
| 237 |
def _describe_alarms(connection, **params): |
| 238 |
paginator = connection.get_paginator("describe_alarms") |
| 239 |
return paginator.paginate(**params).build_full_result() |
| 240 |
|
| 241 |
|
| 242 |
def describe_metric_alarms_info(connection, module): |
| 243 |
params = build_params(module) |
| 244 |
|
| 245 |
alarm_type_to_return = module.params.get("alarm_type") |
| 246 |
|
| 247 |
try: |
| 248 |
describe_metric_alarms_info_response = _describe_alarms(connection, **params) |
| 249 |
# describe_metric_alarms_info_response = describe_metric_alarms_info_response[alarm_type_to_return] |
| 250 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 251 |
module.fail_json_aws(e, msg="Failed to describe cloudwatch metric alarm") |
| 252 |
|
| 253 |
result = [] |
| 254 |
|
| 255 |
if alarm_type_to_return == "CompositeAlarm": |
| 256 |
for response_list_item in describe_metric_alarms_info_response["CompositeAlarms"]: |
| 257 |
result.append(camel_dict_to_snake_dict(response_list_item)) |
| 258 |
module.exit_json(composite_alarms=result) |
| 259 |
|
| 260 |
for response_list_item in describe_metric_alarms_info_response["MetricAlarms"]: |
| 261 |
result.append(camel_dict_to_snake_dict(response_list_item)) |
| 262 |
|
| 263 |
module.exit_json(metric_alarms=result) |
| 264 |
|
| 265 |
|
| 266 |
def build_params(module): |
| 267 |
params = {} |
| 268 |
|
| 269 |
if module.params.get("alarm_names"): |
| 270 |
params["AlarmNames"] = module.params.get("alarm_names") |
| 271 |
|
| 272 |
if module.params.get("alarm_name_prefix"): |
| 273 |
params["AlarmNamePrefix"] = module.params.get("alarm_name_prefix") |
| 274 |
|
| 275 |
if module.params.get("children_of_alarm_name"): |
| 276 |
params["ChildrenOfAlarmName"] = module.params.get("children_of_alarm_name") |
| 277 |
|
| 278 |
if module.params.get("parents_of_alarm_name"): |
| 279 |
params["ParentsOfAlarmName"] = module.params.get("parents_of_alarm_name") |
| 280 |
|
| 281 |
if module.params.get("state_value"): |
| 282 |
params["StateValue"] = module.params.get("state_value") |
| 283 |
|
| 284 |
if module.params.get("action_prefix"): |
| 285 |
params["ActionPrefix"] = module.params.get("action_prefix") |
| 286 |
|
| 287 |
return params |
| 288 |
|
| 289 |
|
| 290 |
def main(): |
| 291 |
argument_spec = dict( |
| 292 |
alarm_names=dict(type="list", elements="str", required=False), |
| 293 |
alarm_name_prefix=dict(type="str", required=False), |
| 294 |
alarm_type=dict(type="str", choices=["CompositeAlarm", "MetricAlarm"], default="MetricAlarm", required=False), |
| 295 |
children_of_alarm_name=dict(type="str", required=False), |
| 296 |
parents_of_alarm_name=dict(type="str", required=False), |
| 297 |
state_value=dict(type="str", choices=["OK", "ALARM", "INSUFFICIENT_DATA"], required=False), |
| 298 |
action_prefix=dict(type="str", required=False), |
| 299 |
) |
| 300 |
|
| 301 |
module = AnsibleAWSModule( |
| 302 |
argument_spec=argument_spec, mutually_exclusive=[["alarm_names", "alarm_name_prefix"]], supports_check_mode=True |
| 303 |
) |
| 304 |
|
| 305 |
try: |
| 306 |
connection = module.client("cloudwatch", retry_decorator=AWSRetry.jittered_backoff()) |
| 307 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 308 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 309 |
|
| 310 |
describe_metric_alarms_info(connection, module) |
| 311 |
|
| 312 |
|
| 313 |
if __name__ == "__main__": |
| 314 |
main() |