Home > php教程 > PHP源码 > PHP监控服务器【LNMPA】

PHP监控服务器【LNMPA】

PHP中文网
Release: 2016-05-25 17:11:21
Original
1411 people have browsed it

php代码

<?php
$url=array();
$url[]=&#39;http://hk.xx.com&#39;;
$url[]=&#39;http://sz.xx.com&#39;;
$url[]=&#39;http://gz.xx.com&#39;;
$url[]=&#39;http://bj.xx.com&#39;;
$url[]=&#39;http://wh.xx.com&#39;;
$url[]=&#39;http://sh.xx.com&#39;;
//上面就是取一部分用来访问
$killapache=&#39;killall -9 httpd&#39;;//干掉apache
$kill_mongod = &#39;killall mongod&#39;;//干掉mongod
$start_xx1_mongod = &#39;mongod --config /etc/mongod_xx1.conf&#39;;//启动xx1 Mongod
$start_xx2_mongod = &#39;mongod --config /etc/mongod_xx2.conf&#39;;//启动xx2 Mongod


$restart_lnmpa = &#39;/root/lnmpa restart&#39;;//重启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, &#39;EMongoException&#39;) !== false)
	{
		$mongo_status++;//mongod无法链接次数+1
	}
	else if(stripos($res, &#39;500 Internal Server&#39;) !== false)
	{
		$server_500_status++;
	}
	else if(stripos($res, &#39;502 Bad Gateway&#39;) !== 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(&#39;xx1@139.com&#39;,&#39;xx3@139.com&#39;),&#39;服务器无法链接&#39;,&#39;5次检测服务器,&#39;.$connect_status.&#39;次无法链接&#39;);exit;
}
else if($mongo_status>=2)
{
	sendmail(array(&#39;xx1@139.com&#39;,&#39;xx3@139.com&#39;),&#39;Mongod挂了&#39;,&#39;5次检测 Mongod,&#39;.$mongo_status.&#39;次挂掉&#39;);exit;
}
else if($server_500_status>=2)
{
	sendmail(array(&#39;xx1@139.com&#39;,&#39;xx3@139.com&#39;),&#39;500 Internal Server&#39;,&#39;5次检测服务器,&#39;.$server_500_status.&#39;次出现500 Internal Server&#39;);exit;
}
else if($server_502_status>=2)
{
	sendmail(array(&#39;xx@139.com&#39;,&#39;xx1@xx.com&#39;,&#39;xx2@139.com&#39;),&#39;502 Bad Gateway&#39;,&#39;5次检测服务器,&#39;.$server_502_status.&#39;次出现502 Bad Gateway&#39;);exit;
}
else {
	//服务器正常,生产时,请关闭这个邮件发送。。。
	sendmail(array(&#39;xx@xx.com&#39;),&#39;Good!!! 服务器正常&#39;,&#39;Good 服务器正常:&#39;.$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 &#39;./PHPMailer_5.2.1/class.phpmailer.php&#39;;//这个Class不用解释把。google去找
   	require_once &#39;./PHPMailer_5.2.1/class.smtp.php&#39;;
	$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(&#39;xx@xxoo.com&#39;, &#39;监控&#39;);
    $mail->Subject    = $subject;
    $mail->MsgHTML($body);
    foreach ($to as $t)
    {
    	$mail->AddAddress($t);
    }
    if(!$mail->Send()) {
        return false;
    } 
    else {
        return true;
    }
}
?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template