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

# Source Code for Module Products.ZenRRD.Thresholds

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