Performance Statistics Collection for UNIX flavored OS

Introduction:

This post explains how to setup performance counter collection in Unix, Solaris, Linux or HP-UX environment and how to extract the log and present it in a Graphical format with simple ruby program which i have written.

Please download ruby from http://www.ruby-lang.org

The 3 major steps to follow to collect and report performance statistics are as follows:

1.Configure Performance counters
2.Conversion of Performance logs to csv
3.Reporting

1.Configure Performance counters:
90% of the time people are concerned with the memory and cpu statistics, so let’s see how to configure , collect and report memory and CPU statisctis in unix, solaris, linux or HP-UX environment.

Write .sh File:

1.memory statistics
Most of the time people are concerned about the total memory usage rather than breakdown of cache, swap, free, buffer memory etc. hence we can measure the overall memory usage by “free” command available in UNIX flavored OSes.Write a .sh file which will log the free and used memory as follows,

#!/bin/sh
free -m -s1 >> free_mem.log

The above command means the total free and used memory will be collected for every second and stored in the free_mem log file.

2.CPU and other statistics
sometimes people are concerned about knowing everything like cache, swap, free, buffer memory apart from CPU. we can measure all these by “vmstat” command available in UNIX flavored OSes.Write a .sh file which will log these information as follows,

#!/bin/sh
vmstat 1 >> vmstat.log
The above command means the statistics such as

Procs r: The number of processes waiting for run time. b: The number of processes in uninterruptible sleep.

Memory swpd: the amount of virtual memory used. free: the amount of idle memory. buff: the amount of memory used as buffers. cache: the amount of memory used as cache. inact: the amount of inactive memory. (-a option) active: the amount of active memory. (-a option)

Swap si: Amount of memory swapped in from disk (/s). so: Amount of memory swapped to disk (/s).

IO bi: Blocks received from a block device (blocks/s). bo: Blocks sent to a block device (blocks/s).

System in: The number of interrupts per second, including the clock. cs: The number of context switches per second.

CPU These are percentages of total CPU time. us: Time spent running non-kernel code. (user time, including nice time) sy: Time spent running kernel code. (system time) id: Time spent idle. wa: Time spent waiting for IO.

will be collected for every second and stored in the vmstat.log file.

Run the .sh Files during the tests and stop it once the test is done.collect the log files once the test is done for conversion.

2.Conversion of Performance logs to csv:

Convert memory log to CSV:
Execute the below ruby program to convert the log file to the csv file

infile = ARGV[0] || ‘C:\free_mem.log’
freq = ARGV[1] || 1
outfile = ‘C:\Results’
stat = File.stat(infile)
now  = stat.mtime
t=Time.now
lines=[]

File.readlines(infile).each { |line|
    line.chomp
    if line.match(/^-/) then
        data = line.split(/[^-\/\+\w+:$][\s\t]+/)
    temp = ”
    for i in 0 .. (data.length – 1)
        temp = temp + data[i].to_s + ‘,’
    end
    temp[temp.length-1] = ”
    lines << temp
    end
}

lines.each do |row|
  File.open(outfile+t.strftime(“%m%d%Y_%H%M%S”)+”.csv”,”w”) do |the_file|
  the_file.puts “total,used,free”   
      the_file.puts lines                     
    end
  end

Convert vmstat log to CSV:
Execute the below ruby program to convert the log file to the csv file

infile = ARGV[0] || ‘C:\vmstat.log’
freq = ARGV[1] || 1
outfile = ‘C:\Results’
stat = File.stat(infile)
now  = stat.mtime
t=Time.now
lines=[]

File.readlines(infile).each { |line|
    line.chomp
    if line.match(/^\s+\d+/m) then
        now  = now + freq
         date = “#{now.day}/#{now.month}/#{now.year}”
        time = “#{now.hour}:#{now.min}:#{now.sec}”
        data = line.gsub(/^\s+/m,”").gsub(/[\s\t]+/m,”,”).gsub(/,$/m,”")
    lines<<”#{date},#{time},#{data}”
    end
}

lines.each do |row|
  File.open(outfile+t.strftime(“%m%d%Y_%H%M%S”)+”.csv”,”w”) do |the_file|
  the_file.puts “date,time,r,b,swpd,free,buff,cache,si,so,bi,bo,in,cs,us,sy,id,wa,st”    
      the_file.puts lines                      
    end
  end 

3.Reporting:
Open the csv file in which the necessary performance counters data has been collected and generate graph using excel Chart Wizard feature as below CPU sample.
Thanks for Reading the blog. Hope it was useful for you. Please don’t hesitate to post comments if you got any clarification/feedback. Thanks a lot.

Advertisement

About Raja Ganapathy
Simple, Down to earth, cool, fun loving individual who has aspirations to be an entrepreneur

One Response to Performance Statistics Collection for UNIX flavored OS

  1. shenmin says:

    It’s a superduper blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.