Package Products :: Package ZenUtils :: Module DaemonStats
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenUtils.DaemonStats

  1  ############################################################################## 
  2  #  
  3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved. 
  4  #  
  5  # This content is made available according to terms specified in 
  6  # License.zenoss under the directory where your Zenoss product is installed. 
  7  #  
  8  ############################################################################## 
  9   
 10   
 11  import Globals 
 12  from Products.ZenUtils.Utils import zenPath 
 13   
 14  # FIXME dependency from Utils to Thresholds 
 15  from Products.ZenRRD.Thresholds import Thresholds 
 16   
 17  import rrdtool 
 18  import os 
 19  import time 
 20   
 21  import logging 
 22  log = logging.getLogger("zen.DaemonStats") 
 23   
24 -def fullname(partial):
25 return zenPath('perf', partial + '.rrd')
26
27 -class DaemonStats(object):
28 "Utility for a daemon to write out internal performance statistics" 29
30 - def __init__(self):
31 self.name = "" 32 self.monitor = "" 33 self.rrdCreateCommand = "" 34 35 self.thresholds = Thresholds()
36 37
38 - def config(self, name, monitor, thresholds, rrdCreateCommand = None):
39 """Initialize the object. We could do this in __init__, but 40 that would delay creation to after configuration time, which 41 may run asynchronously with collection or heartbeats. By 42 deferring initialization, this object implements the Null 43 Object pattern until the application is ready to start writing 44 real statistics. 45 """ 46 self.name = name 47 self.monitor = monitor 48 if not rrdCreateCommand: 49 from Products.ZenModel.PerformanceConf import PerformanceConf 50 rrdCreateCommand = PerformanceConf.defaultRRDCreateCommand 51 if not isinstance(rrdCreateCommand, basestring): 52 self.createCommand = rrdCreateCommand 53 else: 54 self.createCommand = rrdCreateCommand.split('\n') 55 self.thresholds = Thresholds() 56 self.thresholds.updateList(thresholds)
57 58
59 - def rrdFile(self, type, cycleTime, name, minVal = 'U', maxVal = 'U'):
60 """Create an RRD file if it does not exist. 61 Returns the basename of the rrdFile, suitable for checking thresholds. 62 """ 63 if not self.name: return None 64 base = os.path.join('Daemons', self.name) 65 directory = zenPath('perf', base) 66 if not os.path.exists(directory): 67 os.makedirs(directory) 68 base = os.path.join(base, '%s_%s' % (self.monitor, name)) 69 fileName = fullname(base) 70 if not os.path.exists(fileName): 71 rrdtool.create(fileName, 72 '--step', "%d" % cycleTime, 73 'DS:ds0:%s:%s:%s:%s' % (type, 74 cycleTime * 3, 75 minVal, 76 maxVal), 77 *self.createCommand) 78 return base
79 80
81 - def derive(self, name, cycleTime, value):
82 "Write a DERIVE value, return threshold events" 83 return self.counter(name, cycleTime, value)
84
85 - def counter(self, name, cycleTime, value):
86 "Write a DERIVE(! NOT COUNTER!) value, return threshold events" 87 fileName = self.rrdFile('DERIVE', cycleTime, name, 0) 88 if fileName: 89 full = fullname(fileName) 90 try: 91 rrdtool.update(full, 'N:%s' % int(value)) 92 startStop, names, values = \ 93 rrdtool.fetch(full, 'AVERAGE', 94 '-s', 'now-%d' % (cycleTime*2), 95 '-e', 'now') 96 value = values[0][0] 97 if value is not None: 98 return self.thresholds.check(fileName, time.time(), value) 99 except rrdtool.error, err: 100 log.error('rrdtool reported error %s %s', err, full) 101 return []
102 103
104 - def gauge(self, name, cycleTime, value):
105 "Write a gauge value, return threshold events" 106 fileName = self.rrdFile('GAUGE', cycleTime, name) 107 if fileName: 108 full = fullname(fileName) 109 try: 110 rrdtool.update(full, 'N:%s' % value) 111 except rrdtool.error, err: 112 log.error('rrdtool reported error %s %s', err, full) 113 if value is not None: 114 return self.thresholds.check(fileName, time.time(), value) 115 return []
116