| 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_auditlog |
| 12 |
short_description: NetApp E-Series manage audit-log configuration |
| 13 |
description: |
| 14 |
- This module allows an e-series storage system owner to set audit-log configuration parameters. |
| 15 |
author: Nathan Swartz (@ndswartz) |
| 16 |
extends_documentation_fragment: |
| 17 |
- netapp_eseries.santricity.santricity.santricity_doc |
| 18 |
options: |
| 19 |
max_records: |
| 20 |
description: |
| 21 |
- The maximum number log messages audit-log will retain. |
| 22 |
- Max records must be between and including 100 and 50000. |
| 23 |
type: int |
| 24 |
default: 50000 |
| 25 |
log_level: |
| 26 |
description: Filters the log messages according to the specified log level selection. |
| 27 |
choices: |
| 28 |
- all |
| 29 |
- writeOnly |
| 30 |
type: str |
| 31 |
default: writeOnly |
| 32 |
full_policy: |
| 33 |
description: Specifies what audit-log should do once the number of entries approach the record limit. |
| 34 |
choices: |
| 35 |
- overWrite |
| 36 |
- preventSystemAccess |
| 37 |
type: str |
| 38 |
default: overWrite |
| 39 |
threshold: |
| 40 |
description: |
| 41 |
- This is the memory full percent threshold that audit-log will start issuing warning messages. |
| 42 |
- Percent range must be between and including 60 and 90. |
| 43 |
type: int |
| 44 |
default: 90 |
| 45 |
force: |
| 46 |
description: |
| 47 |
- Forces the audit-log configuration to delete log history when log messages fullness cause immediate |
| 48 |
warning or full condition. |
| 49 |
- Warning! This will cause any existing audit-log messages to be deleted. |
| 50 |
- This is only applicable for I(full_policy=preventSystemAccess). |
| 51 |
type: bool |
| 52 |
default: no |
| 53 |
notes: |
| 54 |
- Check mode is supported. |
| 55 |
- Use I(ssid=="0") or I(ssid=="proxy") to configure SANtricity Web Services Proxy auditlog settings otherwise. |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = """ |
| 59 |
- name: Define audit-log to prevent system access if records exceed 50000 with warnings occurring at 60% capacity. |
| 60 |
na_santricity_auditlog: |
| 61 |
ssid: "1" |
| 62 |
api_url: "https://192.168.1.100:8443/devmgr/v2" |
| 63 |
api_username: "admin" |
| 64 |
api_password: "adminpass" |
| 65 |
validate_certs: true |
| 66 |
max_records: 50000 |
| 67 |
log_level: all |
| 68 |
full_policy: preventSystemAccess |
| 69 |
threshold: 60 |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = """ |
| 73 |
msg: |
| 74 |
description: Success message |
| 75 |
returned: on success |
| 76 |
type: str |
| 77 |
sample: The settings have been updated. |
| 78 |
""" |
| 79 |
import json |
| 80 |
|
| 81 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule |
| 82 |
from ansible.module_utils._text import to_native |
| 83 |
|
| 84 |
|
| 85 |
class NetAppESeriesAuditLog(NetAppESeriesModule): |
| 86 |
"""Audit-log module configuration class.""" |
| 87 |
MAX_RECORDS = 50000 |
| 88 |
|
| 89 |
def __init__(self): |
| 90 |
ansible_options = dict(max_records=dict(type="int", default=50000), |
| 91 |
log_level=dict(type="str", default="writeOnly", choices=["all", "writeOnly"]), |
| 92 |
full_policy=dict(type="str", default="overWrite", choices=["overWrite", "preventSystemAccess"]), |
| 93 |
threshold=dict(type="int", default=90), |
| 94 |
force=dict(type="bool", default=False)) |
| 95 |
super(NetAppESeriesAuditLog, self).__init__(ansible_options=ansible_options, |
| 96 |
web_services_version="02.00.0000.0000", |
| 97 |
supports_check_mode=True) |
| 98 |
|
| 99 |
args = self.module.params |
| 100 |
self.log_level = args["log_level"] |
| 101 |
self.force = args["force"] |
| 102 |
self.full_policy = args["full_policy"] |
| 103 |
self.max_records = args["max_records"] |
| 104 |
self.threshold = args["threshold"] |
| 105 |
|
| 106 |
if self.max_records < 100 or self.max_records > self.MAX_RECORDS: |
| 107 |
self.module.fail_json(msg="Audit-log max_records count must be between 100 and 50000: [%s]" % self.max_records) |
| 108 |
|
| 109 |
if self.threshold < 60 or self.threshold > 90: |
| 110 |
self.module.fail_json(msg="Audit-log percent threshold must be between 60 and 90: [%s]" % self.threshold) |
| 111 |
|
| 112 |
|
| 113 |
self.url_path_prefix = "" |
| 114 |
if not self.is_embedded() and self.ssid != "0" and self.ssid.lower() != "proxy": |
| 115 |
self.url_path_prefix = "storage-systems/%s/forward/devmgr/v2/" % self.ssid |
| 116 |
|
| 117 |
def get_configuration(self): |
| 118 |
"""Retrieve the existing audit-log configurations. |
| 119 |
|
| 120 |
:returns: dictionary containing current audit-log configuration |
| 121 |
""" |
| 122 |
try: |
| 123 |
if self.is_proxy() and (self.ssid == "0" or self.ssid.lower() != "proxy"): |
| 124 |
rc, data = self.request("audit-log/config") |
| 125 |
else: |
| 126 |
rc, data = self.request(self.url_path_prefix + "storage-systems/1/audit-log/config") |
| 127 |
return data |
| 128 |
except Exception as err: |
| 129 |
self.module.fail_json(msg="Failed to retrieve the audit-log configuration! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 130 |
|
| 131 |
def build_configuration(self): |
| 132 |
"""Build audit-log expected configuration. |
| 133 |
|
| 134 |
:returns: Tuple containing update boolean value and dictionary of audit-log configuration |
| 135 |
""" |
| 136 |
config = self.get_configuration() |
| 137 |
|
| 138 |
current = dict(auditLogMaxRecords=config["auditLogMaxRecords"], |
| 139 |
auditLogLevel=config["auditLogLevel"], |
| 140 |
auditLogFullPolicy=config["auditLogFullPolicy"], |
| 141 |
auditLogWarningThresholdPct=config["auditLogWarningThresholdPct"]) |
| 142 |
|
| 143 |
body = dict(auditLogMaxRecords=self.max_records, |
| 144 |
auditLogLevel=self.log_level, |
| 145 |
auditLogFullPolicy=self.full_policy, |
| 146 |
auditLogWarningThresholdPct=self.threshold) |
| 147 |
|
| 148 |
update = current != body |
| 149 |
return update, body |
| 150 |
|
| 151 |
def delete_log_messages(self): |
| 152 |
"""Delete all audit-log messages.""" |
| 153 |
try: |
| 154 |
if self.is_proxy() and (self.ssid == "0" or self.ssid.lower() != "proxy"): |
| 155 |
rc, result = self.request("audit-log?clearAll=True", method="DELETE") |
| 156 |
else: |
| 157 |
rc, result = self.request(self.url_path_prefix + "storage-systems/1/audit-log?clearAll=True", method="DELETE") |
| 158 |
except Exception as err: |
| 159 |
self.module.fail_json(msg="Failed to delete audit-log messages! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 160 |
|
| 161 |
def update_configuration(self, update=None, body=None, attempt_recovery=True): |
| 162 |
"""Update audit-log configuration.""" |
| 163 |
if update is None or body is None: |
| 164 |
update, body = self.build_configuration() |
| 165 |
|
| 166 |
if update and not self.module.check_mode: |
| 167 |
try: |
| 168 |
if self.is_proxy() and (self.ssid == "0" or self.ssid.lower() != "proxy"): |
| 169 |
rc, result = self.request("audit-log/config", data=json.dumps(body), method='POST', ignore_errors=True) |
| 170 |
else: |
| 171 |
rc, result = self.request(self.url_path_prefix + "storage-systems/1/audit-log/config", |
| 172 |
data=json.dumps(body), method='POST', ignore_errors=True) |
| 173 |
|
| 174 |
if rc == 422: |
| 175 |
if self.force and attempt_recovery: |
| 176 |
self.delete_log_messages() |
| 177 |
update = self.update_configuration(update, body, False) |
| 178 |
else: |
| 179 |
self.module.fail_json(msg="Failed to update audit-log configuration! Array Id [%s]. Error [%s]." % (self.ssid, to_native(rc, result))) |
| 180 |
|
| 181 |
except Exception as error: |
| 182 |
self.module.fail_json(msg="Failed to update audit-log configuration! Array Id [%s]. Error [%s]." % (self.ssid, to_native(error))) |
| 183 |
return update |
| 184 |
|
| 185 |
def update(self): |
| 186 |
"""Update the audit-log configuration.""" |
| 187 |
update = self.update_configuration() |
| 188 |
if update: |
| 189 |
self.module.exit_json(msg="Audit-log update complete", changed=update) |
| 190 |
else: |
| 191 |
self.module.exit_json(msg="No audit-log changes required", changed=update) |
| 192 |
|
| 193 |
|
| 194 |
def main(): |
| 195 |
auditlog = NetAppESeriesAuditLog() |
| 196 |
auditlog.update() |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__": |
| 200 |
main() |