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

Source Code for Module ZenRRD.parsers.uptime

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2008, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 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   
30 -def parseUptime(output):
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
64 -def asTimeticks(days=0, hours=0, minutes=0):
65 return ((days * 24 + hours) * 60 + minutes) * 60 * 100
66 67
68 -def parseSysUpTime(output):
69 """ 70 Parse the sysUpTime (measured in timeticks) from the output of the uptime 71 command. 72 """ 73 uptime = parseUptime(output) 74 75 if uptime: sysUpTime = asTimeticks(**uptime) 76 else : sysUpTime = None 77 78 return sysUpTime
79 80
81 -class uptime(CommandParser):
82 83
84 - def processResults(self, cmd, result):
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