blob: 482bdfaa166ef36ca914d8abce60ddf0bf036338 [file] [log] [blame]
Alexe0c5b9e2019-04-23 18:51:23 -05001import ipaddress
2import json
Alex6b633ec2019-06-06 19:44:34 -05003from copy import deepcopy
Alexe0c5b9e2019-04-23 18:51:23 -05004
5from cfg_checker.common import logger_cli
6from cfg_checker.common.exception import InvalidReturnException
7from cfg_checker.modules.network.network_errors import NetworkErrors
8from 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": {},
Alex6b633ec2019-06-06 19:44:34 -050016 "proto": "",
Alexe0c5b9e2019-04-23 18:51:23 -050017 "ip": [],
18 "parameters": {}
19}
20
21# collection of configurations
22_network_item = {
23 "runtime": {},
24 "config": {},
25 "reclass": {}
26}
27
28
29class 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")
Alex6b633ec2019-06-06 19:44:34 -050036 # init networks and nodes
Alexe0c5b9e2019-04-23 18:51:23 -050037 self.networks = {}
38 self.nodes = salt_master.get_nodes()
Alex836fac82019-08-22 13:36:16 -050039 self.cluster = salt_master.get_info()
Alex6b633ec2019-06-06 19:44:34 -050040 # init and pre-populate interfaces
41 self.interfaces = {k: {} for k in self.nodes}
42 # Init errors class
Alexe0c5b9e2019-04-23 18:51:23 -050043 if errors_class:
44 self.errors = errors_class
45 else:
46 logger_cli.debug("... init error logs folder")
47 self.errors = NetworkErrors()
48
Alex836fac82019-08-22 13:36:16 -050049 def prepare_all_maps(self):
50 self.map_network(self.RECLASS)
51 self.map_network(self.RUNTIME)
52 self.map_network(self.CONFIG)
53
Alexe0c5b9e2019-04-23 18:51:23 -050054 # 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
Alex92e07ce2019-05-31 16:00:03 -0500115 # for example on APT node
Alexe0c5b9e2019-04-23 18:51:23 -0500116 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
Alex92e07ce2019-05-31 16:00:03 -0500125
Alex6b633ec2019-06-06 19:44:34 -0500126 # build map based on IPs and save info too
Alexb3dc8592019-06-11 13:20:36 -0500127 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
Alex6b633ec2019-06-06 19:44:34 -0500131 if _if_name not in self.interfaces[node]:
132 self.interfaces[node][_if_name] = deepcopy(_network_item)
Alexb3dc8592019-06-11 13:20:36 -0500133 self.interfaces[node][_if_name]['reclass'] = deepcopy(_dat)
Alex6b633ec2019-06-06 19:44:34 -0500134 # map network if any
Alexb3dc8592019-06-11 13:20:36 -0500135 if 'address' in _dat:
Alexe0c5b9e2019-04-23 18:51:23 -0500136 _if = ipaddress.IPv4Interface(
Alexb3dc8592019-06-11 13:20:36 -0500137 _dat['address'] + '/' + _dat['netmask']
Alexe0c5b9e2019-04-23 18:51:23 -0500138 )
Alexb3dc8592019-06-11 13:20:36 -0500139 _dat['name'] = _if_name
140 _dat['ifs'] = [_if]
Alexe0c5b9e2019-04-23 18:51:23 -0500141
142 _reclass = self._map_network_for_host(
143 node,
144 _if,
145 _reclass,
Alexb3dc8592019-06-11 13:20:36 -0500146 _dat
Alexe0c5b9e2019-04-23 18:51:23 -0500147 )
148
149 return _reclass
150
151 def _map_configured_networks(self):
152 # class uses nodes from self.nodes dict
153 _confs = {}
154
Alex92e07ce2019-05-31 16:00:03 -0500155 # TODO: parse /etc/network/interfaces
156
Alexe0c5b9e2019-04-23 18:51:23 -0500157 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():
Alexb3dc8592019-06-11 13:20:36 -0500203 # cut net name
204 _i = net_name.find('@')
205 _name = net_name if _i < 0 else net_name[:_i]
Alexe0c5b9e2019-04-23 18:51:23 -0500206 # get ips and calculate subnets
Alexb3dc8592019-06-11 13:20:36 -0500207 if _name in ['lo']:
Alexe0c5b9e2019-04-23 18:51:23 -0500208 # skip the localhost
209 continue
Alex6b633ec2019-06-06 19:44:34 -0500210 else:
211 # add collected data to interface storage
Alexb3dc8592019-06-11 13:20:36 -0500212 if _name not in self.interfaces[host]:
213 self.interfaces[host][_name] = \
Alex6b633ec2019-06-06 19:44:34 -0500214 deepcopy(_network_item)
Alexb3dc8592019-06-11 13:20:36 -0500215 self.interfaces[host][_name]['runtime'] = \
Alex6b633ec2019-06-06 19:44:34 -0500216 deepcopy(net_data)
217
Alexe0c5b9e2019-04-23 18:51:23 -0500218 # 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:
Alexb3dc8592019-06-11 13:20:36 -0500232 net_data['name'] = _name
Alexe0c5b9e2019-04-23 18:51:23 -0500233 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
Alex1839bbf2019-08-22 17:17:21 -0500246 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
Alexf3dbe862019-10-07 15:17:04 -0500255 _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
Alex1839bbf2019-08-22 17:17:21 -0500266 # if None, put empty list
267 _p = _p if _p else []
Alex1839bbf2019-08-22 17:17:21 -0500268 # if None, put empty list
269 _c = _c if _c else []
270 tree[lvl].update({
271 interface: {
Alexf3dbe862019-10-07 15:17:04 -0500272 "note": _n,
Alex1839bbf2019-08-22 17:17:21 -0500273 "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):
Alexf3dbe862019-10-07 15:17:04 -0500289 _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):
Alex1839bbf2019-08-22 17:17:21 -0500299 # add child per index
300 # if space is free
301 if not _list[_cI]:
302 _list[_cI] = cNet
Alexf3dbe862019-10-07 15:17:04 -0500303 _added = True
304 _actual_index = _cI
Alex1839bbf2019-08-22 17:17:21 -0500305 break
Alexf3dbe862019-10-07 15:17:04 -0500306 if not _added:
307 # grow list by one entry
308 _list = _list + [cNet]
309 _actual_index = len(_list) - 1
310 return _actual_index, _list
Alex1839bbf2019-08-22 17:17:21 -0500311
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]:
Alexf3dbe862019-10-07 15:17:04 -0500343 _, matrix[x] = _put(
344 _net,
345 y,
346 matrix[x]
347 )
Alex1839bbf2019-08-22 17:17:21 -0500348 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])
Alexf3dbe862019-10-07 15:17:04 -0500361 _, matrix[x+1] = _put(
362 _c,
363 _nI,
364 matrix[x+1]
365 )
Alex1839bbf2019-08-22 17:17:21 -0500366 else:
367 # there is no such interface
368 # add it
Alexf3dbe862019-10-07 15:17:04 -0500369 _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 )
Alex1839bbf2019-08-22 17:17:21 -0500380 # 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
Alexe0c5b9e2019-04-23 18:51:23 -0500400 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
Alex836fac82019-08-22 13:36:16 -0500416 def create_map(self):
417 """Create all needed elements for map output
Alexe0c5b9e2019-04-23 18:51:23 -0500418
419 :return: none
420 """
421 _runtime = self.networks[self.RUNTIME]
422 _reclass = self.networks[self.RECLASS]
Alex836fac82019-08-22 13:36:16 -0500423
424 # main networks, target vars
425 _map = {}
Alex6b633ec2019-06-06 19:44:34 -0500426 # No matter of proto, at least one IP will be present for the network
Alex836fac82019-08-22 13:36:16 -0500427 # we interested in, since we are to make sure that L3 level
428 # is configured according to reclass model
Alexe0c5b9e2019-04-23 18:51:23 -0500429 for network in _reclass:
430 # shortcuts
431 _net = str(network)
Alex836fac82019-08-22 13:36:16 -0500432 _map[_net] = {}
Alexe0c5b9e2019-04-23 18:51:23 -0500433 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 )
Alex1839bbf2019-08-22 17:17:21 -0500439 logger_cli.warn(
440 "WARN: {}: {}".format(
441 " No runtime network ", str(network)
442 )
443 )
Alexe0c5b9e2019-04-23 18:51:23 -0500444 continue
Alex6b633ec2019-06-06 19:44:34 -0500445 # hostnames
Alexe0c5b9e2019-04-23 18:51:23 -0500446 names = sorted(_runtime[network].keys())
447 for hostname in names:
Alex836fac82019-08-22 13:36:16 -0500448 _notes = []
Alex6b633ec2019-06-06 19:44:34 -0500449 node = hostname.split('.')[0]
Alexe0c5b9e2019-04-23 18:51:23 -0500450 if not salt_master.is_node_available(hostname, log=False):
451 logger_cli.info(
Alex6b633ec2019-06-06 19:44:34 -0500452 " {0:8} {1}".format(node, "node not available")
Alexe0c5b9e2019-04-23 18:51:23 -0500453 )
454 # add non-responsive node erorr
455 self.errors.add_error(
456 self.errors.NET_NODE_NON_RESPONSIVE,
457 host=hostname
458 )
Alex836fac82019-08-22 13:36:16 -0500459 _notes.append(
460 self.errors.get_error_type_text(
461 self.errors.NET_NODE_NON_RESPONSIVE
462 )
463 )
Alexe0c5b9e2019-04-23 18:51:23 -0500464 continue
Alex6b633ec2019-06-06 19:44:34 -0500465 # lookup interface name on node using network CIDR
466 _if_name = _runtime[network][hostname][0]["name"]
Alex836fac82019-08-22 13:36:16 -0500467 _raw = self.interfaces[hostname][_if_name]['runtime']
Alex6b633ec2019-06-06 19:44:34 -0500468 # get proper reclass
469 _r = self.interfaces[hostname][_if_name]['reclass']
Alex6b633ec2019-06-06 19:44:34 -0500470 _if_name_suffix = ""
471 # get the proto value
Alex3b8e5432019-06-11 15:21:59 -0500472 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 )
Alex836fac82019-08-22 13:36:16 -0500480 _notes.append(
481 self.errors.get_error_type_text(
482 self.errors.NET_NODE_UNEXPECTED_IF
483 )
484 )
Alex3b8e5432019-06-11 15:21:59 -0500485 _if_rc = "*"
486
Alex6b633ec2019-06-06 19:44:34 -0500487 if "proto" in _r:
488 _proto = _r['proto']
Alexe0c5b9e2019-04-23 18:51:23 -0500489 else:
Alex6b633ec2019-06-06 19:44:34 -0500490 _proto = "-"
Alexe0c5b9e2019-04-23 18:51:23 -0500491
Alex6b633ec2019-06-06 19:44:34 -0500492 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
Alex6b633ec2019-06-06 19:44:34 -0500500 # 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
Alex6b633ec2019-06-06 19:44:34 -0500505 # 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
Alexb3dc8592019-06-11 13:20:36 -0500512 _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 "-"
Alex6b633ec2019-06-06 19:44:34 -0500518 else:
519 # in case of manual and dhcp, no check possible
520 _gate = "-"
521 _d_gate = "-"
Alex4067f002019-06-11 10:47:16 -0500522 _d_gate_str = "-"
Alex6b633ec2019-06-06 19:44:34 -0500523 # iterate through interfaces
Alexe0c5b9e2019-04-23 18:51:23 -0500524 _a = _runtime[network][hostname]
525 for _host in _a:
526 for _if in _host['ifs']:
Alexe0c5b9e2019-04-23 18:51:23 -0500527 _ip_str = str(_if.exploded)
Alexab232e42019-06-06 19:44:34 -0500528 _gate_error = ""
529 _up_error = ""
530 _mtu_error = ""
Alexe0c5b9e2019-04-23 18:51:23 -0500531
Alexb3dc8592019-06-11 13:20:36 -0500532 # Match gateway
Alexab232e42019-06-06 19:44:34 -0500533 if _proto == 'static':
Alexb3dc8592019-06-11 13:20:36 -0500534 # default reclass gate
Alex6b633ec2019-06-06 19:44:34 -0500535 _r_gate = "-"
536 if "gateway" in _r:
537 _r_gate = _r["gateway"]
Alexb3dc8592019-06-11 13:20:36 -0500538
Alexab232e42019-06-06 19:44:34 -0500539 # if values not match, put *
Alexb3dc8592019-06-11 13:20:36 -0500540 if _gate != _r_gate and _d_gate_str != _r_gate:
541 # if values not match, check if default match
Alex3b8e5432019-06-11 15:21:59 -0500542 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 )
Alex836fac82019-08-22 13:36:16 -0500549 _notes.append(
550 self.errors.get_error_type_text(
551 self.errors.NET_UNEXPECTED_GATEWAY
552 )
553 )
Alexab232e42019-06-06 19:44:34 -0500554 _gate_error = "*"
Alexe0c5b9e2019-04-23 18:51:23 -0500555
556 # IF status in reclass
Alex6b633ec2019-06-06 19:44:34 -0500557 _e = "enabled"
Alexab232e42019-06-06 19:44:34 -0500558 if _e not in _r:
Alex3b8e5432019-06-11 15:21:59 -0500559 self.errors.add_error(
560 self.errors.NET_NO_RC_IF_STATUS,
561 host=hostname,
562 if_name=_if_name
563 )
Alex836fac82019-08-22 13:36:16 -0500564 _notes.append(
565 self.errors.get_error_type_text(
566 self.errors.NET_NO_RC_IF_STATUS
567 )
568 )
Alexab232e42019-06-06 19:44:34 -0500569 _up_error = "*"
Alexe0c5b9e2019-04-23 18:51:23 -0500570
Alexe0c5b9e2019-04-23 18:51:23 -0500571 _rc_mtu = _r['mtu'] if 'mtu' in _r else None
Alexab232e42019-06-06 19:44:34 -0500572 _rc_mtu_s = ""
Alexe0c5b9e2019-04-23 18:51:23 -0500573 # check if this is a VIP address
574 # no checks needed if yes.
575 if _host['vip'] != _ip_str:
576 if _rc_mtu:
Alex3b8e5432019-06-11 15:21:59 -0500577 _rc_mtu_s = str(_rc_mtu)
Alexe0c5b9e2019-04-23 18:51:23 -0500578 # 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,
Alex6b633ec2019-06-06 19:44:34 -0500583 if_name=_if_name,
Alexe0c5b9e2019-04-23 18:51:23 -0500584 if_cidr=_ip_str,
585 reclass_mtu=_rc_mtu,
586 runtime_mtu=_host['mtu']
587 )
Alex836fac82019-08-22 13:36:16 -0500588 _notes.append(
589 self.errors.get_error_type_text(
590 self.errors.NET_MTU_MISMATCH
591 )
592 )
Alexb3dc8592019-06-11 13:20:36 -0500593 _rc_mtu_s = "/" + _rc_mtu_s
Alexab232e42019-06-06 19:44:34 -0500594 _mtu_error = "*"
595 else:
596 # empty the matched value
597 _rc_mtu_s = ""
Alex3b8e5432019-06-11 15:21:59 -0500598 elif _host['mtu'] != '1500' and \
599 _proto not in ["-", "dhcp"]:
Alexe0c5b9e2019-04-23 18:51:23 -0500600 # 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,
Alex6b633ec2019-06-06 19:44:34 -0500605 if_name=_if_name,
Alexe0c5b9e2019-04-23 18:51:23 -0500606 if_cidr=_ip_str,
607 if_mtu=_host['mtu']
608 )
Alex836fac82019-08-22 13:36:16 -0500609 _notes.append(
610 self.errors.get_error_type_text(
611 self.errors.NET_MTU_EMPTY
612 )
613 )
Alexab232e42019-06-06 19:44:34 -0500614 _mtu_error = "*"
Alexe0c5b9e2019-04-23 18:51:23 -0500615 else:
616 # this is a VIP
Alex6b633ec2019-06-06 19:44:34 -0500617 _if_name = " "*7
Alex6b633ec2019-06-06 19:44:34 -0500618 _if_name_suffix = ""
Alexe0c5b9e2019-04-23 18:51:23 -0500619 _ip_str += " VIP"
Alex836fac82019-08-22 13:36:16 -0500620 # Save all data
621 _values = {
622 "interface": _if_name,
623 "interface_error": _if_rc,
624 "interface_note": _if_name_suffix,
Alex1839bbf2019-08-22 17:17:21 -0500625 "interface_map": "\n".join(_host['lines']),
626 "interface_matrix": _host['matrix'],
Alex836fac82019-08-22 13:36:16 -0500627 "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']
Alexe0c5b9e2019-04-23 18:51:23 -0500691 )
Alexe0c5b9e2019-04-23 18:51:23 -0500692 logger_cli.info(
Alex836fac82019-08-22 13:36:16 -0500693 " {0:8} {1}".format(
694 node,
695 _text
696 )
Alexe0c5b9e2019-04-23 18:51:23 -0500697 )
Alex836fac82019-08-22 13:36:16 -0500698
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")