Skip to content

Commit b338f6b

Browse files
feat: TrueNAS Scale is now enhanced (#694)
* Update app.json * Update TrueNASSCALE.php * Add files for enhanced features * Replace tab with spaces to satisfy linter --------- Co-authored-by: Martijn van der Kleijn <[email protected]>
1 parent 6ccbdda commit b338f6b

File tree

4 files changed

+170
-3
lines changed

4 files changed

+170
-3
lines changed

TrueNASSCALE/TrueNASSCALE.php

+127-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,132 @@
22

33
namespace App\SupportedApps\TrueNASSCALE;
44

5-
class TrueNASSCALE extends \App\SupportedApps
5+
class TrueNASSCALE extends \App\SupportedApps implements \App\EnhancedApps
66
{
7+
public $config;
8+
9+
//protected $login_first = true; // Uncomment if api requests need to be authed first
10+
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST
11+
12+
public function __construct()
13+
{
14+
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
15+
}
16+
17+
public function test()
18+
{
19+
$test = parent::appTest($this->url("core/ping"), $this->attrs());
20+
if ($test->code === 200) {
21+
$data = $test->response;
22+
if ($test->response != '"pong"') {
23+
$test->status = "Failed: " . $data;
24+
}
25+
}
26+
echo $test->status;
27+
}
28+
29+
public function livestats()
30+
{
31+
$status = "inactive";
32+
$data = [];
33+
34+
$res = parent::execute($this->url("system/info"), $this->attrs());
35+
$details = json_decode($res->getBody());
36+
$seconds = $details->uptime_seconds ?? 0;
37+
$data["uptime"] = $this->uptime($seconds);
38+
39+
$res = parent::execute($this->url("alert/list"), $this->attrs());
40+
$details = json_decode($res->getBody(), true);
41+
list($data["alert_tot"], $data["alert_crit"]) = $this->alerts($details);
42+
43+
return parent::getLiveStats($status, $data);
44+
}
45+
46+
public function url($endpoint)
47+
{
48+
$api_url =
49+
parent::normaliseurl($this->config->url) . "api/v2.0/" . $endpoint;
50+
return $api_url;
51+
}
52+
53+
public function attrs()
54+
{
55+
$ignoreTls = $this->getConfigValue("ignore_tls", false);
56+
$apikey = $this->config->apikey;
57+
$attrs["headers"] = [
58+
"content-type" => "application/json",
59+
"Authorization" => "Bearer " . $apikey,
60+
];
61+
62+
if ($ignoreTls) {
63+
$attrs["verify"] = false;
64+
}
65+
66+
return $attrs;
67+
}
68+
69+
public function uptime($inputSeconds)
70+
{
71+
// Adapted from https://stackoverflow.com/questions/8273804/convert-seconds-into-days-hours-minutes-and-seconds
72+
73+
$res = "";
74+
$secondsInAMinute = 60;
75+
$secondsInAnHour = 60 * $secondsInAMinute;
76+
$secondsInADay = 24 * $secondsInAnHour;
77+
78+
// extract days
79+
$days = floor($inputSeconds / $secondsInADay);
80+
81+
// extract hours
82+
$hourSeconds = $inputSeconds % $secondsInADay;
83+
$hours = floor($hourSeconds / $secondsInAnHour);
84+
85+
// extract minutes
86+
$minuteSeconds = $hourSeconds % $secondsInAnHour;
87+
$minutes = floor($minuteSeconds / $secondsInAMinute);
88+
89+
// extract the remaining seconds
90+
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
91+
$seconds = ceil($remainingSeconds);
92+
93+
//$res = strval($days).'d '.strval($hours).':'.sprintf('%02d', $minutes).':'.sprintf('%02d', $seconds);
94+
if ($days > 0) {
95+
$res =
96+
strval($days) .
97+
"d " .
98+
strval($hours) .
99+
":" .
100+
sprintf("%02d", $minutes);
101+
} else {
102+
$res =
103+
strval($hours) .
104+
":" .
105+
sprintf("%02d", $minutes) .
106+
":" .
107+
sprintf("%02d", $seconds);
108+
}
109+
return $res;
110+
}
111+
112+
public function alerts($alert)
113+
{
114+
$count_total = $count_critical = 0;
115+
foreach ($alert as $key => $value) {
116+
if ($value["dismissed"] == false) {
117+
$count_total += 1;
118+
if (!in_array($value["level"], array("NOTICE", "INFO"))) {
119+
$count_critical += 1;
120+
}
121+
}
122+
}
123+
124+
return array(strval($count_total), strval($count_critical));
125+
}
126+
127+
public function getConfigValue($key, $default = null)
128+
{
129+
return isset($this->config) && isset($this->config->$key)
130+
? $this->config->$key
131+
: $default;
132+
}
7133
}

TrueNASSCALE/app.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"website": "https://www.truenas.com/truenas-scale",
55
"license": "BSD 3-Clause \"New\" or \"Revised\" License",
66
"description": "TrueNAS® SCALE is an Open Source Hyperconverged Infrastructure (HCI) solution. Built on TrueNAS CORE, SCALE adds Linux Containers, VMs (KVM), and scale-out ZFS storage capabilities.",
7-
"enhanced": false,
7+
"enhanced": true,
88
"tile_background": "dark",
99
"icon": "truenasscale.png"
10-
}
10+
}

TrueNASSCALE/config.blade.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
2+
<div class="items">
3+
<div class="input">
4+
<label>{{ strtoupper(__('app.url')) }}</label>
5+
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
6+
</div>
7+
<div class="input">
8+
<label>{{ __('app.apps.apikey') }}</label>
9+
{!! Form::text('config[apikey]', isset($item) ? $item->getconfig()->apikey : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
10+
</div>
11+
<div class="input">
12+
<label>Skip TLS verification</label>
13+
<div class="toggleinput" style="margin-top: 26px; padding-left: 15px;">
14+
{!! Form::hidden('config[ignore_tls]', 0, ['class' => 'config-item', 'data-config' => 'ignore_tls']) !!}
15+
<label class="switch">
16+
<?php
17+
$checked = false;
18+
if (isset($item) && !empty($item) && isset($item->getconfig()->ignore_tls)) {
19+
$checked = $item->getconfig()->ignore_tls;
20+
}
21+
$set_checked = $checked ? ' checked="checked"' : '';
22+
?>
23+
<input type="checkbox" class="config-item" data-config="ignore_tls" name="config[ignore_tls]" value="1" <?php echo $set_checked; ?> />
24+
<span class="slider round"></span>
25+
</label>
26+
</div>
27+
</div>
28+
<div class="input">
29+
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
30+
</div>
31+
</div>

TrueNASSCALE/livestats.blade.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<ul class="livestats">
2+
<li>
3+
<span class="title">Uptime</span>
4+
<strong>{!! $uptime !!}</strong>
5+
</li>
6+
<li>
7+
<span class="title">Alerts</span>
8+
<strong>{!! $alert_tot !!} / <span style="background-color: #d64d55; padding: 3px; color: white; font-size: 12px; border-radius: 3px; opacity: 1;">{!! $alert_crit !!}</span></strong>
9+
</li>
10+
</ul>

0 commit comments

Comments
 (0)