功能:监测负载,当负载达到设定值时给你发Email,当负载降低后也给你发Email
把以下代码保存成monit.php- <?php
- $email = '你的Email';
- $high_load = 1.5; //负载达到多少时发邮件通知你
- $load = get_load();
- if($load > $high_load){
- if(!file_exists("load.txt")){
- $fp = fopen("load.txt","w");
- fwrite($fp,$load);
- fclose($fp);
- mail( $email, "high load $load", "xxx auto monit");
- }
- }else{
- if(file_exists("load.txt")){
- unlink("load.txt");
- mail( $email, "load down", "xxx auto monit");
- }
- }
- function get_load(){
- if ( false === ( $str = @file( "/proc/loadavg" ) ) )
- {
- return false;
- }
- $str = explode( " ", implode( "", $str ) );
- $str = array_chunk( $str, 3 );
- return $str[0][0];
- }
- ?>
复制代码 在crontab里添加- */1 * * * * /usr/local/php/bin/php /root/monit.php > /dev/null 2>&1
复制代码 这里php的路径和monit.php的路径要填写正确 |