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