| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_monitormetricalerts_info |
| 13 |
version_added: "3.7.0" |
| 14 |
short_description: Get metric alerts |
| 15 |
description: |
| 16 |
- Get metric alerts |
| 17 |
|
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- The name of the metric alert you're trying to get details about. |
| 22 |
type: str |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- The name of the resource group in which the metric alert is (if you use name) |
| 26 |
- The name of the resource group where you want to list metric alerts (if you don't use name) |
| 27 |
type: str |
| 28 |
extends_documentation_fragment: |
| 29 |
- azure.azcollection.azure |
| 30 |
|
| 31 |
author: |
| 32 |
- Klaas Demter (@Klaas-) |
| 33 |
''' |
| 34 |
|
| 35 |
EXAMPLES = ''' |
| 36 |
- name: Get metric alert details |
| 37 |
azure.azcollection.azure_rm_monitormetricalerts_info: |
| 38 |
name: metric_alert_name |
| 39 |
resource_group: resource_group_name |
| 40 |
|
| 41 |
- name: Get all metric alerts in specific resource group |
| 42 |
azure.azcollection.azure_rm_monitormetricalerts_info: |
| 43 |
resource_group: resource_group_name |
| 44 |
|
| 45 |
- name: Get all metric alerts in the current subscription |
| 46 |
azure.azcollection.azure_rm_monitormetricalerts_info: |
| 47 |
''' |
| 48 |
|
| 49 |
RETURN = ''' |
| 50 |
metricalerts: |
| 51 |
description: |
| 52 |
- List of metric alerts |
| 53 |
- Can be empty if listing metric alerts or metric alert does not exist |
| 54 |
type: list |
| 55 |
returned: always |
| 56 |
sample: [ |
| 57 |
{ |
| 58 |
"actions": [ |
| 59 |
{ |
| 60 |
"action_group_id": \ |
| 61 |
"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resource_group_name/providers/microsoft.insights/actionGroups/action_group_name" |
| 62 |
} |
| 63 |
], |
| 64 |
"auto_mitigate": true, |
| 65 |
"criteria": { |
| 66 |
"all_of": [ |
| 67 |
{ |
| 68 |
"criterion_type": "StaticThresholdCriterion", |
| 69 |
"dimensions": [ |
| 70 |
{ |
| 71 |
"name": "dataSourceURL", |
| 72 |
"operator": "Include", |
| 73 |
"values": [ |
| 74 |
"*" |
| 75 |
] |
| 76 |
}, |
| 77 |
{ |
| 78 |
"name": "healthStatus", |
| 79 |
"operator": "Exclude", |
| 80 |
"values": [ |
| 81 |
"Healthy" |
| 82 |
] |
| 83 |
} |
| 84 |
], |
| 85 |
"metric_name": "BackupHealthEvent", |
| 86 |
"metric_namespace": "Microsoft.RecoveryServices/vaults", |
| 87 |
"name": "Metric1", |
| 88 |
"operator": "GreaterThan", |
| 89 |
"skip_metric_validation": false, |
| 90 |
"threshold": 0.0, |
| 91 |
"time_aggregation": "Count" |
| 92 |
} |
| 93 |
], |
| 94 |
"odata_type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria" |
| 95 |
}, |
| 96 |
"description": "Alert on Backup Health Events", |
| 97 |
"enabled": true, |
| 98 |
"evaluation_frequency": "PT1H", |
| 99 |
"id": \ |
| 100 |
"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resource_group_name/providers/Microsoft.Insights/metricAlerts/metric_alert", |
| 101 |
"location": "Global", |
| 102 |
"name": "Monitoring-e-app-name-Backup_Health", |
| 103 |
"scopes": [ |
| 104 |
"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resource_group_name/providers/Microsoft.RecoveryServices/vaults/recovery_vault_name" |
| 105 |
], |
| 106 |
"severity": 3, |
| 107 |
"tags": { |
| 108 |
"TestTag1": "TestValue1" |
| 109 |
}, |
| 110 |
"target_resource_type": "Microsoft.RecoveryServices/vaults", |
| 111 |
"type": "Microsoft.Insights/metricAlerts", |
| 112 |
"window_size": "P1D" |
| 113 |
} |
| 114 |
] |
| 115 |
''' |
| 116 |
|
| 117 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 118 |
|
| 119 |
try: |
| 120 |
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError |
| 121 |
|
| 122 |
except ImportError: |
| 123 |
|
| 124 |
pass |
| 125 |
|
| 126 |
AZURE_OBJECT_CLASS = 'metricalerts' |
| 127 |
|
| 128 |
|
| 129 |
class AzureRMmetricalertsInfo(AzureRMModuleBase): |
| 130 |
"""Information class for an Azure RM metric alerts""" |
| 131 |
|
| 132 |
def __init__(self): |
| 133 |
self.module_arg_spec = dict( |
| 134 |
name=dict(type='str', required=False), |
| 135 |
resource_group=dict(type='str', required=False), |
| 136 |
) |
| 137 |
|
| 138 |
self.required_by = { |
| 139 |
'name': 'resource_group' |
| 140 |
} |
| 141 |
|
| 142 |
self.name = None |
| 143 |
self.resource_group = None |
| 144 |
self.log_path = None |
| 145 |
self.log_mode = None |
| 146 |
|
| 147 |
self.results = dict( |
| 148 |
changed=False, |
| 149 |
metricalerts=[] |
| 150 |
) |
| 151 |
|
| 152 |
super(AzureRMmetricalertsInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 153 |
supports_check_mode=True, |
| 154 |
supports_tags=False, |
| 155 |
facts_module=True, |
| 156 |
required_by=self.required_by) |
| 157 |
|
| 158 |
def exec_module(self, **kwargs): |
| 159 |
"""Main module execution method""" |
| 160 |
|
| 161 |
for key in self.module_arg_spec: |
| 162 |
setattr(self, key, kwargs[key]) |
| 163 |
|
| 164 |
if self.name: |
| 165 |
result = self.get_metric_alert() |
| 166 |
else: |
| 167 |
result = self.list_metric_alerts() |
| 168 |
|
| 169 |
self.results['metricalerts'] = result |
| 170 |
|
| 171 |
return self.results |
| 172 |
|
| 173 |
def get_metric_alert(self): |
| 174 |
''' |
| 175 |
Gets the properties of the specified metric alert. |
| 176 |
|
| 177 |
:return: List of metric alerts |
| 178 |
''' |
| 179 |
self.log("Checking if metric alert {0} in resource group {1} is present".format(self.name, |
| 180 |
self.resource_group)) |
| 181 |
|
| 182 |
result = [] |
| 183 |
metric_alert = None |
| 184 |
|
| 185 |
try: |
| 186 |
metric_alert = self.monitor_management_client_metric_alerts.metric_alerts.get(rule_name=self.name, |
| 187 |
resource_group_name=self.resource_group) |
| 188 |
except ResourceNotFoundError as ex: |
| 189 |
self.log("Could not find metric alert {0} in resource group {1}".format(self.name, self.resource_group)) |
| 190 |
return [] |
| 191 |
except HttpResponseError as ex: |
| 192 |
if ex.error.code == 'InvalidSubscriptionId': |
| 193 |
self.log("Could not find subscription id") |
| 194 |
return [] |
| 195 |
else: |
| 196 |
raise Exception(ex) |
| 197 |
if metric_alert: |
| 198 |
result = [self.serialize_obj(metric_alert, AZURE_OBJECT_CLASS)] |
| 199 |
|
| 200 |
return result |
| 201 |
|
| 202 |
def list_metric_alerts(self): |
| 203 |
''' |
| 204 |
Gets the properties of the specified metric alerts in resource group or subscription. |
| 205 |
|
| 206 |
:return: List of metric alerts |
| 207 |
''' |
| 208 |
|
| 209 |
result = [] |
| 210 |
metric_alerts = None |
| 211 |
|
| 212 |
if self.resource_group: |
| 213 |
self.log("Checking if the metric alerts in resource group {0} are present".format(self.resource_group)) |
| 214 |
metric_alerts_mgmt_client = self.monitor_management_client_metric_alerts.metric_alerts |
| 215 |
metric_alerts = metric_alerts_mgmt_client.list_by_resource_group(resource_group_name=self.resource_group) |
| 216 |
else: |
| 217 |
self.log("Checking if the metric alerts are present in subscription") |
| 218 |
metric_alerts = self.monitor_management_client_metric_alerts.metric_alerts.list_by_subscription() |
| 219 |
if metric_alerts: |
| 220 |
|
| 221 |
try: |
| 222 |
for item in metric_alerts: |
| 223 |
result.append(self.serialize_obj(item, AZURE_OBJECT_CLASS)) |
| 224 |
except ResourceNotFoundError as ex: |
| 225 |
self.log("Could not find resource group {0}".format(self.resource_group)) |
| 226 |
except HttpResponseError as ex: |
| 227 |
if ex.error.code == 'InvalidSubscriptionId': |
| 228 |
self.log("Could not find subscription id") |
| 229 |
else: |
| 230 |
raise Exception(ex) |
| 231 |
|
| 232 |
return result |
| 233 |
|
| 234 |
|
| 235 |
def main(): |
| 236 |
"""Main execution""" |
| 237 |
AzureRMmetricalertsInfo() |
| 238 |
|
| 239 |
|
| 240 |
if __name__ == '__main__': |
| 241 |
main() |