PHP监控服务器【LNMPA】
<?php $url=array(); $url[]='http://hk.qeejoo.com'; $url[]='http://sz.qeejoo.com'; $url[]='http://gz.qeejoo.com'; $url[]='http://bj.qeejoo.com'; $url[]='http://wh.qeejoo.com'; $url[]='http://sh.qeejoo.com'; //上面就是取一部分用来访问 $killapache='killall -9 httpd';//干掉apache $kill_mongod = 'killall mongod';//干掉mongod $start_xx1_mongod = 'mongod --config /etc/mongod_xx1.conf';//启动xx1 Mongod $start_xx2_mongod = 'mongod --config /etc/mongod_xx2.conf';//启动xx2 Mongod $restart_lnmpa = '/root/lnmpa restart';//重启lnmpa $connect_status=0;//服务器链接状态 $mongo_status=0;//mongo状态 $server_500_status=0;//服务器500 $server_502_status=0;//服务器502 for($i = 0; $i < 6; $i ++) { srand((double)microtime()*1000000); //产生随机种子seed $k=rand(0,5); $go_url=$url[$k]; $exec = "curl --connect-timeout 5 -i $go_url 2>/dev/null"; $res = shell_exec($exec); if($res=="") { $connect_status++;//服务器无法连接次数+1 } else if(stripos($res, 'EMongoException') !== false) { $mongo_status++;//mongod无法链接次数+1 } else if(stripos($res, '500 Internal Server') !== false) { $server_500_status++; } else if(stripos($res, '502 Bad Gateway') !== false) { $server_502_status++; } else { //服务器正常 } sleep(3); } ///echo $connect_status."__".$mongo_status."__".$server_500_status."__".$server_502_status;exit; //只要服务器无法正常工作,则重起相关服务 if($connect_status >=2 || $mongo_status>=2 || $server_500_status>=2 || $server_502_status>=2) { shell_exec($killapache); shell_exec($killapache);//2次干掉apache shell_exec($kill_mongod); shell_exec($kill_mongod); shell_exec($kill_mongod); shell_exec($kill_mongod);//4次干掉mongod shell_exec($start_xx1_mongod);//启动xx1 Mongod shell_exec($start_xx2_mongod);//启动xx2 Mongod shell_exec($restart_lnmpa);//重启lnmpa } //发送邮件 if($connect_status>=2) { sendmail(array('xx1@139.com','xx3@139.com'),'服务器无法链接','5次检测服务器,'.$connect_status.'次无法链接');exit; } else if($mongo_status>=2) { sendmail(array('xx1@139.com','xx3@139.com'),'Mongod挂了','5次检测 Mongod,'.$mongo_status.'次挂掉');exit; } else if($server_500_status>=2) { sendmail(array('xx1@139.com','xx3@139.com'),'500 Internal Server','5次检测服务器,'.$server_500_status.'次出现500 Internal Server');exit; } else if($server_502_status>=2) { sendmail(array('xx@139.com','xx1@xx.com','xx2@139.com'),'502 Bad Gateway','5次检测服务器,'.$server_502_status.'次出现502 Bad Gateway');exit; } else { //服务器正常,生产时,请关闭这个邮件发送。。。 sendmail(array('xx@xx.com'),'Good!!! 服务器正常','Good 服务器正常:'.$connect_status."__".$mongo_status."__".$server_500_status."__".$server_502_status);exit; } function sendmail($to,$subject = "",$body = "") { error_reporting(E_STRICT); date_default_timezone_set("Asia/Shanghai");//设定时区东八区 require_once './PHPMailer_5.2.1/class.phpmailer.php';//这个Class不用解释把。google去找 require_once './PHPMailer_5.2.1/class.smtp.php'; $mail= new PHPMailer(); //new一个PHPMailer对象出来 $mail->CharSet ="UTF-8";//设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 $mail->IsSMTP(); // 设定使用SMTP服务 $mail->SMTPDebug = 1; // 启用SMTP调试功能 // 1 = errors and messages // 2 = messages only $mail->SMTPAuth = true; // 启用 SMTP 验证功能 $mail->SMTPSecure = "ssl"; // 安全协议 $mail->Host = "smtp.exmail.xx.com"; // SMTP 服务器 $mail->Port = 465; // SMTP服务器的端口号 $mail->Username = "xx@xxoo.com"; // SMTP服务器用户名 $mail->Password = "xxoo"; // SMTP服务器密码 $mail->SetFrom('xx@xxoo.com', '监控'); $mail->Subject = $subject; $mail->MsgHTML($body); foreach ($to as $t) { $mail->AddAddress($t); } if(!$mail->Send()) { return false; } else { return true; } } ?>