bin/pvl.hosts-graph
author Tero Marttila <terom@paivola.fi>
Mon, 31 Mar 2014 14:27:39 +0300
changeset 406 92a4de88b86f
parent 405 97b436f9363a
child 408 32b7a0f2e7dc
permissions -rwxr-xr-x
pvl.verkko-graph: name nodes by host
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 = { }
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
   109
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
   110
    for host_name, attr, value in _load_snmp_data(options, file) :
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   111
        host = hosts.get(host_name)
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   112
        
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   113
        if value :
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   114
            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
   115
        else :
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   116
            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
   117
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
   118
        item = root.setdefault(host, { })
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
   119
        
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
   120
        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
   121
            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
   122
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
        a = attr[-1]
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
   124
        
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   125
        if value is None :
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   126
            pass
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   127
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   128
        elif isinstance(value, set) :
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
   129
            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
   130
404
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   131
        elif isinstance(value, dict) :
78e3d83135ab pvl.hosts-graph: support new syntax, with dict updates
Tero Marttila <terom@paivola.fi>
parents: 402
diff changeset
   132
            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
   133
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
   134
        else :
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   135
            item[a] = value
400
41dd2a867e0a pvl.hosts-graph: parse new attribute-values format
Tero Marttila <terom@paivola.fi>
parents: 399
diff changeset
   136
            
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
   137
    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
   138
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   139
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
   140
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   141
        {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
   142
    """
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   143
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   144
    ports = set()
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   145
    vlans_untagged = { }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   146
    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
   147
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   148
    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
   149
        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
   150
            ports.add(port)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   151
            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
   152
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   153
        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
   154
            ports.add(port)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   155
            vlans_untagged[port] = vlan
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   156
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   157
    for port in ports :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   158
        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
   159
        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
   160
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   161
        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
   162
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   163
        yield port, (untag, tagged)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   164
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   165
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
   166
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   167
        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
   168
            { node: label }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   169
            { (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
   170
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   171
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   172
    nodes = { } # host: label
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   173
    links = { }
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   174
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   175
    hosts_by_lldp = { }
405
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
    # first scan: lldp hosts
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   178
    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
   179
        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
   180
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   181
        if lldp :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   182
            lldp_local = lldp['local']
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   183
            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
   184
            
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   185
            nodes[host] = host.location
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   186
            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
   187
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   188
    # 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
   189
    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
   190
        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
   191
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   192
        if not lldp :
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   193
            continue
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   194
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   195
        local = lldp['local']['chassis']
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   196
        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
   197
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   198
        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
   199
            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
   200
        else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   201
            vlans = None
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   202
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   203
        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
   204
            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
   205
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   206
            for remote, remote_attrs in port_attrs['remote'].iteritems() :
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   207
                if remote in hosts_by_lldp :
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   208
                    remote_node = hosts_by_lldp[remote]
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   209
                else :
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   210
                    remote_node = remote
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   211
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   212
                    # non-snmp lldp host
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   213
                    nodes[remote_node] = remote_attrs['sys_name']
402
3b6fb4ae56fb pvl.hosts-graph: value-less attr handling, compat for apply_graph()
Tero Marttila <terom@paivola.fi>
parents: 400
diff changeset
   214
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   215
                remote_port = remote_attrs['port']
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   216
                
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   217
                # local vlans
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   218
                if vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   219
                    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
   220
                else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   221
                    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
   222
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   223
                if port_vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   224
                    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
   225
                
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   226
                # bidirectional mappings
406
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   227
                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
   228
                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
   229
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   230
                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
   231
                    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
   232
                else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   233
                    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
   234
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   235
                    # merge
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   236
                    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
   237
                        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
   238
                                host, local_port, local_untag,
92a4de88b86f pvl.verkko-graph: name nodes by host
Tero Marttila <terom@paivola.fi>
parents: 405
diff changeset
   239
                                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
   240
                        )
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   241
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   242
                    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
   243
                        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
   244
                                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
   245
                                ':'.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
   246
                        )
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   247
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   248
                    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
   249
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   250
    return nodes, links
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   251
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   252
class GraphVlans (object) :
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
        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
   255
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   256
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   257
    SERIES = 'paired12'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   258
    NONE = 'black'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   259
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   260
    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
   261
        if vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   262
            self.vlans = dict(vlans)
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   263
        else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   264
            self.vlans = { }
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   265
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   266
    def color (self, vlan) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   267
        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
   268
            return self.vlans[vlan]
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   269
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   270
        # alloc
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   271
        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
   272
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   273
        self.vlans[vlan] = color
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   274
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   275
        return color
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
def dot_quote (value) :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   278
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   279
        Quote a dot value.
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   280
    """
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
    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
   283
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   284
def dot (*line, **attrs) :
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
        Build dot-syntax:
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   287
            *line {
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   288
                *line [**attrs];
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
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   291
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   292
    if line and attrs :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   293
        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
   294
            ', '.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
   295
        ']', ';'))
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   296
    elif line :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   297
        return ' '.join(line) + ' {'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   298
    else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   299
        return '}'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   300
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   301
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
   302
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   303
        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
   304
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   305
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   306
    if vlans is True :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   307
        vlans = { }
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
    yield dot(type, 'verkko')
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
    # defaults
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   312
    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
   313
            # 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
   314
            #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
   315
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
   316
            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
   317
            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
   318
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
   319
            # 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
   320
            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
   321
    )
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   322
    yield dot('edge',
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   323
        labeldistance   = 3.0,
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   324
        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
   325
    )
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   326
    yield dot('node',
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   327
        fontsize        = 18,
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   328
    )
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   329
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   330
    # nodes
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   331
    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
   332
        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
   333
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   334
    # links
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   335
    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
   336
        if vlans :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   337
            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
   338
            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
   339
            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
   340
        else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   341
            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
   342
            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
   343
            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
   344
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   345
        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
   346
            dir = 'both'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   347
            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
   348
        elif head_color :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   349
            dir = 'forward'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   350
            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
   351
        elif tail_color :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   352
            dir = 'back'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   353
            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
   354
        else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   355
            dir = 'none'
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   356
            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
   357
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   358
        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
   359
            taillabel   = local_port,
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   360
            headlabel   = remote_port,
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   361
            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
   362
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   363
            fillcolor   = 'black',
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   364
            color       = ':'.join(colors),
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   365
        )
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
   366
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   367
    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
   368
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   369
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
   370
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   371
        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
   372
    """
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   373
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   374
    for line in dot :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   375
        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
   376
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
   377
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
   378
    """
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
   379
        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
   380
    """
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
   381
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
   382
    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
   383
    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
   384
    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
   385
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
   386
    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
   387
            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
   388
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
   389
    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
   390
            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
   391
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   392
    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
   393
            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
   394
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   395
    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
   396
            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
   397
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
    # 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
   399
    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
   400
    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
   401
    
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
   402
    # load hosts for correlation
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
   403
    hosts = dict((str(host), host) for host in pvl.hosts.apply(options, 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
   404
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
   405
    # 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
   406
    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
   407
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   408
    # process data into graph
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   409
    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
   410
    
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   411
    # process graph into dot
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   412
    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
   413
        graph_vlans = None
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   414
    else :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   415
        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
   416
405
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   417
    if options.graph_dot :
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   418
        # process to dot
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   419
        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
   420
        
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   421
        # write out
97b436f9363a pvl.hosts-graph: split out graph-building for dot-building
Tero Marttila <terom@paivola.fi>
parents: 404
diff changeset
   422
        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
   423
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
    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
   425
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
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
   427
    pvl.args.main(main)