1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import re
16 import logging
17
18 from Products.ZenRRD.CommandParser import CommandParser
19
20
21 log = logging.getLogger("zen.zencommand")
22
23
24 UPTIME_PATTERN = re.compile(
25 r"up +(?:(?P<days>\d+) days?, +)?(?:(?P<hours>\d+):)?(?P<minutes>\d+)")
26
27 UPTIME_FORMAT = "uptime: days=%(days)s, hours=%(hours)s, minutes=%(minutes)s"
28
29
31 """
32 Parse the uptime command's output capturing the days, hours and minutes
33 that the system has been up. Returns a dictionary of the captured values.
34
35 >>> UPTIME_FORMAT % parseUptime("up 1 day, 1:42")
36 'uptime: days=1, hours=1, minutes=42'
37
38 >>> UPTIME_FORMAT % parseUptime("up 5 days, 1:42")
39 'uptime: days=5, hours=1, minutes=42'
40
41 >>> UPTIME_FORMAT % parseUptime("up 3 days, 6 min")
42 'uptime: days=3, hours=0, minutes=6'
43
44 >>> UPTIME_FORMAT % parseUptime("up 1:14")
45 'uptime: days=0, hours=1, minutes=14'
46
47 >>> UPTIME_FORMAT % parseUptime("up 4 min")
48 'uptime: days=0, hours=0, minutes=4'
49
50 """
51
52 match = UPTIME_PATTERN.search(output)
53
54 if match:
55 uptime = dict([(k, int(v)) for k, v in match.groupdict(0).items()])
56 log.debug(UPTIME_FORMAT % uptime)
57 else:
58 uptime = None
59 log.debug("uptime: no match")
60
61 return uptime
62
63
65 return ((days * 24 + hours) * 60 + minutes) * 60 * 100
66
67
79
80
82
83
85 """
86 Parse the results of the uptime command to get sysUptime and load
87 averages.
88 """
89 output = cmd.result.output
90
91 dps = dict([(dp.id, dp) for dp in cmd.points])
92
93 if 'sysUpTime' in dps:
94 sysUpTime = parseSysUpTime(output)
95 if sysUpTime:
96 result.values.append((dps['sysUpTime'], sysUpTime))
97
98 match = re.search(r' load averages?: '
99 r'([0-9.]+),? ([0-9.]+),? ([0-9.]+)$',
100 output)
101 if match:
102 for i, dp in enumerate(['laLoadInt1', 'laLoadInt5', 'laLoadInt15']):
103 if dp in dps:
104 result.values.append( (dps[dp], float(match.group(i + 1))) )
105 return result
106