--- a/bin/pvl.rrd-interfaces Thu May 16 01:14:35 2013 +0300
+++ b/bin/pvl.rrd-interfaces Thu Jul 04 03:49:27 2013 +0300
@@ -6,137 +6,15 @@
__version__ = '0.1'
-import shlex
import os
import pvl.args
+import pvl.rrd.hosts
+from pvl.rrd.hosts import hostreverse
import optparse
import logging; log = logging.getLogger('main')
-def hostjoin (*hosts) :
- """
- DNS hostname join.
- """
-
- return '.'.join(hosts)
-
-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.
-
- 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
-
- log.info("scanning %s/<host>.%s/%s/%s-<port>.rrd", options.collectd_rrd, collectd_domain, collectd_plugin, options.collectd_type)
-
- for idx, line in enumerate(file, 1) :
- line = line.rstrip()
-
- if not line :
- continue
-
- # comment?
- if line.startswith('#') :
- continue
-
- # line
- parts = shlex.split(line)
-
- if not line[0].isspace() :
- host = parts.pop(0)
-
- # host-spec?
- if '=' in host :
- collectd_host, interface_host = host.split('=')
- else :
- collectd_host = interface_host = host
-
- # flip from DNS-ordering -> path-ordering
- if options.reverse_host :
- interface_host = hostreverse(interface_host)
-
- # host has domain in collectd?
- if collectd_domain :
- collectd_host = hostjoin(collectd_host, collectd_domain)
-
- if not parts :
- # keep host for following lines
- continue
-
- port = parts.pop(0)
-
- # possibly multiple tags..
- for tag in parts :
- if options.collectd_instance == 'type' :
- type = options.collectd_type + '-' + port
- else :
- type = options.collectd_type
-
- if options.collectd_instance == 'plugin' :
- plugin = collectd_plugin + '-' + port
- else :
- plugin = collectd_plugin
-
- collectd_rrd = os.path.join(options.collectd_rrd, collectd_host, plugin, type) + '.rrd'
-
- if not os.path.exists(collectd_rrd) :
- log.warn("%s: missing collectd rrd: %s", idx, collectd_rrd)
- continue
-
- # out
- interface_rrd = os.path.join(interface_host, tag + '.rrd')
-
- log.debug("%s: %s", interface_rrd, collectd_rrd)
-
- yield collectd_rrd, interface_rrd
-
-def sync_links (options, links, dir) :
- """
- Sync given (collectd, name) symlinks in given dir.
- """
-
- log.info("%s", dir)
-
- for path, name in links :
- link = os.path.join(dir, name)
-
- # sync
- if os.path.exists(link) :
- continue
-
- log.info("%s: %s: %s", dir, name, path)
-
- yield link, path
-
-def apply_links (options, links) :
- """
- Apply given symlinks
- """
-
- for link, path in links :
- linkdir = os.path.dirname(link)
-
- # do
- if not os.path.exists(linkdir) :
- log.warn("mkdir: %s", linkdir)
- os.mkdir(linkdir)
-
- print path
-
- os.symlink(path, link)
-
COLLECTD_RRD = '/var/lib/collectd/rrd'
COLLECTD_PLUGIN = 'interfaces'
COLLECTD_TYPE = 'if_octets'
@@ -201,6 +79,41 @@
return options, args
+def sync_links (options, links, dir) :
+ """
+ Sync given (collectd, name) symlinks in given dir.
+ """
+
+ log.info("%s", dir)
+
+ for path, name in links :
+ link = os.path.join(dir, name)
+
+ # sync
+ if os.path.exists(link) :
+ continue
+
+ log.info("%s: %s: %s", dir, name, path)
+
+ yield link, path
+
+def apply_links (options, links) :
+ """
+ Apply given symlinks
+ """
+
+ for link, path in links :
+ linkdir = os.path.dirname(link)
+
+ # do
+ if not os.path.exists(linkdir) :
+ log.warn("mkdir: %s", linkdir)
+ os.mkdir(linkdir)
+
+ print path
+
+ os.symlink(path, link)
+
def main (argv) :
options, args = parse_argv(argv)
@@ -233,7 +146,7 @@
rrd = basepath
# generate links from spec
- links = list(collectd_interfaces(options, open(txt),
+ links = list(pvl.rrd.hosts.collectd_interfaces(options, open(txt),
collectd_domain = domain,
collectd_plugin = options.collectd_plugin or collectd_plugin or COLLECTD_PLUGIN,
))
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl/rrd/hosts.py Thu Jul 04 03:49:27 2013 +0300
@@ -0,0 +1,117 @@
+"""
+ Handle host-interface mappings for stats.
+"""
+
+import shlex
+import os.path
+
+import logging; log = logging.getLogger('pvl.rrd.hosts')
+
+def hostjoin (*hosts) :
+ """
+ DNS hostname join.
+ """
+
+ return '.'.join(hosts)
+
+def hostreverse (host) :
+ """
+ Reverse hostname.
+ """
+
+ return '.'.join(reversed(host.split('.')))
+
+def load (file, domain=None) :
+ """
+ Parse hosts from file, yielding
+ (host, node, iface, tag)
+
+ file - read host/ifaces from file
+ domain - append given domain to hostname
+ """
+
+ host = None
+
+ for idx, line in enumerate(file, 1) :
+ line = line.rstrip()
+
+ if not line :
+ continue
+
+ # comment?
+ if line.startswith('#') :
+ continue
+
+ # line
+ parts = shlex.split(line)
+
+ if not line[0].isspace() :
+ host = parts.pop(0)
+
+ # host-spec?
+ if '=' in host :
+ host, node = host.split('=')
+ else :
+ node = host
+
+ # host has domain in collectd?
+ if domain :
+ host = hostjoin(host, domain)
+
+ if not parts :
+ # keep host for following lines
+ continue
+
+ iface = parts.pop(0)
+
+ # possibly multiple tags..
+ for tag in parts :
+ yield host, node, iface, tag
+
+def map_interfaces (options, file):
+ """
+ Read (hostname, interface}: (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)
+
+def collectd_interfaces (options, file, collectd_domain, collectd_plugin) :
+ """
+ Read collectd (host, type-instance, name) items, and yield (collectd-rrd, out-rrd) 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)
+
+ 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)
+
+ if options.collectd_instance == 'type' :
+ type = options.collectd_type + '-' + port
+ else :
+ type = options.collectd_type
+
+ if options.collectd_instance == 'plugin' :
+ plugin = collectd_plugin + '-' + port
+ else :
+ plugin = collectd_plugin
+
+ collectd_rrd = os.path.join(options.collectd_rrd, collectd_host, plugin, type) + '.rrd'
+
+ if not os.path.exists(collectd_rrd) :
+ log.warn("%s/%s: missing collectd rrd: %s", collectd_host, port, collectd_rrd)
+ continue
+
+ # out
+ interface_rrd = os.path.join(interface_host, tag + '.rrd')
+
+ log.debug("%s: %s", interface_rrd, collectd_rrd)
+
+ yield collectd_rrd, interface_rrd