Package Products :: Package ZenStatus :: Package ping :: Module PingResult |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenStatus.ping.PingResult

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2011, 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  __doc__ = """PingResult
     12
     13  Utilities to parse nmap output and represent results.
     14  """
     15
     16  import logging
     17  log = logging.getLogger("zen.zenping.cmdping.PingResult")
     18
     19  _STATE_TO_STRING_MAP = { True: 'up', False: 'down'}
     20  _NAN = float('nan')
     21
     22  import Globals
     23  from Products.ZenStatus import TraceHop, interfaces
     24  from zope import interface
     25  import time
     26
     27 -class PingResult(object):
     28      """
     29      Model of an ping/traceroute result.
     30      """
     31      interface.implements(interfaces.IPingResult)
     32
     33 -    def __init__(self, ip, exitCode, pingOutput, timestamp=None,):
     34          """Ping output container."""
     35          if timestamp is None:
     36              self._timestamp = time.time()
     37          else:
     38              self._timestamp = timestamp
     39          self._address = ip
     40          self._trace = tuple()
     41          if exitCode:
     42              self._isUp = False
     43          else:
     44              self._isUp = True
     45          self._rtt = _NAN
     46          self._mdev = _NAN
     47          if exitCode == 0:
     48              parsedOutput = self._parse(pingOutput)
     49              if parsedOutput is not None:
     50                  (min, avg, max, mdev) = parsedOutput
     51                  self._rtt = avg
     52                  self._mdev = mdev
     53
     54 -    def _parse(self, output):
     55          try:
     56              lastLine = output.splitlines(False)[-1]
     57              valueList = lastLine.split('=')[1].strip()
     58              min, avg, max, mdev = [float(x) for x in valueList.split()[0].split('/')]
     59          except Exception as ex:
     60              log.exception(ex)
     61              log.error("Could not parse ping output.")
     62              log.debug("Ping output %s: %s", self._address, output)
     63              return None
     64          return (min, avg, max, mdev)
     65
     66
     67      @property
     68 -    def timestamp(self):
     69          """Timestamp of when ping was returned (seconds since epoch)."""
     70          return self._timestamp
     71
     72      @property
     73 -    def address(self):
     74          """Address of the host"""
     75          return self._address
     76
     77      @property
     78 -    def trace(self):
     79          """traceroute of the host"""
     80          return tuple(self._trace)
     81
     82 -    def getStatusString(self):
     83          """status string: up or down"""
     84          return _STATE_TO_STRING_MAP[self._isUp]
     85
     86 -    def __repr__(self):
     87          return "PingResult [%s, %s]" % (self._address, self.getStatusString())
     88
     89      @property
     90 -    def isUp(self):
     91          """true if host is up, false if host is down"""
     92          return self._isUp
     93
     94      @property
     95 -    def rtt(self):
     96          """round trip time aka ping time aka rtt; nan if host was down"""
     97          return self._rtt
     98
     99      @property
    100 -    def variance(self):
    101          """variance of the rtt; nan if host was down"""
    102          return self._mdev * self._mdev
    103
    104      @property
    105 -    def stdDeviation(self):
    106          """standard deviation of the rtt; nan if host was down"""
    107          return self._mdev
    108