Package Products :: Package ZenEvents :: Module zensendsyslog |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenEvents.zensendsyslog

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2007, Zenoss Inc.
     5  #
     6  # This program is free software; you can redistribute it and/or modify it
     7  # under the terms of the GNU General Public License version 2 as published by
     8  # the Free Software Foundation.
     9  #
    10  # For complete information please visit: http://www.zenoss.com/oss/
    11  #
    12  ###########################################################################
    13  import socket
    14  import time
    15  import Globals
    16  from Products.ZenUtils.Utils import zenPath
    17  defaultInfile = zenPath("log/origsyslog.log")
    18
    19  from Products.ZenUtils.CmdBase import CmdBase
    20
    21  SYSLOG_PORT = socket.getservbyname('syslog', 'udp')
    22
    23 -class ZenSendSyslog(CmdBase):
    24
    25
    26 -    def __init__(self):
    27          CmdBase.__init__(self)
    28          self.host = socket.gethostbyname(self.options.host)
    29          self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    30
    31
    32 -    def run(self):
    33          self.log.info("sending messages to host %s", self.host)
    34          count = 0
    35          start = time.time()
    36          def linefilt(line):
    37              line = line.strip()
    38              return line and not line.startswith("#")
    39          lines = filter(linefilt, open(self.options.infile).readlines())
    40          rate = self.options.rate
    41          nextsec = time.time() + .9
    42          for line in lines:
    43              #self.log.debug(line)
    44              if count%rate==0 and nextsec>time.time():
    45                  while nextsec>time.time(): pass
    46                  nextsec = time.time() + .9
    47              count+=1
    48              self.sock.sendto(line, (self.host, SYSLOG_PORT))
    49          sendtime = time.time()-start
    50          arate = count/sendtime
    51          self.log.info("sent %d events in %.2f secs rate %.2f ev/sec",
    52                          count, time.time()-start, arate)
    53
    54
    55 -    def buildOptions(self):
    56          CmdBase.buildOptions(self)
    57          self.parser.add_option('--infile',
    58              dest='infile', default=defaultInfile,
    59              help="file from which to draw events")
    60
    61          self.parser.add_option('--rate',
    62              dest='rate', type="int", default=80,
    63              help="events per sec to send")
    64
    65          self.parser.add_option('-H', '--host',
    66              dest='host', default='localhost',
    67              help="host to send to")
    68
    69
    70  if __name__ == "__main__":
    71      sender = ZenSendSyslog()
    72      sender.run()
    73