#!/usr/bin/perl
#use strict;
#Monitor the cpu memory and IO of LINUX
#Create the log file
print "This is a program for collect the system stat,\n";
print "if you want to continue,pls enter c.If you \n";
print "to quit,pls ent q.\n";
print "It was published by Mr.Li.\n";
while (){
chomp($_);
print "I saw $_ .\n";
if ($_ eq "q"){
exit;
}elsif ($_ eq "c"){
last;
}
else{
print "The input is illegal,pls input again!\n";
next;
}
}
$DATE=`date +%F`;
chomp($DATE);
$log_file="/tmp/monitor_$DATE";
$DATE_TIME=`date`;
if (-e $log_file)
{
open LOG,">> $log_file";
print LOG "the log file already exist at $DATE_TIME.\n";
close LOG;
}else
{
print "file $log_file does not exist!\n";
open LOG,">> $log_file";
print LOG "The log file is created at $DATE_TIME.\n";
close LOG;
}
#get the cpu idle
$CPU_IDLE=`vmstat 2 2 |tail -1|awk '{printf ("%d",\$15)}'`;
chomp$CPU_IDLE;
$CPU_UTIL=100-$CPU_IDLE;
#get the mem and swap util by the command free -m
@SWAP=`free -m`;
#remark
print @SWAP;
$i=0;
foreach $SWAP(@SWAP){
print "\$swap[$i]is:$SWAP\n";
$i=$i+1;
};
#remark
#calculate the mem util
@SWAP_1=split/\s+/,$SWAP[1];
$MEM_UTIL_PER=$SWAP_1[2]/$SWAP_1[1]*100;
printf "mem is:%.2f%%\n",$MEM_UTIL_PER;
#calculate the swap util
@SWAP_3=split/\s+/,$SWAP[3];
$SWAP_UTIL_PER=$SWAP_3[2]/$SWAP_3[1]*100;
printf "swap is:%.2f%%\n",$SWAP_UTIL_PER;
#calculate the space util
@SPACE=`df -k`;
@SPACE_1=split/\s+/,$SPACE[1];
$SPACE_UTIL=$SPACE_1[4];
printf "space is:%s\n",$SPACE_UTIL;
#calculate the io util
$IO_UTIL=`sar -d 2 2|tail -1|awk '{printf ("%s",\$10)}'`;
chomp$IO_UTIL;
#all the data log into the logfile
open LOG,">> $log_file";
printf LOG "%.2f\t%.2f\t%.2f\t%s\t%s\n",$CPU_IDLE,$MEM_UTIL_PER,$SWAP_UTIL_PER,$SPACE_UTIL,$IO_UTIL;
close LOG;
#Mail the log file
阅读(716) | 评论(0) | 转发(0) |