1
2
3
4
5
6
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
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
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
69 """Timestamp of when ping was returned (seconds since epoch)."""
70 return self._timestamp
71
72 @property
74 """Address of the host"""
75 return self._address
76
77 @property
79 """traceroute of the host"""
80 return tuple(self._trace)
81
85
88
89 @property
91 """true if host is up, false if host is down"""
92 return self._isUp
93
94 @property
96 """round trip time aka ping time aka rtt; nan if host was down"""
97 return self._rtt
98
99 @property
101 """variance of the rtt; nan if host was down"""
102 return self._mdev * self._mdev
103
104 @property
106 """standard deviation of the rtt; nan if host was down"""
107 return self._mdev
108