示例#1
0
if len(sys.argv) < 3:
    print(usage)
    raise SystemExit(1)

nm_host = sys.argv[1]
cls = sys.argv[2]

if nm_host == "-h" or nm_host == "--help":
    print(usage)
    raise SystemExit(1)

print("Searching NodeMeister {nm_host} for classes containing '{cls}'".format(cls=cls, nm_host=nm_host))

nodes = get_node_names(nm_host)
groups = get_group_names(nm_host)
found = False
j = get_json("http://{nm_host}/enc/classes/nodes".format(nm_host=nm_host))
for c in j:
    if cls in c["classname"]:
        found = True
        print(
            "\tnode {node} class '{classname}' <{url}>".format(
                node=nodes[c["node"]],
                classname=c["classname"],
                url="http://{nm_host}/admin/enc/node/{num}/".format(nm_host=nm_host, num=c["node"]),
            )
        )
j = get_json("http://{nm_host}/enc/classes/groups".format(nm_host=nm_host))
for c in j:
    if cls in c["classname"]:
示例#2
0
def class_to_node_graph(nm_host):
    """
    loads all class, group and node data from the specified ENC instance,
    then constructs and returns a NetworkX DiGraph of the configuration,
    directed from classes to groups to nodemeister nodes

    each node in the graph has three attributes, "_id" (the ID of the object),
    "_name" (the name of the object), and "_type", a string of either "class",
    "group" or "node"

    :param nm_host: NodeMeister hostname/FQDN/IP
    :type nm_host: string
    :rtype: networkx.DiGraph
    """
    logger.debug("getting node names")
    nodes = get_node_names(nm_host)
    logger.debug("getting group names")
    groups = get_group_names(nm_host)
    logger.debug("getting group classes")
    group_classes = get_nm_group_classes(nm_host)

    g = nx.DiGraph()

    logger.debug("adding groups to graph")
    for group in groups:
        groupname = groups[group]
        g_name = 'Group[{g}]'.format(g=groupname)
        logger.debug("adding group {g} to graph".format(g=groupname))
        g.add_node(g_name, _type='group', _name=groupname, _id=group)

    logger.debug("getting node json")
    node_json = get_json("http://{nm_host}/enc/nodes".format(nm_host=nm_host))
    logger.debug("adding node groups to graph")
    for node in node_json:
        nodename = node['hostname']
        n_name = 'Node[{n}]'.format(n=nodename)
        logger.debug("adding node {n} to graph".format(n=nodename))
        g.add_node(n_name, _type='node', _name=nodename, _id=node['id'])
        for g_id in node['groups']:
            g_name = 'Group[{g}]'.format(g=groups[g_id])
            logger.debug("adding edge {n} -> {g}".format(n=n_name, g=g_name))
            g.add_edge(n_name, g_name)

    logger.debug("getting node classes json")
    node_json = get_json("http://{nm_host}/enc/classes/nodes".format(nm_host=nm_host))
    logger.debug("adding nodes to graph")
    for node in node_json:
        nodename = nodes[node['node']]
        n_name = 'Node[{n}]'.format(n=nodename)
        cls_name = node['classname']
        cn_name = 'Class[{c}]'.format(c=cls_name)
        try:
            g.node[cn_name]
        except KeyError:
            logger.debug("adding class {c} to graph".format(c=cls_name))
            g.add_node(cn_name, _type='class', _name=cls_name)
        logger.debug("adding edge {c} -> {n}".format(c=cn_name, n=n_name))
        g.add_edge(n_name, cn_name)

    logger.debug("adding group classes")
    for gc in group_classes:
        g_name = 'Group[{g}]'.format(g=groups[group_classes[gc]['group']])
        c_name = 'Class[{c}]'.format(c=group_classes[gc]['classname'])
        try:
            g.node[c_name]
        except KeyError:
            logger.debug("adding class {c} to graph".format(c=group_classes[gc]['classname']))
            g.add_node(c_name, _type='class', _name=group_classes[gc]['classname'])
        logger.debug("adding edge {g} -> {c}".format(g=g_name, c=c_name))
        g.add_edge(g_name, c_name)

    logger.debug("getting group json")
    group_json = get_json("http://{nm_host}/enc/groups/".format(nm_host=nm_host))
    logger.debug("adding group to group associations")
    for group in group_json:
        groupname = group['name']
        g_name = 'Group[{g}]'.format(g=groupname)
        for gg in group['groups']:
            gg_name = 'Group[{gg}]'.format(gg=groups[gg])
            logger.debug("adding edge {g} -> {gg}".format(g=g_name, gg=gg_name))
            g.add_edge(g_name, gg_name)

    logger.debug("done creating graph")
    return g
