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

Source Code for Module ZenUtils.DaemonStats

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