#!/usr/bin/env python
# pynfdump-top-talkers
# Copyright (C) 2008 Justin Azoff JAzoff@uamail.albany.edu
#
# This module is released under the MIT License:
# http://www.opensource.org/licenses/mit-license.php

from dateutil.parser import parse
import pynfdump

def show_top(data_dir, remote_host, profile, sources, start_date=None):
    if start_date is None:
        start_date = parse("")
    d=pynfdump.Dumper(data_dir, profile, sources, remote_host)
    d.set_where(start_date)

    print "%-19s %-10s %-10s %-10s" % ('ip','flows','packets','bytes')
    for tt in d.search('',statistics='ip',statistics_order='bytes', limit=5):
        print "%(ip)-19s %(flows)-10s %(packets)-10s %(bytes)-10s" % tt
        for (x,r) in enumerate(d.search('host %s' % tt['ip'], statistics='ip',statistics_order='bytes', limit=4)):
            if x==0: continue
            print "    %(ip)-15s %(flows)-10s %(packets)-10s %(bytes)-10s" % r
            
def main():
    import sys
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-s", "--startdate", dest="start_date", action="store", help="start date")
    parser.add_option("-d", "--datadir",   dest="dir",        action="store", help="data directory")
    parser.add_option("-r", "--remote",    dest="remote",     action="store", help="remote host")
    parser.add_option("-p", "--profile",   dest="profile",    action="store", help="profile", default="live")
    parser.add_option("-o", "--source",    dest="sources",    action="append", help="source")

    (options, args) = parser.parse_args()

    if not (options.dir and options.sources):
        sys.stderr.write("Specify a datadir and a source\n")
        parser.print_help()
        sys.exit(1)

    if 1:
        pass

    show_top(options.dir, options.remote, options.profile, options.sources, options.start_date)

if __name__ == "__main__":
    main()
