-
-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathTrueNASCORE.php
More file actions
124 lines (102 loc) · 3.18 KB
/
TrueNASCORE.php
File metadata and controls
124 lines (102 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace App\SupportedApps\TrueNASCORE;
class TrueNASCORE extends \App\SupportedApps implements \App\EnhancedApps
{
use TrueNASApiTrait;
public $config;
public function __construct()
{
}
public function test()
{
$test = $this->testApi();
echo $test->status;
}
public function livestats()
{
$status = "inactive";
$data = [];
try {
$systemInfo = $this->apiCall('system.info');
$seconds = $systemInfo['uptime_seconds'] ?? 0;
$data["uptime"] = $this->uptime($seconds);
$alerts = $this->apiCall('alert.list');
list($data["alert_tot"], $data["alert_crit"]) = $this->alerts($alerts);
} catch (\Exception $e) {
$data["uptime"] = "Error";
$data["alert_tot"] = "?";
$data["alert_crit"] = "?";
} finally {
$this->disconnectWebSocket();
}
return parent::getLiveStats($status, $data);
}
public function url($endpoint)
{
$api_url =
parent::normaliseurl($this->config->url) . "api/v2.0/" . $endpoint;
return $api_url;
}
public function attrs()
{
$ignoreTls = $this->getConfigValue("ignore_tls", false);
$apikey = $this->config->apikey;
$attrs["headers"] = [
"content-type" => "application/json",
"Authorization" => "Bearer " . $apikey,
];
if ($ignoreTls) {
$attrs["verify"] = false;
}
return $attrs;
}
public function uptime($inputSeconds)
{
$res = "";
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
$days = floor($inputSeconds / $secondsInADay);
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
if ($days > 0) {
$res =
strval($days) .
"d " .
strval($hours) .
":" .
sprintf("%02d", $minutes);
} else {
$res =
strval($hours) .
":" .
sprintf("%02d", $minutes) .
":" .
sprintf("%02d", $seconds);
}
return $res;
}
public function alerts($alert)
{
$count_total = $count_critical = 0;
foreach ($alert as $key => $value) {
if ($value["dismissed"] == false) {
$count_total += 1;
if (!in_array($value["level"], array("NOTICE", "INFO"))) {
$count_critical += 1;
}
}
}
return array(strval($count_total), strval($count_critical));
}
public function getConfigValue($key, $default = null)
{
return isset($this->config) && isset($this->config->$key)
? $this->config->$key
: $default;
}
}