加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl监控linux系统资源的脚步

发布时间:2020-12-15 21:06:30 所属栏目:大数据 来源:网络整理
导读:#!/usr/bin/perl -wuse 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 Log
#!/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 $HostNamen");	
	my $MoniterTimes = 0;
	&CheckLogFile();	
	while(1)
	{
		&MoniterSysResInfo();
		sleep($MoniterPeriod);
		$MoniterTimes++; 
		print "$MoniterTimes:$MoniterTimes n";
		if ($MoniterTimes > 1000)
		{
			&CheckLogFile();
			$MoniterTimes = 0;
		}
	}
	print ("End Moniter $HostNamen");
}

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,$utiln";
				$result = ",$util";
				return $result;
			}
			elsif (scalar(@IoStatArray) == 12)
			{
				my ($Device,$RkBs,$_)[0,6,10,11];
				my $IOkBS=$RkBs+$WkBs;
				print "$Device,$util";
				return $result;
			}
			elsif (scalar(@IoStatArray) == 14)
			{
				my ($Device,12,13];
				my $IOkBS=$RkBs+$WkBs;
				print "$Device,$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:$LogFilen";
	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("$OutPutn");
};


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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读