检查内存状态,自动KILL进程

#!/usr/bin/perl 
 use strict;
 # 打印此脚本使用信息和输出说明。
 ################################################################ 
 # Output the usage of this perl script 
 ################################################################ 
 sub usage 
 { 
    print "memmonitor: Monitor system memory usage\n"; 
    print "1. if used memory is more than 75% of total memory, memmonitor\n"; 
    print "will output one warning message per minites.\n"; 
    print "2. if used memory is more than 90% of total momory, memmonitor\n"; 
    print "will kill the processes that occupied the most memory untill\n"; 
    print "used memory is less than 75% of total memory.\n\n";
    return 0; 
 }
 usage
 my $total_mem; 
 my $used_mem; 
 my $next_line = 0; 
 my @rc; 
 my $rate;
 # $total_mem 和 $used_mem 变量分别代表了总计实际内存大小和已经使用的内存大小
 rc = `vmstat`; 
 foreach ( @rc ) { 
    if ( $next_line ) { 
      my @value = split /\s+/, $_; 
         $used_mem = @value[3] * 4; 
         $next_line = 0; 
         last; 
     } elsif ( /^.*mem=(\d+)MB$/ ) { 
         $total_mem = $1 * 1024; 
     } elsif ( /^.*avm.*$/ ) { 
         $next_line = 1; 
     } 
 }
 # 这段代码使用了 vmstat 命令得到系统内存状态信息,并对结果进行逐行解析,得到各个字段的数据。
 if ( $total_mem ) { 
    $rate = $used_mem / $total_mem; 
 }
 # 计算得到了当前全局内存使用率
 if ( $rate > 0.75 ) { 
    print "Warning: Memory is not enough\n"; 
 }
 # 如果内存使用率大于 0.75, 将打印出警告信息。
 if ( $rate > 0.9 ) { 
    my $line_count = 0; 
    my @output = `ps aux | head -1;ps aux | sort -rn +5`; 
    foreach ( @output ) { 
        if ( $line_count ) { 
            my @killed_process = split /\s+/,$_; 
            print "Warning: Out of memory. Kill process: @killed_process[1]\n"; 
            # 发送警告信息给 root 用户,保存程序运行记录。
`echo "Process @killed_process[1] has been killed because of unlimited memory \
allocation" | mail -s "Out of memory" root`
            `kill -11 @killed_process[1]`; 
            last; 
        } 
        $line_count = $line_count + 1; 
    }
 # 如果内存使用率大于 0.9,脚本将调用 ps 命令并且找出内存使用率最高的进程,打印出将要杀掉进程的警告信息,
 #杀掉内存使用率最高的进程。 此过程将循环进行知道内存占用率低于 0.9 
 }


转载于:https://my.oschina.net/OliverTwist/blog/226500

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值