Sar.py

From PeformIQ Upgrade
Revision as of 15:10, 25 February 2008 by PeterHarding (talk | contribs) (New page: =Overview= A Python script to summarize Systems Activity Reporting (sar) data. Intention was to fit this on top of gnuplot or matplotlib to generate graphs ==Script== <pre> #!/usr/bin/...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview

A Python script to summarize Systems Activity Reporting (sar) data. Intention was to fit this on top of gnuplot or matplotlib to generate graphs

Script

#!/usr/bin/env python
#
#  Purpose: Process SAR data
#
#  Copyright (C) Peter Harding, 2003
#                All rights reserved
#
#  $Id:$
#
#---------------------------------------------------------------------

"""
Usage:  sar.py -o <option> -p <prefix> -s <starttime> -e <endtime> -d

  Args:

   -d             - Run in debug mode
   -e             - End time on the graph
   -h             - Help.  Print this syntax message.
   -p <prefix>    - Specify file prefix
   -o <option>    - Specify a sar function, such u, g, p, ...
                    The default is u (CPU).
   -s             - Start time on the graph
   -v             - Run in verbose mode

Examples:

   'sar.py -ou -i20030705'  Process 20030705.sadc

"""

#---------------------------------------------------------------------

import os
import sys
import getopt
import string
import whrandom

#---------------------------------------------------------------------

__version__ = "@(#) 1.0.1"

OPTS        = 'df:he:o:p:s:vV?'

debugFlg    = 0
verboseFlg  = 0

cpu         = 0

range_start = "0:00"
range_end   = "23:59"

start_min   = -1
end_min     = 1440

opt         = ''
prefix      = 'pre'

SADCDIR     = "."
WRKDIR      = "."

datecode    = ''
sadcfile    = ''
inputfile   = ''
csvfile     = ''
plotfile    = ''
psfile      = ''

##### Some stuff #####################################################

def setup_xtics():
   if (( range / 5 ) < 15 ):
      increment = 5
   elif (( range / 10 ) < 15 ):
      increment = 10
   elif (( range / 15 ) < 15 ):
      increment = 15
   elif (( range / 20 ) < 15 ):
      increment = 20
   elif (( range / 30 ) < 15 ):
      increment = 30
   elif (( range / 60 ) < 15 ):
      increment = 60
   elif (( range / 120 ) < 15 ):
      increment = 120

   no = ( range - ( range % increment ) ) / increment

   start = start_time - ( start_time % increment )
   end   = end_time   - ( end_time % increment )

   xtics = "("

   for i in range(start, end, increment):
      hour = i / 60
      min  = i % 60

      str = "'%02d:%02d' %d" % hour, min, i

      xtics = xtics + str

      if ( i != end ):
         xtics = xtics + ","

   xtics = xtics + ")"

#---------------------------------------------------------------------

def parse(filename):
   global cpu

   try:
      f = open(filename, 'r')
   except IOError, msg:
      err(filename + ': cannot open: ' + `msg` + '\n')
      return 1

   lineno = 0

   while 1:
      line = f.readline()

      if not line: break

      x = line.find("%usr")

      if (line.find("%usr") != -1):
         # Start of CPU
         cpu = 1

      lineno += 1

      if '\0' in line:
         # Check for binary files
         err(filename + ': contains null bytes\n')
         f.close()
         return 1

      line = line[:-1]

      if (cpu):
         print"[" + line + "]"

      if (len(line) == 0):
         cpu = 0

      if (lineno > 2000):
         break

   f.close()

   return 0

#---------------------------------------------------------------------

def setup():
   global opt, prefix
   global sadcfile, inputfile, csvfile, plotfile, psfile

   WRKDIR = prefix

   print WRKDIR

   os.chdir(WRKDIR)

   sadcfile   = SADCDIR + "/" + datecode + ".sadc"
   inputfile  = "sar_" + opt + "." + datecode
   csvfile    = datecode + "-" + opt + ".csv"
   plotfile   = datecode + "-" + opt + ".plt"
   psfile     = datecode + "-" + opt + ".ps"

   print psfile

#---------------------------------------------------------------------

def process(str):
   global filename
   global datecode

   datecode = str

   print datecode

   filename = "sar_A." + datecode

   setup()

   print "Processing %s...\n" % inputfile

   cmd = "sar -" + opt + " -f " + sadcfile + " > " + inputfile

   print cmd

   # os.system( cmd )

   parse(filename)

#---------------------------------------------------------------------

def minutes(str):
   t = str.split(":")
   min = (string.atoi(t[0]) * 60) + string.atoi(t[1])
   return min

#---------------------------------------------------------------------

def main():
   global filename
   global debugFlg
   global verboseFlg
   global prefix
   global opt

   try:
      opts, args = getopt.getopt(sys.argv[1:], OPTS)
   except getopt.error, msg:
      print __doc__
      return 1

   filename   = "xx"

   for o, a in opts:
      if o == '-d':
         debugFlg = 1
      elif o == '-o':
         opt = a
      elif o == '-p':
         prefix = a
      elif o == '-f':
         filename = a
         #filename = string.atoi(a)
      elif o == '-h':
         print __doc__
         return 1
      elif o == '-s':
         range_start = minutes(a)
         start_min = a
      elif o == '-e':
         range_end_s = minutes(a)
         end_min = a
      elif o == '-v':
         verboseFlg = 1
      elif o == '-V':
         print "Version: %s" % __version__
         return 1
      elif o == '-?':
         print __doc__
         return 1

   if (debugFlg):
      print ">> Opt    %s" % opt
      print ">> Prefix %s" % prefix

   if args:
      for arg in args:
         print arg
   else:
      pass

   xxx()

#---------------------------------------------------------------------

if __name__ == '__main__' or __name__ == sys.argv[0]:
   sys.exit(main())

#---------------------------------------------------------------------

##### Process the data file ##########################################
# 
# range_start = $opt_s
# range_end = $opt_e
# @t = split( ":", $range_start )
# $start_time = $t[0] * 60 + $t[1]
# 
# @t = split( ":", $range_end )
# $end_time   = $t[0] * 60 + $t[1]
# 
# 
# $before_start_time = 1
# $no                = 0
# 
# @data              = ()
# @time              = ()
# 

"""
Revision History:

     Date     Who   Description
   --------   ---   --------------------------------------------------
   20030920   plh   Initial implementation
   20031002   plh   Cleaned up args in main().  Added '-h', '-?', '-V'

Problems to fix:

To Do:

Issues:

"""