bin/rrdweb-pmacct
changeset 29 c756e522c9ac
equal deleted inserted replaced
28:89a4d9879171 29:c756e522c9ac
       
     1 #!/usr/bin/env python
       
     2 """
       
     3     Import data from pmacct to RRD
       
     4 """
       
     5 
       
     6 import optparse
       
     7 import logging
       
     8 
       
     9 from rrdweb import pmacct
       
    10 
       
    11 log = logging.getLogger('rrdweb-pmacct')
       
    12 
       
    13 def parse_args (args) :
       
    14     global options
       
    15 
       
    16     parser = optparse.OptionParser(
       
    17         usage   = "%prog [options]"
       
    18     )
       
    19     
       
    20     # generic
       
    21     parser.add_option('-q', "--quiet", help="No output in normal operation",
       
    22         action='store_const', dest="loglvl", const=logging.WARNING,
       
    23     )
       
    24     parser.add_option('-v', "--verbose", help="More output",
       
    25         action='store_const', dest="loglvl", const=logging.INFO,
       
    26     )
       
    27     parser.add_option('-D', "--debug", help="Even more output",
       
    28         action='store_const', dest="loglvl", const=logging.DEBUG,
       
    29     )
       
    30 
       
    31     # paths
       
    32     parser.add_option('-R', "--rrd-dir", default="var/rrd", help="Path to directory containing .rrd files")
       
    33     parser.add_option('-S', "--rrd-step", help="RRD step interval (seconds) for new .rrd's", metavar='STEP')
       
    34 
       
    35     parser.add_option('-I', "--in-sock", help="Path to pmacct host-in.sock", metavar='SOCK')
       
    36     parser.add_option('-O', "--out-sock", help="Path to pmacct host-out.sock", metavar='SOCK')
       
    37     
       
    38 
       
    39     # defaults
       
    40     parser.set_defaults(
       
    41         loglvl      = logging.INFO,
       
    42     )
       
    43     
       
    44     ## parse
       
    45     options, args = parser.parse_args(args)
       
    46     
       
    47     # validate
       
    48     if not (options.in_sock and options.out_sock) :
       
    49         raise Exception("Both --in-sock and --out-sock are required options")
       
    50 
       
    51     ## apply
       
    52     logging.basicConfig(
       
    53         format      = "[%(levelname)8s] %(funcName)20s : %(message)s",
       
    54         level       = options.loglvl,
       
    55     )
       
    56     
       
    57     return args
       
    58 
       
    59 def get_hosts_data () :
       
    60     """
       
    61         Returns the in/out host tables.
       
    62     """
       
    63 
       
    64     log.debug("fetching hosts data from %s + %s", options.in_sock, options.out_sock)
       
    65 
       
    66     # read summaries
       
    67     in_table = list(pmacct.pmacct_summary(options.in_sock))
       
    68     out_table = list(pmacct.pmacct_summary(options.out_sock))
       
    69 
       
    70     # merge into host data
       
    71     hosts = pmacct.host_counters(in_table, out_table)
       
    72     
       
    73     log.debug("got %d in entries + %d out entries -> %d hosts", len(in_table), len(out_table), len(hosts))
       
    74 
       
    75     return hosts.values()
       
    76 
       
    77 def main (args) :
       
    78     # parse
       
    79     args = parse_args(args)
       
    80 
       
    81     # list of Host objects
       
    82     hosts = get_hosts_data()
       
    83     
       
    84     log.debug("full set of host data: %s", hosts)
       
    85     log.info("Updating %d hosts...", len(hosts))
       
    86 
       
    87     # update
       
    88     for host in hosts :
       
    89         log.info("Updating host %s...", host.ip)
       
    90 
       
    91         pmacct.update_host(host, rrd_root=options.rrd_dir, rrd_step=options.rrd_step)
       
    92 
       
    93 if __name__ == '__main__' :
       
    94     from sys import argv
       
    95 
       
    96     main(argv[1:])