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+) day\(?s?\)?, +)?(?:(?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 12 day(s), 1:42")
36 'uptime: days=12, hours=1, minutes=42'
37
38 >>> UPTIME_FORMAT % parseUptime("up 1 day, 1:42")
39 'uptime: days=1, hours=1, minutes=42'
40
41 >>> UPTIME_FORMAT % parseUptime("up 5 days, 1:42")
42 'uptime: days=5, hours=1, minutes=42'
43
44 >>> UPTIME_FORMAT % parseUptime("up 3 days, 6 min")
45 'uptime: days=3, hours=0, minutes=6'
46
47 >>> UPTIME_FORMAT % parseUptime("up 1:14")
48 'uptime: days=0, hours=1, minutes=14'
49
50 >>> UPTIME_FORMAT % parseUptime("up 4 min")
51 'uptime: days=0, hours=0, minutes=4'
52
53 """
54
55 match = UPTIME_PATTERN.search(output)
56
57 if match:
58 uptime = dict([(k, int(v)) for k, v in match.groupdict(0).items()])
59 log.debug(UPTIME_FORMAT % uptime)
60 else:
61 uptime = None
62 log.debug("uptime: no match")
63
64 return uptime
65
66
68 return ((days * 24 + hours) * 60 + minutes) * 60 * 100
69
70
82
83
85
86
88 """
89 Parse the results of the uptime command to get sysUptime and load
90 averages.
91 """
92 output = cmd.result.output
93
94 dps = dict([(dp.id, dp) for dp in cmd.points])
95
96 if 'sysUpTime' in dps:
97 sysUpTime = parseSysUpTime(output)
98 if sysUpTime:
99 result.values.append((dps['sysUpTime'], sysUpTime))
100
101 match = re.search(r' load averages?: '
102 r'([0-9.]+),? ([0-9.]+),? ([0-9.]+).*$',
103 output)
104 if match:
105 for i, dp in enumerate(['laLoadInt1', 'laLoadInt5', 'laLoadInt15']):
106 if dp in dps:
107 result.values.append( (dps[dp], float(match.group(i + 1))) )
108 return result
109