| 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_syslog |
| 12 |
short_description: NetApp E-Series manage syslog servers receiving storage system alerts. |
| 13 |
description: |
| 14 |
- Manage the list of syslog servers that will notifications on potentially critical events. |
| 15 |
author: |
| 16 |
- Nathan Swartz (@swartzn) |
| 17 |
- Vu Tran (@VuTran007) |
| 18 |
extends_documentation_fragment: |
| 19 |
- netapp_eseries.santricity.santricity.santricity_doc |
| 20 |
options: |
| 21 |
servers: |
| 22 |
description: |
| 23 |
- List of dictionaries where each dictionary contains a syslog server entry. |
| 24 |
type: list |
| 25 |
elements: raw |
| 26 |
required: False |
| 27 |
suboptions: |
| 28 |
address: |
| 29 |
description: |
| 30 |
- Syslog server address can be a fully qualified domain name, IPv4 address, or IPv6 address. |
| 31 |
required: true |
| 32 |
port: |
| 33 |
description: |
| 34 |
- UDP Port must be a numerical value between 0 and 65535. Typically, the UDP Port for syslog is 514. |
| 35 |
required: false |
| 36 |
default: 514 |
| 37 |
test: |
| 38 |
description: |
| 39 |
- This forces a test syslog message to be sent to the stated syslog server. |
| 40 |
- Test will only be issued when a change is made. |
| 41 |
type: bool |
| 42 |
required: false |
| 43 |
default: false |
| 44 |
notes: |
| 45 |
- Check mode is supported. |
| 46 |
- This API is currently only supported with the Embedded Web Services API v2.12 (bundled with |
| 47 |
SANtricity OS 11.40.2) and higher. |
| 48 |
""" |
| 49 |
|
| 50 |
EXAMPLES = """ |
| 51 |
- name: Add two syslog server configurations to NetApp E-Series storage array. |
| 52 |
na_santricity_alerts_syslog: |
| 53 |
ssid: "1" |
| 54 |
api_url: "https://192.168.1.100:8443/devmgr/v2" |
| 55 |
api_username: "admin" |
| 56 |
api_password: "adminpass" |
| 57 |
validate_certs: true |
| 58 |
servers: |
| 59 |
- address: "192.168.1.100" |
| 60 |
- address: "192.168.2.100" |
| 61 |
port: 514 |
| 62 |
- address: "192.168.3.100" |
| 63 |
port: 1000 |
| 64 |
""" |
| 65 |
|
| 66 |
RETURN = """ |
| 67 |
msg: |
| 68 |
description: Success message |
| 69 |
returned: on success |
| 70 |
type: str |
| 71 |
sample: The settings have been updated. |
| 72 |
""" |
| 73 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule |
| 74 |
from ansible.module_utils._text import to_native |
| 75 |
|
| 76 |
|
| 77 |
class NetAppESeriesAlertsSyslog(NetAppESeriesModule): |
| 78 |
def __init__(self): |
| 79 |
ansible_options = dict(servers=dict(type="list", required=False, elements='raw'), |
| 80 |
test=dict(type="bool", default=False, required=False)) |
| 81 |
|
| 82 |
required_if = None |
| 83 |
mutually_exclusive = None |
| 84 |
|
| 85 |
super(NetAppESeriesAlertsSyslog, self).__init__(ansible_options=ansible_options, |
| 86 |
web_services_version="02.00.0000.0000", |
| 87 |
mutually_exclusive=mutually_exclusive, |
| 88 |
required_if=required_if, |
| 89 |
supports_check_mode=True) |
| 90 |
args = self.module.params |
| 91 |
if args["servers"] and len(args["servers"]) > 5: |
| 92 |
self.module.fail_json(msg="Maximum number of syslog servers is 5! Array Id [%s]." % self.ssid) |
| 93 |
|
| 94 |
self.servers = {} |
| 95 |
if args["servers"] is not None: |
| 96 |
for server in args["servers"]: |
| 97 |
port = 514 |
| 98 |
if "port" in server: |
| 99 |
port = server["port"] |
| 100 |
self.servers.update({server["address"]: port}) |
| 101 |
|
| 102 |
self.test = args["test"] |
| 103 |
self.check_mode = self.module.check_mode |
| 104 |
|
| 105 |
|
| 106 |
self.url_path_prefix = "" |
| 107 |
if not self.is_embedded() and self.ssid != "0" and self.ssid.lower() != "proxy": |
| 108 |
self.url_path_prefix = "storage-systems/%s/forward/devmgr/v2/" % self.ssid |
| 109 |
|
| 110 |
def get_current_configuration(self): |
| 111 |
"""Retrieve existing alert-syslog configuration.""" |
| 112 |
try: |
| 113 |
rc, result = self.request(self.url_path_prefix + "storage-systems/%s/device-alerts/alert-syslog" % ("1" if self.url_path_prefix else self.ssid)) |
| 114 |
return result |
| 115 |
except Exception as error: |
| 116 |
self.module.fail_json(msg="Failed to retrieve syslog configuration! Array Id [%s]. Error [%s]." % (self.ssid, to_native(error))) |
| 117 |
|
| 118 |
def is_change_required(self): |
| 119 |
"""Determine whether changes are required.""" |
| 120 |
current_config = self.get_current_configuration() |
| 121 |
|
| 122 |
|
| 123 |
if self.servers: |
| 124 |
for entry in current_config["syslogReceivers"]: |
| 125 |
if entry["serverName"] not in self.servers.keys() or entry["portNumber"] != self.servers[entry["serverName"]]: |
| 126 |
return True |
| 127 |
|
| 128 |
for server, port in self.servers.items(): |
| 129 |
for entry in current_config["syslogReceivers"]: |
| 130 |
if server == entry["serverName"] and port == entry["portNumber"]: |
| 131 |
break |
| 132 |
else: |
| 133 |
return True |
| 134 |
return False |
| 135 |
|
| 136 |
elif current_config["syslogReceivers"]: |
| 137 |
return True |
| 138 |
|
| 139 |
return False |
| 140 |
|
| 141 |
def make_request_body(self): |
| 142 |
"""Generate the request body.""" |
| 143 |
body = {"syslogReceivers": [], "defaultFacility": 3, "defaultTag": "StorageArray"} |
| 144 |
|
| 145 |
for server, port in self.servers.items(): |
| 146 |
body["syslogReceivers"].append({"serverName": server, "portNumber": port}) |
| 147 |
|
| 148 |
return body |
| 149 |
|
| 150 |
def test_configuration(self): |
| 151 |
"""Send syslog test message to all systems (only option).""" |
| 152 |
try: |
| 153 |
rc, result = self.request(self.url_path_prefix + "storage-systems/%s/device-alerts/alert-syslog-test" |
| 154 |
% ("1" if self.url_path_prefix else self.ssid), method="POST") |
| 155 |
except Exception as error: |
| 156 |
self.module.fail_json(msg="Failed to send test message! Array Id [%s]. Error [%s]." % (self.ssid, to_native(error))) |
| 157 |
|
| 158 |
def update(self): |
| 159 |
"""Update configuration and respond to ansible.""" |
| 160 |
change_required = self.is_change_required() |
| 161 |
|
| 162 |
if change_required and not self.check_mode: |
| 163 |
try: |
| 164 |
rc, result = self.request(self.url_path_prefix + "storage-systems/%s/device-alerts/alert-syslog" % ("1" if self.url_path_prefix else self.ssid), |
| 165 |
method="POST", data=self.make_request_body()) |
| 166 |
except Exception as error: |
| 167 |
self.module.fail_json(msg="Failed to add syslog server! Array Id [%s]. Error [%s]." % (self.ssid, to_native(error))) |
| 168 |
|
| 169 |
if self.test and self.servers: |
| 170 |
self.test_configuration() |
| 171 |
|
| 172 |
self.module.exit_json(msg="The syslog settings have been updated.", changed=change_required) |
| 173 |
|
| 174 |
|
| 175 |
def main(): |
| 176 |
settings = NetAppESeriesAlertsSyslog() |
| 177 |
settings.update() |
| 178 |
|
| 179 |
|
| 180 |
if __name__ == '__main__': |
| 181 |
main() |