php实时交易数据监控,个人记录 php monitor监控代码

index.php

define("ROOT", dirname(__FILE__) . "/");

require ROOT . "init.php";

define("DEBUG", 0);

$runtimes = $config["runtime"];

/*

foreach ($runtimes as $runtime) {

$header = $runtime["header"];

$name = $runtime["name"];

foreach ($runtime["server"] as $single_runtime) {

$pid = pcntl_fork();

if ($pid == 0) {

subroutine($name, $single_runtime, $header);

exit;

}

}

}

*/

foreach ($runtimes as $runtime) {

$header = $runtime["header"];

$name = $runtime["name"];

$pid = pcntl_fork();

if ($pid == 0) {

foreach ($runtime["server"] as $single_runtime) {

subroutine($name, $single_runtime, $header);

}

exit;

}

}

function subroutine($runtime_name, $runtime_addr, $header)

{

global $config, $filter;

//foreach ($runtime["server"] as $single_runtime) {

foreach ($config["services"] as $service) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, "{$runtime_addr}/?format=xml&service={$service}");

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

$service = array_key_exists($service, $config["filter"]) ? $config["filter"]["$service"] : $service;

$content = curl_exec($ch);

$error = curl_errno($ch) == 0 ? null : curl_error($ch);

if ($error) {

$msg = "curl monitor failed: $error, service ($service)";

$content .= print_r(curl_getinfo($ch), true);

send_alert($msg, $content, "sms", "webruntime", $runtime_name, $runtime_addr);

save("webruntime", $msg, $content, "critical");

} else {

save("webruntime", "", "", "ok");

try {

$xml = new SimpleXMLElement($content);

} catch (Exception $e) {

$msg = "parse monitor response failed: " . substr($content, 0, 20) . " service {$service}";

send_alert($msg, $content, "sms", "$service", $runtime_name, $runtime_addr);

return;

}

analyze2($xml, $runtime_name, $runtime_addr);

}

curl_close($ch);

}

//}

}

function save($service, $title, $content, $status)

{

global $config;

if (isset($config["log"]["mysql"]) && $config["log"]["mysql"]) {

$conn = mysql_con();

$title = mysql_real_escape_string($title, $conn);

$content = mysql_real_escape_string($content, $conn);

$now = time();

$sql = "insert into `monitor_result` values ('', '{$service}', 'monitor', '{$title}', '{$content}', '{$status}', $now)";

mysql_query($sql, $conn) or errors_log(mysql_error($conn) . " sql: {$sql} ");

mysql_close($conn);

}

}

function analyze2($xml, $runtime_name, $runtime_addr)

{

global $config;

$title = array();

$content = array();

$type = "";

foreach($xml as $resource) {

if (isset($resource->error->assert)) {

$failed = "[{$resource->item} failed]";

$title[] = $failed;

$content[] = $failed;

foreach ($resource->error->assert as $assert) {

$title[] = $assert->source;

$content[] = $assert->info;

}

$type = "sms";

} else if ($resource->timedelta > 8) {

if ($resource->item == "testSaeStorage" && $resource->timedelta < 10) {

continue;

}

//对SaeImage做特殊处理

if ($resource->item == "testSaeImage" && $resource->timedelta < 6) {

continue;

}

$notice = "[{$resource->item} notice]";

$title[] = $notice;

$content[] = $notice;

$type = $type ? $type : "mail";

$msg = "execute time is longer than 3s ({$resource->timedelta}s)";

$title[] = $msg;

$content[] = $msg;

}

}

$service = (string)$xml->resource->service;

$title = implode("\n", $title);

$content = implode("\n", $content);

if ($type) {

send_alert($title, $content, $type, $service, $runtime_name, $runtime_addr);

}

$status = ( $type == "sms" ) ? "critical" : (( $type == "mail" ) ? "notice" : "ok");

save($service, $title, $content, $status);

}

config.php

