首页 > php教程 > PHP源码 > 正文

PHP监控服务器【LNMPA】

PHP中文网
发布: 2016-05-25 17:11:21
原创
1893人浏览过

php代码

启科网络PHP商城系统
启科网络PHP商城系统

启科网络商城系统由启科网络技术开发团队完全自主开发,使用国内最流行高效的PHP程序语言,并用小巧的MySql作为数据库服务器,并且使用Smarty引擎来分离网站程序与前端设计代码,让建立的网站可以自由制作个性化的页面。 系统使用标签作为数据调用格式,网站前台开发人员只要简单学习系统标签功能和使用方法,将标签设置在制作的HTML模板中进行对网站数据、内容、信息等的调用,即可建设出美观、个性的网站。

启科网络PHP商城系统 0
查看详情 启科网络PHP商城系统
<?php
$url=array();
$url[]='http://hk.xx.com';
$url[]='http://sz.xx.com';
$url[]='http://gz.xx.com';
$url[]='http://bj.xx.com';
$url[]='http://wh.xx.com';
$url[]='http://sh.xx.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;
    }
}
?>
登录后复制
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号