pvl.verkko-rrd-interfaces: change <path>/<domain>-<plugin>.txt semantics for input file names
authorTero Marttila <terom@paivola.fi>
Sun, 27 Jan 2013 15:22:28 +0200
changeset 188 6b3f30fcded7
parent 187 cbf9371a472d
child 189 1ef2f7bf5a03
pvl.verkko-rrd-interfaces: change <path>/<domain>-<plugin>.txt semantics for input file names
bin/pvl.verkko-rrd-interfaces
--- a/bin/pvl.verkko-rrd-interfaces	Sun Jan 27 15:07:04 2013 +0200
+++ b/bin/pvl.verkko-rrd-interfaces	Sun Jan 27 15:22:28 2013 +0200
@@ -21,12 +21,20 @@
 
     return '.'.join(hosts)
 
-def collectd_interfaces (options, file, collectd_domain) :
+def hostreverse (host) :
+    """
+        Reverse hostname.
+    """
+    
+    return '.'.join(reversed(host.split('.')))
+
+def collectd_interfaces (options, file, collectd_domain, collectd_plugin) :
     """
         Read collectd (host, type-instance, name) items, and yield (collectd-rrd, out-rrd) tuples.
-
-            collectd_domain     - append given domain to collectd hosts
-            dir         - output directory
+            
+            file                - read host/ports from file
+            collectd_domain     - append given domain to collectd hostname
+            collectd_plugin     - use given collectd plugin's type-instances
     """
 
     host = None
@@ -69,11 +77,11 @@
         
         # possibly multiple tags..
         for tag in parts :
-            collectd_name = options.collectd_type + '-' + port + '.rrd'
-            collectd_rrd = os.path.join(options.collectd_rrd, collectd_host, options.collectd_plugin, collectd_name)
+            collectd_type = options.collectd_type + '-' + port
+            collectd_rrd = os.path.join(options.collectd_rrd, collectd_host, collectd_plugin, collectd_type) + '.rrd'
 
             if not os.path.exists(collectd_rrd) :
-                log.warn("%s: non-existant rrd: %s", idx, rrd)
+                log.warn("%s: missing collectd rrd: %s", idx, collectd_rrd)
                 continue
 
             # out
@@ -142,8 +150,8 @@
     # options
     parser.add_option('--collectd-rrd',     metavar='PATH',     default=COLLECTD_RRD,
             help="Path to collectd rrd: %default")
-    parser.add_option('--collectd-plugin',  metavar='PLUGIN',   default=COLLECTD_PLUGIN,
-            help="Collectd plugin to use: %default")
+    parser.add_option('--collectd-plugin',  metavar='PLUGIN',   default=None,
+            help="Collectd plugin to use: <input>-<plugin>.txt")
     parser.add_option('--collectd-type',    metavar='TYPE',     default=COLLECTD_TYPE,
             help="Collectd type to use: %default")
     
@@ -177,12 +185,21 @@
     options, args = parse_argv(argv)
     
     for txt in args :
-        # defaults from .txt name
-        base, _ = os.path.splitext(txt)
-        _, basename = os.path.split(base)
+        # <path>/<domain>-<plugin>.txt -> <path>/<domain>-<plugin>
+        basepath, _ = os.path.splitext(txt)
+        
+        # <path>/<domain>-<plugin> -> <path>/<domain>, <plugin>
+        if '-' in basepath :
+            basepath, collectd_plugin = basepath.rsplit('-', 1)
+        else :
+            collectd_plugin = None
+
+        # <path>/<domain> -> <domain>
+        _, basename = os.path.split(basepath)
 
         if options.domain is None :
-            domain = '.'.join(reversed(basename.split('.')))
+            # reverse-order hostname
+            domain = hostreverse(basename)
         else :
             # may be ''
             domain = options.domain
@@ -190,11 +207,12 @@
         # generate links from spec
         links = list(collectd_interfaces(options, open(txt),
             collectd_domain     = domain,
+            collectd_plugin     = options.collectd_plugin or collectd_plugin or COLLECTD_PLUGIN,
         ))
         
         # sync missing links
         links = list(sync_links(options, links,
-            dir     = options.rrd or base,
+            dir     = options.rrd or basepath,
         ))
         
         # verbose