1. laravel测试连接可用性

1.1. 创建命令

php artisan make:console Testing --command app:test

1.2. 附上代码

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;

class Testing extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'TEst';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->testDB();
        $this->info('-------------------------------------------------- 华丽的分割线 ------------------------------------------------');
        $this->testRedis();
    }

    public function testRedis()
    {
        $RedisCons = Config::get("database.redis");
        $this->warn("= = = == = = = = = = =   Redis Start = = = = = = = = = = = = ");
        foreach ($RedisCons as $key => $val) {
            if (is_array($val)) {
                $this->warn($key);
                try {
                    if (isset($val['host']) && isset($val['port'])) {
                        $host = $val['host'];
                        $port = $val['port'];
                        $this->telnetIpPort($host, $port);
                        $this->pingAddress($host);
                        $msg = \RedisDB::connection("{$key}")->ping();
                        $this->info("{$key} ===>>> {$msg}");
                        $this->info("---------------------------------------------------------------------------------------------------------");
                    }
                }
                catch (\Exception $e) {
                    $this->error("{$key} ===>>> {$e->getMessage()}");
                }
            }
        }
        $this->warn("= = = == = = = = = = =   Redis End= = = = = = = = = = = = ");
    }

    public function testDB()
    {
        $DbCons = Config::get("database.connections");
        $this->warn("= = = == = =  = = = ==   DB/Mongo Start = = = = = = = = = = = = ");
        foreach ($DbCons as $key => $val) {
            if (is_array($val)) {
                if (isset($val['host']) && isset($val['port'])) {
                    $this->warn($key);
                    $host = $val['host'];
                    $port = $val['port'];
                    try {
                        $this->telnetIpPort($host, $port);
                        $this->pingAddress($host);
                    }
                    catch (\Exception $e) {
                        $this->error("{$key} ===>>> {$e->getMessage()}");
                    }
                    $this->info("---------------------------------------------------------------------------------------------------------");
                }
            }
        }
        $this->warn("= = = == = = = = = = =   DB/Mongo End= = =  = = = == = = = = ");
    }

    /**
     * 使用PHP检测能否ping通IP或域名
     * @param type $address
     * @return boolean
     */
    function pingAddress($address)
    {
        $status     = -1;
        $pingresult = "";
        if (strcasecmp(PHP_OS, 'WINNT') === 0) {
            // Windows 服务器下
            $pingresult = exec("ping -n 1 {$address}", $outcome, $status);
        }
        elseif (strcasecmp(PHP_OS, 'Linux') === 0) {
            // Linux 服务器下
            $pingresult = exec("ping -c 1 {$address}", $outcome, $status);
        }
        else {
            $pingresult = exec("ping -c 1 {$address}", $outcome, $status);
        }
        $this->info("PING ==>> { $address } +++ CODE[{$status}] _ MSG[{$pingresult}] ");
    }

    function telnetIpPort($ip, $port)
    {
        try {
            $socket    = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>2, "usec"=>0 ) );
            socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>3, "usec"=>0 ) );
            $errorcode = socket_last_error();
            if ($errorcode > 0) {
                $errormsg = socket_strerror($errorcode);
                throw new \Exception($errormsg);
            }
            $connect_res = socket_connect($socket, $ip, $port);
            if (!$connect_res) {
                throw new \Exception("连接失败");
            }
            $send_msg_res = socket_write($socket, "Test", strlen("Test"));
            if ($send_msg_res === false) {
                throw new \Exception("发送测试数据失败");
            }
            $this->info("Telnet {$ip}:{$port}  is OK ,msgLen:{$send_msg_res}");
            socket_close($socket);
            return;
        }
        catch (\Exception $e) {
            $this->error("Telnet {$ip}:{$port}  is Err[{$e->getMessage()}]");
            return;
        }
    }
}

results matching ""

    No results matching ""