1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #!/usr/bin/perl use Mail::Mailer; sub get_cpu_stat { my @stats; my $fh; #Linux 系统,从 /proc/stat 来获取 CPU 信息 open($fh, "cat /proc/stat |") or die "$!"; while (<$fh>) { #get the cpu stat if (/^cpu .*/) { @stats = split; } } close $fh; #then, we parse the cpu stat information; #the information from "man proc", #the format is for Linux 2.6.11 or higher #cpu user nice system idle iowait irq softirq steal #cpu 628808 1642 61861 24978051 22640 349 3086 0 0 # 数据格式如上所示,更详细的信息请参考 proc 的 manpage my $total = $stats[1] + $stats[2] + $stats[3] + $stats[4]; my $idle = $stats[4]; # 返回当前 CPU 的 total 和 idle 时间片计数 return ($total, $idle); } my $mailer = Mail::Mailer->new('sendmail'); my ($t1, $i1) = get_cpu_stat(); sleep 5; my ($t2, $i2) = get_cpu_stat(); my $total = $t2 - $t1; my $idle = $i2 - $i1; # 计算获取 5 秒钟之内的 CPU 利用率 my $per = 100 * ($total - $idle) / $total; if( $per > 85) { # 如果 CPU 利用率大于 85% my $msg = sprintf("!!!Attention\nThe CPU usage on your server is %.2f" . "% right now!\nPlease check it right now!\n", $per); #send mail to the assigned user # 发送邮件给指定的用户 $mailer->open( { From => 'root@minjun-desktop', To => 'minjun.xi@cn.ibm.com', Subject => 'Attention for your cluster', }) or die "Mail::Mailer failed!\n"; print $mailer $msg; $mailer->close(); } |
1 | 30 * * * * perl /path/to/mon.pl |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |