| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: na_santricity_alerts |
| 12 |
short_description: NetApp E-Series manage email notification settings |
| 13 |
description: |
| 14 |
- Certain E-Series systems have the capability to send email notifications on potentially critical events. |
| 15 |
- This module will allow the owner of the system to specify email recipients for these messages. |
| 16 |
author: |
| 17 |
- Michael Price (@lmprice) |
| 18 |
- Vu Tran (@VuTran007) |
| 19 |
extends_documentation_fragment: |
| 20 |
- netapp_eseries.santricity.santricity.santricity_doc |
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- Enable/disable the sending of email-based alerts. |
| 25 |
type: str |
| 26 |
default: enabled |
| 27 |
required: false |
| 28 |
choices: |
| 29 |
- enabled |
| 30 |
- disabled |
| 31 |
server: |
| 32 |
description: |
| 33 |
- A fully qualified domain name, IPv4 address, or IPv6 address of a mail server. |
| 34 |
- To use a fully qualified domain name, you must configure a DNS server on both controllers using |
| 35 |
M(netapp_eseries.santricity.na_santricity_mgmt_interface). |
| 36 |
- Required when I(state=enabled). |
| 37 |
type: str |
| 38 |
required: false |
| 39 |
sender: |
| 40 |
description: |
| 41 |
- This is the sender that the recipient will see. It doesn't necessarily need to be a valid email account. |
| 42 |
- Required when I(state=enabled). |
| 43 |
type: str |
| 44 |
required: false |
| 45 |
contact: |
| 46 |
description: |
| 47 |
- Allows the owner to specify some free-form contact information to be included in the emails. |
| 48 |
- This is typically utilized to provide a contact phone number. |
| 49 |
type: str |
| 50 |
required: false |
| 51 |
recipients: |
| 52 |
description: |
| 53 |
- The email addresses that will receive the email notifications. |
| 54 |
- Required when I(state=enabled). |
| 55 |
type: list |
| 56 |
elements: str |
| 57 |
required: false |
| 58 |
test: |
| 59 |
description: |
| 60 |
- When a change is detected in the configuration, a test email will be sent. |
| 61 |
- This may take a few minutes to process. |
| 62 |
- Only applicable if I(state=enabled). |
| 63 |
type: bool |
| 64 |
required: false |
| 65 |
default: false |
| 66 |
notes: |
| 67 |
- Check mode is supported. |
| 68 |
- Alertable messages are a subset of messages shown by the Major Event Log (MEL), of the storage-system. Examples |
| 69 |
of alertable messages include drive failures, failed controllers, loss of redundancy, and other warning/critical |
| 70 |
events. |
| 71 |
- This API is currently only supported with the Embedded Web Services API v2.0 and higher. |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = """ |
| 75 |
- name: Enable email-based alerting |
| 76 |
na_santricity_alerts: |
| 77 |
state: enabled |
| 78 |
sender: [email protected] |
| 79 |
server: [email protected] |
| 80 |
contact: "Phone: 1-555-555-5555" |
| 81 |
recipients: |
| 82 |
- [email protected] |
| 83 |
- [email protected] |
| 84 |
api_url: "10.1.1.1:8443" |
| 85 |
api_username: "admin" |
| 86 |
api_password: "myPass" |
| 87 |
|
| 88 |
- name: Disable alerting |
| 89 |
na_santricity_alerts: |
| 90 |
state: disabled |
| 91 |
api_url: "10.1.1.1:8443" |
| 92 |
api_username: "admin" |
| 93 |
api_password: "myPass" |
| 94 |
""" |
| 95 |
|
| 96 |
RETURN = """ |
| 97 |
msg: |
| 98 |
description: Success message |
| 99 |
returned: on success |
| 100 |
type: str |
| 101 |
sample: The settings have been updated. |
| 102 |
""" |
| 103 |
import re |
| 104 |
|
| 105 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule |
| 106 |
from ansible.module_utils._text import to_native |
| 107 |
|
| 108 |
|
| 109 |
class NetAppESeriesAlerts(NetAppESeriesModule): |
| 110 |
def __init__(self): |
| 111 |
ansible_options = dict(state=dict(type='str', required=False, default='enabled', choices=['enabled', 'disabled']), |
| 112 |
server=dict(type='str', required=False), |
| 113 |
sender=dict(type='str', required=False), |
| 114 |
contact=dict(type='str', required=False), |
| 115 |
recipients=dict(type='list', elements='str', required=False), |
| 116 |
test=dict(type='bool', required=False, default=False)) |
| 117 |
|
| 118 |
required_if = [['state', 'enabled', ['server', 'sender', 'recipients']]] |
| 119 |
super(NetAppESeriesAlerts, self).__init__(ansible_options=ansible_options, |
| 120 |
web_services_version="02.00.0000.0000", |
| 121 |
required_if=required_if, |
| 122 |
supports_check_mode=True) |
| 123 |
|
| 124 |
args = self.module.params |
| 125 |
self.alerts = args['state'] == 'enabled' |
| 126 |
self.server = args['server'] |
| 127 |
self.sender = args['sender'] |
| 128 |
self.contact = args['contact'] |
| 129 |
self.recipients = args['recipients'] |
| 130 |
self.test = args['test'] |
| 131 |
self.check_mode = self.module.check_mode |
| 132 |
|
| 133 |
|
| 134 |
email = re.compile(r"[^@]+@[^@]+\.[^@]+") |
| 135 |
|
| 136 |
if self.sender and not email.match(self.sender): |
| 137 |
self.module.fail_json(msg="The sender (%s) provided is not a valid email address." % self.sender) |
| 138 |
|
| 139 |
if self.recipients is not None: |
| 140 |
for recipient in self.recipients: |
| 141 |
if not email.match(recipient): |
| 142 |
self.module.fail_json(msg="The recipient (%s) provided is not a valid email address." % recipient) |
| 143 |
|
| 144 |
if len(self.recipients) < 1: |
| 145 |
self.module.fail_json(msg="At least one recipient address must be specified.") |
| 146 |
|
| 147 |
def get_configuration(self): |
| 148 |
"""Retrieve the current storage system alert settings.""" |
| 149 |
if self.is_proxy(): |
| 150 |
if self.is_embedded_available(): |
| 151 |
try: |
| 152 |
rc, result = self.request("storage-systems/%s/forward/devmgr/v2/storage-systems/1/device-alerts" % self.ssid) |
| 153 |
return result |
| 154 |
except Exception as err: |
| 155 |
self.module.fail_json(msg="Failed to retrieve the alerts configuration! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 156 |
else: |
| 157 |
self.module.fail_json(msg="Setting SANtricity alerts is only available from SANtricity Web Services Proxy if the storage system has" |
| 158 |
" SANtricity Web Services Embedded available. Array [%s]." % self.ssid) |
| 159 |
else: |
| 160 |
try: |
| 161 |
rc, result = self.request("storage-systems/%s/device-alerts" % self.ssid) |
| 162 |
return result |
| 163 |
except Exception as err: |
| 164 |
self.module.fail_json(msg="Failed to retrieve the alerts configuration! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 165 |
|
| 166 |
def update_configuration(self): |
| 167 |
"""Update the storage system alert settings.""" |
| 168 |
config = self.get_configuration() |
| 169 |
update = False |
| 170 |
body = dict() |
| 171 |
|
| 172 |
if self.alerts: |
| 173 |
body = dict(alertingEnabled=True) |
| 174 |
if not config['alertingEnabled']: |
| 175 |
update = True |
| 176 |
|
| 177 |
body.update(emailServerAddress=self.server) |
| 178 |
if config['emailServerAddress'] != self.server: |
| 179 |
update = True |
| 180 |
|
| 181 |
body.update(additionalContactInformation=self.contact, sendAdditionalContactInformation=True) |
| 182 |
if self.contact and (self.contact != config['additionalContactInformation'] |
| 183 |
or not config['sendAdditionalContactInformation']): |
| 184 |
update = True |
| 185 |
|
| 186 |
body.update(emailSenderAddress=self.sender) |
| 187 |
if config['emailSenderAddress'] != self.sender: |
| 188 |
update = True |
| 189 |
|
| 190 |
self.recipients.sort() |
| 191 |
if config['recipientEmailAddresses']: |
| 192 |
config['recipientEmailAddresses'].sort() |
| 193 |
|
| 194 |
body.update(recipientEmailAddresses=self.recipients) |
| 195 |
if config['recipientEmailAddresses'] != self.recipients: |
| 196 |
update = True |
| 197 |
|
| 198 |
elif config['alertingEnabled']: |
| 199 |
body = {"alertingEnabled": False, "emailServerAddress": "", "emailSenderAddress": "", "sendAdditionalContactInformation": False, |
| 200 |
"additionalContactInformation": "", "recipientEmailAddresses": []} |
| 201 |
update = True |
| 202 |
|
| 203 |
if update and not self.check_mode: |
| 204 |
if self.is_proxy() and self.is_embedded_available(): |
| 205 |
try: |
| 206 |
rc, result = self.request("storage-systems/%s/forward/devmgr/v2/storage-systems/1/device-alerts" % self.ssid, method="POST", data=body) |
| 207 |
except Exception as err: |
| 208 |
self.module.fail_json(msg="We failed to set the storage-system name! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 209 |
|
| 210 |
else: |
| 211 |
try: |
| 212 |
rc, result = self.request("storage-systems/%s/device-alerts" % self.ssid, method="POST", data=body) |
| 213 |
except Exception as err: |
| 214 |
self.module.fail_json(msg="We failed to set the storage-system name! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 215 |
|
| 216 |
return update |
| 217 |
|
| 218 |
def send_test_email(self): |
| 219 |
"""Send a test email to verify that the provided configuration is valid and functional.""" |
| 220 |
if not self.check_mode: |
| 221 |
if self.is_proxy() and self.is_embedded_available(): |
| 222 |
try: |
| 223 |
rc, resp = self.request("storage-systems/%s/forward/devmgr/v2/storage-systems/1/device-alerts/alert-email-test" % self.ssid, method="POST") |
| 224 |
if resp['response'] != 'emailSentOK': |
| 225 |
self.module.fail_json(msg="The test email failed with status=[%s]! Array Id [%s]." % (resp['response'], self.ssid)) |
| 226 |
except Exception as err: |
| 227 |
self.module.fail_json(msg="We failed to send the test email! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 228 |
|
| 229 |
else: |
| 230 |
try: |
| 231 |
rc, resp = self.request("storage-systems/%s/device-alerts/alert-email-test" % self.ssid, method="POST") |
| 232 |
if resp['response'] != 'emailSentOK': |
| 233 |
self.module.fail_json(msg="The test email failed with status=[%s]! Array Id [%s]." % (resp['response'], self.ssid)) |
| 234 |
except Exception as err: |
| 235 |
self.module.fail_json(msg="We failed to send the test email! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 236 |
|
| 237 |
def update(self): |
| 238 |
update = self.update_configuration() |
| 239 |
|
| 240 |
if self.test and update: |
| 241 |
self.send_test_email() |
| 242 |
|
| 243 |
if self.alerts: |
| 244 |
msg = 'Alerting has been enabled using server=%s, sender=%s.' % (self.server, self.sender) |
| 245 |
else: |
| 246 |
msg = 'Alerting has been disabled.' |
| 247 |
|
| 248 |
self.module.exit_json(msg=msg, changed=update) |
| 249 |
|
| 250 |
|
| 251 |
def main(): |
| 252 |
alerts = NetAppESeriesAlerts() |
| 253 |
alerts.update() |
| 254 |
|
| 255 |
|
| 256 |
if __name__ == '__main__': |
| 257 |
main() |