SHARE
TWEET

Ping Reporter

a guest Apr 1st, 2012 232 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import threading, logging, time, locale
  2.  
  3. logging.TRACE = 5
  4. logging.NOTICE = 15
  5. logging.addLevelName(logging.TRACE, 'TRACE')
  6. logging.addLevelName(logging.NOTICE, 'NOTICE')
  7.  
  8. def setup_log(name,level=logging.ERROR):
  9.         log = logging.getLogger(name)
  10.         setattr(log.__class__, 'notice', log_notice)
  11.         setattr(log.__class__, 'trace', log_trace)
  12.         log.setLevel(level)
  13.         return log
  14.  
  15. def start_log(logger,stream=logging.NOTICE,logs=logging.TRACE):
  16.         logger.setLevel(min(stream,logs))
  17.         addStreamHandler(logger,stream)
  18.         addFileHandler(logger,logs)
  19.  
  20. def addFileHandler(log,level=None):
  21.         formatter = logging.Formatter('[%(levelname)-6s] '+'[%s]'%log.name+' %(message)s')
  22.         handler = logging.FileHandler('pingfs.log')
  23.         if level: handler.setLevel(level)
  24.         handler.setFormatter(formatter)
  25.         log.addHandler(handler)
  26.  
  27. def addStreamHandler(log,level=None):
  28.         formatter = logging.Formatter('[%(levelname)s] %(message)s')
  29.         handler = logging.StreamHandler()
  30.         if level: handler.setLevel(level)
  31.         handler.setFormatter(formatter)
  32.         log.addHandler(handler)
  33.  
  34. def log_generic(self, level, msg, *args, **kwargs):
  35.         if self.manager.disable >= level: return
  36.         if level >= self.getEffectiveLevel():
  37.                 apply(self._log, (level,msg,args), kwargs)
  38.  
  39. def log_notice(self, msg, *args, **kwargs):
  40.         log_generic(self,logging.NOTICE,msg,*args,**kwargs)
  41.  
  42. def log_trace(self, msg, *args, **kwargs):
  43.         log_generic(self,logging.TRACE,msg,*args,**kwargs)
  44.  
  45. def enableAllLogs(screen=logging.INFO,logs=logging.TRACE):
  46.         import ping_filesystem, ping_fuse
  47.         import ping, ping_disk, ping_server
  48.         #start_log(ping.log,          screen,logs)
  49.         start_log(ping_server.log,    screen,logs)
  50.         start_log(ping_disk.log,      screen,logs)
  51.         start_log(ping_filesystem.log,screen,logs)
  52.         start_log(ping_fuse.log,      screen,logs)
  53.  
  54. import ping
  55. def humanize_bytes(bytes, precision=2):
  56.         # by Doug Latornell
  57.         # http://code.activestate.com/recipes/577081-humanized-representation-of-a-number-of-bytes/
  58.         abbrevs = (
  59.                 (1<<50L, 'PB'),
  60.                 (1<<40L, 'TB'),
  61.                 (1<<30L, 'GB'),
  62.                 (1<<20L, 'MB'),
  63.                 (1<<10L, 'kB'),
  64.                 (1, 'bytes')
  65.         )
  66.         if bytes == 1: return '1 byte'
  67.         for factor, suffix in abbrevs:
  68.                 if bytes >= factor: break
  69.         return '%.*f %s' % (precision, float(bytes) / factor, suffix)
  70.  
  71. class PingReporter(threading.Thread):
  72.         def __init__(self, log, server, interval=90):
  73.                 locale.setlocale(locale.LC_ALL,'')
  74.                 threading.Thread.__init__(self)
  75.                 self.interval = interval
  76.                 self.server = server
  77.                 self.running = 1
  78.                 self.log = log
  79.  
  80.         def stop(self):
  81.                 self.log.info('reporter terminated')
  82.                 self.running = 0
  83.                 self.stats()
  84.  
  85.         def run(self):
  86.                 self.start = time.time()
  87.                 self.log.info('reporter started at %s'%time.ctime())
  88.                 self.last_time = self.start
  89.                 self.last_data = 0
  90.                 while self.running:
  91.                         time.sleep(self.interval)
  92.                         self.stats()
  93.  
  94.         def stats(self):
  95.                 interval_time = time.time() - self.last_time
  96.                 interval_data = ping.ping_bandwidth - self.last_data
  97.                 bw = humanize_bytes(interval_data / interval_time)
  98.                        
  99.                 data = humanize_bytes(ping.ping_bandwidth)
  100.                 num = locale.format('%d', ping.ping_count, True)
  101.                 tstr = time.strftime('%H:%M:%S',time.gmtime(time.time()-self.start))
  102.                 self.log.info('%s (%s pings) -> %s elapsed (%s / second)'%(data,num,tstr,bw))
  103.  
  104.                 self.last_data = ping.ping_bandwidth
  105.                 self.last_time = time.time()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top