|
2 | 2 |
|
3 | 3 | namespace App\SupportedApps\TrueNASSCALE;
|
4 | 4 |
|
5 |
| -class TrueNASSCALE extends \App\SupportedApps |
| 5 | +class TrueNASSCALE extends \App\SupportedApps implements \App\EnhancedApps |
6 | 6 | {
|
| 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 | + } |
7 | 133 | }
|
0 commit comments