使用权重为

时间:2018-02-16 16:37:48

标签: python networking graph networkx

我试图检测网络中有问题/慢的节点,为此,我有一个节点之间的路径列表以及将消息发送到目的地所花费的错误或重传次数。例如:

[
    {'path':['a', 'c', 'e', 'z'], 'errors': 0},
    ...
    {'path':['a', 'b', 'd', 'z'], 'errors': 4},
    {'path':['a', 'c', 'd', 'z'], 'errors': 4},
    ...
    {'path':['a', 'b', 'e', 'z'], 'errors': 0}
]

理论上,我拥有节点之间的所有可能路径及其各自的延迟。因此,对于这些数据,我想检测有问题的节点'。在前面的示例中,有几个路径,但是通过 d 节点的所有路径都会有更多延迟,因此必须将此节点(以及其他类似节点)精确定位为有问题。

是否有任何已知算法可以解决此问题?

我天真的方法是为每个路径使用错误计数器,并将这些计数器添加到路径中的每个节点,然后在处理完所有路径/节点时,将错误计数器除以此节点具有的邻居数。但这并没有给我一个好的结果,显示不同的节点是有问题的。

代码示例:

import networkx as nx
import matplotlib.pyplot as plt
import random


def get_problematic_nodes(path_list):
    node_connections = {}
    node_counter_dict = {}
    for e in path_list:
        value = 0
        if e['retransmits'] > 1:
            value = 1

        path_len = len(e['path'])
        for i in xrange(path_len):
            node = e['path'][i]
            if node not in node_counter_dict:
                node_counter_dict[node] = value
            else:
                node_counter_dict[node] += value

            if node not in node_connections:
                node_connections[node] = set()

            # previous node
            if i - 1 >= 0:
                node_connections[node].add(e['path'][i - 1])

            # next node
            if i + 1 <= path_len - 1:
                node_connections[node].add(e['path'][i + 1])

    nodes_score = {}

    print "Link size for every node:"
    for k, v in node_connections.items():
        link_number = len(v)
        print "Node: {} links:{}".format(k, link_number)
        nodes_score[k] = node_counter_dict[k]/link_number

    print "\nHeuristic score for every node:"
    for k,v in nodes_score.items():
        print "Node: {} score:{}".format(k, v)

    max_score_node_key = max(node_counter_dict.iterkeys(), key=(lambda key: node_counter_dict[key]/len(node_connections[key]) ))
    print "\nMax scored node: {}".format(max_score_node_key)

edge_list = [
    ('host1', 'leaf1'),
    ('host2', 'leaf2'),
    ('leaf1', 'spine1'),
    ('leaf1', 'spine2'),
    ('leaf2', 'spine1'),
    ('leaf2', 'spine2'),
    ('spine1', 'vmx8'),
    ('spine1', 'vmx9'),
    ('spine2', 'vmx8'),
    ('spine2', 'vmx9'),
    ('vmx8', 'vmx7'),
    ('vmx9', 'vmx7'),
    ('spine3', 'vmx8'),
    ('spine3', 'vmx9'),
    ('spine4', 'vmx8'),
    ('spine4', 'vmx9'),
    ('leaf3', 'spine3'),
    ('leaf3', 'spine4'),
    ('leaf4', 'spine3'),
    ('leaf4', 'spine4'),
    ('host3', 'leaf3'),
    ('host4', 'leaf4'),
]

# prepare graph
G = nx.Graph()
for e in edge_list:
    G.add_edge(*e)

# define problematic nodes
test_problem_nodes = ['spine3']

# generate the paths. Paths that touches problematic nodes have more retransmits
test_path_list = []
hosts = ['host1', 'host2', 'host3', 'host4']
for h1 in hosts:
    for h2 in hosts:
        if h1 == h2:
            continue

        all_paths = nx.all_simple_paths(G, h1, h2)
        for path in all_paths:
            retransmits = 0
            if len(set(path).intersection(set(test_problem_nodes))) > 0:
                retransmits = 10

            test_path_list.append({
                'src': h1,
                'dst': h2,
                'path': path,
                'retransmits': retransmits
                })

# nx.draw(G, with_labels=True)
# plt.draw()
# plt.show()

get_problematic_nodes(test_path_list)

1 个答案:

答案 0 :(得分:1)

我认为您希望通过观察到的路径数量来规范错误计数。将get_problematic_nodes更改为

def get_problematic_nodes(event_list):
    numerator = dict()
    denominator = dict()
    for event in event_list:
        for node in event['path']:
            try:
                numerator[node] += event['retransmits']
                denominator[node] += 1
            except KeyError:
                numerator[node] = event['retransmits']
                denominator[node] = 1

    node_score = {node : numerator[node] / denominator[node] for node in numerator.keys()}

    print "\nHeuristic score for every node:"
    for k,v in node_score.items():
        print "Node: {} score:{}".format(k, v)

    max_score = None
    for k,v in node_score.items():
        if v > max_score:
            max_score_node_key = k
            max_score = v
    print "\nMax scored node: {}".format(max_score_node_key)

的产率:

Heuristic score for every node:
Node: vmx9 score:8
Node: vmx8 score:8
Node: host1 score:7
Node: vmx7 score:7
Node: spine1 score:7
Node: leaf4 score:8
Node: spine3 score:10
Node: spine2 score:7
Node: leaf1 score:7
Node: spine4 score:7
Node: leaf3 score:8
Node: leaf2 score:7
Node: host3 score:8
Node: host4 score:8
Node: host2 score:7

Max scored node: spine3