Posted on 2010-09-30 16:23
kongkongzi 阅读(594)
评论(1) 编辑 收藏 引用 所属分类:
perl
#!/usr/bin/perl -w
# file: infogather
use strict;
if (@ARGV < 1) # @ARGV return size of array ARGV in this context
{
die "Usage: $0 processname\n";
}
my $processname = $ARGV[0];
my $outfilename = "procinfo"; # one new name during one min.
my $outfile;
my $cmd;
my @cmdout;
my @arrCol;
my $Cpu;
my $Mem;
my $cpu;
my $mem;
# Create Log Dir
if (! -e $processname)
{
$cmd = sprintf("mkdir %s", $processname);
@cmdout = `$cmd`;
}
# Get Process ID
my $pid;
my $hasProcess = 0;
while (!$hasProcess)
{
$cmd = sprintf("ps aux |grep %s |grep -v grep |grep -v $0 |grep -v mysqld_safe", $processname);
@cmdout = `$cmd`;
if (@cmdout < 1)
{
sleep 60;
}
else
{
print $cmdout[0];
@arrCol = split(/\s+/, $cmdout[0]);
$pid = $arrCol[1]; # The second column of text
$hasProcess = 1;
}
}
my @ethCol;
my $prevTime0;
my $prevByteRec0;
my $prevByteTran0;
my $lastTime0;
my $lastByteRec0;
my $lastByteTran0;
my $prevTime1;
my $prevByteRec1;
my $prevByteTran1;
my $lastTime1;
my $lastByteRec1;
my $lastByteTran1;
my $hasEth0 = 1;
my $hasEth1 = 1;
@cmdout = `cat /proc/net/dev |grep eth0`;
if (@cmdout < 1)
{
$hasEth0 = 0;
}
@cmdout = `cat /proc/net/dev |grep eth1`;
if (@cmdout < 1)
{
$hasEth1 = 0;
}
if ($hasEth0)
{
$prevTime0 = `date +%s`;
@cmdout = `cat /proc/net/dev |grep eth0`;
@arrCol = split(/:/, $cmdout[0]); # first split by ":"
$_ = $arrCol[1];
s/^\s+//; # remove at the beginning of the blank, return true or false.
$arrCol[1] = $_;
@ethCol = split(/\s+/, $arrCol[1]);
$prevByteRec0 = $ethCol[0];
$prevByteTran0 = $ethCol[8];
}
if ($hasEth1)
{
$prevTime1 = `date +%s`;
@cmdout = `cat /proc/net/dev |grep eth1`;
@arrCol = split(/:/, $cmdout[0]); # first split by ":"
$_ = $arrCol[1];
s/^\s+//; # remove at the beginning of the blank, return true or false.
$arrCol[1] = $_;
@ethCol = split(/\s+/, $arrCol[1]);
$prevByteRec1 = $ethCol[0];
$prevByteTran1 = $ethCol[8];
}
while (1)
{
sleep 60;
$outfilename = `date +%Y-%m-%d_%H_%M_%S`;
chomp($outfilename);
$outfile = sprintf("%s\/%s", $processname, $outfilename);
my $success = open STATSOUT, ">", $outfile;
if (!$success)
{
die "Cannot open $outfile:$!";
}
@cmdout = `top -bn 1 |grep Cpu`;
foreach (@cmdout)
{
chomp($_);
print STATSOUT $_;
print STATSOUT "\n";
}
$Mem = `top -bn 1 |grep Mem`;
chomp($Mem);
print STATSOUT $Mem, "\n";
$cmd = sprintf("top -p %d -b n1 | tail -n2 |head -n1", $pid);
@cmdout = `$cmd`;
chomp($cmdout[0]);
$_ = $cmdout[0];
s/^\s+//; # remove at the beginning of the blank, return true or false.
$cmdout[0] = $_;
print STATSOUT $cmdout[0], "\n";
@arrCol = split(/\s+/, $cmdout[0]); # $_ # split $_ by spaces
# foreach (@arrCol)
# {
# print $_;
# print "\n";
# }
# print "\n\n";
$cpu = $arrCol[8];
print STATSOUT $processname, ":cpu=", $cpu, "%\n";
$mem = $arrCol[9];
print STATSOUT $processname, ":mem=", $mem, "%\n";
$cmd = sprintf("netstat -anp |grep %s |grep %d |wc", $processname, $pid);
@cmdout = `$cmd`;
@arrCol = split(/\s+/, $cmdout[0]); # $_ # split $_ by spaces
print STATSOUT "connecting count=", $arrCol[1], "\n";
if ($hasEth0)
{
$lastTime0 = `date +%s`;
@cmdout = `cat /proc/net/dev |grep eth0`;
@arrCol = split(/:/, $cmdout[0]); # first split by ":"
$_ = $arrCol[1];
s/^\s+//; # remove at the beginning of the blank, return true or false.
$arrCol[1] = $_;
@ethCol = split(/\s+/, $arrCol[1]);
$lastByteRec0 = $ethCol[0];
$lastByteTran0 = $ethCol[8];
print STATSOUT "eth0 Incoming:", ($lastByteRec0 - $prevByteRec0) * 8 / ($lastTime0 - $prevTime0) / 1000, "kBit/s", "\n";
print STATSOUT "eth0 Outgoing:", ($lastByteTran0 - $prevByteTran0) * 8 / ($lastTime0 - $prevTime0) / 1000, "kBit/s", "\n";
$prevTime0 = $lastTime0;
$prevByteRec0 = $lastByteRec0;
$prevByteTran0 = $lastByteTran0;
}
if ($hasEth1)
{
$lastTime1 = `date +%s`;
@cmdout = `cat /proc/net/dev |grep eth1`;
@arrCol = split(/:/, $cmdout[0]); # first split by ":"
$_ = $arrCol[1];
s/^\s+//; # remove at the beginning of the blank, return true or false.
$arrCol[1] = $_;
@ethCol = split(/\s+/, $arrCol[1]);
$lastByteRec1 = $ethCol[0];
$lastByteTran1 = $ethCol[8];
print STATSOUT "eth1 Incoming:", ($lastByteRec1 - $prevByteRec1) * 8 / ($lastTime1 - $prevTime1) / 1000, "kBit/s", "\n";
print STATSOUT "eth1 Outgoing:", ($lastByteTran1 - $prevByteTran1) * 8 / ($lastTime1 - $prevTime1) / 1000, "kBit/s", "\n";
$prevTime1 = $lastTime1;
$prevByteRec1 = $lastByteRec1;
$prevByteTran1 = $lastByteTran1;
}
$cmd = sprintf("netstat -anp |grep %s |grep %d |sort -rnk2 |head -n 5", $processname, $pid);
print STATSOUT $cmd, "\n";
@cmdout = `$cmd`;
print STATSOUT @cmdout;
$cmd = sprintf("netstat -anp |grep %s |grep %d |sort -rnk3 |head -n 5", $processname, $pid);
print STATSOUT $cmd, "\n";
@cmdout = `$cmd`;
print STATSOUT @cmdout;
close STATSOUT;
}