示例#3
0
def class_to_node_graph(nm_host):
    """
    loads all class, group and node data from the specified ENC instance,
    then constructs and returns a NetworkX DiGraph of the configuration,
    directed from classes to groups to nodemeister nodes

    each node in the graph has three attributes, "_id" (the ID of the object),
    "_name" (the name of the object), and "_type", a string of either "class",
    "group" or "node"

    :param nm_host: NodeMeister hostname/FQDN/IP
    :type nm_host: string
    :rtype: networkx.DiGraph
    """
    logger.debug("getting node names")
    nodes = get_node_names(nm_host)
    logger.debug("getting group names")
    groups = get_group_names(nm_host)
    logger.debug("getting group classes")
    group_classes = get_nm_group_classes(nm_host)

    g = nx.DiGraph()

    logger.debug("adding groups to graph")
    for group in groups:
        groupname = groups[group]
        g_name = 'Group[{g}]'.format(g=groupname)
        logger.debug("adding group {g} to graph".format(g=groupname))
        g.add_node(g_name, _type='group', _name=groupname, _id=group)

    logger.debug("getting node json")
    node_json = get_json("http://{nm_host}/enc/nodes".format(nm_host=nm_host))
    logger.debug("adding node groups to graph")
    for node in node_json:
        nodename = node['hostname']
        n_name = 'Node[{n}]'.format(n=nodename)
        logger.debug("adding node {n} to graph".format(n=nodename))
        g.add_node(n_name, _type='node', _name=nodename, _id=node['id'])
        for g_id in node['groups']:
            g_name = 'Group[{g}]'.format(g=groups[g_id])
            logger.debug("adding edge {n} -> {g}".format(n=n_name, g=g_name))
            g.add_edge(n_name, g_name)

    logger.debug("getting node classes json")
    node_json = get_json(
        "http://{nm_host}/enc/classes/nodes".format(nm_host=nm_host))
    logger.debug("adding nodes to graph")
    for node in node_json:
        nodename = nodes[node['node']]
        n_name = 'Node[{n}]'.format(n=nodename)
        cls_name = node['classname']
        cn_name = 'Class[{c}]'.format(c=cls_name)
        try:
            g.node[cn_name]
        except KeyError:
            logger.debug("adding class {c} to graph".format(c=cls_name))
            g.add_node(cn_name, _type='class', _name=cls_name)
        logger.debug("adding edge {c} -> {n}".format(c=cn_name, n=n_name))
        g.add_edge(n_name, cn_name)

    logger.debug("adding group classes")
    for gc in group_classes:
        g_name = 'Group[{g}]'.format(g=groups[group_classes[gc]['group']])
        c_name = 'Class[{c}]'.format(c=group_classes[gc]['classname'])
        try:
            g.node[c_name]
        except KeyError:
            logger.debug("adding class {c} to graph".format(
                c=group_classes[gc]['classname']))
            g.add_node(c_name,
                       _type='class',
                       _name=group_classes[gc]['classname'])
        logger.debug("adding edge {g} -> {c}".format(g=g_name, c=c_name))
        g.add_edge(g_name, c_name)

    logger.debug("getting group json")
    group_json = get_json(
        "http://{nm_host}/enc/groups/".format(nm_host=nm_host))
    logger.debug("adding group to group associations")
    for group in group_json:
        groupname = group['name']
        g_name = 'Group[{g}]'.format(g=groupname)
        for gg in group['groups']:
            gg_name = 'Group[{gg}]'.format(gg=groups[gg])
            logger.debug("adding edge {g} -> {gg}".format(g=g_name,
                                                          gg=gg_name))
            g.add_edge(g_name, gg_name)

    logger.debug("done creating graph")
    return g
if len(sys.argv) < 3:
    print(usage)
    raise SystemExit(1)

nm_host = sys.argv[1]
cls = sys.argv[2]

if nm_host == "-h" or nm_host == "--help":
    print(usage)
    raise SystemExit(1)

print("Searching NodeMeister {nm_host} for classes containing '{cls}'".format(
    cls=cls, nm_host=nm_host))

nodes = get_node_names(nm_host)
groups = get_group_names(nm_host)
found = False
j = get_json("http://{nm_host}/enc/classes/nodes".format(nm_host=nm_host))
for c in j:
    if cls in c['classname']:
        found = True
        print("\tnode {node} class '{classname}' <{url}>".format(
            node=nodes[c['node']],
            classname=c['classname'],
            url="http://{nm_host}/admin/enc/node/{num}/".format(
                nm_host=nm_host,
                num=c['node'],
            ),
        ))
j = get_json("http://{nm_host}/enc/classes/groups".format(nm_host=nm_host))
for c in j: