bin/pvl.hosts-graph
author Tero Marttila <terom@paivola.fi>
Mon, 31 Mar 2014 14:47:53 +0300
changeset 408 32b7a0f2e7dc
parent 406 92a4de88b86f
child 409 b2b1bc488195
permissions -rwxr-xr-x
pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
#!/usr/bin/env python
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     2
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     3
"""
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     4
    Requirements:
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     5
        pydot
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     6
"""
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     7
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     8
import pvl.args
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     9
import pvl.hosts
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
    10
from pvl.invoke import merge
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    11
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    12
import collections
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    13
import logging; log = logging.getLogger('pvl.hosts-graph')
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    14
import optparse
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    15
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    16
COLOR_VLANS = {
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    17
    1:      'grey',         # pvl-lan
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    18
    2:      'blue',         # pvl-lan2
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    19
    3:      'red',          # pvl-san
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    20
    4:      'green',        # pvl-veturi
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    21
    7:      'orange',       # pvl-ranssi
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    22
    8:      'yellow',       # pvl-mgmt
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    23
    10:     'brown',        # pvl-public
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    24
    100:    'navyblue',     # pvl-test
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    25
    103:    'red4',         # pvl-test-san
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    26
    104:    'red2',         # pvl-ganeti
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    27
    192:    'purple',       # paivola-services
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    28
    255:    'magenta',      # pvl-sonera
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    29
}
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    30
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    31
class ParseError (Exception) :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
    def __init__ (self, file, line, msg) :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
        self.file = file
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    34
        self.line = line
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    35
        self.msg = msg
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
  
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
    def __str__ (self) :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
        return "{self.file}:{self.line}: {self.msg}".format(self=self)
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    40
def _parse_snmp_part (part) :
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    41
    if part.isdigit() :
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    42
        return int(part)
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    43
    else :
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    44
        return part
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    45
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    46
def _parse_snmp_attr (line) :
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    47
    for part in line.split() :
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    48
        yield _parse_snmp_part(part)
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    49
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    50
def _parse_snmp_value (line) :
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    51
    if '\t' in line :
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    52
        key, value = line.split('\t', 1)
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
    53
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    54
        return { _parse_snmp_part(key): _parse_snmp_part(value) }
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
    55
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
    56
    else :
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    57
        return set((_parse_snmp_part(line), ))
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    58
    
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    59
def _load_snmp_data (options, file) :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    60
    """
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    61
        Load a data dict generated by pvl.hosts-snmp from a file.
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    62
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    63
        Yields (host, attr, value)
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    64
    """
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    65
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    66
    host = None
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    67
    attr = None
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    68
    value = None
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    69
    
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    70
    for idx, line in enumerate(file, 1) :
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    71
        indent = 0
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    72
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    73
        while line.startswith('\t') :
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    74
            indent += 1
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    75
            line = line[1:]
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    76
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    77
        line = line.lstrip('\t').rstrip('\n')
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    78
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    79
        if indent == 0 :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    80
            host = line
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    81
            attr = None
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    82
            value = None
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    83
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    84
        elif indent == 1 :
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    85
            attr = tuple(_parse_snmp_attr(line))
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    86
            value = None
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    87
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    88
            yield host, attr, None
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    89
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    90
        elif indent == 2 :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    91
            if not attr :
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    92
                raise ParseError(file, line, "[%s] %s: value outside of attr" % (host, attr))
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
    93
            
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    94
            value = _parse_snmp_value(line)
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    95
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
    96
            yield host, attr, value
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    97
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    98
def load_snmp_data (options, file, hosts) :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    99
    """
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   100
        Load snmp data as dict, from given file path, or stdin.
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   101
    """
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   102
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   103
    if file :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   104
        file = open(file)
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   105
    else :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   106
        file = sys.stdin
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   107
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   108
    root = { }
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   109
    
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   110
    hosts_by_namedomain = dict(
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   111
        (
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   112
            '{host}@{domain}'.format(host=host, domain=host.domain), host
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   113
        ) for host in hosts
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   114
    )
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   115
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   116
    for host_domain, attr, value in _load_snmp_data(options, file) :
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   117
        host = hosts_by_namedomain.get(host_domain)
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   118
        
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   119
        if value :
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   120
            log.debug("[%s] %s: %s", host, ' '.join(str(a) for a in attr), value)
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   121
        else :
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   122
            log.debug("[%s] %s", host, ' '.join(str(a) for a in attr),)
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   123
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   124
        item = root.setdefault(host, { })
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
   125
        
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   126
        for a in attr[:-1] :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   127
            item = item.setdefault(a, {})
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   128
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   129
        a = attr[-1]
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
   130
        
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   131
        if value is None :
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   132
            pass
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   133
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   134
        elif isinstance(value, set) :
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
   135
            item.setdefault(a, set()).update(value)
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   136
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   137
        elif isinstance(value, dict) :
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   138
            item.setdefault(a, dict()).update(value)
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   139
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   140
        else :
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   141
            item[a] = value
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
   142
            
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   143
    return root
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   144
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   145
def host_vlans (host, host_vlans) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   146
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   147
        {vlan: { tagged/untagged: [port] } } -> (port, (untag, [tag])).
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   148
    """
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   149
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   150
    ports = set()
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   151
    vlans_untagged = { }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   152
    vlans_tagged = collections.defaultdict(set)
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   153
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   154
    for vlan, vlan_attrs in host_vlans.iteritems() :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   155
        for port in vlan_attrs.get('tagged', ()) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   156
            ports.add(port)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   157
            vlans_tagged[port].add(vlan)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   158
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   159
        for port in vlan_attrs.get('untagged', ()) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   160
            ports.add(port)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   161
            vlans_untagged[port] = vlan
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   162
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   163
    for port in ports :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   164
        untag = vlans_untagged.get(port)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   165
        tagged = vlans_tagged.get(port, ())
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   166
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   167
        log.debug("%s: %s: untag=%s tag=%s", host, port, untag, tagged)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   168
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   169
        yield port, (untag, tagged)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   170
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   171
def build_graph (snmp, hosts) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   172
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   173
        Combine given snmp data and { host: Host } into
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   174
            { node: label }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   175
            { (remote, remote_port, local_port, local): (local_untag, tagged, remote_untag) }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   176
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   177
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   178
    nodes = { } # host: label
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   179
    links = { }
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   180
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   181
    hosts_by_lldp = { } # chassis: host
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   182
    hosts_by_ethernet = { } # ethernet: host
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   183
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   184
    # first scan: lldp hosts
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   185
    for host, host_attrs in snmp.iteritems() :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   186
        lldp = host_attrs.get('lldp')
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   187
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   188
        if lldp :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   189
            lldp_local = lldp['local']
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   190
            local = lldp_local['chassis']
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   191
            
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   192
            nodes[host] = host.location or str(host)
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   193
            hosts_by_lldp[local] = host
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   194
    
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   195
    # second scan: nodes by ethernet
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   196
    for host in hosts :
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   197
        for ethernet in host.ethernet.itervalues() :
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   198
            hosts_by_ethernet[ethernet] = host
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   199
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   200
    # second scan: lldp remotes
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   201
    for host, host_attrs in snmp.iteritems() :
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   202
        lldp = host_attrs.get('lldp')
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   203
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   204
        if not lldp :
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   205
            continue
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   206
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   207
        local = lldp['local']['chassis']
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   208
        local_node = host
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   209
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   210
        if 'vlan' in host_attrs :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   211
            vlans = dict(host_vlans(host, host_attrs['vlan']))
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   212
        else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   213
            vlans = None
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   214
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   215
        for port, port_attrs in lldp.get('port', { }).iteritems() :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   216
            local_port = port_attrs['local']['port']
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   217
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   218
            for remote, remote_attrs in port_attrs['remote'].iteritems() :
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   219
                # determine remote node
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   220
                remote_label = remote_attrs['sys_name']
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   221
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   222
                if remote in hosts_by_lldp :
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   223
                    remote_node = remote_host = hosts_by_lldp[remote]
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   224
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   225
                elif remote in hosts_by_ethernet :
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   226
                    remote_node = remote_host = hosts_by_ethernet[remote]
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   227
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   228
                    if remote_host.location :
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   229
                        remote_label = remote_host.location
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   230
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   231
                    log.info("%s:%s: guessing lldp host %s -> %s (%s)", host, port, remote, remote_host, remote_label)
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   232
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   233
                else :
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   234
                    remote_node = remote
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   235
                    remote_host = None
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   236
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   237
                    log.warning("%s:%s: unknown remote %s (%s)", host, port, remote, remote_label)
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   238
                
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   239
                # ensure remote node
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   240
                if remote_node not in nodes :
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   241
                    log.info("%s:%s: lazy-add remote %s (%s)", host, port, remote_node, remote_label)
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   242
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   243
                    nodes[remote_node] = remote_label
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   244
                
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   245
                # local vlans
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   246
                if vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   247
                    port_vlans = vlans.get(port)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   248
                else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   249
                    port_vlans = None
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   250
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   251
                if port_vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   252
                    local_untag, local_tagged = port_vlans
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   253
                
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   254
                # bidirectional mappings
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   255
                remote_port = remote_attrs['port']
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   256
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   257
                forward = (local_node, local_port, remote_port, remote_node)
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   258
                reverse = (remote_node, remote_port, local_port, local_node)
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   259
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   260
                if reverse not in links :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   261
                    links[forward] = (local_untag, local_tagged, None)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   262
                else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   263
                    remote_untag, remote_tagged, _ = links[reverse]
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   264
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   265
                    # merge
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   266
                    if remote_untag != local_untag :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   267
                        log.warning("%s:%s untag %s <=> %s untag %s:%s",
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   268
                                host, local_port, local_untag,
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   269
                                remote_untag, remote_node, remote_port
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   270
                        )
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   271
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   272
                    if remote_tagged != local_tagged :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   273
                        log.warning("%s:%s tagged %s <-> %s tagged %s:%s",
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   274
                                host, local_port, ':'.join(str(x) for x in sorted(local_tagged)),
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   275
                                ':'.join(str(x) for x in sorted(remote_tagged)), remote_node, remote_port
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   276
                        )
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   277
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   278
                    links[reverse] = (remote_untag, remote_tagged, local_untag)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   279
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   280
    return nodes, links
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   281
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   282
class GraphVlans (object) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   283
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   284
        Maintain vlan -> dot style/color mappings
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   285
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   286
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   287
    SERIES = 'paired12'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   288
    NONE = 'black'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   289
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   290
    def __init__ (self, vlans=None) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   291
        if vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   292
            self.vlans = dict(vlans)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   293
        else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   294
            self.vlans = { }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   295
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   296
    def color (self, vlan) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   297
        if vlan in self.vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   298
            return self.vlans[vlan]
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   299
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   300
        # alloc
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   301
        color = '/{series}/{index}'.format(series=self.SERIES, index=len(self.vlans) + 1)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   302
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   303
        self.vlans[vlan] = color
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   304
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   305
        return color
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   306
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   307
def dot_quote (value) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   308
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   309
        Quote a dot value.
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   310
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   311
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   312
    return '"{value}"'.format(value=value)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   313
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   314
def dot (*line, **attrs) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   315
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   316
        Build dot-syntax:
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   317
            *line {
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   318
                *line [**attrs];
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   319
            }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   320
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   321
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   322
    if line and attrs :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   323
        return ''.join(('\t', ' '.join(str(x) for x in line), ' [',
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   324
            ', '.join('{name}="{value}"'.format(name=name, value=value) for name, value in attrs.iteritems()),
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   325
        ']', ';'))
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   326
    elif line :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   327
        return ' '.join(line) + ' {'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   328
    else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   329
        return '}'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   330
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   331
def build_dot (options, nodes, links, type='digraph', vlans=None) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   332
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   333
        Construct a dot description of the given node/links graph.
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   334
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   335
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   336
    if vlans is True :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   337
        vlans = { }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   338
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   339
    yield dot(type, 'verkko')
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   340
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   341
    # defaults
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   342
    yield dot('graph',
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   343
            # XXX: breaks multi-edges?
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   344
            #splines     = 'true',
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   345
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   346
            sep             = '+25,25',
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   347
            overlap         = 'scalexy',
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   348
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   349
            # only applies to loops
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   350
            nodesep     = 0.5,
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   351
    )
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   352
    yield dot('edge',
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   353
        labeldistance   = 3.0,
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   354
        penwidth        = 2.0,
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   355
    )
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   356
    yield dot('node',
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   357
        fontsize        = 18,
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   358
    )
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   359
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   360
    # nodes
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   361
    for node, node_label in nodes.iteritems() :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   362
        yield dot(dot_quote(node), label=node_label)
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   363
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   364
    # links
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   365
    for (local, local_port, remote_port, remote), (local_untag, tagged, remote_untag) in links.iteritems() :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   366
        if vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   367
            head_color = vlans.color(local_untag) if local_untag else None
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   368
            tail_color = vlans.color(remote_untag) if remote_untag else None
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   369
            line_colors = [vlans.color(tag) for tag in sorted(tagged)]
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   370
        else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   371
            head_color = GraphVlans.NONE if local_untag else None
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   372
            tail_color = GraphVlans.NONE if remote_untag else None
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   373
            line_colors = []
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   374
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   375
        if head_color and tail_color :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   376
            dir = 'both'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   377
            colors = [head_color, tail_color] + line_colors
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   378
        elif head_color :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   379
            dir = 'forward'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   380
            colors = [head_color] + line_colors
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   381
        elif tail_color :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   382
            dir = 'back'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   383
            colors = [vlans.NONE, tail_color] + line_colors
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   384
        else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   385
            dir = 'none'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   386
            colors = line_colors
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   387
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   388
        yield dot(dot_quote(local), '->', dot_quote(remote),
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   389
            taillabel   = local_port,
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   390
            headlabel   = remote_port,
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   391
            dir         = dir,
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   392
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   393
            fillcolor   = 'black',
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   394
            color       = ':'.join(colors),
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   395
        )
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   396
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   397
    yield dot()
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   398
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   399
def apply_dot (options, file, dot) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   400
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   401
        Output dot file for given graphbits
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   402
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   403
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   404
    for line in dot :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   405
        file.write(line + '\n')
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   406
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   407
def main (argv) :
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   408
    """
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   409
        Graph network
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   410
    """
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   411
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   412
    parser = optparse.OptionParser(main.__doc__)
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   413
    parser.add_option_group(pvl.args.parser(parser))
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   414
    parser.add_option_group(pvl.hosts.optparser(parser))
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   415
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   416
    parser.add_option('--snmp-data', metavar='FILE', default=None,
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   417
            help="Load snmp data from FILE")
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   418
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   419
    parser.add_option('--graph-dot', metavar='FILE',
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   420
            help="Output .dot graph data to file")
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   421
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   422
    parser.add_option('--graph-vlans', action='store_true', dest='graph_vlans', 
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   423
            help="Graph all VLANs")
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   424
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   425
    parser.add_option('--no-vlans', action='store_false', dest='graph_vlans',
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   426
            help="Do not color VLANs")
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   427
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   428
    # input
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   429
    options, args = parser.parse_args(argv[1:])
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   430
    pvl.args.apply(options)
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   431
    
408
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   432
    # load hosts
32b7a0f2e7dc pvl.hosts-graph: update for new hostname format, map non-snmp lldp hosts by Host.ethernet
Tero Marttila <terom@paivola.fi>
parents: 406
diff changeset
   433
    hosts = list(pvl.hosts.apply(options, args))
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   434
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   435
    # load raw snmp data
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   436
    snmp = load_snmp_data(options, options.snmp_data, hosts)
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   437
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   438
    # process data into graph
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   439
    nodes, links = build_graph(snmp, hosts)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   440
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   441
    # process graph into dot
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   442
    if options.graph_vlans is False :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   443
        graph_vlans = None
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   444
    else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   445
        graph_vlans = GraphVlans()
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   446
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   447
    if options.graph_dot :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   448
        # process to dot
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   449
        dot = build_dot(options, nodes, links, vlans=graph_vlans)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   450
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   451
        # write out
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   452
        apply_dot(options, open(options.graph_dot, 'w'), dot)
399
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   453
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   454
    return 0
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   455
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   456
if __name__ == '__main__':
aadf76a05ec1 :pvl.hosts-lldp: split into pvl.hosts-snmp to gather data, and pvl.hosts-graph to process/graph it
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   457
    pvl.args.main(main)