Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 1 | import ipaddress |
| 2 | import json |
| 3 | |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 4 | from cfg_checker.common import logger_cli |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 5 | from cfg_checker.helpers.console_utils import Progress |
| 6 | from cfg_checker.modules.network.mapper import NetworkMapper |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 7 | from cfg_checker.modules.network.network_errors import NetworkErrors |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 8 | from cfg_checker.nodes import salt_master |
| 9 | |
| 10 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 11 | # This is independent class with a salt.nodes input |
| 12 | class NetworkPinger(object): |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 13 | def __init__( |
| 14 | self, |
| 15 | mtu=None, |
| 16 | detailed=False, |
| 17 | errors_class=None, |
| 18 | skip_list=None, |
| 19 | skip_list_file=None |
| 20 | ): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 21 | logger_cli.info("# Initializing") |
| 22 | # all active nodes in the cloud |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 23 | self.target_nodes = salt_master.get_nodes( |
| 24 | skip_list=skip_list, |
| 25 | skip_list_file=skip_list_file |
| 26 | ) |
| 27 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 28 | # default MTU value |
| 29 | self.target_mtu = mtu if mtu else 64 |
| 30 | # only data |
| 31 | self.packet_size = int(self.target_mtu) - 20 - 8 |
| 32 | self.detailed_summary = detailed |
| 33 | |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 34 | if errors_class: |
| 35 | self.errors = errors_class |
| 36 | else: |
| 37 | logger_cli.debug("... init error logs folder") |
| 38 | self.errors = NetworkErrors() |
| 39 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 40 | def _collect_node_addresses(self, target_net): |
| 41 | # use reclass model and standard methods |
| 42 | # to create list of nodes with target network |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 43 | _mapper = NetworkMapper(errors_class=self.errors) |
| 44 | _reclass = _mapper.map_network(_mapper.RUNTIME) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 45 | if target_net in _reclass: |
| 46 | return _reclass[target_net] |
| 47 | else: |
| 48 | logger_cli.info( |
| 49 | "# Target network of {} not found in reclass".format( |
| 50 | target_net.exploded |
| 51 | ) |
| 52 | ) |
| 53 | return None |
| 54 | |
| 55 | def ping_nodes(self, network_cidr_str): |
| 56 | # Conduct actual ping using network CIDR |
| 57 | logger_cli.info("# Collecting node pairs") |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 58 | _fake_if = ipaddress.IPv4Interface(str(network_cidr_str)) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 59 | _net = _fake_if.network |
| 60 | # collect nodes and ips from reclass |
| 61 | nodes = self._collect_node_addresses(_net) |
| 62 | # build list of packets to be sent |
| 63 | # source -> target |
| 64 | _count = 0 |
| 65 | _packets = {} |
| 66 | _nodes = sorted(nodes.keys()) |
| 67 | _nodes_total = len(_nodes) |
| 68 | logger_cli.info("-> {} nodes found within subnet of '{}'".format( |
| 69 | _nodes_total, |
| 70 | network_cidr_str |
| 71 | )) |
| 72 | while len(_nodes) > 0: |
| 73 | src_host = _nodes.pop() |
| 74 | src_data = nodes[src_host] |
| 75 | src_if_name = src_data[0]['name'] |
| 76 | src_ips = [str(_if.ip) for _if in src_data[0]['ifs']] |
| 77 | _packets[src_host] = { |
| 78 | "ip": src_ips[0], |
| 79 | "if_name": src_if_name, |
| 80 | "targets": {} |
| 81 | } |
| 82 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 83 | for tgt_host, tgt_data in nodes.items(): |
Alex | e9547d8 | 2019-06-03 15:22:50 -0500 | [diff] [blame] | 84 | _t = _packets[src_host]["targets"] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 85 | for tgt_if in tgt_data: |
| 86 | tgt_if_name = tgt_if['name'] |
| 87 | _ip_index = 0 |
| 88 | for tgt_ip in tgt_if['ifs']: |
| 89 | _ip = str(tgt_ip.ip) |
| 90 | if _ip not in src_ips: |
Alex | e9547d8 | 2019-06-03 15:22:50 -0500 | [diff] [blame] | 91 | if tgt_host not in _t: |
| 92 | _t[tgt_host] = [] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 93 | _tgt = { |
| 94 | "ip": _ip, |
| 95 | "tgt_host": tgt_host, |
| 96 | "ip_index": _ip_index, |
| 97 | "if_name": tgt_if_name, |
| 98 | "mtu": self.target_mtu, |
| 99 | "size": self.packet_size |
| 100 | } |
Alex | e9547d8 | 2019-06-03 15:22:50 -0500 | [diff] [blame] | 101 | _t[tgt_host].append( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 102 | _tgt |
| 103 | ) |
| 104 | _count += 1 |
| 105 | _ip_index += 1 |
| 106 | else: |
| 107 | pass |
| 108 | logger_cli.info("-> {} packets to send".format(_count)) |
| 109 | |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 110 | if not _count: |
| 111 | logger_cli.warning( |
| 112 | "\n# WARNING: No packets to send for '{}', " |
| 113 | "check network configuration\n".format(network_cidr_str) |
| 114 | ) |
| 115 | |
| 116 | return -1 |
| 117 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 118 | # do ping of packets |
| 119 | logger_cli.info("# Pinging nodes: MTU={}".format(self.target_mtu)) |
| 120 | salt_master.prepare_script_on_active_nodes("ping.py") |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 121 | _progress = Progress(_count) |
| 122 | _progress_index = 0 |
| 123 | _node_index = 0 |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 124 | for src, src_data in _packets.items(): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 125 | _targets = src_data["targets"] |
| 126 | _node_index += 1 |
| 127 | # create 'targets.json' on source host |
| 128 | _path = salt_master.prepare_json_on_node( |
| 129 | src, |
| 130 | _targets, |
| 131 | "targets.json" |
| 132 | ) |
| 133 | # execute ping.py |
| 134 | _results = salt_master.execute_script_on_node( |
| 135 | src, |
| 136 | "ping.py", |
| 137 | args=[_path] |
| 138 | ) |
| 139 | _progress_index += len(_targets) |
| 140 | # print progress |
| 141 | _progress.write_progress( |
| 142 | _progress_index, |
| 143 | note='/ {}/{} nodes / current {}'.format( |
| 144 | _node_index, |
| 145 | _nodes_total, |
| 146 | src |
| 147 | ) |
| 148 | ) |
| 149 | # Parse salt output |
| 150 | _result = _results[src] |
Alex | e9547d8 | 2019-06-03 15:22:50 -0500 | [diff] [blame] | 151 | try: |
| 152 | _result = json.loads(_result) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 153 | except (ValueError, TypeError): |
Alex | e9547d8 | 2019-06-03 15:22:50 -0500 | [diff] [blame] | 154 | _progress.clearline() |
| 155 | logger_cli.error( |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 156 | "# ERROR: Unexpected salt return for '{}': '{}'\n".format( |
| 157 | src, |
| 158 | _result |
| 159 | ) |
| 160 | ) |
| 161 | self.errors.add_error( |
| 162 | self.errors.NET_NODE_NON_RESPONSIVE, |
| 163 | node=src, |
| 164 | response=_result |
Alex | e9547d8 | 2019-06-03 15:22:50 -0500 | [diff] [blame] | 165 | ) |
| 166 | continue |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 167 | # Handle return codes |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 168 | for tgt_node, _tgt_ips in _result.items(): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 169 | for _params in _tgt_ips: |
| 170 | _body = "{}({}) --{}--> {}({}@{})\n".format( |
| 171 | src, |
| 172 | src_data["if_name"], |
| 173 | _params["returncode"], |
| 174 | tgt_node, |
| 175 | _params["if_name"], |
| 176 | _params["ip"] |
| 177 | ) |
| 178 | _stdout = "" |
| 179 | _stderr = "" |
| 180 | if len(_params["stdout"]) > 0: |
| 181 | _stdout = "stdout:\n{}\n".format(_params["stdout"]) |
| 182 | if len(_params["stderr"]) > 0: |
| 183 | _stderr = "stderr:\n{}\n".format(_params["stderr"]) |
| 184 | |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 185 | if not _params["returncode"]: |
| 186 | # 0 |
| 187 | self.errors.add_error( |
| 188 | self.errors.NET_PING_SUCCESS, |
| 189 | ping_path=_body, |
| 190 | stdout=_stdout, |
| 191 | stderr=_stderr |
| 192 | ) |
| 193 | elif _params["returncode"] == 68: |
| 194 | # 68 is a 'can't resove host error' |
| 195 | self.errors.add_error( |
| 196 | self.errors.NET_PING_NOT_RESOLVED, |
| 197 | ping_path=_body, |
| 198 | stdout=_stdout, |
| 199 | stderr=_stderr |
| 200 | ) |
| 201 | elif _params["returncode"] > 1: |
| 202 | # >1 is when no actial (any) response |
| 203 | self.errors.add_error( |
| 204 | self.errors.NET_PING_ERROR, |
| 205 | ping_path=_body, |
| 206 | stdout=_stdout, |
| 207 | stderr=_stderr |
| 208 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 209 | else: |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 210 | # 1 is for timeouts amd/or packet lost |
| 211 | self.errors.add_error( |
| 212 | self.errors.NET_PING_TIMEOUT, |
| 213 | ping_path=_body, |
| 214 | stdout=_stdout, |
| 215 | stderr=_stderr |
| 216 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 217 | |
| 218 | # Parse results back in place |
| 219 | src_data["targets"] = _result |
| 220 | |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 221 | _progress.end() |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 222 | |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 223 | return 0 |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 224 | |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 225 | def print_summary(self): |
| 226 | logger_cli.info(self.errors.get_summary(print_zeros=False)) |
| 227 | |
| 228 | def print_details(self): |
| 229 | # Detailed errors |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 230 | logger_cli.info( |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 231 | "\n{}\n".format( |
| 232 | self.errors.get_errors() |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 233 | ) |
| 234 | ) |