Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_messages.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2022, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_messages
20 version_added: '1.14.0'
21 short_description: List FlashArray Alert Messages
22 description:
23 - List Alert messages based on filters provided
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 severity:
28 description:
29 - severity of the alerts to show
30 type: str
31 choices: [ critical, warning, info ]
32 default: info
33 state:
34 description:
35 - State of alerts to show
36 default: open
37 choices: [ open, closed ]
38 type: str
39 flagged:
40 description:
41 - Show alerts that have been acknowledged or not
42 default: false
43 type: bool
44 history:
45 description:
46 - Historical time period to show alerts for, from present time
47 - Allowed time period are hour(h), day(d), week(w) and year(y)
48 type: str
49 default: 1w
50 context:
51 description:
52 - Name of fleet member on which to perform the operation.
53 - This requires the array receiving the request is a member of a fleet
54 and the context name to be a member of the same fleet.
55 type: str
56 default: ""
57 version_added: '1.39.0'
58 extends_documentation_fragment:
59 - purestorage.flasharray.purestorage.fa
60 """
61
62 EXAMPLES = r"""
63 - name: Show critical alerts from past 4 weeks that haven't been acknowledged
64 purefa_messages:
65 history: 4w
66 flagged: false
67 severity: critical
68 fa_url: 10.10.10.2
69 api_token: 89a9356f-c203-d263-8a89-c229486a13ba
70 """
71
72 RETURN = r"""
73 """
74
75 import time
76
77 from ansible.module_utils.basic import AnsibleModule
78 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
79 get_array,
80 purefa_argument_spec,
81 )
82 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
83 LooseVersion,
84 )
85
86 MIN_REQUIRED_API_VERSION = "2.2"
87 ALLOWED_PERIODS = ["h", "d", "w", "y"]
88 # Time periods in micro-seconds
89 HOUR = 3600000
90 DAY = HOUR * 24
91 WEEK = DAY * 7
92 YEAR = WEEK * 52
93 CONTEXT_VERSION = "2.38"
94
95
96 def _create_time_window(window):
97 period = window[-1].lower()
98 multiple = int(window[0:-1])
99 if period == "h":
100 return HOUR * multiple
101 if period == "d":
102 return DAY * multiple
103 if period == "w":
104 return WEEK * multiple
105 if period == "y":
106 return YEAR * multiple
107 return None
108
109
110 def main():
111 argument_spec = purefa_argument_spec()
112 argument_spec.update(
113 dict(
114 state=dict(type="str", default="open", choices=["open", "closed"]),
115 history=dict(type="str", default="1w"),
116 flagged=dict(type="bool", default=False),
117 severity=dict(
118 type="str",
119 default="info",
120 choices=["critical", "warning", "info"],
121 ),
122 context=dict(type="str", default=""),
123 )
124 )
125
126 module = AnsibleModule(argument_spec, supports_check_mode=True)
127 time_now = int(time.time() * 1000)
128 array = get_array(module)
129 api_version = array.get_rest_version()
130 if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
131 module.fail_json(
132 msg="FlashArray REST version not supported. "
133 "Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
134 )
135 if module.params["history"][-1].lower() not in ALLOWED_PERIODS:
136 module.fail_json(msg="historical window value is not an allowsd time period")
137 since_time = str(time_now - _create_time_window(module.params["history"].lower()))
138 if module.params["flagged"]:
139 flagged = " and flagged='True'"
140 else:
141 flagged = " and flagged='False'"
142
143 messages = {}
144 severity = " and severity='" + module.params["severity"] + "'"
145 state = " and state='" + module.params["state"] + "'"
146 filter_string = "notified>" + since_time + state + flagged + severity
147 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
148 res = array.get_alerts(
149 filter=filter_string, context_names=[module.params["context"]]
150 )
151 else:
152 res = array.get_alerts(filter=filter_string)
153 if res.status_code == 200:
154 alerts = list(res.items)
155 else:
156 module.fail_json(
157 msg="Failed to get alert messages. Error: {0}".format(res.errors[0].message)
158 )
159 for message in alerts:
160 name = message.name
161 messages[name] = {
162 "summary": message.summary,
163 "component_type": message.component_type,
164 "component_name": message.component_name,
165 "code": message.code,
166 "severity": message.severity,
167 "actual": message.actual,
168 "issue": message.issue,
169 "state": message.state,
170 "flagged": message.flagged,
171 "closed": None,
172 "created": time.strftime(
173 "%Y-%m-%d %H:%M:%S",
174 time.gmtime(message.created / 1000),
175 )
176 + " UTC",
177 "updated": time.strftime(
178 "%Y-%m-%d %H:%M:%S",
179 time.gmtime(message.updated / 1000),
180 )
181 + " UTC",
182 }
183 if message.state == "closed":
184 messages[name]["closed"] = (
185 time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(message.closed / 1000))
186 + " UTC"
187 )
188 module.exit_json(changed=False, purefa_messages=messages)
189
190
191 if __name__ == "__main__":
192 main()