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

perl 监控主机报警

发布时间:2020-12-15 23:44:53 所属栏目:大数据 来源:网络整理
导读:#l!/usr/bin/perl##采集系统资源use Sys::Hostname;use HTTP::Date qw(time2iso str2time time2iso time2isoz); use Net::SMTP;my $cpu_trigger=1.6;my $disk_trigger=90;my $memory_trigger=40;my $io_trigger=80;my $cpu_event;my $memory_event;my $red="
#l!/usr/bin/perl
##采集系统资源
use Sys::Hostname;
use HTTP::Date qw(time2iso str2time time2iso time2isoz);  
use Net::SMTP;

my $cpu_trigger=1.6;
my $disk_trigger=90;
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   = 'zhao.yangjian@163.com';
    my $mail_pwd    = 'xx';
    my $mail_server = 'smtp.163.com';

    my $from    = "From: $mail_usern";
    my $subject = "Subject: zjcap infon";
    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("nn");
    $smtp->datasend("$messagen"); # 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_modeln";
    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('zhaoyangjian@zjcap.cn',"cpu_15mn警告$cpu_15m")
    }

    if ($cpu_5m > $cpu_number*$cpu_trigger) {
        $uptime[-2]="$red$cpu_5m$normal,"cpu_5mn警告$cpu_5m")
    }

    if ($cpu_1m > $cpu_number*$cpu_trigger) {
        $uptime[-3]="$red$cpu_1m$normal,"cpu_1mn警告$cpu_1m")
    }

    print "@uptimen";
    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,$mounted)=split /s+/,$i;
        substr($usage,-1,1)="";
        if ($usage > $disk_trigger ) {
            printf("%-36s%-6s%-6s%-6s%-6s${red}%-6s${normal}%sn","$fs","$usage%",$mounted);
            send_mail('zhaoyangjian@zjcap.cn',"@ip--disk_usagenfs type size used avail usage mountedn$i");
        } else { 
            printf("%-36s%-6s%-6s%-6s%-6s%-6s%sn",$fs,$mounted);
        }
    }
    print "-" x 80 ."n";
}



##监控磁盘util 
sub iostat() {
    §ion("IOSTAT");
    open (FH,"iostat -dNkx 1 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('zhaoyangjian@zjcap.cn',"@ip-iostatnDEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %utiln$_");
        } else {
            printf("$format",@array[1..11]);
        }
    }
    print "-" x 80 ."n";
}

###监控CPU idle 和交换分区
sub vmstat() {
    §ion("VMSTAT");
    open (FH,"vmstat -w 2 5|");
    while(<FH>) {
        chomp;
        next if /Linux/;
        my @array=split /s+/,$_;
    ##匹配开头和结尾
        if (($array[-3] =~ /Ad+z/ and $array[-3] < 21) or ($array[7] > 1000)  or ($array[8] > 1000 )) {
            print "$red$_$normaln";
  send_mail('zhaoyangjian@zjcap.cn',"请检查CPU和交换分区nprocs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----n r  b       swpd       free       buff      cache   si   so    bi    bo   in   cs  us sy  id wa stn$_");
 
        }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.1z/;
        }
    }
    print "${yellow}HOST: $host => IP: @ip$normaln";
    print "-" x 80 ."n";
}



system("clear");
&basic();
&get_cpu();
#&memory();
&disk_space();
&iostat();
&vmstat();

(编辑:李大同)

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

    推荐文章
      热点阅读