zoukankan      html  css  js  c++  java
  • perl 监控主机报警

    #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个参数
    ";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_user
    ";
        my $subject = "Subject: zjcap info
    ";
        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("
    
    ");
        $smtp->datasend("$message
    "); # set content
        $smtp->dataend();
        $smtp->quit();
    };
    
    
    
    sub section() {
        my $section=shift;
        print ">>>>>$green $section $normal  
    ";
    }
    
    
    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
    ";
        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_15m
    警告$cpu_15m")
        }
    
        if ($cpu_5m > $cpu_number*$cpu_trigger) {
            $uptime[-2]="$red$cpu_5m$normal,";
            send_mail('zhaoyangjian@zjcap.cn',"cpu_5m
    警告$cpu_5m")
        }
    
        if ($cpu_1m > $cpu_number*$cpu_trigger) {
            $uptime[-3]="$red$cpu_1m$normal,";
            send_mail('zhaoyangjian@zjcap.cn',"cpu_1m
    警告$cpu_1m")
        }
    
        print "@uptime
    ";
        print "-" x 80 ."
    ";
    }
    
    ##监控磁盘使用率
    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
    ", "$fs",$type,$size,$used,$avail,"$usage%",$mounted);
                send_mail('zhaoyangjian@zjcap.cn',"@ip--disk_usage
    fs type size used avail usage mounted
    $i");
            } else { 
                printf("%-36s%-6s%-6s%-6s%-6s%-6s%s
    ", $fs,$type,$size,$used,$avail,"$usage%",$mounted);
            }
        }
        print "-" x 80 ."
    ";
    }
    
    
    
    ##监控磁盘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 ."
    ";
        
            if ($array[-1] > $io_trigger) {
                printf("$red$format$normal",$array[0],@array[1..11]);
                send_mail('zhaoyangjian@zjcap.cn',"@ip-iostat
    DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
    $_");
            } else {
                printf("$format",$array[0],@array[1..11]);
            }
        }
        print "-" x 80 ."
    ";
    }
    
    ###监控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$_$normal
    ";
      send_mail('zhaoyangjian@zjcap.cn',"请检查CPU和交换分区
    procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
     r  b       swpd       free       buff      cache   si   so    bi    bo   in   cs  us sy  id wa st
    $_");
     
            }else {
    print "$_
    "}; 
    }
        }
        print "-" x 80 ."
    ";
    
    
    
    sub basic() {
         $host = hostname;
         @lines=qx|/sbin/ifconfig|;
         @ip;
        print "-" x 80 ."
    ";
        foreach(@lines){
            if(/inet addr:([d.]+)/){
                push @ip,$1 unless $1 =~ /A127.0.0.1z/;
            }
        }
        print "${yellow}HOST: $host => IP: @ip$normal
    ";
        print "-" x 80 ."
    ";
    }
    
    
    
    system("clear");
    &basic();
    &get_cpu();
    #&memory();
    &disk_space();
    &iostat();
    &vmstat();
    

  • 相关阅读:
    Asp.net实现URL重写
    IHttpModule不起作用的两个原因
    从客户端中检测到有潜在危险的 request.form值[解决方法]
    PHP $_SERVER详解
    string.Format 格式化日期格式
    图解正向代理、反向代理、透明代理
    Javacard 解释器怎样在API类库中找到源文件调用的类、方法或者静态域?
    API
    指令集
    机器码与字节码
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351690.html
Copyright © 2011-2022 走看看