#l!/usr/bin/perl ##采集系统资源 use Sys::Hostname; use HTTP::Date qw(time2iso str2time time2iso time2isoz); use Net::SMTP; my $cpu_trigger=3; my $disk_trigger=70; my $memory_trigger=40; my $io_trigger=80; my $cpu_event; my $memory_event; my $red="\e[1;31m"; my $green="\e[1;32m"; my $yellow="\e[1;33m"; my $normal="\e[0m"; sub send_mail{ if (@_ != 2){print "请输入2个参数\n";exit 1}; ($m,$n) = @_; #将参数赋值给变量 my $to_address = $m; my $CurrTime = time2iso(time()); my $to_address = $m; my $mail_user = '[email protected]'; my $mail_pwd = 'xxxxxxx'; my $mail_server = 'smtp.163.com'; my $from = "From: $mail_user\n"; my $subject = "Subject: zjcap info\n"; my $info = "$CurrTime--$n"; my $message = <<CONTENT; $info CONTENT my $smtp = Net::SMTP->new($mail_server); $smtp->auth($mail_user, $mail_pwd) || die "Auth Error! $!"; $smtp->mail($mail_user); $smtp->to($to_address); $smtp->data(); # begin the data $smtp->datasend($from); # set user $smtp->datasend($subject); # set subject $smtp->datasend("\n\n"); $smtp->datasend("$message\n"); # set content $smtp->dataend(); $smtp->quit(); }; sub section() { my $section=shift; print ">>>>>$green $section $normal \n"; } sub get_cpu { my $cpu_number=0; my $cpu_model; §ion("CPU"); open(CPU,"<","/proc/cpuinfo"); while (<CPU>) { chomp; if( /^model name.*: (.*$)/) { $cpu_number += 1; #正则分组,取第一个位置值 $cpu_model="$1"; $cpu_model =~ s/\s+/ /g; } } print " CPU: $cpu_number X $cpu_model\n"; close(CPU); ## 取当前5分钟,10分钟,15分钟系统负载,并根据阀值判断 my $uptime=`uptime`; chomp $uptime; my @uptime=split / /,$uptime; $uptime[-2] =~ s/,//; $uptime[-3] =~ s/,//; my $cpu_15m=$uptime[-1]; my $cpu_5m=$uptime[-2]; my $cpu_1m=$uptime[-3]; if ($cpu_15m > $cpu_number*$cpu_trigger) { $uptime[-1]="$red$cpu_15m$normal,"; send_mail('[email protected]',"@ip-cpu_15m\nwarning$cpu_15m") } if ($cpu_5m > $cpu_number*$cpu_trigger) { $uptime[-2]="$red$cpu_5m$normal,"; send_mail('[email protected]',"@ip-cpu_5m\nwarning$cpu_5m") } if ($cpu_1m > $cpu_number*$cpu_trigger) { $uptime[-3]="$red$cpu_1m$normal,"; send_mail('[email protected]',"@ip-cpu_1m\nwarning$cpu_1m") } print "@uptime\n"; print "-" x 80 ."\n"; } ##监控磁盘使用率 sub disk_space() { §ion("DISK SPACE"); my $line; my @array=`df -PTh`; foreach my $i (@array) { my ($fs,$type,$size,$used,$avail,$usage,$mounted); chomp $i; $i =~ s/(^\s+|\s+$)//g; $i =~ s/\s+/ /g; ($fs,$type,$size,$used,$avail,$usage,$mounted)=split /\s+/,$i; substr($usage, -1, 1)=""; if ($usage > $disk_trigger ) { printf("%-36s%-6s%-6s%-6s%-6s${red}%-6s${normal}%s\n", "$fs",$type,$size,$used,$avail,"$usage%",$mounted); send_mail('[email protected]',"@ip--disk_usage\nfs type size used avail usage mounted\n$i"); } else { printf("%-36s%-6s%-6s%-6s%-6s%-6s%s\n", $fs,$type,$size,$used,$avail,"$usage%",$mounted); } } print "-" x 80 ."\n"; } ##监控磁盘util sub iostat() { §ion("IOSTAT"); open (FH,"iostat -dNkx 2 4|"); while(<FH>) { next if /Linux/; my @array=split /\s+/,$_; my $format="%-25s"."%-9s" x 11 ."\n"; if ($array[-1] > $io_trigger) { printf("$red$format$normal",$array[0],@array[1..11]); send_mail('[email protected]',"@ip-iostat\nDEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util\n$_"); } else { printf("$format",$array[0],@array[1..11]); } } print "-" x 80 ."\n"; } ###监控CPU idle 和交换分区 sub vmstat() { §ion("VMSTAT"); open (FH,"vmstat 2 5|"); while(<FH>) { chomp; next if /Linux/; my @array=split /\s+/,$_; ##匹配开头和结尾 print "\$array[-3] is $array[-3]\n"; print "\$array[7] is $array[7]\n"; print "\$array[8] is $array[8]\n"; if (($array[-3] =~ /\A\d+\z/ and $array[-3] < 50) or ($array[7] =~ /\A\d+\z/ and $array[7] > 100) or ($array[8] =~ /\A\d+\z/ and $array[8] > 100 )) { print "$red$_$normal\n"; send_mail('[email protected]',"@ip-\nprocs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----\n r b swpd free buff cache si so bi bo in cs us sy id wa st\n$_"); }else { print "$_\n"}; } } print "-" x 80 ."\n"; sub basic() { $host = hostname; @lines=qx|/sbin/ifconfig|; @ip; print "-" x 80 ."\n"; foreach(@lines){ if(/inet addr:([\d.]+)/){ push @ip,$1 unless $1 =~ /\A127.0.0.1\z/; } } print "${yellow}HOST: $host => IP: @ip$normal\n"; print "-" x 80 ."\n"; } system("clear"); &basic(); &get_cpu(); #&memory(); &disk_space(); &iostat(); &vmstat();