Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 1 | import json |
| 2 | import os |
| 3 | import sys |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 4 | import ipaddress |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 5 | |
| 6 | from copy import deepcopy |
| 7 | |
Alex Savatieiev | f526dc0 | 2019-03-06 10:11:32 -0600 | [diff] [blame] | 8 | from cfg_checker.reports import reporter |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 9 | from cfg_checker.common import utils, const |
| 10 | from cfg_checker.common import config, logger, logger_cli, pkg_dir |
| 11 | from cfg_checker.common import salt_utils |
| 12 | from cfg_checker.nodes import SaltNodes, node_tmpl |
| 13 | |
| 14 | |
| 15 | class NetworkChecker(SaltNodes): |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 16 | @staticmethod |
| 17 | def _map_network_for_host(host, if_class, net_list, data): |
Alex Savatieiev | 08428f2 | 2019-03-14 21:21:31 -0500 | [diff] [blame] | 18 | if if_class.network in net_list.keys(): |
| 19 | # There is a network |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 20 | net_list[if_class.network][host] = data |
| 21 | else: |
Alex Savatieiev | 08428f2 | 2019-03-14 21:21:31 -0500 | [diff] [blame] | 22 | # create subnet key |
| 23 | net_list[if_class.network] = {} |
| 24 | # add the host to the dict |
| 25 | net_list[if_class.network][host] = data |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 26 | |
| 27 | return net_list |
| 28 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 29 | def collect_network_info(self): |
| 30 | """ |
| 31 | Collects info on the network using ifs_data.py script |
| 32 | |
| 33 | :return: none |
| 34 | """ |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 35 | logger_cli.info("# Mapping node runtime network data") |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 36 | _result = self.execute_script_on_active_nodes("ifs_data.py", args=["json"]) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 37 | |
| 38 | for key in self.nodes.keys(): |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 39 | # check if we are to work with this node |
| 40 | if not self.is_node_available(key): |
| 41 | continue |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 42 | # due to much data to be passed from salt, it is happening in order |
| 43 | if key in _result: |
| 44 | _text = _result[key] |
| 45 | _dict = json.loads(_text[_text.find('{'):]) |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 46 | self.nodes[key]['routes'] = _dict.pop("routes") |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 47 | self.nodes[key]['networks'] = _dict |
| 48 | else: |
| 49 | self.nodes[key]['networks'] = {} |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 50 | self.nodes[key]['routes'] = {} |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 51 | logger_cli.debug("... {} has {} networks".format( |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 52 | key, |
| 53 | len(self.nodes[key]['networks'].keys()) |
| 54 | )) |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 55 | logger_cli.info("-> done collecting networks data") |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 56 | |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 57 | logger_cli.info("### Building network tree") |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 58 | # match interfaces by IP subnets |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 59 | _all_nets = {} |
Alex Savatieiev | a05921f | 2019-02-21 18:21:39 -0600 | [diff] [blame] | 60 | for host, node_data in self.nodes.iteritems(): |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 61 | if not self.is_node_available(host): |
| 62 | continue |
| 63 | |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 64 | for net_name, net_data in node_data['networks'].iteritems(): |
| 65 | # get ips and calculate subnets |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 66 | if net_name in ['lo']: |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 67 | # skip the localhost |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 68 | continue |
| 69 | _ip4s = net_data['ipv4'] |
| 70 | for _ip_str in _ip4s.keys(): |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 71 | # create interface class |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 72 | _if = ipaddress.IPv4Interface(_ip_str) |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 73 | net_data['name'] = net_name |
| 74 | net_data['if'] = _if |
| 75 | |
| 76 | _all_nets = self._map_network_for_host( |
| 77 | host, |
| 78 | _if, |
| 79 | _all_nets, |
| 80 | net_data |
| 81 | ) |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 82 | |
| 83 | # save collected info |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 84 | self.all_nets = _all_nets |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 85 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 86 | |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 87 | def collect_reclass_networks(self): |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 88 | logger_cli.info("# Mapping reclass networks") |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 89 | # Get networks from reclass and mark them |
| 90 | _reclass_nets = {} |
| 91 | # Get required pillars |
| 92 | self.get_specific_pillar_for_nodes("linux:network") |
| 93 | for node in self.nodes.keys(): |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 94 | # check if this node |
| 95 | if not self.is_node_available(node): |
| 96 | continue |
| 97 | # get the reclass value |
| 98 | _pillar = self.nodes[node]['pillars']['linux']['network'] |
Alex Savatieiev | a1f6f8c | 2019-03-18 17:13:55 -0500 | [diff] [blame] | 99 | # we should be ready if there is no interface in reclass for a node |
| 100 | # for example on APT node |
| 101 | if 'interface' in _pillar: |
| 102 | _pillar = _pillar['interface'] |
| 103 | else: |
Alex Savatieiev | 30d8c52 | 2019-03-18 17:27:21 -0500 | [diff] [blame] | 104 | logger_cli.info("...skipping node '{}', no IF section in reclass".format( |
Alex Savatieiev | a1f6f8c | 2019-03-18 17:13:55 -0500 | [diff] [blame] | 105 | node |
| 106 | )) |
| 107 | continue |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 108 | for _if_name, _if_data in _pillar.iteritems(): |
| 109 | if 'address' in _if_data: |
| 110 | _if = ipaddress.IPv4Interface( |
| 111 | _if_data['address'] + '/' + _if_data['netmask'] |
| 112 | ) |
| 113 | _if_data['name'] = _if_name |
| 114 | _if_data['if'] = _if |
| 115 | |
| 116 | _reclass_nets = self._map_network_for_host( |
| 117 | node, |
| 118 | _if, |
| 119 | _reclass_nets, |
| 120 | _if_data |
| 121 | ) |
| 122 | |
| 123 | self.reclass_nets = _reclass_nets |
| 124 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 125 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 126 | def print_network_report(self): |
| 127 | """ |
| 128 | Create text report for CLI |
| 129 | |
| 130 | :return: none |
| 131 | """ |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 132 | _all_nets = self.all_nets.keys() |
| 133 | logger_cli.info("# Reclass networks") |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 134 | logger_cli.info( |
| 135 | " {0:17} {1:25}: {2:19} {3:5}{4:10} {5}{6} {7} / {8} / {9}".format( |
| 136 | "Hostname", |
| 137 | "IF", |
| 138 | "IP", |
| 139 | "rtMTU", |
| 140 | "rcMTU", |
| 141 | "rtState", |
| 142 | "rcState", |
| 143 | "rtGate", |
| 144 | "rtDef.Gate", |
| 145 | "rcGate" |
| 146 | ) |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 147 | ) |
| 148 | |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 149 | _reclass = [n for n in _all_nets if n in self.reclass_nets] |
| 150 | for network in _reclass: |
| 151 | # shortcuts |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 152 | _net = str(network) |
| 153 | logger_cli.info("-> {}".format(_net)) |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 154 | names = sorted(self.all_nets[network].keys()) |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 155 | for hostname in names: |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 156 | if not self.is_node_available(hostname, log=False): |
| 157 | logger_cli.info( |
| 158 | " {0:17} {1}".format( |
| 159 | hostname.split('.')[0], |
| 160 | "... no data for the node" |
| 161 | ) |
| 162 | ) |
| 163 | |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 164 | # get the gateway for current net |
| 165 | _routes = self.nodes[hostname]['routes'] |
| 166 | _route = _routes[_net] if _net in _routes else None |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 167 | if not _route: |
| 168 | _gate = "no route!" |
| 169 | else: |
| 170 | _gate = _route['gateway'] if _route['gateway'] else "empty" |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 171 | |
| 172 | # get the default gateway |
| 173 | if 'default' in _routes: |
| 174 | _d_gate = ipaddress.IPv4Address( |
| 175 | _routes['default']['gateway'] |
| 176 | ) |
| 177 | else: |
| 178 | _d_gate = None |
| 179 | _d_gate_str = _d_gate if _d_gate else "No default gateway!" |
| 180 | |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 181 | _a = self.all_nets[network][hostname] |
Alex Savatieiev | 30d8c52 | 2019-03-18 17:27:21 -0500 | [diff] [blame] | 182 | # Check if reclass has such network |
| 183 | if hostname in self.reclass_nets[network]: |
| 184 | _r = self.reclass_nets[network][hostname] |
| 185 | else: |
| 186 | # Supply empty dict if there is no reclass gathered |
| 187 | _r = {} |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 188 | |
| 189 | # Take gateway parameter for this IF |
| 190 | # from corresponding reclass record |
Alex Savatieiev | ccebcbe | 2019-03-18 17:31:15 -0500 | [diff] [blame] | 191 | # TODO: Update gateway search mechanism |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 192 | if not self.is_node_available(hostname): |
| 193 | _r_gate = "-" |
Alex Savatieiev | 08428f2 | 2019-03-14 21:21:31 -0500 | [diff] [blame] | 194 | elif _a['if'].network not in self.reclass_nets: |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 195 | _r_gate = "no IF in reclass!" |
Alex Savatieiev | ccebcbe | 2019-03-18 17:31:15 -0500 | [diff] [blame] | 196 | elif not hostname in self.reclass_nets[_a['if'].network]: |
| 197 | _r_gate = "no IF on node in reclass!" |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 198 | else: |
Alex Savatieiev | 08428f2 | 2019-03-14 21:21:31 -0500 | [diff] [blame] | 199 | _rd = self.reclass_nets[_a['if'].network][hostname] |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 200 | _r_gate = _rd['gateway'] if 'gateway' in _rd else "empty" |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 201 | |
Alex Savatieiev | 30d8c52 | 2019-03-18 17:27:21 -0500 | [diff] [blame] | 202 | if not 'enabled' in _r: |
| 203 | _enabled = "no record!" |
| 204 | else: |
| 205 | _enabled = "(enabled)" if _r['enabled'] else "(disabled)" |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 206 | _text = "{0:25}: {1:19} {2:5}{3:10} {4:4}{5:10} {6} / {7} / {8}".format( |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 207 | _a['name'], |
| 208 | str(_a['if'].ip), |
| 209 | _a['mtu'], |
| 210 | '('+str(_r['mtu'])+')' if 'mtu' in _r else '(unset!)', |
| 211 | _a['state'], |
Alex Savatieiev | 30d8c52 | 2019-03-18 17:27:21 -0500 | [diff] [blame] | 212 | _enabled, |
Alex Savatieiev | d79dde1 | 2019-03-13 19:07:46 -0500 | [diff] [blame] | 213 | _gate, |
| 214 | _d_gate_str, |
| 215 | _r_gate |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 216 | ) |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 217 | logger_cli.info( |
Alex Savatieiev | 588b2c4 | 2019-03-11 10:39:21 -0500 | [diff] [blame] | 218 | " {0:17} {1}".format(hostname.split('.')[0], _text) |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 219 | ) |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 220 | |
| 221 | logger_cli.info("\n# Other networks") |
| 222 | _other = [n for n in _all_nets if n not in self.reclass_nets] |
| 223 | for network in _other: |
| 224 | logger_cli.info("-> {}".format(str(network))) |
| 225 | names = sorted(self.all_nets[network].keys()) |
| 226 | |
| 227 | for hostname in names: |
Alex Savatieiev | 588b2c4 | 2019-03-11 10:39:21 -0500 | [diff] [blame] | 228 | _text = "{0:25}: {1:19} {2:5} {3:4}".format( |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 229 | self.all_nets[network][hostname]['name'], |
| 230 | str(self.all_nets[network][hostname]['if'].ip), |
| 231 | self.all_nets[network][hostname]['mtu'], |
| 232 | self.all_nets[network][hostname]['state'] |
| 233 | ) |
| 234 | logger_cli.info( |
Alex Savatieiev | 588b2c4 | 2019-03-11 10:39:21 -0500 | [diff] [blame] | 235 | " {0:17} {1}".format(hostname.split('.')[0], _text) |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 236 | ) |
| 237 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 238 | |
| 239 | def create_html_report(self, filename): |
| 240 | """ |
| 241 | Create static html showing network schema-like report |
| 242 | |
| 243 | :return: none |
| 244 | """ |
| 245 | logger_cli.info("### Generating report to '{}'".format(filename)) |
| 246 | _report = reporter.ReportToFile( |
| 247 | reporter.HTMLNetworkReport(), |
| 248 | filename |
| 249 | ) |
| 250 | _report({ |
| 251 | "nodes": self.nodes, |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 252 | "network": {}, |
| 253 | "mcp_release": self.mcp_release, |
| 254 | "openstack_release": self.openstack_release |
| 255 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 256 | }) |
| 257 | logger_cli.info("-> Done") |