• Perl 监控OS脚本


    #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个参数
    ";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    = 'xxxxxxx';
        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',"@ip-cpu_15m
    warning$cpu_15m")
        }
    
        if ($cpu_5m > $cpu_number*$cpu_trigger) {
            $uptime[-2]="$red$cpu_5m$normal,";
            send_mail('zhaoyangjian@zjcap.cn',"@ip-cpu_5m
    warning$cpu_5m")
        }
    
        if ($cpu_1m > $cpu_number*$cpu_trigger) {
            $uptime[-3]="$red$cpu_1m$normal,";
            send_mail('zhaoyangjian@zjcap.cn',"@ip-cpu_1m
    warning$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 2 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  2 5|");
        while(<FH>) {
            chomp;
            next if /Linux/;
            my @array=split /s+/,$_;
        ##匹配开头和结尾
               print "$array[-3] is $array[-3]
    ";
               print "$array[7] is $array[7]
    ";
               print "$array[8] is $array[8]
    ";
            if (($array[-3] =~ /Ad+z/ and $array[-3] < 50) or ($array[7] =~ /Ad+z/ and $array[7] > 100)  or ($array[8] =~ /Ad+z/ and $array[8] > 100 )) {
                print "$red$_$normal
    ";
      send_mail('zhaoyangjian@zjcap.cn',"@ip-
    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();
    
    

  • 相关阅读:
    scss使用指南--每天一点
    Egret引擎开发基础(一)
    vue-awesome-swiper使用自动轮播和循环轮播不生效(loop和autoplay)
    Vue项目中title的设置,使用document.title返回时不生效
    charles系列破解激活注册码
    Vscode中vue项目中无法对js进行提示
    JS数组与对象的遍历方法大全
    手把手教你用webpack3搭建react项目(开发环境和生产环境)(二)
    服务器安装redis-stat和easyNmon
    Shell备份数据库
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351671.html
Copyright © 2020-2023  润新知