服务器域名访问监控

<?php
define("LAST_SEND_TIMES", 2);//发送告警间隔时间
define("PING_FAIL_TIMES", 3);//连续ping失败几次发送告警
define("RE_COUNT_TIMES", 60);//间隔多久重新统计失败次数
//机器ip=>域名映射
$_domain_arr = array(
    'ip' => array(
        'domain' => array(
            'baiducom',
        ),
        'admin' => ''
    ),
    'ip' => array(
        'domain' => array(),
        'admin' => ''),
);

class Check_Domain_Helper
{

    //判断是linux系统还是win系统
    public static function check_machine_os()
    {
        //判断是linux系统还是win系统
        if (PATH_SEPARATOR == ':' || DIRECTORY_SEPARATOR == '/')//Linux
        {
            return "Linux";
        } else //windows
        {
            return 'Windows';
        }
        return false;
    }

    /**获取服务器ip地址
     * @return array|false|string
     */
    public static function get_server_ip()
    {
        $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
        $os = Check_Domain_Helper::check_machine_os();
        if ($os === 'Windows') {
            //获取操作系统为win2000/xp、win7的本机IP真实地址
            exec("ipconfig", $out, $stats);
            if (!empty($out)) {
                foreach ($out AS $row) {
                    if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv6")) {
                        $tmpIp = explode(":", $row);
                        if (preg_match($preg, trim($tmpIp[1]))) {
                            return trim($tmpIp[1]);
                        }
                    }
                }
            }
        } else {//根据机器选择 linux  exec('ifconfig eth1 |grep "inet "| cut -f 2 -d ":"|cut -f 1 -d " "');
            return exec('ifconfig eth1 |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " "');
        }
        return '127.0.0.1';
    }


    /**ping操作
     * @param $address  ip or domain
     * @param int $times ping times
     * @return array
     */
    function do_ping_domain()
    {
        global $_domain_arr;
        $times = 1;
        $response = array();
        $result = array();
        $status = -1;
        $machie = Check_Domain_Helper::check_machine_os();//判断是lin系统还是win系统
        $command = ($machie == 'Linux') ? '-c' : '-n';
        $server_ip = Check_Domain_Helper::get_server_ip();
        $domains = $_domain_arr["{$server_ip}"]['domain'];//获取机器需要访问的域名
        $admin = $_domain_arr["{$server_ip}"]['admin'];
        $ipkeys = array_keys($_domain_arr);

        if (!in_array($server_ip, $ipkeys))//机器不在ip列表中
        {
            return;
        }

        foreach ($domains as $domain) {
            $result["{$domain}"]['lastsendtime'] = 0;
            $result["{$domain}"]['faliltimes'] = 0;//统计失败次数
            $result["{$domain}"]['lastfaliltime']=0;
        }

        //失败告警,半天发送一次邮件
        while (1) {

            $currtime = time();
            
            foreach ($domains as $domain) {
                $gap_time = abs($currtime - $result["{$domain}"]['lastfaliltime']);
                if( $gap_time > RE_COUNT_TIMES)
                $result["{$domain}"]['faliltimes'] = 0;
            }
            
            foreach ($domains as $domain) {

                //exec("ping {$domain} -n $times", $response, $status);
                exec("ping {$domain} {$command} $times", $response, $status);

                if (0 == $status) {
                    $status = true;
                } else {
                    $status = false;
                }

                $result["{$domain}"]['response'] = $response;
                $result["{$domain}"]['status'] = $status;
                //上次发送时间大于间隔时间,标记为需要发送
                $is_need_send = ($currtime - $result["{$domain}"]['lastsendtime']) > LAST_SEND_TIME ? true : false;
                //失败次数+1
                $result["{$domain}"]['faliltimes']++;
                //记录最新失败时间
                $result["{$domain}"]['lastfaliltime']=$currtime;
                
                if (!$result["{$domain}"]['status'] && $is_need_send&&$result["{$domain}"]['faliltimes']>PING_FAIL_TIMES) {
                    $msg = "{$server_ip} Ping {$domain} fail! Machine admin is {$admin}";
                    Check_Domain_Helper::send_Rtx($msg);
                    Check_Domain_Helper::send_WeiXin($msg);
                    $result["{$domain}"]['lastsendtime'] = $currtime;
                    $result["{$domain}"]['faliltimes'] = 0;
                }
                sleep(5);
            }
            sleep(5);
        }
    }
     /**
     * 发送告警
     */
   public static function send_Rtx($msgInfo)
    {
      send();
    }
}
  $mHelper = new Check_Domain_Helper();
  $mHelper->do_ping_domain();
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值