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 |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 255 | _n = '' |
| 256 | if interface not in res: |
| 257 | _n = 'unknown IF' |
| 258 | _p = None |
| 259 | _c = None |
| 260 | else: |
| 261 | # -- get parents, add |
| 262 | _p = res[interface]['lower'] |
| 263 | # -- get childs, add |
| 264 | _c = res[interface]['upper'] |
| 265 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 266 | # if None, put empty list |
| 267 | _p = _p if _p else [] |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 268 | # if None, put empty list |
| 269 | _c = _c if _c else [] |
| 270 | tree[lvl].update({ |
| 271 | interface: { |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 272 | "note": _n, |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 273 | "parents": _p, |
| 274 | "children": _c, |
| 275 | "size": len(_p) if len(_p) > len(_c) else len(_c) |
| 276 | } |
| 277 | }) |
| 278 | for p_if in tree[lvl][interface]["parents"]: |
| 279 | # -- cycle: execute process for next parent, lvl-1 |
| 280 | process_interface(lvl-1, p_if, tree, res) |
| 281 | for c_if in tree[lvl][interface]["children"]: |
| 282 | # -- cycle: execute process for next child, lvl+1 |
| 283 | process_interface(lvl+1, c_if, tree, res) |
| 284 | else: |
| 285 | # - IF present - exit (been here already) |
| 286 | return |
| 287 | |
| 288 | def _put(cNet, cIndex, _list): |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 289 | _added = False |
| 290 | _actual_index = -1 |
| 291 | # Check list len |
| 292 | _len = len(_list) |
| 293 | if cIndex >= _len: |
| 294 | # grow list to meet index |
| 295 | _list = _list + [''] * (cIndex - _len + 1) |
| 296 | _len = len(_list) |
| 297 | |
| 298 | for _cI in range(cIndex, _len): |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 299 | # add child per index |
| 300 | # if space is free |
| 301 | if not _list[_cI]: |
| 302 | _list[_cI] = cNet |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 303 | _added = True |
| 304 | _actual_index = _cI |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 305 | break |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 306 | if not _added: |
| 307 | # grow list by one entry |
| 308 | _list = _list + [cNet] |
| 309 | _actual_index = len(_list) - 1 |
| 310 | return _actual_index, _list |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 311 | |
| 312 | # build network hierachy |
| 313 | nr = node_data['networks'] |
| 314 | # walk interface tree |
| 315 | for _ifname in node_data['networks']: |
| 316 | _tree = {} |
| 317 | _level = 0 |
| 318 | process_interface(_level, _ifname, _tree, nr) |
| 319 | # save tree for node/if |
| 320 | node_data['networks'][_ifname]['tree'] = _tree |
| 321 | |
| 322 | # debug, print built tree |
| 323 | # logger_cli.debug("# '{}'".format(_ifname)) |
| 324 | lvls = _tree.keys() |
| 325 | lvls.sort() |
| 326 | n = len(lvls) |
| 327 | m = max([len(_tree[k].keys()) for k in _tree.keys()]) |
| 328 | matrix = [["" for i in range(m)] for j in range(n)] |
| 329 | x = 0 |
| 330 | while True: |
| 331 | _lv = lvls.pop(0) |
| 332 | # get all interfaces on this level |
| 333 | nets = _tree[_lv].keys() |
| 334 | while True: |
| 335 | y = 0 |
| 336 | # get next interface |
| 337 | _net = nets.pop(0) |
| 338 | # all nets |
| 339 | _a = [_net] |
| 340 | # put current interface if this is only one left |
| 341 | if not _tree[_lv][_net]['children']: |
| 342 | if _net not in matrix[x]: |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 343 | _, matrix[x] = _put( |
| 344 | _net, |
| 345 | y, |
| 346 | matrix[x] |
| 347 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 348 | y += 1 |
| 349 | else: |
| 350 | # get all nets with same child |
| 351 | for _c in _tree[_lv][_net]['children']: |
| 352 | for _o_net in nets: |
| 353 | if _c in _tree[_lv][_o_net]['children']: |
| 354 | _a.append(_o_net) |
| 355 | # flush collected nets |
| 356 | for idx in range(len(_a)): |
| 357 | if _a[idx] in matrix[x]: |
| 358 | # there is such interface on this level |
| 359 | # get index |
| 360 | _nI = matrix[x].index(_a[idx]) |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 361 | _, matrix[x+1] = _put( |
| 362 | _c, |
| 363 | _nI, |
| 364 | matrix[x+1] |
| 365 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 366 | else: |
| 367 | # there is no such interface |
| 368 | # add it |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 369 | _t, matrix[x] = _put( |
| 370 | _a[idx], |
| 371 | 0, |
| 372 | matrix[x] |
| 373 | ) |
| 374 | # also, put child |
| 375 | _, matrix[x+1] = _put( |
| 376 | _c, |
| 377 | _t, |
| 378 | matrix[x+1] |
| 379 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 380 | # remove collected nets from processing |
| 381 | if _a[idx] in nets: |
| 382 | nets.remove(_a[idx]) |
| 383 | y += len(_a) |
| 384 | if not nets: |
| 385 | x += 1 |
| 386 | break |
| 387 | if not lvls: |
| 388 | break |
| 389 | |
| 390 | lines = [] |
| 391 | _columns = [len(max([i for i in li])) for li in matrix] |
| 392 | for idx_y in range(m): |
| 393 | line = "" |
| 394 | for idx_x in range(n): |
| 395 | _fmt = "{" + ":{}".format(_columns[idx_x]) + "} " |
| 396 | line += _fmt.format(matrix[idx_x][idx_y]) |
| 397 | lines.append(line) |
| 398 | node_data['networks'][_ifname]['matrix'] = matrix |
| 399 | node_data['networks'][_ifname]['lines'] = lines |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 400 | return _runtime |
| 401 | |
| 402 | def map_network(self, source): |
| 403 | # maps target network using given source |
| 404 | _networks = None |
| 405 | |
| 406 | if source == self.RECLASS: |
| 407 | _networks = self._map_reclass_networks() |
| 408 | elif source == self.CONFIG: |
| 409 | _networks = self._map_configured_networks() |
| 410 | elif source == self.RUNTIME: |
| 411 | _networks = self._map_runtime_networks() |
| 412 | |
| 413 | self.networks[source] = _networks |
| 414 | return _networks |
| 415 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 416 | def create_map(self): |
| 417 | """Create all needed elements for map output |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 418 | |
| 419 | :return: none |
| 420 | """ |
| 421 | _runtime = self.networks[self.RUNTIME] |
| 422 | _reclass = self.networks[self.RECLASS] |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 423 | |
| 424 | # main networks, target vars |
| 425 | _map = {} |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 426 | # 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] | 427 | # we interested in, since we are to make sure that L3 level |
| 428 | # is configured according to reclass model |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 429 | for network in _reclass: |
| 430 | # shortcuts |
| 431 | _net = str(network) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 432 | _map[_net] = {} |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 433 | if network not in _runtime: |
| 434 | # reclass has network that not found in runtime |
| 435 | self.errors.add_error( |
| 436 | self.errors.NET_NO_RUNTIME_NETWORK, |
| 437 | reclass_net=str(network) |
| 438 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 439 | logger_cli.warn( |
| 440 | "WARN: {}: {}".format( |
| 441 | " No runtime network ", str(network) |
| 442 | ) |
| 443 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 444 | continue |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 445 | # hostnames |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 446 | names = sorted(_runtime[network].keys()) |
| 447 | for hostname in names: |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 448 | _notes = [] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 449 | node = hostname.split('.')[0] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 450 | if not salt_master.is_node_available(hostname, log=False): |
| 451 | logger_cli.info( |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 452 | " {0:8} {1}".format(node, "node not available") |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 453 | ) |
| 454 | # add non-responsive node erorr |
| 455 | self.errors.add_error( |
| 456 | self.errors.NET_NODE_NON_RESPONSIVE, |
| 457 | host=hostname |
| 458 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 459 | _notes.append( |
| 460 | self.errors.get_error_type_text( |
| 461 | self.errors.NET_NODE_NON_RESPONSIVE |
| 462 | ) |
| 463 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 464 | continue |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 465 | # lookup interface name on node using network CIDR |
| 466 | _if_name = _runtime[network][hostname][0]["name"] |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 467 | _raw = self.interfaces[hostname][_if_name]['runtime'] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 468 | # get proper reclass |
| 469 | _r = self.interfaces[hostname][_if_name]['reclass'] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 470 | _if_name_suffix = "" |
| 471 | # get the proto value |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 472 | if _r: |
| 473 | _if_rc = "" |
| 474 | else: |
| 475 | self.errors.add_error( |
| 476 | self.errors.NET_NODE_UNEXPECTED_IF, |
| 477 | host=hostname, |
| 478 | if_name=_if_name |
| 479 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 480 | _notes.append( |
| 481 | self.errors.get_error_type_text( |
| 482 | self.errors.NET_NODE_UNEXPECTED_IF |
| 483 | ) |
| 484 | ) |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 485 | _if_rc = "*" |
| 486 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 487 | if "proto" in _r: |
| 488 | _proto = _r['proto'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 489 | else: |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 490 | _proto = "-" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 491 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 492 | if "type" in _r: |
| 493 | _if_name_suffix += _r["type"] |
| 494 | if "use_interfaces" in _r: |
| 495 | _if_name_suffix += "->" + ",".join(_r["use_interfaces"]) |
| 496 | |
| 497 | if _if_name_suffix: |
| 498 | _if_name_suffix = "({})".format(_if_name_suffix) |
| 499 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 500 | # get gate and routes if proto is static |
| 501 | if _proto == 'static': |
| 502 | # get the gateway for current net |
| 503 | _routes = salt_master.nodes[hostname]['routes'] |
| 504 | _route = _routes[_net] if _net in _routes else None |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 505 | # get the default gateway |
| 506 | if 'default' in _routes: |
| 507 | _d_gate = ipaddress.IPv4Address( |
| 508 | _routes['default']['gateway'] |
| 509 | ) |
| 510 | else: |
| 511 | _d_gate = None |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 512 | _d_gate_str = str(_d_gate) if _d_gate else "No default!" |
| 513 | # match route with default |
| 514 | if not _route: |
| 515 | _gate = "?" |
| 516 | else: |
| 517 | _gate = _route['gateway'] if _route['gateway'] else "-" |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 518 | else: |
| 519 | # in case of manual and dhcp, no check possible |
| 520 | _gate = "-" |
| 521 | _d_gate = "-" |
Alex | 4067f00 | 2019-06-11 10:47:16 -0500 | [diff] [blame] | 522 | _d_gate_str = "-" |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 523 | # iterate through interfaces |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 524 | _a = _runtime[network][hostname] |
| 525 | for _host in _a: |
| 526 | for _if in _host['ifs']: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 527 | _ip_str = str(_if.exploded) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 528 | _gate_error = "" |
| 529 | _up_error = "" |
| 530 | _mtu_error = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 531 | |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 532 | # Match gateway |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 533 | if _proto == 'static': |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 534 | # default reclass gate |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 535 | _r_gate = "-" |
| 536 | if "gateway" in _r: |
| 537 | _r_gate = _r["gateway"] |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 538 | |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 539 | # if values not match, put * |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 540 | if _gate != _r_gate and _d_gate_str != _r_gate: |
| 541 | # if values not match, check if default match |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 542 | self.errors.add_error( |
| 543 | self.errors.NET_UNEXPECTED_GATEWAY, |
| 544 | host=hostname, |
| 545 | if_name=_if_name, |
| 546 | ip=_ip_str, |
| 547 | gateway=_gate |
| 548 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 549 | _notes.append( |
| 550 | self.errors.get_error_type_text( |
| 551 | self.errors.NET_UNEXPECTED_GATEWAY |
| 552 | ) |
| 553 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 554 | _gate_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 555 | |
| 556 | # IF status in reclass |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 557 | _e = "enabled" |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 558 | if _e not in _r: |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 559 | self.errors.add_error( |
| 560 | self.errors.NET_NO_RC_IF_STATUS, |
| 561 | host=hostname, |
| 562 | if_name=_if_name |
| 563 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 564 | _notes.append( |
| 565 | self.errors.get_error_type_text( |
| 566 | self.errors.NET_NO_RC_IF_STATUS |
| 567 | ) |
| 568 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 569 | _up_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 570 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 571 | _rc_mtu = _r['mtu'] if 'mtu' in _r else None |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 572 | _rc_mtu_s = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 573 | # check if this is a VIP address |
| 574 | # no checks needed if yes. |
| 575 | if _host['vip'] != _ip_str: |
| 576 | if _rc_mtu: |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 577 | _rc_mtu_s = str(_rc_mtu) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 578 | # if there is an MTU value, match it |
| 579 | if _host['mtu'] != _rc_mtu_s: |
| 580 | self.errors.add_error( |
| 581 | self.errors.NET_MTU_MISMATCH, |
| 582 | host=hostname, |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 583 | if_name=_if_name, |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 584 | if_cidr=_ip_str, |
| 585 | reclass_mtu=_rc_mtu, |
| 586 | runtime_mtu=_host['mtu'] |
| 587 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 588 | _notes.append( |
| 589 | self.errors.get_error_type_text( |
| 590 | self.errors.NET_MTU_MISMATCH |
| 591 | ) |
| 592 | ) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 593 | _rc_mtu_s = "/" + _rc_mtu_s |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 594 | _mtu_error = "*" |
| 595 | else: |
| 596 | # empty the matched value |
| 597 | _rc_mtu_s = "" |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 598 | elif _host['mtu'] != '1500' and \ |
| 599 | _proto not in ["-", "dhcp"]: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 600 | # there is no MTU value in reclass |
| 601 | # and runtime value is not default |
| 602 | self.errors.add_error( |
| 603 | self.errors.NET_MTU_EMPTY, |
| 604 | host=hostname, |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 605 | if_name=_if_name, |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 606 | if_cidr=_ip_str, |
| 607 | if_mtu=_host['mtu'] |
| 608 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 609 | _notes.append( |
| 610 | self.errors.get_error_type_text( |
| 611 | self.errors.NET_MTU_EMPTY |
| 612 | ) |
| 613 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 614 | _mtu_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 615 | else: |
| 616 | # this is a VIP |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 617 | _if_name = " "*7 |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 618 | _if_name_suffix = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 619 | _ip_str += " VIP" |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 620 | # Save all data |
| 621 | _values = { |
| 622 | "interface": _if_name, |
| 623 | "interface_error": _if_rc, |
| 624 | "interface_note": _if_name_suffix, |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 625 | "interface_map": "\n".join(_host['lines']), |
| 626 | "interface_matrix": _host['matrix'], |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 627 | "ip_address": _ip_str, |
| 628 | "address_type": _proto, |
| 629 | "rt_mtu": _host['mtu'], |
| 630 | "rc_mtu": _rc_mtu_s, |
| 631 | "mtu_error": _mtu_error, |
| 632 | "status": _host['state'], |
| 633 | "status_error": _up_error, |
| 634 | "subnet_gateway": _gate, |
| 635 | "subnet_gateway_error": _gate_error, |
| 636 | "default_gateway": _d_gate_str, |
| 637 | "raw_data": _raw, |
| 638 | "error_note": " and ".join(_notes) |
| 639 | } |
| 640 | if node in _map[_net]: |
| 641 | # add if to host |
| 642 | _map[_net][node].append(_values) |
| 643 | else: |
| 644 | _map[_net][node] = [_values] |
| 645 | _notes = [] |
| 646 | |
| 647 | # save map |
| 648 | self.map = _map |
| 649 | # other runtime networks found |
| 650 | # docker, etc |
| 651 | |
| 652 | return |
| 653 | |
| 654 | def print_map(self): |
| 655 | """ |
| 656 | Create text report for CLI |
| 657 | |
| 658 | :return: none |
| 659 | """ |
| 660 | logger_cli.info("# Networks") |
| 661 | logger_cli.info( |
| 662 | " {0:8} {1:25} {2:25} {3:6} {4:10} {5:10} {6}/{7}".format( |
| 663 | "Host", |
| 664 | "IF", |
| 665 | "IP", |
| 666 | "Proto", |
| 667 | "MTU", |
| 668 | "State", |
| 669 | "Gate", |
| 670 | "Def.Gate" |
| 671 | ) |
| 672 | ) |
| 673 | for network in self.map.keys(): |
| 674 | logger_cli.info("-> {}".format(network)) |
| 675 | for hostname in self.map[network].keys(): |
| 676 | node = hostname.split('.')[0] |
| 677 | _n = self.map[network][hostname] |
| 678 | for _i in _n: |
| 679 | # Host IF IP Proto MTU State Gate Def.Gate |
| 680 | _text = "{:7} {:17} {:25} {:6} {:10} " \ |
| 681 | "{:10} {} / {}".format( |
| 682 | _i['interface'] + _i['interface_error'], |
| 683 | _i['interface_note'], |
| 684 | _i['ip_address'], |
| 685 | _i['address_type'], |
| 686 | _i['rt_mtu'] + _i['rc_mtu'] + _i['mtu_error'], |
| 687 | _i['status'] + _i['status_error'], |
| 688 | _i['subnet_gateway'] + |
| 689 | _i['subnet_gateway_error'], |
| 690 | _i['default_gateway'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 691 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 692 | logger_cli.info( |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 693 | " {0:8} {1}".format( |
| 694 | node, |
| 695 | _text |
| 696 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 697 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 698 | |
| 699 | # logger_cli.info("\n# Other networks") |
| 700 | # _other = [n for n in _runtime if n not in _reclass] |
| 701 | # for network in _other: |
| 702 | # logger_cli.info("-> {}".format(str(network))) |
| 703 | # names = sorted(_runtime[network].keys()) |
| 704 | |
| 705 | # for hostname in names: |
| 706 | # for _n in _runtime[network][hostname]: |
| 707 | # _ifs = [str(ifs.ip) for ifs in _n['ifs']] |
| 708 | # _text = "{:25} {:25} {:6} {:10} {}".format( |
| 709 | # _n['name'], |
| 710 | # ", ".join(_ifs), |
| 711 | # "-", |
| 712 | # _n['mtu'], |
| 713 | # _n['state'] |
| 714 | # ) |
| 715 | # logger_cli.info( |
| 716 | # " {0:8} {1}".format(hostname.split('.')[0], _text) |
| 717 | # ) |
| 718 | # logger_cli.info("\n") |