Package Products :: Package ZenRRD :: Module Thresholds |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRRD.Thresholds

     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
    14  import logging
    15  log = logging.getLogger('zen.thresholds')
    16
    17 -class Thresholds:
    18      "Class for holding multiple Thresholds, used in most collectors"
    19
    20 -    def __init__(self):
    21          self.byKey = {}
    22          self.byFilename = {}
    23          self.byDevice = {}
    24
    25 -    def remove(self, threshold):
    26          d = self.byDevice.get(threshold.context().deviceName, None)
    27          if d and threshold.key() in d:
    28              del d[threshold.key()]
    29          doomed = self.byKey.get(threshold.key(), None)
    30          if doomed:
    31              del self.byKey[doomed.key()]
    32              ctx = doomed.context()
    33              for dp in doomed.dataPoints():
    34                  lst = self.byFilename[ctx.fileKey(dp)]
    35                  if (doomed, dp) in lst:
    36                      lst.remove( (doomed, dp) )
    37                  if not lst:
    38                      del self.byFilename[ctx.fileKey(dp)]
    39          return doomed
    40
    41 -    def add(self, threshold):
    42          self.byKey[threshold.key()] = threshold
    43          d = self.byDevice.setdefault(threshold.context().deviceName, {})
    44          d[threshold.key()] = threshold
    45          ctx = threshold.context()
    46          for dp in threshold.dataPoints():
    47              self.byFilename.setdefault(ctx.fileKey(dp), []).append((threshold, dp))
    48
    49 -    def update(self, threshold):
    50          "Store a threshold instance for future computation"
    51          log.debug("Updating threshold %r", threshold.key())
    52          doomed = self.remove(threshold)
    53          if doomed:
    54              threshold.count = doomed.count
    55          self.add(threshold)
    56
    57 -    def updateList(self, thresholds):
    58          "Store a threshold instance for future computation"
    59          for threshold in thresholds:
    60              self.update(threshold)
    61
    62 -    def thresholdsForDevice(self, device):
    63          return self.byDevice.get(device, {}).values()
    64
    65 -    def updateForDevice(self, device, thresholds):
    66          "Store a threshold instance for future computation"
    67          doomed = dict([(d.key(), d) for d in self.thresholdsForDevice(device)])
    68          self.updateList(thresholds)
    69          for threshold in thresholds:
    70              if threshold.key() in doomed:
    71                  del doomed[threshold.key()]
    72          for d in doomed.values():
    73              self.remove(d)
    74
    75 -    def check(self, filename, timeAt, value):
    76          "Check a given threshold based on an updated value"
    77          result = []
    78          if filename in self.byFilename:
    79              log.debug("Checking value %s on %s", value, filename)
    80              for t, dp in self.byFilename[filename]:
    81                  result += t.checkRaw(dp, timeAt, value)
    82          return result
    83
    84 -def test():
    85      pass
    86
    87  if __name__ == '__main__':
    88      test()
    89