monitor.pl
#!/usr/bin/perl ###monintor start#### if($ARGV[0] eq "start"){ print "monitor start!/nwe are monitoring cpu,memory,iostat,prstat!/n"; $flag=1; while($flag==1){ $fname = `date +%F`; $fname=~ s//-//g; chomp($fname); $cpufile=cpu."$fname"; `touch $cpufile`; $time = `date "+%Y%m%d %H:%M:%S"`; open(FILE,">>$cpufile"); $cpu=`sar 2 1|tail -1`; printf FILE ("%s%s/n",$cpu); $iostatfile=iostat."$fname"; `touch $iostatfile`; open(FILE,">>$iostatfile"); print FILE ("$time"); $io=`iostat -xnzD 2 2 |tail -1 `; printf FILE ("%s/n",$io); $topfile=top."$fname"; `touch $iostatfile`; open(FILE,">>$topfile"); print FILE ("$time"); $w=`w|awk '{print $10}' | head -1`; $vmstat=`vmstat 2 2| tail -1 `; printf FILE ("%s%s/n",$w,$vmstat); $prstatfile=prstat."$fname";
`touch $prstatfile`; open(FILE,">>$prstatfile"); print FILE ("$time"); $prstat=`prstat -a 1 1`; printf FILE ("%s%s/n",$prstat); sleep 5; } } #####monitor stop############ elsif($ARGV[0] eq "stop"){ `ps -ef |grep "perl monitor.pl start" > plist`; open(KEY,"plist"); while(<KEY>){ ($aa,$pid) = split(" ",$_); # print $pid; kill (9,$pid); }
} #################### else{ print "if you want to start monitor script, please input start!/n"; print "if you want to stop monitor script, please input stop!/n"; }
memory.sh
#!/usr/bin/env bash sed -f parse_top.sed top20080524 |awk '{printf("%s/t%f/t%s/n", $2, $19/83886.08, $19)}' > memory.log
mem.plt
set xlabel 'x-time' set ylabel 'y-%' set mxtics 1 set title "free memory ratio analysis (total: 8192M)" set key top left set key box set term post eps color solid enh set output 'mem.eps' set xdata time #set timefmt "%H:%M:%S" set timefmt "%Y%m%d-%H:%M:%S" #set format x "%H:%M" set format x "%H:%M" plot 'memory_monitor.log' using 1:2 title "mem" with lines linecolor rgb "red"
cpu.plt
set xlabel 'x-time' set ylabel 'y-%' set mxtics 1 set title "cpu data analysis" set key top left set key box set term post eps color solid enh set output 'cpu.eps' set xdata time set timefmt "%H:%M:%S" set format x "%H:%M" plot 'cpu' using 1:2 title "usr" with lines linecolor rgb "yellow", 'cpu' using 1:3 title "sys" wit h line linecolor rgb "red" , 'cpu' using 1:4 title "wio" with line linecolor rgb "blue", 'cpu' using 1:5 title "idle" with line linecolor rgb "green"
cpu.pl
#!/usr/bin/perl
################define parameters################### open(FILE,$ARGV[0]); open(RES,">cpu");
##############main ############################################ @cpu = grep{!/^/n/} <FILE>; foreach $cpu(@cpu) { $cpu =~ tr/ //t/s; ($date,$us,$sy,$wio,$id) = split(//t/,$cpu); push(@us,$us); push(@sy,$sy); push(@wio,$wio); push(@id,$id); print RES $cpu; }
print "us/tmax:".get_max(@us)."/n"; print "us/tmin:".get_min(@us)."/n"; print "us/taverage:".get_average(@us)."/n";
print "sy/tmax:".get_max(@sy)."/n"; print "sy/tmin:".get_min(@sy)."/n"; print "sy/taverage:".get_average(@sy)."/n";
print "wio/tmax:".get_max(@wio)."/n";
print "wio/tmin:".get_min(@wio)."/n"; print "wio/taverage:".get_average(@wio)."/n";
print "id/tmax:".get_max(@id); print "id/tmin:".get_min(@id); print "id/taverage:".get_average(@id); print "/n########################/n"; print "Do you want to paint image? if yes, please input /"y/", else input /"n/""; $flag = <STDIN>; chomp($flag); if ($flag eq "y"){ `gnuplot cpu.plt`; }
#######get max ################ sub get_max { my $max ; $max = shift(@_) ; foreach $_(@_) { if ($max<$_) { $max=$_; } } return $max ; }
#############get min################### sub get_min { my $min ; $min = shift(@_); foreach $_(@_) { if ($min>$_) { $min = $_; } } return $min; }
###############get average######################## sub get_average { my $total; # my $average = shift(@_); foreach $_ (@_) { $total = $total + $_ ; } $average = $total/($#_+1) ; return $average;
}
gen_graph.sh
#!/usr/bin/env bash
top_file_name="top20080618" total_mem_size="8388608" /export/home/deric/shell/get_mem.awk $top_file_name | awk '{printf("%s-%s %s %s/n", $1, $2, $7/83886.08, $7)}' > memory_monitor.log gnuplot mem.plt gnuplot cpu.plt
get_mem.awk
#!/usr/bin/awk -f
BEGIN { FS="/n" RS="" } { printf("%s %s/n", $1,$3) }