$config["log"]["mysql"] = 1;

$config["log"]["file"] = 1;

/*$config["runtime"]["bigapp"]["name"] = "***";

$config["runtime"]["bigapp"]["server"] = array("*****");

$config["runtime"]["bigapp"]["header"] = "";

$config["runtime"]["ent"]["server"] = array("*****");

$config["runtime"]["ent"]["header"] = "";*/

$config["runtime"]["common"]["name"] = "***";

$config["runtime"]["common"]["server"] = array("****");

#$config["runtime"]["common"]["header"] = array("****");

$config["runtime"]["common"]["header"] = array("****");

$config["runtime"]["bigapp"]["name"] = "***";

$config["runtime"]["bigapp"]["server"] = array("***");

#$config["runtime"]["bigapp"]["header"] = array("***");

$config["runtime"]["bigapp"]["header"] = array("***");

$config["runtime"]["ent"]["name"] = "****";

$config["runtime"]["ent"]["server"] = array("***");

$config["runtime"]["ent"]["header"] = array("****");

/*

$config["runtime"]["java1"]["name"] = "***";

$config["runtime"]["java1"]["server"] = array("***");

$config["runtime"]["java1"]["header"] = array("****");

$config["runtime"]["java2"]["name"] = "***";

$config["runtime"]["java2"]["server"] = array("****");

$config["runtime"]["java2"]["header"] = array("****");

*/

$config["alert_gateway"] = "***********";

$config["services"] = array('****');

$config["log_file"] = "/data0/logs/monitor_new.log";

$config["mysql"]["host"] = "****";

$config["mysql"]["port"] = "***";

$config["mysql"]["user"] = "***";

$config["mysql"]["passwd"] = "***";

$config["mysql"]["db"] = "***";

$config["filter"] = array(

"kv" => "kvdb",

"cron" => "saecron",

"memcache" => "memcachedx",

"mysql" => "mysqld",

"appconfig" => "webruntime",

"curl" => "fetchurl",

"dom" => "webruntime",

"apibus" => "webruntime",

"header" => "webruntime",

"hook" => "webruntime",

"https" => "fetchurl",

"httpwrapper" => "webruntime",

"localdir" => "webruntime",

"sinatapi" => "fetchurl",

"upload" => "fetchurl",

"image" => "webinternal",

"vcode" => "webinternal",

"taskqueue" => "webinternal",

"rank" => "webinternal",

"segment" => "webinternal",

"proxy" => "webruntime",

);

function.php

function mysql_con()

{

global $config;

$conn = mysql_connect("{$config["mysql"]["host"]}:{$config["mysql"]["port"]}",

$config["mysql"]["user"], $config["mysql"]["passwd"]) or errors_log(mysql_error($conn));

mysql_select_db($config["mysql"]["db"], $conn);

return $conn;

}

function errors_log($msg)

{

global $config;

if (isset($config["log"]["file"]) && $config["log"]["file"]) {

$now = date("Y-m-d H:i:s");

$msg = str_replace("\n", " ", $msg);

error_log("[$now]$msg\n", 3, $config["log_file"]);

}

}

function send_alert($msg, $content ,$type, $service, $role, $node, $log = true)

{

global $config;

$msg = "$msg [$role::$node::$service]\n";

if (DEBUG) {

echo $msg . "\n";

echo $content . "\n";

return;

}

if ($log) {

errors_log($msg);

}

$grade = $type == "sms" ? 1 : 2;

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POST, 1);

$data['service'] = "$service";//$service;

$data['checkpoint'] = "monitor";//monitor

$data['title'] = $msg;

$data['content'] = $content;

$data['cluster'] = "public";

$data['grade'] = $grade;

curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

curl_setopt($ch,CURLOPT_URL,$config["alert_gateway"]);

$res = curl_exec($ch);

}

init.php

require ROOT . "function.php";

require ROOT . "config.php";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值