Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_pingtrace.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2023, 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: purefb_pingtrace
20 version_added: '1.11.0'
21 short_description: Employ the internal FlashBlade ping and trace mechanisms
22 description:
23 - Ping or trace a destination
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 action:
28 description:
29 - Which action is required, ping or trace
30 type: str
31 choices: [ ping, trace ]
32 default: ping
33 count:
34 description:
35 - Used by ping to specify the number of packets to send
36 type: int
37 default: 1
38 resolve:
39 description:
40 - Specify whether or not to map IP addresses to host names
41 type: bool
42 default: True
43 latency:
44 description:
45 - Specify whether or not to print the full user-to-user latency
46 type: bool
47 default: False
48 packet_size:
49 description:
50 - Used by ping to specify the number of data bytes to send per packet
51 type: int
52 default: 56
53 destination:
54 description:
55 - IP addtress or hostname used to run ping or trace against.
56 type: str
57 required: true
58 method:
59 description:
60 - Used by trace to specify the method to use for operations
61 type: str
62 choices: [ icmp, tcp, udp ]
63 default: udp
64 fragment:
65 description:
66 - Used by trace to specify whether or not to fragment packets
67 type: bool
68 default: true
69 discover_mtu:
70 description:
71 - Used by trace to specify whether or not to discover the MTU
72 along the path being traced
73 type: bool
74 default: false
75 port:
76 description:
77 - Used by trace to specify a destination port
78 type: str
79 source:
80 description:
81 - IP address or hostname used by ping and trace to specify where
82 to start to run the specified operation
83 - If not specified will use all available sources
84 type: str
85 component:
86 description:
87 - Used by ping and trace to specify where to run the operation.
88 - Valid values are controllers and blades from hardware list.
89 - If not specified defaults to all available controllers and selected blades
90 type: str
91 extends_documentation_fragment:
92 - purestorage.flashblade.purestorage.fb
93 """
94
95 EXAMPLES = r"""
96 - name: ping Google DNS server
97 purestorage.flashblade.purefb_pingtrace:
98 destination: 8.8.8.8
99 fb_url: 10.10.10.2
100 api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3
101
102 - name: trace to Google DNS server from CH1.FM0
103 purestorage.flashblade.purefb_pingtrace:
104 action: trace
105 destination: 8.8.8.8
106 fragment_packet: true
107 source: CH1.FM0
108 discover_mtu: true
109 fb_url: 10.10.10.2
110 api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3
111 """
112
113 RETURN = r"""
114 """
115
116 from ansible.module_utils.basic import AnsibleModule
117 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
118 get_system,
119 purefb_argument_spec,
120 )
121 import re
122
123 MIN_REQUIRED_API_VERSION = "2.6"
124
125
126 def run_ping(module, blade):
127 """Run network ping"""
128 ping_fact = {}
129 if module.params["source"] and module.params["component"]:
130 res = blade.get_network_interfaces_ping(
131 destination=module.params["destination"],
132 component_name=module.params["component"],
133 source=module.params["source"],
134 packet_size=module.params["packet_size"],
135 count=module.params["count"],
136 print_latency=module.params["latency"],
137 resolve_hostname=module.params["resolve"],
138 )
139 elif module.params["source"] and not module.params["component"]:
140 res = blade.get_network_interfaces_ping(
141 destination=module.params["destination"],
142 source=module.params["source"],
143 packet_size=module.params["packet_size"],
144 count=module.params["count"],
145 print_latency=module.params["latency"],
146 resolve_hostname=module.params["resolve"],
147 )
148 elif not module.params["source"] and module.params["component"]:
149 res = blade.get_network_interfaces_ping(
150 destination=module.params["destination"],
151 component_name=module.params["component"],
152 packet_size=module.params["packet_size"],
153 count=module.params["count"],
154 print_latency=module.params["latency"],
155 resolve_hostname=module.params["resolve"],
156 )
157 else:
158 res = blade.get_network_interfaces_ping(
159 destination=module.params["destination"],
160 packet_size=module.params["packet_size"],
161 count=module.params["count"],
162 print_latency=module.params["latency"],
163 resolve_hostname=module.params["resolve"],
164 )
165 if res.status_code != 200:
166 module.fail_json(
167 msg="Failed to run ping. Error: {0}".format(res.errors[0].message)
168 )
169 else:
170 responses = list(res.items)
171 for resp in range(len(responses)):
172 comp_name = responses[resp].component_name.replace(".", "_")
173 match = re.search(
174 r"(\d+)\s+packets transmitted,\s+(\d+)\s+received,\s+(\d+)%\s+packet loss,\s+time\s+(\d+)ms",
175 responses[resp].details,
176 )
177
178 if not match:
179 # Report the component that failied along with the failed reposne
180 module.warn("{0}: {1}.".format(comp_name, responses[resp].details))
181 continue
182
183 transmitted, received, packet_loss, time_ms = map(int, match.groups())
184 ping_fact[comp_name] = {
185 "destination": module.params["destination"],
186 "source": module.params["source"],
187 "packet_loss": str(packet_loss) + "%",
188 "packet_tx": transmitted,
189 "packet_rx": received,
190 "time": str(time_ms) + "ms",
191 "details": responses[resp].details,
192 }
193
194 module.exit_json(changed=False, pingfact=ping_fact)
195
196
197 def run_trace(module, blade):
198 """Run network trace"""
199 trace_fact = {}
200 if module.params["source"] and module.params["component"]:
201 res = blade.get_network_interfaces_trace(
202 port=module.params["port"],
203 destination=module.params["destination"],
204 component_name=module.params["component"],
205 discover_mtu=module.params["discover_mtu"],
206 source=module.params["source"],
207 fragment_packet=module.params["fragment"],
208 method=module.params["method"],
209 resolve_hostname=module.params["resolve"],
210 )
211 elif module.params["source"] and not module.params["component"]:
212 res = blade.get_network_interfaces_trace(
213 port=module.params["port"],
214 destination=module.params["destination"],
215 discover_mtu=module.params["discover_mtu"],
216 source=module.params["source"],
217 fragment_packet=module.params["fragment"],
218 method=module.params["method"],
219 resolve_hostname=module.params["resolve"],
220 )
221 elif not module.params["source"] and module.params["component"]:
222 res = blade.get_network_interfaces_trace(
223 port=module.params["port"],
224 destination=module.params["destination"],
225 discover_mtu=module.params["discover_mtu"],
226 component_name=module.params["component"],
227 fragment_packet=module.params["fragment"],
228 method=module.params["method"],
229 resolve_hostname=module.params["resolve"],
230 )
231 else:
232 res = blade.get_network_interfaces_trace(
233 port=module.params["port"],
234 destination=module.params["destination"],
235 discover_mtu=module.params["discover_mtu"],
236 fragment_packet=module.params["fragment"],
237 method=module.params["method"],
238 resolve_hostname=module.params["resolve"],
239 )
240 if res.status_code != 200:
241 module.fail_json(
242 msg="Failed to run trace. Error: {0}".format(res.errors[0].message)
243 )
244 else:
245 responses = list(res.items)
246 for resp in range(len(responses)):
247 if hasattr(responses[resp], "details"):
248 comp_name = responses[resp].component_name.replace(".", "_")
249 trace_fact[comp_name] = {
250 "details": responses[resp].details,
251 }
252
253 module.exit_json(changed=False, tracefact=trace_fact)
254
255
256 def main():
257 argument_spec = purefb_argument_spec()
258 argument_spec.update(
259 dict(
260 action=dict(type="str", choices=["ping", "trace"], default="ping"),
261 method=dict(type="str", choices=["icmp", "tcp", "udp"], default="udp"),
262 destination=dict(type="str", required=True),
263 source=dict(type="str"),
264 component=dict(type="str"),
265 port=dict(type="str"),
266 count=dict(type="int", default=1),
267 packet_size=dict(type="int", default=56),
268 resolve=dict(type="bool", default=True),
269 fragment=dict(type="bool", default=True),
270 latency=dict(type="bool", default=False),
271 discover_mtu=dict(type="bool", default=False),
272 )
273 )
274
275 module = AnsibleModule(argument_spec, supports_check_mode=True)
276
277 blade = get_system(module)
278 api_version = list(blade.get_versions().items)
279
280 if MIN_REQUIRED_API_VERSION not in api_version:
281 module.fail_json(
282 msg="FlashBlade REST version not supported. "
283 "Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
284 )
285
286 blade = get_system(module)
287 if module.params["action"] == "ping":
288 run_ping(module, blade)
289 else:
290 run_trace(module, blade)
291
292 module.exit_json(changed=False)
293
294
295 if __name__ == "__main__":
296 main()