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
|
|
|
$this->updateStatus($server["jsid"], 1); |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (empty($pong)) { |
38
|
|
|
$this->updateStatus($server["jsid"], 1); |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($pong["status_code"]==200) { |
43
|
|
|
$pong=$pong["body"]; |
44
|
|
|
$load=4 * $pong->data->cpu+0.6 * $pong->data->memory; |
45
|
|
|
$this->updateStatus($server["jsid"], 0, $load); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function ping($url, $port, $token) |
51
|
|
|
{ |
52
|
|
|
$curl=curl_init(); |
53
|
|
|
|
54
|
|
|
if ($curl===false) { |
55
|
|
|
return []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
curl_setopt_array($curl, array( |
59
|
|
|
CURLOPT_PORT => $port, |
60
|
|
|
CURLOPT_URL => $url, |
61
|
|
|
CURLOPT_RETURNTRANSFER => true, |
62
|
|
|
CURLOPT_ENCODING => "", |
63
|
|
|
CURLOPT_MAXREDIRS => 10, |
64
|
|
|
CURLOPT_TIMEOUT => 30, |
65
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
66
|
|
|
CURLOPT_CUSTOMREQUEST => "POST", |
67
|
|
|
CURLOPT_POSTFIELDS => "", |
68
|
|
|
CURLOPT_HTTPHEADER => array( |
69
|
|
|
"Content-Type: application/json", |
70
|
|
|
"X-Judge-Server-Token: ".$token, |
71
|
|
|
"cache-control: no-cache" |
72
|
|
|
), |
73
|
|
|
)); |
74
|
|
|
|
75
|
|
|
$response=curl_exec($curl); |
76
|
|
|
$err=curl_error($curl); |
77
|
|
|
$httpCode=curl_getinfo($curl, CURLINFO_HTTP_CODE); |
78
|
|
|
|
79
|
|
|
curl_close($curl); |
80
|
|
|
|
81
|
|
|
if ($err) { |
82
|
|
|
\Log::error($err); |
83
|
|
|
return []; |
84
|
|
|
} else { |
85
|
|
|
return [ |
86
|
|
|
"status_code"=>$httpCode, |
87
|
|
|
"body"=>json_decode($response) |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|