Thursday, July 8, 2010

Inserting sar output to database table

This shell commands shows how to insert couple of sar commands' output into a database table.

Key
" - double quote
' - single quote


First command is
sar
Linux 2.6.18-194.el5 07/08/2010

12:00:01 AM CPU %user %nice %system %iowait %steal %idle
12:10:01 AM all 1.06 0.00 0.43 1.29 0.00 97.21
12:20:01 AM all 0.34 0.00 0.43 1.04 0.00 98.19
12:30:01 AM all 0.35 0.00 0.46 1.15 0.00 98.05
12:40:01 AM all 0.34 0.00 0.46 1.11 0.00 98.09
12:50:01 AM all 0.33 0.00 0.49 1.06 0.00 98.13
01:00:01 AM all 0.34 0.00 0.46 1.09 0.00 98.11
01:10:01 AM all 0.37 0.00 0.48 1.09 0.00 98.06
01:20:01 AM all 0.33 0.00 0.49 1.12 0.00 98.06
Create the table with
Create Table Sar_Cpu (Collection_Time Timestamp (6),CPU_PCT NUMBER(5,2),SYSTEM_PCT NUMBER(5,2),IOWAIT_PCT NUMBER(5,2),IDLE_PCT  Number(5,2));
Execute the command to insert data into the table
sar | grep -v "^[a-Z]\|CPU" | awk '{print "insert into sar_cpu values (to_timestamp(""'\''"$1"'\''"",
""
'\''HH24:MI:SS""'\''""),"$4","$6","$7","$9");"}' | sqlplus -s asanga/***

Next is sar -q
Linux 2.6.18-194.el5       07/08/2010

12:00:01 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
12:10:01 AM 1 598 0.20 0.10 0.09
12:20:01 AM 0 596 0.21 0.27 0.16
12:30:01 AM 0 595 0.39 0.27 0.20
12:40:01 AM 0 596 0.66 0.40 0.29
12:50:01 AM 0 596 0.32 0.29 0.27
01:00:01 AM 0 595 0.01 0.08 0.16
01:10:01 AM 1 595 0.14 0.25 0.20
01:20:01 AM 0 596 0.03 0.10 0.14
Create the table to insert the data
create table sar_loadavg (sample_time timestamp,run_queue number(2),process_lis number(4),load_avg_1 number(5,2),load_avg_5 number(5,2),load_avg_15 number(5,2));
Run the shell command
sar -q | grep -v "^[a-Z]\|CPU" | awk '{print "insert into sar_loadavg values (to_timestamp(""'\''"$1"'\''"",
""
'\''HH24:MI:SS""'\''""),"$3","$4","$5","$6","$7");"}' | sqlplus -s asanga/***
Choose something similar to ADMon to plot a graph with the inserted data