Package Products :: Package ZenRRD :: Package parsers :: Module uptime
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenRRD.parsers.uptime

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