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

# Source Code for Module Products.ZenStatus.PingJob

      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__ = """PingJob
     12  Class that contains the information about pinging an individual IP address.
     13  """
     14
     15  from math import fsum, sqrt, pow
     16  import logging
     17  log = logging.getLogger("zen.PingJob")
     18  import socket
     19
     20  from twisted.internet import defer
     21
     22
     23 -class PingJob(object):
     24      """
     25      Class representing a single target to be pinged.
     26      """
     27 -    def __init__(self, ipaddr, hostname="", status=0,
     28                   ttl=60, maxtries=2, sampleSize=1,
     29                   iface=''):
     30          self.ipaddr = ipaddr
     31          self.iface = iface
     32          self.hostname = hostname
     33          self._ttl = ttl
     34          self.status = status
     35          self.maxtries = maxtries
     36          self.sampleSize = sampleSize
     37          self.points = []  # For storing datapoints
     38
     39          self.family, self._sockaddr = self._getaddrinfo()
     40          self.address = self._sockaddr[0]
     41          self.ipVersion = 4 if self.family == socket.AF_INET else 6
     42          self.data_size = 56
     43
     44          self.reset()
     45
     46 -    def _getaddrinfo(self):
     47          family = socket.AF_UNSPEC
     48          for info in socket.getaddrinfo(self.ipaddr, None, socket.AF_UNSPEC):
     49              family = info[0]
     50              sockaddr = info[-1]
     51              if family in (socket.AF_INET, socket.AF_INET6):
     52                  return family, sockaddr
     53
     54          raise StandardError("Could not resolve IP address '%s' on %s for family %s" % (
     55                              self.ipaddr, self.hostname, family))
     56
     57 -    def reset(self):
     58          self.deferred = defer.Deferred()
     59          self.start = 0
     60          self.sent = 0
     61          self.rcvCount = 0
     62          self.loss = 0
     63          self.message = ""
     64          self.severity = 5
     65          self.inprocess = False
     66
     67          self.results = []
     68          self.rtt = 0
     69          self.rtt_max = 0.0
     70          self.rtt_min = 0.0
     71          self.rtt_avg = 0.0
     72          self.rtt_stddev = 0.0
     73          self.rtt_losspct = 0.0
     74
     75          self._lastSequenceNumber = 0
     76
     77 -    def calculateStatistics(self):
     78          n = len(self.results)
     79          if n == 0:
     80              return
     81
     82          elif n == 1:
     83              self.rtt_avg = self.rtt
     84              self.rtt_max = self.rtt
     85              self.rtt_min = self.rtt
     86              self.rtt_stddev = 0.0
     87              return
     88
     89          total = fsum(self.results)
     90          self.rtt_avg = total / n
     91          self.rtt_stddev = self.stdDev(self.rtt_avg)
     92
     93          self.rtt_min = min(self.results)
     94          self.rtt_max = max(self.results)
     95
     96          self.rtt_losspct = ((self.sent - self.loss) / self.sent) * 100
     97
     98 -    def stdDev(self, avg):
     99          """
    100          Calculate the sample standard deviation.
    101          """
    102          n = len(self.results)
    103          if n < 1:
    104              return 0
    105          # sum of squared differences from the average
    106          total = fsum( map(lambda x: pow(x - avg, 2), self.results) )
    107          # Bessel's correction uses n - 1 rather than n
    108          return sqrt( total / (n - 1) )
    109
    110 -    def pingArgs(self):
    111          self._lastSequenceNumber += 1
    112          echo_kwargs = dict(
    113                             sequence=self._lastSequenceNumber,
    114                             data_size=self.data_size,
    115                             )
    116          socket_kwargs = dict(ttl=self._ttl,)
    117          return self.family, self._sockaddr, echo_kwargs, socket_kwargs
    118
    119 -    def __str__(self):
    120          return "%s %s" % (self.hostname, self.ipaddr)
    121