bin/rrdweb-pmacct
author Tero Marttila <terom@fixme.fi>
Sun, 23 Jan 2011 13:50:13 +0200
changeset 29 c756e522c9ac
permissions -rwxr-xr-x
pmacct: load pmacct data to rrd
#!/usr/bin/env python
"""
    Import data from pmacct to RRD
"""

import optparse
import logging

from rrdweb import pmacct

log = logging.getLogger('rrdweb-pmacct')

def parse_args (args) :
    global options

    parser = optparse.OptionParser(
        usage   = "%prog [options]"
    )
    
    # generic
    parser.add_option('-q', "--quiet", help="No output in normal operation",
        action='store_const', dest="loglvl", const=logging.WARNING,
    )
    parser.add_option('-v', "--verbose", help="More output",
        action='store_const', dest="loglvl", const=logging.INFO,
    )
    parser.add_option('-D', "--debug", help="Even more output",
        action='store_const', dest="loglvl", const=logging.DEBUG,
    )

    # paths
    parser.add_option('-R', "--rrd-dir", default="var/rrd", help="Path to directory containing .rrd files")
    parser.add_option('-S', "--rrd-step", help="RRD step interval (seconds) for new .rrd's", metavar='STEP')

    parser.add_option('-I', "--in-sock", help="Path to pmacct host-in.sock", metavar='SOCK')
    parser.add_option('-O', "--out-sock", help="Path to pmacct host-out.sock", metavar='SOCK')
    

    # defaults
    parser.set_defaults(
        loglvl      = logging.INFO,
    )
    
    ## parse
    options, args = parser.parse_args(args)
    
    # validate
    if not (options.in_sock and options.out_sock) :
        raise Exception("Both --in-sock and --out-sock are required options")

    ## apply
    logging.basicConfig(
        format      = "[%(levelname)8s] %(funcName)20s : %(message)s",
        level       = options.loglvl,
    )
    
    return args

def get_hosts_data () :
    """
        Returns the in/out host tables.
    """

    log.debug("fetching hosts data from %s + %s", options.in_sock, options.out_sock)

    # read summaries
    in_table = list(pmacct.pmacct_summary(options.in_sock))
    out_table = list(pmacct.pmacct_summary(options.out_sock))

    # merge into host data
    hosts = pmacct.host_counters(in_table, out_table)
    
    log.debug("got %d in entries + %d out entries -> %d hosts", len(in_table), len(out_table), len(hosts))

    return hosts.values()

def main (args) :
    # parse
    args = parse_args(args)

    # list of Host objects
    hosts = get_hosts_data()
    
    log.debug("full set of host data: %s", hosts)
    log.info("Updating %d hosts...", len(hosts))

    # update
    for host in hosts :
        log.info("Updating host %s...", host.ip)

        pmacct.update_host(host, rrd_root=options.rrd_dir, rrd_step=options.rrd_step)

if __name__ == '__main__' :
    from sys import argv

    main(argv[1:])