perl监控linux系统资源的脚步

#!/usr/bin/perl -w
use strict;

my @MoniterDevideName=("cciss/c0d0p1","cciss/c0d0p3","sda");
my @MoniterMountDir=("/");
my $MoniterPeriod = 3;

my $LogFileDir='./';
my $HostName = (map {chomp;$_} (`hostname`))[0];


my $LogFileHandle;


&MainLoop;
sub LogMsg()
{
	my $LogMsg = shift;
	$LogMsg = &GetLogRecordTime().",".$LogMsg;
	print $LogFileHandle $LogMsg;
}

sub MainLoop()
{
	print ("Begin Moniter $HostName\n");	
	my $MoniterTimes = 0;
	&CheckLogFile();	
	while(1)
	{
		&MoniterSysResInfo();
		sleep($MoniterPeriod);
		$MoniterTimes++; 
		print "\$MoniterTimes:$MoniterTimes \n";
		if ($MoniterTimes > 1000)
		{
			&CheckLogFile();
			$MoniterTimes = 0;
		}
	}
	print ("End Moniter $HostName\n");
}

sub GetDeviceData()
{
	my $MoniterDeviceName = shift;
	print $MoniterDeviceName."\n";
	my $IostatCMDHandle;
	open $IostatCMDHandle,"iostat  -dkx  $MoniterDeviceName |" or die "cannot fork iostat -x: $!";
	my $DeviceDataBegin = 0;
	my $Device="--";
	my $result = ",-,-,-,-";
	while(<$IostatCMDHandle>)
	{
		chomp;
		s/^\s+//;
		s/\s+$//;
		next if ($_ eq "");

		if($DeviceDataBegin==1)
		{    
			my @IoStatArray = split /\s+/,$_;	
			my $Size = scalar(@IoStatArray);
			print "Num items: $Size \n";
			if (scalar(@IoStatArray) == 1)
			{
				$Device = $IoStatArray[0];
				next;
			}
			elsif (scalar(@IoStatArray) == 11)
			{
				my ($RkBs,$WkBs,$AWait,$Svctm,$util) = (split /\s+/,$_)[4,5,8,9,10];
				my $IOkBS=$RkBs+$WkBs;
				print "$Device,$IOkBS,$AWait,$Svctm,$util\n";
				$result = ",$IOkBS,$AWait,$Svctm,$util";
				return $result;
			}
			elsif (scalar(@IoStatArray) == 12)
			{
				my ($Device,$RkBs,$WkBs,$AWait,$Svctm,$util) = (split /\s+/,$_)[0,5,6,9,10,11];
				my $IOkBS=$RkBs+$WkBs;
				print "$Device,$IOkBS,$AWait,$Svctm,$util\n";
				$result = ",$IOkBS,$AWait,$Svctm,$util";
				return $result;
			}
			elsif (scalar(@IoStatArray) == 14)
			{
				my ($Device,$RkBs,$WkBs,$AWait,$Svctm,$util) = (split /\s+/,$_)[0,5,6,9,12,13];
				my $IOkBS=$RkBs+$WkBs;
				print "$Device,$IOkBS,$AWait,$Svctm,$util\n";
				$result = ",$IOkBS,$AWait,$Svctm,$util";
				return $result;
			}
		}
		if(/^Device:/)
		{
			$DeviceDataBegin = 1;        
		}
	}
	return $result;
}

sub GetMountFSInfo()
{
	my $MountDir = shift;
	my $CMD = "df  -m  $MountDir |grep $MountDir | awk '{print \$4/1024,\$5}'";
	my $result = (map {chomp; s#\s+#,#g;$_} (`$CMD`))[0];
	return $result;
}

sub CheckLogFile()
{
	my $LogFile = &GetLogFileName();
	print "\$LogFile:$LogFile\n";
	if (-f $LogFile)
	{
		my @StatLogFile = stat ($LogFile);
		my $Logfilesize = $StatLogFile[7]/1024/1024; 
		print "LogFile Size: $Logfilesize \n";
		if ($Logfilesize>50)
		{
			my $NewLogFilename=$LogFile."_".&GetLogRecordTime();
			print "\$NewLogFilename : $NewLogFilename \n";
			`mv $LogFile $NewLogFilename`;
		}
		else
		{
			return;
		}
	}

	open ($LogFileHandle,">>",$LogFile) or die "can't create logfile:$LogFile, for:$?";	
	print $LogFileHandle ("Time,CPULoadAverage,CPUUtil(%),CPUIOWait(%),MemFree(%)");

	my $Item;
	my $DeviceName;
	foreach $Item (@MoniterDevideName)
	{
		$DeviceName = $Item."-";
		print $LogFileHandle (",".$DeviceName."kB/s,",$DeviceName."Await,",$DeviceName."Svctm,",$DeviceName."Util");

	}
	my $MountDir;
	foreach $Item (@MoniterMountDir)
	{
		$MountDir = $Item."-";
		print $LogFileHandle (",".$MountDir."Total(GB),",$MountDir."Available(GB)");
	}

	print  $LogFileHandle "\n";	
}


sub MoniterSysResInfo()
{
	my $Split = ",";
	my $OutPut = $HostName.$Split.&GetTopCmdInfo().$Split.&GetMemFree();

	my $DeviceName;
	foreach $DeviceName (@MoniterDevideName)
	{
		$OutPut .= &GetDeviceData($DeviceName); 
	}
	my $MountDir;
	foreach $MountDir (@MoniterMountDir)
	{
		$OutPut .= &GetMountFSInfo($MountDir); 
	}

	&LogMsg("$OutPut\n");
};


sub GetTopCmdInfo()
{
	my $CpuUtilResult = "--";
	my $CpuLoadResult = "--";
	my $CpuIOWaitResult= "--";
	my @TopResult = `top -b -n 1 | head -5 `;
	my $TopLine1 = $TopResult[0];
	my $TopLine3 = $TopResult[2];
	chomp $TopLine3;
	chomp $TopLine1;
	if ($TopLine1 =~ /\s+load average:\s*(\S+),/)
	{
		$CpuLoadResult = $1;
	}
	if ($TopLine3 =~ /\s+(\S+)%id,\s+(\S+)%wa,\s+/)
	{
		$CpuUtilResult = 100-$1;
		$CpuIOWaitResult = $2;
	}
	return $CpuLoadResult.",".$CpuUtilResult.",".$CpuIOWaitResult;
}
sub GetMemFree()
{
	my $Result = `free | grep buffers/cache | awk '{print \$4*100/(\$4+\$3)}'`;
	chomp $Result;
	return $Result;
}




sub GetLogRecordTime()
{
	my $Result = `date +%m_%d_%H_%M_%S`;
	chomp $Result;
	return $Result;
}
sub GetLogFileName()
{
	my $Result = `date +%Y%m%d`;
	chomp $Result;
	my $LogFile = $HostName."_".$Result."_Moniter.csv";
	return $LogFileDir.$LogFile;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值