pvl.rrd: support a separate @domain for interface target nodes, instead of using a single global domain for both the collectd host and the output rrd symlink
authorTero Marttila <terom@paivola.fi>
Sun, 07 Sep 2014 14:22:47 +0300
changeset 425 4e828d47421a
parent 424 e77e967d59b0
child 426 b2078645456a
pvl.rrd: support a separate @domain for interface target nodes, instead of using a single global domain for both the collectd host and the output rrd symlink
bin/pvl.rrd-interfaces
pvl/rrd/hosts.py
--- a/bin/pvl.rrd-interfaces	Sun Sep 07 14:21:56 2014 +0300
+++ b/bin/pvl.rrd-interfaces	Sun Sep 07 14:22:47 2014 +0300
@@ -79,23 +79,27 @@
 
     return options, args
 
-def sync_links (options, links, dir) :
+def sync_links (options, links, rrddir) :
     """
         Sync given (collectd, name) symlinks in given dir.
     """
 
-    log.info("%s", dir)
+    log.info("%s", rrddir)
 
-    for path, name in links :
-        link = os.path.join(dir, name)
+    for rrdpath, (domain, host, port) in links :
+        linkpath = os.path.join(rrddir,
+                hostreverse(domain) if options.reverse_host else domain,
+                hostreverse(host) if options.reverse_host else host,
+                port + '.rrd'
+        )
 
         # sync
-        if os.path.exists(link) :
+        if os.path.exists(linkpath) and os.path.readlink(linkpath) == rrdpath :
             continue
             
-        log.info("%s: %s: %s", dir, name, path)
+        log.info("%s: %s", linkpath, rrdpath)
         
-        yield link, path
+        yield linkpath, rrdpath
 
 def apply_links (options, links) :
     """
@@ -107,10 +111,8 @@
 
         # do
         if not os.path.exists(linkdir) :
-            log.warn("mkdir: %s", linkdir)
-            os.mkdir(linkdir)
-
-        print path
+            log.warn("makedirs: %s", linkdir)
+            os.makedirs(linkdir)
 
         os.symlink(path, link)
 
@@ -140,10 +142,9 @@
         
         # output dir?
         if options.rrd :
-            rrd_domain = hostreverse(domain) if options.reverse_host else domain
-            rrd = os.path.join(options.rrd, rrd_domain)
+            rrddir = options.rrd
         else :
-            rrd = basepath
+            rrddir = basepath
 
         # generate links from spec
         links = list(pvl.rrd.hosts.collectd_interfaces(options, open(txt),
@@ -151,15 +152,13 @@
             collectd_plugin     = options.collectd_plugin or collectd_plugin or COLLECTD_PLUGIN,
         ))
 
-        if not os.path.exists(rrd) :
-            log.warn("mkdir: %s", rrd)
+        if not os.path.exists(rrddir) :
+            log.error("given --rrd must already exist: %s", rrddir)
+            return 1
 
-            if not options.noop :
-                os.mkdir(rrd)
-        
         # sync missing links
         links = list(sync_links(options, links,
-            dir     = rrd,
+            rrddir  = rrddir,
         ))
         
         # verbose
--- a/pvl/rrd/hosts.py	Sun Sep 07 14:21:56 2014 +0300
+++ b/pvl/rrd/hosts.py	Sun Sep 07 14:22:47 2014 +0300
@@ -53,6 +53,12 @@
                 host, node = host.split('=')
             else :
                 node = host
+
+            if '@' in node :
+                node, node_domain = node.split('@')
+            else:
+                # as for collectd host
+                node_domain = domain
             
             # host has domain in collectd?
             if domain :
@@ -62,56 +68,50 @@
             # keep host for following lines
             continue
 
-        iface = parts.pop(0)
+        instance = parts.pop(0)
  
         # possibly multiple tags..
         for tag in parts :
-            yield host, node, iface, tag
+            yield host, instance, (node_domain, node, tag)
 
 def map_interfaces (options, file):
     """
-        Read (hostname, interface}: (nodename, tag) pairs from file.
+        Read (hostname, instance}: (nodename, tag) pairs from file.
     """
 
-    for host, node, iface, tag in load(file) :
-        log.debug("%s/%s -> %s/%s", host, iface, node, tag)
-        yield (host, iface), (node, tag)
+    for host, instance, node_info in load(file) :
+        log.debug("%s/%s -> %s", host, instance, node_info)
+        yield (host, instance), node_info
 
 def collectd_interfaces (options, file, collectd_domain, collectd_plugin) :
     """
-        Read collectd (host, type-instance, name) items, and yield (collectd-rrd, out-rrd) tuples.
+        Read collectd (host, instance, (domain, node, tag)) items, and yield (collectd-rrd, node_info) tuples.
             
             file                - read host/ports from file
             collectd_domain     - append given domain to collectd hostname
             collectd_plugin     - use given collectd plugin's type-instances
     """
 
-    log.info("scanning %s/<host>.%s/%s/%s-<port>.rrd", options.collectd_rrd, collectd_domain, collectd_plugin, options.collectd_type)
+    log.info("%s/${host}.%s/%s/%s-${instance}.rrd", options.collectd_rrd, collectd_domain, collectd_plugin, options.collectd_type)
 
-    for collectd_host, interface_host, port, tag in load(file, domain=collectd_domain) :
-        # flip from DNS-ordering -> path-ordering
-        if options.reverse_host :
-            interface_host = hostreverse(interface_host)
-
+    for host, instance, node_info in load(file, domain=collectd_domain) :
         if options.collectd_instance == 'type' :
-            type = options.collectd_type + '-' + port
+            type = options.collectd_type + '-' + instance
         else :
             type = options.collectd_type
 
         if options.collectd_instance == 'plugin' :
-            plugin = collectd_plugin + '-' + port
+            plugin = collectd_plugin + '-' + instance
         else :
             plugin = collectd_plugin
 
-        collectd_rrd = os.path.join(options.collectd_rrd, collectd_host, plugin, type) + '.rrd'
+        rrd = os.path.join(options.collectd_rrd, host, plugin, type) + '.rrd'
 
-        if not os.path.exists(collectd_rrd) :
-            log.warn("%s/%s: missing collectd rrd: %s", collectd_host, port, collectd_rrd)
+        if not os.path.exists(rrd) :
+            log.warn("%s/%s: missing collectd rrd: %s", host, instance, rrd)
             continue
 
         # out
-        interface_rrd = os.path.join(interface_host, tag + '.rrd')
+        log.debug("%s: %s", rrd, node_info)
 
-        log.debug("%s: %s", interface_rrd, collectd_rrd)
-
-        yield collectd_rrd, interface_rrd
+        yield rrd, node_info