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

# Source Code for Module Products.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+) day\(?s?\)?, +)?(?:(?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 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
     67 -def asTimeticks(days=0, hours=0, minutes=0):
     68      return ((days * 24 + hours) * 60 + minutes) * 60 * 100
     69
     70
     71 -def parseSysUpTime(output):
     72      """
     73      Parse the sysUpTime (measured in timeticks) from the output of the uptime
     74      command.
     75      """
     76      uptime = parseUptime(output)
     77
     78      if uptime: sysUpTime = asTimeticks(**uptime)
     79      else     : sysUpTime = None
     80
     81      return sysUpTime
     82
     83
     84 -class uptime(CommandParser):
     85
     86
     87 -    def processResults(self, cmd, result):
     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