pvl.hosts-snmp: really commit new output syntax with attrs, change semantic structure
authorTero Marttila <terom@paivola.fi>
Wed, 19 Mar 2014 00:49:43 +0200
changeset 401 e57c200f3e26
parent 400 41dd2a867e0a
child 402 3b6fb4ae56fb
pvl.hosts-snmp: really commit new output syntax with attrs, change semantic structure
bin/pvl.hosts-snmp
--- a/bin/pvl.hosts-snmp	Tue Mar 18 23:53:16 2014 +0200
+++ b/bin/pvl.hosts-snmp	Wed Mar 19 00:49:43 2014 +0200
@@ -205,26 +205,38 @@
     if options.scan or options.scan_lldp :
         # discover node/port graph
         for host, local, port, remote in apply_hosts_lldp(options, hosts) :
-            yield host, ('lldp', ), set((local['chassis'], ))
-
-            yield host, ('port', port['port'], 'lldp', remote['chassis'], 'port', remote['port']), None
+            # XXX: duplicates galore
+            yield host, ('lldp', 'local'), {
+                    'chassis':  local['chassis'],
+                    'sys_name': local['sys_name'],
+            }
+            
+            # XXX: duplicates galore
+            yield host, ('lldp', 'port', port['port_id'], 'local'), {
+                    'port':     port['port'],
+            }
+        
+            yield host, ('lldp', 'port', port['port_id'], 'remote', remote['chassis']), {
+                    'sys_name':     remote['sys_name'],
+                    'port':         remote['port'],
+            }
     
     if options.scan or options.scan_vlan :
         # discover vlan ports
         for host, port, untag, tagged in apply_hosts_vlan(options, hosts) :
             if untag :
-                yield host, ('port', port, 'untagged', untag), None
+                yield host, ('vlan', untag, 'untagged'), set((port, ))
 
-            if tagged :
-                yield host, ('port', port, 'tagged'), set(tagged)
+            for tag in tagged :
+                yield host, ('vlan', tag, 'tagged'), set((port, ))
     
     if options.scan or options.scan_bridge :
         # discover edge nodes
         for host, vlan, port, ethers in apply_hosts_bridge(options, hosts) :
             if vlan :
-                yield host, ('port', port, 'bridge', 'vlan', vlan), set(ethers)
+                yield host, ('vlan', vlan, 'bridge', port), set(ethers)
             else :
-                yield host, ('port', port, 'bridge'), set(ethers)
+                yield host, ('bridge', port), set(ethers)
 
 def main (argv) :
     """
@@ -251,24 +263,36 @@
     data = collections.defaultdict(dict)
 
     for host, attr, values in apply_hosts(options, hosts) :
-        log.info("[%s] %s%s", host, ' '.join(str(a) for a in attr), (': ' + ' '.join(str(value) for value in values)) if values else '')
-        
-        if values is None :
-            data[host][attr] = None
+        if isinstance(values, set) :
+            log.info("[%s] %s + %s", host, attr, values)
+
+            data[host].setdefault(attr, set()).update(values)
+
+        elif isinstance(values, dict) :
+            log.info("[%s] %s = %s", host, attr, values)
+
+            data[host][attr] = values
+
         else :
-            # merge
-            data[host].setdefault(attr, set()).update(values)
+            raise Exception("No value for [%s] %s" % (host, attr))
 
     # output
     for host, attrs in sorted(data.items()) :
         print "{host}".format(host=host)
 
         for attr, value in sorted(attrs.items()) :
-            print "\t{attr}".format(attr=' '.join(str(a) for a in attr))
+            print "\t{attr}".format(attr=' '.join(str(a) for a in attr)),
 
-            if value :
+            if isinstance(value, set) :
+                print
                 for v in sorted(value) :
                     print "\t\t{value}".format(value=v)
+            elif isinstance(value, dict) :
+                for k, v in sorted(value.items()) :
+                    print "\t{key} {value}".format(key=k, value=v),
+                print
+            else :
+                raise Exception("[%s] %s: invalid value: %s" % (host, attr, value))
 
     return 0