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]]
);
public function hasData() : bool
{
return $this->lastRecord() != null;
}
$last_record = $this->lastRecord();
if ($last_record === null) {
return \Carbon\Carbon::createFromTimestamp(0);
}
return \Carbon\Carbon::createFromTimestamp($last_record->time);
$last_record = $this->lastRecord();
if ($last_record == null) {
return "none";
}
return $last_record->version;
public function lastClientUrl()
{
$client_sensor = new \App\Sensor\ClientVersion($this);
return $client_sensor->latestUrl();
}
return AbstractSensor::getBadgeForStatus($this->status());
}
$sensor_name = \get_class($sensor);
try {
$status_array[$sensor_name] = $sensor->status();
} catch (\Exception $ex) {
$status_array[$sensor_name] = Sensor::STATUS_UNKNOWN;
Log::error("Sensor $sensor_name failed : " . $ex->getTraceAsString());
}
$sensorsNOK = [];
foreach ($this->getSensors() as $sensor) {
if ($sensor->status() > 0) {
$sensorsNOK[] = $sensor;
}
}
return $sensorsNOK;
}
return self::getNameForStatus($this->status());
}
public static function getNameForStatus($status)
{
switch ($status) {
case 0:
return "OK";
case 10:
return "WARNING";
case 20:
return "ERROR";
default:
return "Unknown";
}
return AbstractSensor::getBadgeForStatus($this->status());
}
return AbstractSensor::getColorForStatus($this->status());
}
$sensors = [];
foreach (self::$sensors as $sensor) {
$sensors[] = new $sensor($this);
}
return $sensors;
}
/**
* 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 \App\StatusChange::getLastChangesForServer($this->id, $count);
}