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

# Source Code for Module Products.ZenRRD.ComponentCommandParser

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2008, 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  from Products.ZenRRD.CommandParser import CommandParser
    15  from Products.ZenUtils.Utils import prepId as globalPrepId
    16  import re
    17  from pprint import pformat
    18  import logging
    19
    20  log = logging.getLogger("zen.ComponentCommandParser")
    21
    22 -class ComponentCommandParser(CommandParser):
    23
    24      componentSplit = '\n'
    25
    26      componentScanner = ''
    27
    28      scanners = ()
    29
    30      componentScanValue = 'id'
    31
    32 -    def prepId(self, id, subchar='_'):
    33          return globalPrepId(id, subchar)
    34
    35 -    def dataForParser(self, context, dp):
    36          return dict(componentScanValue = getattr(context, self.componentScanValue))
    37
    38 -    def processResults(self, cmd, result):
    39
    40          # Map datapoints by data you can find in the command output
    41          ifs = {}
    42          for dp in cmd.points:
    43              points = ifs.setdefault(dp.data['componentScanValue'], {})
    44              points[dp.id] = dp
    45
    46          # split data into component blocks
    47          parts = cmd.result.output.split(self.componentSplit)
    48
    49          for part in parts:
    50              # find the component match
    51              match = re.search(self.componentScanner, part)
    52              if not match: continue
    53              component = match.groupdict()['component'].strip()
    54              if self.componentScanValue == 'id': component = self.prepId(component)
    55              points = ifs.get(component, None)
    56              if not points: continue
    57
    58              # find any datapoints
    59              for search in self.scanners:
    60                  match = re.search(search, part)
    61                  if match:
    62                      for name, value in match.groupdict().items():
    63                          dp = points.get(name, None)
    64                          if dp is not None:
    65                              if value in ('-', ''): value = 0
    66                              result.values.append( (dp, float(value) ) )
    67
    68          log.debug(pformat(result))
    69          return result
    70