Newer
Older
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
class Server extends Model
{
protected $fillable = ["token"];
/**
* Last record from this server (used for caching).
* @var String
*/
private $last_record = null;
\App\Sensor\CPUtemperature::class,
$attributes["token"] = str_random(32);
parent::__construct($attributes);
}
if ($this->last_record == null) {
$collection = \Mongo::get()->monitoring->records;
$this->last_record = $collection->findOne(
["server_id" => $this->id],
["sort" => ["_id" => -1]]
);
if ($this->records_1day !== null) {
return $this->records_1day;
}
"server_id" => $this->id,
"time" => ['$gte' => $start]])
->toArray();
public function hasData() : bool
{
return $this->lastRecord() != null;
}
$hearbeat = new \App\Sensor\Heartbeat($this);
return $hearbeat->lastRecordTime($this->lastRecord());
public function clientVersion(array $records) : string
$sensor = new \App\Sensor\ClientVersion($this);
return $sensor->installedVersion($records);
public function lastClientUrl()
{
$client_sensor = new \App\Sensor\ClientVersion($this);
return $client_sensor->latestUrl();
}
return SensorWrapper::getBadgeForStatus($this->status($records));
$status_array[$sensor_name] = $sensor->status($records);
} catch (\Exception $ex) {
$status_array[$sensor_name] = Sensor::STATUS_UNKNOWN;
Log::error("Sensor $sensor_name failed : " . $ex->getTraceAsString());
}
$sensorsNOK[] = $sensor;
}
}
return $sensorsNOK;
}
public static function getNameForStatus(int $status)
case 0:
return "OK";
case 10:
return "WARNING";
case 20:
return "ERROR";
default:
return "Unknown";
}
return SensorWrapper::getBadgeForStatus($this->status($records));
return SensorWrapper::getColorForStatus($this->status($records));
$sensors = [];
foreach (self::$sensors as $sensor) {
/**
* Human readable uptime.
*
* @return string
*/
public function uptime() : string
$record = $this->lastRecord();
if (! isset($record["upaimte"])) {
return "unknown";
{
$pieces = explode(' ', $string);
$uptime = \Carbon\Carbon::now()->subSeconds($pieces[0]);
return $uptime->diffForHumans(null, true);
}
return "";
}
return $this->parseUUID($record->system);
}
const UUID = "/\s*UUID: (.*)/m";
public function parseUUID(string $string) : string
{
$matches = array();
preg_match(self::UUID, $string, $matches);
if (! isset($matches[1])) {
return "unknown";
}
$record = $this->lastRecord();
if (! isset($record["cpu"])) {
return ["threads" => 0,
"cpu" => "unknown"];
$matches = array();
preg_match_all(self::CPU_INFO, $string, $matches);
$result["threads"] = count($matches[0]);
return round($this->memoryTotal() / 1000 / 1000) . " GB";
}
/**
*
* @return int total memory (in KB)
*/
$matches = array();
preg_match(self::MEMINFO, $string, $matches);
$total = $matches[1];
return $total;
$record = $this->lastRecord();
if (! isset($record["lsb"])) {
return "unknown";
}
return $this->parseLsb($record->lsb);
}
const LSB = "/^Description: (.+)$/m";
$matches = [];
preg_match(self::LSB, $string, $matches);
return $matches[1];
const REGEX_MANUFACTURER = "/^\s*Manufacturer: (.*)$/m";
public function parseManufacturer(string $string) : string
$matches = [];
preg_match(self::REGEX_MANUFACTURER, $string, $matches);
if (!isset($matches[1])) {
return "unkwnown";
}
$record = $this->lastRecord();
if (! isset($record["system"])) {
return "unknown";
}
return $this->parseManufacturer($record->system);
}
const REGEX_PRODUCT_NAME = "/^\s*Product Name: (.*)$/m";
public function parseProductName(string $string) : string
preg_match(self::REGEX_PRODUCT_NAME, $string, $matches);
if (!isset($matches[1])) {
return "unkwnown";
}
$record = $this->lastRecord();
if (! isset($record["system"])) {
return "unknown";
return $this->parseProductName($record->system);
return StatusChange::getLastChangesForServer($this->id, $count);