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",

    "p_w_picpath" => "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";