| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['deprecated'], |
| 12 |
'supported_by': 'community'} |
| 13 |
|
| 14 |
DOCUMENTATION = """ |
| 15 |
--- |
| 16 |
module: netapp_e_alerts |
| 17 |
short_description: NetApp E-Series manage email notification settings |
| 18 |
description: |
| 19 |
- Certain E-Series systems have the capability to send email notifications on potentially critical events. |
| 20 |
- This module will allow the owner of the system to specify email recipients for these messages. |
| 21 |
version_added: '2.7.0' |
| 22 |
author: Michael Price (@lmprice) |
| 23 |
extends_documentation_fragment: |
| 24 |
- netapp_eseries.santricity.santricity.netapp.eseries |
| 25 |
options: |
| 26 |
state: |
| 27 |
description: |
| 28 |
- Enable/disable the sending of email-based alerts. |
| 29 |
default: enabled |
| 30 |
required: false |
| 31 |
type: str |
| 32 |
choices: |
| 33 |
- enabled |
| 34 |
- disabled |
| 35 |
server: |
| 36 |
description: |
| 37 |
- A fully qualified domain name, IPv4 address, or IPv6 address of a mail server. |
| 38 |
- To use a fully qualified domain name, you must configure a DNS server on both controllers using |
| 39 |
M(netapp_eseries.santricity.netapp_e_mgmt_interface). |
| 40 |
- Required when I(state=enabled). |
| 41 |
type: str |
| 42 |
required: no |
| 43 |
sender: |
| 44 |
description: |
| 45 |
- This is the sender that the recipient will see. It doesn't necessarily need to be a valid email account. |
| 46 |
- Required when I(state=enabled). |
| 47 |
type: str |
| 48 |
required: no |
| 49 |
contact: |
| 50 |
description: |
| 51 |
- Allows the owner to specify some free-form contact information to be included in the emails. |
| 52 |
- This is typically utilized to provide a contact phone number. |
| 53 |
type: str |
| 54 |
required: no |
| 55 |
recipients: |
| 56 |
description: |
| 57 |
- The email addresses that will receive the email notifications. |
| 58 |
- Required when I(state=enabled). |
| 59 |
type: list |
| 60 |
elements: str |
| 61 |
required: no |
| 62 |
test: |
| 63 |
description: |
| 64 |
- When a change is detected in the configuration, a test email will be sent. |
| 65 |
- This may take a few minutes to process. |
| 66 |
- Only applicable if I(state=enabled). |
| 67 |
default: no |
| 68 |
type: bool |
| 69 |
log_path: |
| 70 |
description: |
| 71 |
- Path to a file on the Ansible control node to be used for debug logging |
| 72 |
type: str |
| 73 |
required: no |
| 74 |
notes: |
| 75 |
- Check mode is supported. |
| 76 |
- Alertable messages are a subset of messages shown by the Major Event Log (MEL), of the storage-system. Examples |
| 77 |
of alertable messages include drive failures, failed controllers, loss of redundancy, and other warning/critical |
| 78 |
events. |
| 79 |
- This API is currently only supported with the Embedded Web Services API v2.0 and higher. |
| 80 |
""" |
| 81 |
|
| 82 |
EXAMPLES = """ |
| 83 |
- name: Enable email-based alerting |
| 84 |
netapp_e_alerts: |
| 85 |
state: enabled |
| 86 |
sender: [email protected] |
| 87 |
server: [email protected] |
| 88 |
contact: "Phone: 1-555-555-5555" |
| 89 |
recipients: |
| 90 |
- [email protected] |
| 91 |
- [email protected] |
| 92 |
api_url: "10.1.1.1:8443" |
| 93 |
api_username: "admin" |
| 94 |
api_password: "myPass" |
| 95 |
|
| 96 |
- name: Disable alerting |
| 97 |
netapp_e_alerts: |
| 98 |
state: disabled |
| 99 |
api_url: "10.1.1.1:8443" |
| 100 |
api_username: "admin" |
| 101 |
api_password: "myPass" |
| 102 |
""" |
| 103 |
|
| 104 |
RETURN = """ |
| 105 |
msg: |
| 106 |
description: Success message |
| 107 |
returned: on success |
| 108 |
type: str |
| 109 |
sample: The settings have been updated. |
| 110 |
""" |
| 111 |
|
| 112 |
import json |
| 113 |
import logging |
| 114 |
from pprint import pformat |
| 115 |
import re |
| 116 |
|
| 117 |
from ansible.module_utils.basic import AnsibleModule |
| 118 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.netapp import request, eseries_host_argument_spec |
| 119 |
from ansible.module_utils._text import to_native |
| 120 |
|
| 121 |
HEADERS = { |
| 122 |
"Content-Type": "application/json", |
| 123 |
"Accept": "application/json", |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
class Alerts(object): |
| 128 |
def __init__(self): |
| 129 |
argument_spec = eseries_host_argument_spec() |
| 130 |
argument_spec.update(dict( |
| 131 |
state=dict(type='str', required=False, default='enabled', |
| 132 |
choices=['enabled', 'disabled']), |
| 133 |
server=dict(type='str', required=False, ), |
| 134 |
sender=dict(type='str', required=False, ), |
| 135 |
contact=dict(type='str', required=False, ), |
| 136 |
recipients=dict(type='list', elements="str", required=False, ), |
| 137 |
test=dict(type='bool', required=False, default=False, ), |
| 138 |
log_path=dict(type='str', required=False), |
| 139 |
)) |
| 140 |
|
| 141 |
required_if = [ |
| 142 |
['state', 'enabled', ['server', 'sender', 'recipients']] |
| 143 |
] |
| 144 |
|
| 145 |
self.module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_if=required_if) |
| 146 |
args = self.module.params |
| 147 |
self.alerts = args['state'] == 'enabled' |
| 148 |
self.server = args['server'] |
| 149 |
self.sender = args['sender'] |
| 150 |
self.contact = args['contact'] |
| 151 |
self.recipients = args['recipients'] |
| 152 |
self.test = args['test'] |
| 153 |
|
| 154 |
self.ssid = args['ssid'] |
| 155 |
self.url = args['api_url'] |
| 156 |
self.creds = dict(url_password=args['api_password'], |
| 157 |
validate_certs=args['validate_certs'], |
| 158 |
url_username=args['api_username'], ) |
| 159 |
|
| 160 |
self.check_mode = self.module.check_mode |
| 161 |
|
| 162 |
log_path = args['log_path'] |
| 163 |
|
| 164 |
# logging setup |
| 165 |
self._logger = logging.getLogger(self.__class__.__name__) |
| 166 |
|
| 167 |
if log_path: |
| 168 |
logging.basicConfig( |
| 169 |
level=logging.DEBUG, filename=log_path, filemode='w', |
| 170 |
format='%(relativeCreated)dms %(levelname)s %(module)s.%(funcName)s:%(lineno)d\n %(message)s') |
| 171 |
|
| 172 |
if not self.url.endswith('/'): |
| 173 |
self.url += '/' |
| 174 |
|
| 175 |
# Very basic validation on email addresses: [email protected] |
| 176 |
email = re.compile(r"[^@]+@[^@]+\.[^@]+") |
| 177 |
|
| 178 |
if self.sender and not email.match(self.sender): |
| 179 |
self.module.fail_json(msg="The sender (%s) provided is not a valid email address." % self.sender) |
| 180 |
|
| 181 |
if self.recipients is not None: |
| 182 |
for recipient in self.recipients: |
| 183 |
if not email.match(recipient): |
| 184 |
self.module.fail_json(msg="The recipient (%s) provided is not a valid email address." % recipient) |
| 185 |
|
| 186 |
if len(self.recipients) < 1: |
| 187 |
self.module.fail_json(msg="At least one recipient address must be specified.") |
| 188 |
|
| 189 |
def get_configuration(self): |
| 190 |
try: |
| 191 |
(rc, result) = request(self.url + 'storage-systems/%s/device-alerts' % self.ssid, headers=HEADERS, |
| 192 |
**self.creds) |
| 193 |
self._logger.info("Current config: %s", pformat(result)) |
| 194 |
return result |
| 195 |
|
| 196 |
except Exception as err: |
| 197 |
self.module.fail_json(msg="Failed to retrieve the alerts configuration! Array Id [%s]. Error [%s]." |
| 198 |
% (self.ssid, to_native(err))) |
| 199 |
|
| 200 |
def update_configuration(self): |
| 201 |
config = self.get_configuration() |
| 202 |
update = False |
| 203 |
body = dict() |
| 204 |
|
| 205 |
if self.alerts: |
| 206 |
body = dict(alertingEnabled=True) |
| 207 |
if not config['alertingEnabled']: |
| 208 |
update = True |
| 209 |
|
| 210 |
body.update(emailServerAddress=self.server) |
| 211 |
if config['emailServerAddress'] != self.server: |
| 212 |
update = True |
| 213 |
|
| 214 |
body.update(additionalContactInformation=self.contact, sendAdditionalContactInformation=True) |
| 215 |
if self.contact and (self.contact != config['additionalContactInformation'] |
| 216 |
or not config['sendAdditionalContactInformation']): |
| 217 |
update = True |
| 218 |
|
| 219 |
body.update(emailSenderAddress=self.sender) |
| 220 |
if config['emailSenderAddress'] != self.sender: |
| 221 |
update = True |
| 222 |
|
| 223 |
self.recipients.sort() |
| 224 |
if config['recipientEmailAddresses']: |
| 225 |
config['recipientEmailAddresses'].sort() |
| 226 |
|
| 227 |
body.update(recipientEmailAddresses=self.recipients) |
| 228 |
if config['recipientEmailAddresses'] != self.recipients: |
| 229 |
update = True |
| 230 |
|
| 231 |
elif config['alertingEnabled']: |
| 232 |
body = dict(alertingEnabled=False) |
| 233 |
update = True |
| 234 |
|
| 235 |
self._logger.debug(pformat(body)) |
| 236 |
|
| 237 |
if update and not self.check_mode: |
| 238 |
try: |
| 239 |
(rc, result) = request(self.url + 'storage-systems/%s/device-alerts' % self.ssid, method='POST', |
| 240 |
data=json.dumps(body), headers=HEADERS, **self.creds) |
| 241 |
# This is going to catch cases like a connection failure |
| 242 |
except Exception as err: |
| 243 |
self.module.fail_json(msg="We failed to set the storage-system name! Array Id [%s]. Error [%s]." |
| 244 |
% (self.ssid, to_native(err))) |
| 245 |
return update |
| 246 |
|
| 247 |
def send_test_email(self): |
| 248 |
"""Send a test email to verify that the provided configuration is valid and functional.""" |
| 249 |
if not self.check_mode: |
| 250 |
try: |
| 251 |
(rc, result) = request(self.url + 'storage-systems/%s/device-alerts/alert-email-test' % self.ssid, |
| 252 |
timeout=300, method='POST', headers=HEADERS, **self.creds) |
| 253 |
|
| 254 |
if result['response'] != 'emailSentOK': |
| 255 |
self.module.fail_json(msg="The test email failed with status=[%s]! Array Id [%s]." |
| 256 |
% (result['response'], self.ssid)) |
| 257 |
|
| 258 |
# This is going to catch cases like a connection failure |
| 259 |
except Exception as err: |
| 260 |
self.module.fail_json(msg="We failed to send the test email! Array Id [%s]. Error [%s]." |
| 261 |
% (self.ssid, to_native(err))) |
| 262 |
|
| 263 |
def update(self): |
| 264 |
update = self.update_configuration() |
| 265 |
|
| 266 |
if self.test and update: |
| 267 |
self._logger.info("An update was detected and test=True, running a test.") |
| 268 |
self.send_test_email() |
| 269 |
|
| 270 |
if self.alerts: |
| 271 |
msg = 'Alerting has been enabled using server=%s, sender=%s.' % (self.server, self.sender) |
| 272 |
else: |
| 273 |
msg = 'Alerting has been disabled.' |
| 274 |
|
| 275 |
self.module.exit_json(msg=msg, changed=update, ) |
| 276 |
|
| 277 |
def __call__(self, *args, **kwargs): |
| 278 |
self.update() |
| 279 |
|
| 280 |
|
| 281 |
def main(): |
| 282 |
alerts = Alerts() |
| 283 |
alerts() |
| 284 |
|
| 285 |
|
| 286 |
if __name__ == '__main__': |
| 287 |
main() |