Passed
Push — master ( f3e3bc...6393b2 )
by John
05:45
created

Monitor::check()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 20
c 2
b 0
f 1
dl 0
loc 28
rs 8.9777
cc 6
nc 6
nop 0
1
<?php
2
namespace App\Babel\Extension\noj;
3
4
use App\Babel\Monit\MonitorBase;
5
use App\Models\OJModel;
6
use App\Models\JudgerModel;
7
use Exception;
8
9
class Monitor extends MonitorBase
10
{
11
    public $ocode="noj";
12
    public $oid=null;
13
14
    public function __construct()
15
    {
16
        $this->oid=OJModel::oid($this->ocode);
17
    }
18
19
    public function check()
20
    {
21
        $judgerModel=new JudgerModel();
22
        $serverList=$judgerModel->fetchServer($this->oid);
23
        foreach ($serverList as $server) {
24
            if ($server["available"]==0) {
25
                $this->updateStatus($server["jsid"], -2);
26
                continue;
27
            }
28
29
            $serverURL="http://".$server["host"].":".$server["port"];
30
            try {
31
                $pong=$this->ping($serverURL.'/ping', $server["port"], hash('sha256', $server["token"]));
32
            } catch (Exception $exception) {
33
                \Log::debug($exception);
34
                $this->updateStatus($server["jsid"], 1);
35
                continue;
36
            }
37
38
            if (empty($pong)) {
39
                $this->updateStatus($server["jsid"], 1);
40
                continue;
41
            }
42
43
            if ($pong["status_code"]==200) {
44
                $pong=$pong["body"];
45
                $load=4 * $pong->data->cpu+0.6 * $pong->data->memory;
46
                $this->updateStatus($server["jsid"], 0, $load);
47
            }
48
        }
49
    }
50
51
    private function ping($url, $port, $token)
52
    {
53
        $curl=curl_init();
54
55
        if ($curl===false) {
56
            return [];
57
        }
58
59
        curl_setopt_array($curl, array(
60
            CURLOPT_PORT => $port,
61
            CURLOPT_URL => $url,
62
            CURLOPT_RETURNTRANSFER => true,
63
            CURLOPT_ENCODING => "",
64
            CURLOPT_MAXREDIRS => 10,
65
            CURLOPT_TIMEOUT => 30,
66
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
67
            CURLOPT_CUSTOMREQUEST => "POST",
68
            CURLOPT_POSTFIELDS => "",
69
            CURLOPT_HTTPHEADER => array(
70
                "Content-Type: application/json",
71
                "X-Judge-Server-Token: ".$token,
72
                "cache-control: no-cache"
73
            ),
74
        ));
75
76
        $response=curl_exec($curl);
77
        $err=curl_error($curl);
78
        $httpCode=curl_getinfo($curl, CURLINFO_HTTP_CODE);
79
80
        curl_close($curl);
81
82
        if ($err) {
83
            return [];
84
        } else {
85
            return [
86
                "status_code"=>$httpCode,
87
                "body"=>json_decode($response)
88
            ];
89
        }
90
    }
91
}
92