Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 1 | import ipaddress |
| 2 | import json |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 3 | from copy import deepcopy |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 4 | |
| 5 | from cfg_checker.common import logger_cli |
| 6 | from cfg_checker.common.exception import InvalidReturnException |
| 7 | from cfg_checker.modules.network.network_errors import NetworkErrors |
| 8 | from cfg_checker.nodes import salt_master |
| 9 | |
| 10 | # TODO: use templated approach |
| 11 | # net interface structure should be the same |
| 12 | _if_item = { |
| 13 | "name": "unnamed interface", |
| 14 | "mac": "", |
| 15 | "routes": {}, |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 16 | "proto": "", |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 17 | "ip": [], |
| 18 | "parameters": {} |
| 19 | } |
| 20 | |
| 21 | # collection of configurations |
| 22 | _network_item = { |
| 23 | "runtime": {}, |
| 24 | "config": {}, |
| 25 | "reclass": {} |
| 26 | } |
| 27 | |
| 28 | |
| 29 | class NetworkMapper(object): |
| 30 | RECLASS = "reclass" |
| 31 | CONFIG = "config" |
| 32 | RUNTIME = "runtime" |
| 33 | |
| 34 | def __init__(self, errors_class=None): |
| 35 | logger_cli.info("# Initializing mapper") |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 36 | # init networks and nodes |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 37 | self.networks = {} |
| 38 | self.nodes = salt_master.get_nodes() |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 39 | self.cluster = salt_master.get_info() |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 40 | # init and pre-populate interfaces |
| 41 | self.interfaces = {k: {} for k in self.nodes} |
| 42 | # Init errors class |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 43 | if errors_class: |
| 44 | self.errors = errors_class |
| 45 | else: |
| 46 | logger_cli.debug("... init error logs folder") |
| 47 | self.errors = NetworkErrors() |
| 48 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 49 | def prepare_all_maps(self): |
| 50 | self.map_network(self.RECLASS) |
| 51 | self.map_network(self.RUNTIME) |
| 52 | self.map_network(self.CONFIG) |
| 53 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 54 | # adding net data to tree |
| 55 | def _add_data(self, _list, _n, _h, _d): |
| 56 | if _n not in _list: |
| 57 | _list[_n] = {} |
| 58 | _list[_n][_h] = [_d] |
| 59 | elif _h not in _list[_n]: |
| 60 | # there is no such host, just create it |
| 61 | _list[_n][_h] = [_d] |
| 62 | else: |
| 63 | # there is such host... this is an error |
| 64 | self.errors.add_error( |
| 65 | self.errors.NET_DUPLICATE_IF, |
| 66 | host=_h, |
| 67 | dup_if=_d['name'] |
| 68 | ) |
| 69 | _list[_n][_h].append(_d) |
| 70 | |
| 71 | # TODO: refactor map creation. Build one map instead of two separate |
| 72 | def _map_network_for_host(self, host, if_class, net_list, data): |
| 73 | # filter networks for this IF IP |
| 74 | _nets = [n for n in net_list.keys() if if_class.ip in n] |
| 75 | _masks = [n.netmask for n in _nets] |
| 76 | if len(_nets) > 1: |
| 77 | # There a multiple network found for this IP, Error |
| 78 | self.errors.add_error( |
| 79 | self.errors.NET_SUBNET_INTERSECT, |
| 80 | host=host, |
| 81 | ip=str(if_class.exploded), |
| 82 | networks="; ".join([str(_n) for _n in _nets]) |
| 83 | ) |
| 84 | # check mask match |
| 85 | if len(_nets) > 0 and if_class.netmask not in _masks: |
| 86 | self.errors.add_error( |
| 87 | self.errors.NET_MASK_MISMATCH, |
| 88 | host=host, |
| 89 | if_name=data['name'], |
| 90 | if_cidr=if_class.exploded, |
| 91 | if_mapped_networks=", ".join([str(_n) for _n in _nets]) |
| 92 | ) |
| 93 | |
| 94 | if len(_nets) < 1: |
| 95 | self._add_data(net_list, if_class.network, host, data) |
| 96 | else: |
| 97 | # add all data |
| 98 | for net in _nets: |
| 99 | self._add_data(net_list, net, host, data) |
| 100 | |
| 101 | return net_list |
| 102 | |
| 103 | def _map_reclass_networks(self): |
| 104 | # class uses nodes from self.nodes dict |
| 105 | _reclass = {} |
| 106 | # Get required pillars |
| 107 | salt_master.get_specific_pillar_for_nodes("linux:network") |
| 108 | for node in salt_master.nodes.keys(): |
| 109 | # check if this node |
| 110 | if not salt_master.is_node_available(node): |
| 111 | continue |
| 112 | # get the reclass value |
| 113 | _pillar = salt_master.nodes[node]['pillars']['linux']['network'] |
| 114 | # we should be ready if there is no interface in reclass for a node |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 115 | # for example on APT node |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 116 | if 'interface' in _pillar: |
| 117 | _pillar = _pillar['interface'] |
| 118 | else: |
| 119 | logger_cli.info( |
| 120 | "... node '{}' skipped, no IF section in reclass".format( |
| 121 | node |
| 122 | ) |
| 123 | ) |
| 124 | continue |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 125 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 126 | # build map based on IPs and save info too |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 127 | for if_name, _dat in _pillar.iteritems(): |
| 128 | # get proper IF name |
| 129 | _if_name = if_name if 'name' not in _dat else _dat['name'] |
| 130 | # place it |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 131 | if _if_name not in self.interfaces[node]: |
| 132 | self.interfaces[node][_if_name] = deepcopy(_network_item) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 133 | self.interfaces[node][_if_name]['reclass'] = deepcopy(_dat) |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 134 | # map network if any |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 135 | if 'address' in _dat: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 136 | _if = ipaddress.IPv4Interface( |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 137 | _dat['address'] + '/' + _dat['netmask'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 138 | ) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 139 | _dat['name'] = _if_name |
| 140 | _dat['ifs'] = [_if] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 141 | |
| 142 | _reclass = self._map_network_for_host( |
| 143 | node, |
| 144 | _if, |
| 145 | _reclass, |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 146 | _dat |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 147 | ) |
| 148 | |
| 149 | return _reclass |
| 150 | |
| 151 | def _map_configured_networks(self): |
| 152 | # class uses nodes from self.nodes dict |
| 153 | _confs = {} |
| 154 | |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 155 | # TODO: parse /etc/network/interfaces |
| 156 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 157 | return _confs |
| 158 | |
| 159 | def _map_runtime_networks(self): |
| 160 | # class uses nodes from self.nodes dict |
| 161 | _runtime = {} |
| 162 | logger_cli.info("# Mapping node runtime network data") |
| 163 | salt_master.prepare_script_on_active_nodes("ifs_data.py") |
| 164 | _result = salt_master.execute_script_on_active_nodes( |
| 165 | "ifs_data.py", |
| 166 | args=["json"] |
| 167 | ) |
| 168 | for key in salt_master.nodes.keys(): |
| 169 | # check if we are to work with this node |
| 170 | if not salt_master.is_node_available(key): |
| 171 | continue |
| 172 | # due to much data to be passed from salt_master, |
| 173 | # it is happening in order |
| 174 | if key in _result: |
| 175 | _text = _result[key] |
| 176 | if '{' in _text and '}' in _text: |
| 177 | _text = _text[_text.find('{'):] |
| 178 | else: |
| 179 | raise InvalidReturnException( |
| 180 | "Non-json object returned: '{}'".format( |
| 181 | _text |
| 182 | ) |
| 183 | ) |
| 184 | _dict = json.loads(_text[_text.find('{'):]) |
| 185 | salt_master.nodes[key]['routes'] = _dict.pop("routes") |
| 186 | salt_master.nodes[key]['networks'] = _dict |
| 187 | else: |
| 188 | salt_master.nodes[key]['networks'] = {} |
| 189 | salt_master.nodes[key]['routes'] = {} |
| 190 | logger_cli.debug("... {} has {} networks".format( |
| 191 | key, |
| 192 | len(salt_master.nodes[key]['networks'].keys()) |
| 193 | )) |
| 194 | logger_cli.info("-> done collecting networks data") |
| 195 | |
| 196 | logger_cli.info("-> mapping IPs") |
| 197 | # match interfaces by IP subnets |
| 198 | for host, node_data in salt_master.nodes.iteritems(): |
| 199 | if not salt_master.is_node_available(host): |
| 200 | continue |
| 201 | |
| 202 | for net_name, net_data in node_data['networks'].iteritems(): |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 203 | # cut net name |
| 204 | _i = net_name.find('@') |
| 205 | _name = net_name if _i < 0 else net_name[:_i] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 206 | # get ips and calculate subnets |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 207 | if _name in ['lo']: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 208 | # skip the localhost |
| 209 | continue |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 210 | else: |
| 211 | # add collected data to interface storage |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 212 | if _name not in self.interfaces[host]: |
| 213 | self.interfaces[host][_name] = \ |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 214 | deepcopy(_network_item) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 215 | self.interfaces[host][_name]['runtime'] = \ |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 216 | deepcopy(net_data) |
| 217 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 218 | # get data and make sure that wide mask goes first |
| 219 | _ip4s = sorted( |
| 220 | net_data['ipv4'], |
| 221 | key=lambda s: s[s.index('/'):] |
| 222 | ) |
| 223 | for _ip_str in _ip4s: |
| 224 | # create interface class |
| 225 | _if = ipaddress.IPv4Interface(_ip_str) |
| 226 | # check if this is a VIP |
| 227 | # ...all those will have /32 mask |
| 228 | net_data['vip'] = None |
| 229 | if _if.network.prefixlen == 32: |
| 230 | net_data['vip'] = str(_if.exploded) |
| 231 | if 'name' not in net_data: |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 232 | net_data['name'] = _name |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 233 | if 'ifs' not in net_data: |
| 234 | net_data['ifs'] = [_if] |
| 235 | # map it |
| 236 | _runtime = self._map_network_for_host( |
| 237 | host, |
| 238 | _if, |
| 239 | _runtime, |
| 240 | net_data |
| 241 | ) |
| 242 | else: |
| 243 | # data is already there, just add VIP |
| 244 | net_data['ifs'].append(_if) |
| 245 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame^] | 246 | def process_interface(lvl, interface, tree, res): |
| 247 | # get childs for each root |
| 248 | # tree row item (<if_name>, [<parents>], [<childs>]) |
| 249 | if lvl not in tree: |
| 250 | # - no level - add it |
| 251 | tree[lvl] = {} |
| 252 | # there is such interface in this level? |
| 253 | if interface not in tree[lvl]: |
| 254 | # - IF not present |
| 255 | # -- get parents, add |
| 256 | _p = res[interface]['lower'] |
| 257 | # if None, put empty list |
| 258 | _p = _p if _p else [] |
| 259 | # -- get childs, add |
| 260 | _c = res[interface]['upper'] |
| 261 | # if None, put empty list |
| 262 | _c = _c if _c else [] |
| 263 | tree[lvl].update({ |
| 264 | interface: { |
| 265 | "parents": _p, |
| 266 | "children": _c, |
| 267 | "size": len(_p) if len(_p) > len(_c) else len(_c) |
| 268 | } |
| 269 | }) |
| 270 | for p_if in tree[lvl][interface]["parents"]: |
| 271 | # -- cycle: execute process for next parent, lvl-1 |
| 272 | process_interface(lvl-1, p_if, tree, res) |
| 273 | for c_if in tree[lvl][interface]["children"]: |
| 274 | # -- cycle: execute process for next child, lvl+1 |
| 275 | process_interface(lvl+1, c_if, tree, res) |
| 276 | else: |
| 277 | # - IF present - exit (been here already) |
| 278 | return |
| 279 | |
| 280 | def _put(cNet, cIndex, _list): |
| 281 | for _cI in range(cIndex, len(_list)): |
| 282 | # add child per index |
| 283 | # if space is free |
| 284 | if not _list[_cI]: |
| 285 | _list[_cI] = cNet |
| 286 | break |
| 287 | |
| 288 | # build network hierachy |
| 289 | nr = node_data['networks'] |
| 290 | # walk interface tree |
| 291 | for _ifname in node_data['networks']: |
| 292 | _tree = {} |
| 293 | _level = 0 |
| 294 | process_interface(_level, _ifname, _tree, nr) |
| 295 | # save tree for node/if |
| 296 | node_data['networks'][_ifname]['tree'] = _tree |
| 297 | |
| 298 | # debug, print built tree |
| 299 | # logger_cli.debug("# '{}'".format(_ifname)) |
| 300 | lvls = _tree.keys() |
| 301 | lvls.sort() |
| 302 | n = len(lvls) |
| 303 | m = max([len(_tree[k].keys()) for k in _tree.keys()]) |
| 304 | matrix = [["" for i in range(m)] for j in range(n)] |
| 305 | x = 0 |
| 306 | while True: |
| 307 | _lv = lvls.pop(0) |
| 308 | # get all interfaces on this level |
| 309 | nets = _tree[_lv].keys() |
| 310 | while True: |
| 311 | y = 0 |
| 312 | # get next interface |
| 313 | _net = nets.pop(0) |
| 314 | # all nets |
| 315 | _a = [_net] |
| 316 | # put current interface if this is only one left |
| 317 | if not _tree[_lv][_net]['children']: |
| 318 | if _net not in matrix[x]: |
| 319 | _put(_net, y, matrix[x]) |
| 320 | y += 1 |
| 321 | else: |
| 322 | # get all nets with same child |
| 323 | for _c in _tree[_lv][_net]['children']: |
| 324 | for _o_net in nets: |
| 325 | if _c in _tree[_lv][_o_net]['children']: |
| 326 | _a.append(_o_net) |
| 327 | # flush collected nets |
| 328 | for idx in range(len(_a)): |
| 329 | if _a[idx] in matrix[x]: |
| 330 | # there is such interface on this level |
| 331 | # get index |
| 332 | _nI = matrix[x].index(_a[idx]) |
| 333 | _put(_c, _nI, matrix[x+1]) |
| 334 | else: |
| 335 | # there is no such interface |
| 336 | # add it |
| 337 | for _nI in range(len(matrix[x])): |
| 338 | if not matrix[x][_nI]: |
| 339 | matrix[x][_nI] = _a[idx] |
| 340 | # also, put child |
| 341 | _put(_c, _nI, matrix[x+1]) |
| 342 | break |
| 343 | # remove collected nets from processing |
| 344 | if _a[idx] in nets: |
| 345 | nets.remove(_a[idx]) |
| 346 | y += len(_a) |
| 347 | if not nets: |
| 348 | x += 1 |
| 349 | break |
| 350 | if not lvls: |
| 351 | break |
| 352 | |
| 353 | lines = [] |
| 354 | _columns = [len(max([i for i in li])) for li in matrix] |
| 355 | for idx_y in range(m): |
| 356 | line = "" |
| 357 | for idx_x in range(n): |
| 358 | _fmt = "{" + ":{}".format(_columns[idx_x]) + "} " |
| 359 | line += _fmt.format(matrix[idx_x][idx_y]) |
| 360 | lines.append(line) |
| 361 | node_data['networks'][_ifname]['matrix'] = matrix |
| 362 | node_data['networks'][_ifname]['lines'] = lines |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 363 | return _runtime |
| 364 | |
| 365 | def map_network(self, source): |
| 366 | # maps target network using given source |
| 367 | _networks = None |
| 368 | |
| 369 | if source == self.RECLASS: |
| 370 | _networks = self._map_reclass_networks() |
| 371 | elif source == self.CONFIG: |
| 372 | _networks = self._map_configured_networks() |
| 373 | elif source == self.RUNTIME: |
| 374 | _networks = self._map_runtime_networks() |
| 375 | |
| 376 | self.networks[source] = _networks |
| 377 | return _networks |
| 378 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 379 | def create_map(self): |
| 380 | """Create all needed elements for map output |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 381 | |
| 382 | :return: none |
| 383 | """ |
| 384 | _runtime = self.networks[self.RUNTIME] |
| 385 | _reclass = self.networks[self.RECLASS] |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 386 | |
| 387 | # main networks, target vars |
| 388 | _map = {} |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 389 | # No matter of proto, at least one IP will be present for the network |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 390 | # we interested in, since we are to make sure that L3 level |
| 391 | # is configured according to reclass model |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 392 | for network in _reclass: |
| 393 | # shortcuts |
| 394 | _net = str(network) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 395 | _map[_net] = {} |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 396 | if network not in _runtime: |
| 397 | # reclass has network that not found in runtime |
| 398 | self.errors.add_error( |
| 399 | self.errors.NET_NO_RUNTIME_NETWORK, |
| 400 | reclass_net=str(network) |
| 401 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame^] | 402 | logger_cli.warn( |
| 403 | "WARN: {}: {}".format( |
| 404 | " No runtime network ", str(network) |
| 405 | ) |
| 406 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 407 | continue |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 408 | # hostnames |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 409 | names = sorted(_runtime[network].keys()) |
| 410 | for hostname in names: |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 411 | _notes = [] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 412 | node = hostname.split('.')[0] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 413 | if not salt_master.is_node_available(hostname, log=False): |
| 414 | logger_cli.info( |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 415 | " {0:8} {1}".format(node, "node not available") |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 416 | ) |
| 417 | # add non-responsive node erorr |
| 418 | self.errors.add_error( |
| 419 | self.errors.NET_NODE_NON_RESPONSIVE, |
| 420 | host=hostname |
| 421 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 422 | _notes.append( |
| 423 | self.errors.get_error_type_text( |
| 424 | self.errors.NET_NODE_NON_RESPONSIVE |
| 425 | ) |
| 426 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 427 | continue |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 428 | # lookup interface name on node using network CIDR |
| 429 | _if_name = _runtime[network][hostname][0]["name"] |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 430 | _raw = self.interfaces[hostname][_if_name]['runtime'] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 431 | # get proper reclass |
| 432 | _r = self.interfaces[hostname][_if_name]['reclass'] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 433 | _if_name_suffix = "" |
| 434 | # get the proto value |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 435 | if _r: |
| 436 | _if_rc = "" |
| 437 | else: |
| 438 | self.errors.add_error( |
| 439 | self.errors.NET_NODE_UNEXPECTED_IF, |
| 440 | host=hostname, |
| 441 | if_name=_if_name |
| 442 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 443 | _notes.append( |
| 444 | self.errors.get_error_type_text( |
| 445 | self.errors.NET_NODE_UNEXPECTED_IF |
| 446 | ) |
| 447 | ) |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 448 | _if_rc = "*" |
| 449 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 450 | if "proto" in _r: |
| 451 | _proto = _r['proto'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 452 | else: |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 453 | _proto = "-" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 454 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 455 | if "type" in _r: |
| 456 | _if_name_suffix += _r["type"] |
| 457 | if "use_interfaces" in _r: |
| 458 | _if_name_suffix += "->" + ",".join(_r["use_interfaces"]) |
| 459 | |
| 460 | if _if_name_suffix: |
| 461 | _if_name_suffix = "({})".format(_if_name_suffix) |
| 462 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 463 | # get gate and routes if proto is static |
| 464 | if _proto == 'static': |
| 465 | # get the gateway for current net |
| 466 | _routes = salt_master.nodes[hostname]['routes'] |
| 467 | _route = _routes[_net] if _net in _routes else None |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 468 | # get the default gateway |
| 469 | if 'default' in _routes: |
| 470 | _d_gate = ipaddress.IPv4Address( |
| 471 | _routes['default']['gateway'] |
| 472 | ) |
| 473 | else: |
| 474 | _d_gate = None |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 475 | _d_gate_str = str(_d_gate) if _d_gate else "No default!" |
| 476 | # match route with default |
| 477 | if not _route: |
| 478 | _gate = "?" |
| 479 | else: |
| 480 | _gate = _route['gateway'] if _route['gateway'] else "-" |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 481 | else: |
| 482 | # in case of manual and dhcp, no check possible |
| 483 | _gate = "-" |
| 484 | _d_gate = "-" |
Alex | 4067f00 | 2019-06-11 10:47:16 -0500 | [diff] [blame] | 485 | _d_gate_str = "-" |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 486 | # iterate through interfaces |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 487 | _a = _runtime[network][hostname] |
| 488 | for _host in _a: |
| 489 | for _if in _host['ifs']: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 490 | _ip_str = str(_if.exploded) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 491 | _gate_error = "" |
| 492 | _up_error = "" |
| 493 | _mtu_error = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 494 | |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 495 | # Match gateway |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 496 | if _proto == 'static': |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 497 | # default reclass gate |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 498 | _r_gate = "-" |
| 499 | if "gateway" in _r: |
| 500 | _r_gate = _r["gateway"] |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 501 | |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 502 | # if values not match, put * |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 503 | if _gate != _r_gate and _d_gate_str != _r_gate: |
| 504 | # if values not match, check if default match |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 505 | self.errors.add_error( |
| 506 | self.errors.NET_UNEXPECTED_GATEWAY, |
| 507 | host=hostname, |
| 508 | if_name=_if_name, |
| 509 | ip=_ip_str, |
| 510 | gateway=_gate |
| 511 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 512 | _notes.append( |
| 513 | self.errors.get_error_type_text( |
| 514 | self.errors.NET_UNEXPECTED_GATEWAY |
| 515 | ) |
| 516 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 517 | _gate_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 518 | |
| 519 | # IF status in reclass |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 520 | _e = "enabled" |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 521 | if _e not in _r: |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 522 | self.errors.add_error( |
| 523 | self.errors.NET_NO_RC_IF_STATUS, |
| 524 | host=hostname, |
| 525 | if_name=_if_name |
| 526 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 527 | _notes.append( |
| 528 | self.errors.get_error_type_text( |
| 529 | self.errors.NET_NO_RC_IF_STATUS |
| 530 | ) |
| 531 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 532 | _up_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 533 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 534 | _rc_mtu = _r['mtu'] if 'mtu' in _r else None |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 535 | _rc_mtu_s = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 536 | # check if this is a VIP address |
| 537 | # no checks needed if yes. |
| 538 | if _host['vip'] != _ip_str: |
| 539 | if _rc_mtu: |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 540 | _rc_mtu_s = str(_rc_mtu) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 541 | # if there is an MTU value, match it |
| 542 | if _host['mtu'] != _rc_mtu_s: |
| 543 | self.errors.add_error( |
| 544 | self.errors.NET_MTU_MISMATCH, |
| 545 | host=hostname, |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 546 | if_name=_if_name, |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 547 | if_cidr=_ip_str, |
| 548 | reclass_mtu=_rc_mtu, |
| 549 | runtime_mtu=_host['mtu'] |
| 550 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 551 | _notes.append( |
| 552 | self.errors.get_error_type_text( |
| 553 | self.errors.NET_MTU_MISMATCH |
| 554 | ) |
| 555 | ) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 556 | _rc_mtu_s = "/" + _rc_mtu_s |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 557 | _mtu_error = "*" |
| 558 | else: |
| 559 | # empty the matched value |
| 560 | _rc_mtu_s = "" |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 561 | elif _host['mtu'] != '1500' and \ |
| 562 | _proto not in ["-", "dhcp"]: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 563 | # there is no MTU value in reclass |
| 564 | # and runtime value is not default |
| 565 | self.errors.add_error( |
| 566 | self.errors.NET_MTU_EMPTY, |
| 567 | host=hostname, |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 568 | if_name=_if_name, |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 569 | if_cidr=_ip_str, |
| 570 | if_mtu=_host['mtu'] |
| 571 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 572 | _notes.append( |
| 573 | self.errors.get_error_type_text( |
| 574 | self.errors.NET_MTU_EMPTY |
| 575 | ) |
| 576 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 577 | _mtu_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 578 | else: |
| 579 | # this is a VIP |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 580 | _if_name = " "*7 |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 581 | _if_name_suffix = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 582 | _ip_str += " VIP" |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 583 | # Save all data |
| 584 | _values = { |
| 585 | "interface": _if_name, |
| 586 | "interface_error": _if_rc, |
| 587 | "interface_note": _if_name_suffix, |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame^] | 588 | "interface_map": "\n".join(_host['lines']), |
| 589 | "interface_matrix": _host['matrix'], |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 590 | "ip_address": _ip_str, |
| 591 | "address_type": _proto, |
| 592 | "rt_mtu": _host['mtu'], |
| 593 | "rc_mtu": _rc_mtu_s, |
| 594 | "mtu_error": _mtu_error, |
| 595 | "status": _host['state'], |
| 596 | "status_error": _up_error, |
| 597 | "subnet_gateway": _gate, |
| 598 | "subnet_gateway_error": _gate_error, |
| 599 | "default_gateway": _d_gate_str, |
| 600 | "raw_data": _raw, |
| 601 | "error_note": " and ".join(_notes) |
| 602 | } |
| 603 | if node in _map[_net]: |
| 604 | # add if to host |
| 605 | _map[_net][node].append(_values) |
| 606 | else: |
| 607 | _map[_net][node] = [_values] |
| 608 | _notes = [] |
| 609 | |
| 610 | # save map |
| 611 | self.map = _map |
| 612 | # other runtime networks found |
| 613 | # docker, etc |
| 614 | |
| 615 | return |
| 616 | |
| 617 | def print_map(self): |
| 618 | """ |
| 619 | Create text report for CLI |
| 620 | |
| 621 | :return: none |
| 622 | """ |
| 623 | logger_cli.info("# Networks") |
| 624 | logger_cli.info( |
| 625 | " {0:8} {1:25} {2:25} {3:6} {4:10} {5:10} {6}/{7}".format( |
| 626 | "Host", |
| 627 | "IF", |
| 628 | "IP", |
| 629 | "Proto", |
| 630 | "MTU", |
| 631 | "State", |
| 632 | "Gate", |
| 633 | "Def.Gate" |
| 634 | ) |
| 635 | ) |
| 636 | for network in self.map.keys(): |
| 637 | logger_cli.info("-> {}".format(network)) |
| 638 | for hostname in self.map[network].keys(): |
| 639 | node = hostname.split('.')[0] |
| 640 | _n = self.map[network][hostname] |
| 641 | for _i in _n: |
| 642 | # Host IF IP Proto MTU State Gate Def.Gate |
| 643 | _text = "{:7} {:17} {:25} {:6} {:10} " \ |
| 644 | "{:10} {} / {}".format( |
| 645 | _i['interface'] + _i['interface_error'], |
| 646 | _i['interface_note'], |
| 647 | _i['ip_address'], |
| 648 | _i['address_type'], |
| 649 | _i['rt_mtu'] + _i['rc_mtu'] + _i['mtu_error'], |
| 650 | _i['status'] + _i['status_error'], |
| 651 | _i['subnet_gateway'] + |
| 652 | _i['subnet_gateway_error'], |
| 653 | _i['default_gateway'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 654 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 655 | logger_cli.info( |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 656 | " {0:8} {1}".format( |
| 657 | node, |
| 658 | _text |
| 659 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 660 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 661 | |
| 662 | # logger_cli.info("\n# Other networks") |
| 663 | # _other = [n for n in _runtime if n not in _reclass] |
| 664 | # for network in _other: |
| 665 | # logger_cli.info("-> {}".format(str(network))) |
| 666 | # names = sorted(_runtime[network].keys()) |
| 667 | |
| 668 | # for hostname in names: |
| 669 | # for _n in _runtime[network][hostname]: |
| 670 | # _ifs = [str(ifs.ip) for ifs in _n['ifs']] |
| 671 | # _text = "{:25} {:25} {:6} {:10} {}".format( |
| 672 | # _n['name'], |
| 673 | # ", ".join(_ifs), |
| 674 | # "-", |
| 675 | # _n['mtu'], |
| 676 | # _n['state'] |
| 677 | # ) |
| 678 | # logger_cli.info( |
| 679 | # " {0:8} {1}".format(hostname.split('.')[0], _text) |
| 680 | # ) |
| 681 | # logger_cli.info("\n") |