###########################################################################
#
# This program is part of Zenoss Core, an open source monitoring platform.
# Copyright (C) 2007, Zenoss Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# For complete information please visit: http://www.zenoss.com/oss/
#
###########################################################################
import socket
import time
import Globals
from Products.ZenUtils.Utils import zenPath
defaultInfile = zenPath("log/origsyslog.log")

from Products.ZenUtils.CmdBase import CmdBase

SYSLOG_PORT = socket.getservbyname('syslog', 'udp')

class ZenSendSyslog(CmdBase):


    def __init__(self):
        CmdBase.__init__(self)
        self.host = socket.gethostbyname(self.options.host)
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  

    def run(self):
        self.log.info("sending messages to host %s", self.host)
        count = 0
        start = time.time()
        def linefilt(line):
            line = line.strip()
            return line and not line.startswith("#")
        lines = filter(linefilt, open(self.options.infile).readlines())
        rate = self.options.rate
        nextsec = time.time() + .9
        for line in lines:
            #self.log.debug(line)
            if count%rate==0 and nextsec>time.time():
                while nextsec>time.time(): pass
                nextsec = time.time() + .9 
            count+=1
            self.sock.sendto(line, (self.host, SYSLOG_PORT))
        sendtime = time.time()-start
        arate = count/sendtime
        self.log.info("sent %d events in %.2f secs rate %.2f ev/sec", 
                        count, time.time()-start, arate)
            
    
    def buildOptions(self):
        CmdBase.buildOptions(self)
        self.parser.add_option('--infile',
            dest='infile', default=defaultInfile,
            help="file from which to draw events")
        
        self.parser.add_option('--rate',
            dest='rate', type="int", default=80,
            help="events per sec to send")

        self.parser.add_option('-H', '--host',
            dest='host', default='localhost',
            help="host to send to")


if __name__ == "__main__":
    sender = ZenSendSyslog()
    sender.run()
