Skip to content
Snippets Groups Projects
Commit 435c8247 authored by Tibo's avatar Tibo
Browse files

Add PHP CodeSniffer

parent 0e869b73
No related branches found
No related tags found
No related merge requests found
Pipeline #1560 passed
Showing
with 254 additions and 163 deletions
......@@ -7,22 +7,26 @@ namespace App;
*
* @author tibo
*/
abstract class AbstractSensor implements Sensor {
abstract class AbstractSensor implements Sensor
{
/**
*
* @var \App\Server
*/
private $server;
public function __construct(\App\Server $server) {
public function __construct(\App\Server $server)
{
$this->server = $server;
}
protected function getServer() {
protected function getServer()
{
return $this->server;
}
public function getName() : string {
public function getName() : string
{
return (new \ReflectionClass($this))->getShortName();
}
......@@ -32,7 +36,8 @@ abstract class AbstractSensor implements Sensor {
* @param string $field
* @return
*/
function getLastRecord (string $field) {
public function getLastRecord(string $field)
{
return $this->server->lastRecordContaining($field);
}
......@@ -45,10 +50,12 @@ abstract class AbstractSensor implements Sensor {
* @param type $count
* @return type
*/
function getLastRecords($field, $count) {
public function getLastRecords($field, $count)
{
$records = \Mongo::get()->monitoring->records->find(
["server_id" => $this->server->id],
["limit" => $count, "sort" => ["_id" => -1]]);
["server_id" => $this->server->id],
["limit" => $count, "sort" => ["_id" => -1]]
);
$results = [];
foreach ($records as $record) {
......@@ -60,7 +67,8 @@ abstract class AbstractSensor implements Sensor {
return $results;
}
public static function getColorForStatus($status) {
public static function getColorForStatus($status)
{
switch ($status) {
case 0:
return 'success';
......@@ -73,7 +81,8 @@ abstract class AbstractSensor implements Sensor {
}
}
public static function getBadgeForStatus($status) {
public static function getBadgeForStatus($status)
{
switch ($status) {
case 0:
return '<span class="badge badge-success">OK</span>';
......@@ -86,7 +95,8 @@ abstract class AbstractSensor implements Sensor {
}
}
public function getBadge() {
public function getBadge()
{
return self::getBadgeForStatus($this->status());
}
}
......@@ -36,8 +36,9 @@ class OrganizationController extends Controller
public function index()
{
return view(
"organization.index",
array("organizations" => Auth::user()->organizations->sortBy("name")));
"organization.index",
array("organizations" => Auth::user()->organizations->sortBy("name"))
);
}
/**
......
......@@ -10,28 +10,34 @@ use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
class OrganizationUserController extends Controller {
class OrganizationUserController extends Controller
{
public function __construct() {
public function __construct()
{
$this->middleware('auth');
}
public function index() {
public function index()
{
}
/** Show form **/
public function create(Organization $organization) {
public function create(Organization $organization)
{
return view("organization.user.create", ["organization" => $organization]);
}
protected function validator(array $data) {
protected function validator(array $data)
{
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users',
]);
}
/** add user to organization **/
public function store(Organization $organization, Request $request) {
public function store(Organization $organization, Request $request)
{
$current_user = Auth::user();
if (! $current_user->ownsOrganization($organization)) {
return redirect(route("dashboard"));
......@@ -59,16 +65,19 @@ class OrganizationUserController extends Controller {
return redirect(route("dashboard"));
}
public function show($id) {
public function show($id)
{
}
public function edit($id) {
public function edit($id)
{
}
public function update($id) {
public function update($id)
{
}
public function destroy($id) {
public function destroy($id)
{
}
}
\ No newline at end of file
}
......@@ -42,7 +42,8 @@ class StatusChangeDetection implements ShouldQueue
}
}
public function detectChangeForServer(Server $server) {
public function detectChangeForServer(Server $server)
{
$last_change = StatusChange::getLastChangeForServer($server->id);
$current_status = $server->status();
......
......@@ -2,10 +2,11 @@
namespace App\Mongo;
class Facade extends \Illuminate\Support\Facades\Facade {
class Facade extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor() {
protected static function getFacadeAccessor()
{
return 'mongo';
}
}
......@@ -4,16 +4,18 @@ namespace App\Mongo;
use MongoDB\Client;
class Service {
class Service
{
private $mongo;
public function __construct($uri = null, $uriOptions = [], $driverOptions = []) {
public function __construct($uri = null, $uriOptions = [], $driverOptions = [])
{
$this->mongo = new Client($uri, $uriOptions, $driverOptions);
}
public function get() {
public function get()
{
return $this->mongo;
}
}
......@@ -2,12 +2,14 @@
namespace App\Mongo;
class ServiceProvider extends \Illuminate\Support\ServiceProvider {
class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
protected $defer = true;
public function register() {
$this->app->singleton('mongo', function($app) {
public function register()
{
$this->app->singleton('mongo', function ($app) {
$config = $app->make('config');
$uri = $config->get('services.mongo.uri');
$uriOptions = $config->get('services.mongo.uriOptions');
......@@ -16,8 +18,8 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider {
});
}
public function provides() {
public function provides()
{
return ['mongo'];
}
}
......@@ -8,11 +8,13 @@ class Organization extends Model
{
public function users() {
public function users()
{
return $this->belongsToMany("App\User");
}
public function servers() {
public function servers()
{
return $this->hasMany("App\Server");
}
}
......@@ -18,4 +18,4 @@ interface Sensor
* @return string
*/
public function getName() : string;
}
\ No newline at end of file
}
......@@ -7,14 +7,16 @@ namespace App\Sensor;
*
* @author tibo
*/
class ClientVersion extends \App\AbstractSensor {
class ClientVersion extends \App\AbstractSensor
{
const MANIFEST = "https://gitlab.cylab.be/cylab/monitoring/raw/master/php-client/release/manifest.json";
public function latestVersion() {
public function latestVersion()
{
$ctx = stream_context_create(array('http' => ['timeout' => 5]));
$json = @ \file_get_contents(self::MANIFEST, false, $ctx);
if ($json === FALSE) {
if ($json === false) {
return "";
}
......@@ -22,12 +24,14 @@ class ClientVersion extends \App\AbstractSensor {
}
//put your code here
public function report() {
public function report()
{
return "<p>Installed version: " . $this->getServer()->clientVersion() . "</p>"
. "<p>Latest client version: " . $this->latestVersion() . "</p>";
}
public function status() {
public function status()
{
if ($this->getServer()->clientVersion() === $this->latestVersion()) {
return self::STATUS_OK;
}
......
......@@ -13,15 +13,18 @@ namespace App\Sensor;
*
* @author tibo
*/
class Date extends \App\AbstractSensor {
class Date extends \App\AbstractSensor
{
//put your code here
public function report() {
public function report()
{
return "<p>Time drift: " . $this->delta() . " seconds</p>";
}
public function status() {
public function status()
{
$delta = $this->delta();
if ($delta == null) {
return self::STATUS_UNKNOWN;
......@@ -34,7 +37,8 @@ class Date extends \App\AbstractSensor {
return self::STATUS_OK;
}
public function delta() {
public function delta()
{
$record = $this->getLastRecord("date");
if ($record === null) {
return null;
......@@ -42,5 +46,4 @@ class Date extends \App\AbstractSensor {
return $record->date - $record->time;
}
}
<?php
namespace App\Sensor;
/**
* Description of Disk
*
* @author tibo
*/
class Disk
{
public $port = "";
public $box = 0;
public $bay = 0;
public $type = "";
public $size = "";
public $status = "";
}
......@@ -7,11 +7,13 @@ namespace App\Sensor;
*
* @author tibo
*/
class Disks extends \App\AbstractSensor {
class Disks extends \App\AbstractSensor
{
const REGEXP = "/\\n([A-z\/0-9:\\-\\.]+)\s*([0-9]+)\s*([0-9]+)\s*([0-9]+)\s*([0-9]+)%\s*([A-z\/0-9]+)/";
public function report() {
public function report()
{
$record = $this->getLastRecord("disks");
if ($record == null) {
return "<p>No data available...</p>";
......@@ -21,13 +23,16 @@ class Disks extends \App\AbstractSensor {
$return = "<table class='table table-sm'>";
$return .= "<tr><th></th><th></th><th>Usage</th></tr>";
foreach ($partitions as $partition) {
$return .= "<tr><td>" . $partition->filesystem . "</td><td>" . $partition->mounted . "</td><td>" . $partition->usedPercent() . "%</td></tr>";
$return .= "<tr><td>" . $partition->filesystem . "</td><td>"
. $partition->mounted . "</td><td>" . $partition->usedPercent()
. "%</td></tr>";
}
$return .= "</table>";
return $return;
}
public function status() {
public function status()
{
$record = $this->getLastRecord("disks");
if ($record == null) {
return self::STATUS_UNKNOWN;
......@@ -50,7 +55,8 @@ class Disks extends \App\AbstractSensor {
public static $skip_fs = ["none", "tmpfs", "shm"];
public function parse($string) {
public function parse($string)
{
$values = array();
preg_match_all(self::REGEXP, $string, $values);
$partitions = array();
......@@ -71,14 +77,3 @@ class Disks extends \App\AbstractSensor {
return $partitions;
}
}
class Partition {
public $filesystem = "";
public $blocks = 0;
public $used = 0;
public $mounted = "";
public function usedPercent() {
return round(100.0 * $this->used / $this->blocks);
}
}
......@@ -13,20 +13,22 @@ namespace App\Sensor;
*
* @author tibo
*/
class Heartbeat extends \App\AbstractSensor {
class Heartbeat extends \App\AbstractSensor
{
//put your code here
public function report() {
public function report()
{
return "<p>Last heartbeat received "
. $this->getServer()->lastRecordTime()->diffForHumans() . "</p>";
}
public function status() {
public function status()
{
$record = $this->getServer()->lastRecord();
if ($record === null) {
$delta = PHP_INT_MAX;
} else {
$delta = \time() - $record->time;
}
......
......@@ -9,9 +9,11 @@ use \App\AbstractSensor;
*
* @author tibo
*/
class Ifconfig extends AbstractSensor {
class Ifconfig extends AbstractSensor
{
public function report() {
public function report()
{
$interfaces = [];
$record = $this->getLastRecord("ifconfig");
......@@ -23,10 +25,11 @@ class Ifconfig extends AbstractSensor {
"interfaces" => $interfaces]);
}
public function points() {
public function points()
{
// Get records in time ascending order
$records = $this->getLastRecords("ifconfig", 289);
usort($records, function($r1, $r2) {
usort($records, function ($r1, $r2) {
return $r1->time > $r2->time ? 1 : -1;
});
......@@ -66,8 +69,9 @@ class Ifconfig extends AbstractSensor {
$delta = 0;
}
$dataset[$iname . "/RX"]["points"][] = new Point(
$interface->time * 1000,
round(8 / 1024 * $delta / $delta_time));
$interface->time * 1000,
round(8 / 1024 * $delta / $delta_time)
);
// TX
$delta = $interface->tx - $previous_value->tx;
......@@ -76,20 +80,20 @@ class Ifconfig extends AbstractSensor {
$delta = 0;
}
$dataset[$iname . "/TX"]["points"][] = new Point(
$interface->time * 1000,
round(8 / 1024 * $delta / $delta_time));
$interface->time * 1000,
round(8 / 1024 * $delta / $delta_time)
);
// Keep current value for next record
$current_value[$iname] = $interface;
}
}
return array_values($dataset);
}
public function status() {
public function status()
{
return self::STATUS_OK;
}
......@@ -97,7 +101,8 @@ class Ifconfig extends AbstractSensor {
const IPV4 = '/^\\s+inet addr:(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/m';
const RXTX = '/^\\s+RX bytes:(\\d+) .*TX bytes:(\\d+)/m';
public function parseIfconfigRecord($record) {
public function parseIfconfigRecord($record)
{
$interfaces = $this->parseIfconfig($record->ifconfig);
foreach ($interfaces as $interface) {
$interface->time = $record->time;
......@@ -112,7 +117,8 @@ class Ifconfig extends AbstractSensor {
* @param type $string
* @return \App\Sensor\NetworkInterface[]
*/
public function parseIfconfig($string) {
public function parseIfconfig($string)
{
$allowed_prefixes = ["en", "eth", "wl"];
......@@ -158,7 +164,8 @@ class Ifconfig extends AbstractSensor {
return $filtered;
}
public function pregMatchOne($pattern, $string) {
public function pregMatchOne($pattern, $string)
{
$matches = array();
if (preg_match($pattern, $string, $matches) === 1) {
return $matches[1];
......@@ -167,26 +174,3 @@ class Ifconfig extends AbstractSensor {
return false;
}
}
class NetworkInterface {
public $name;
public $address;
public $rx;
public $tx;
public $time;
public function humanReadableSize($bytes, $decimals = 2) {
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
public function humanReadableRx() {
return $this->humanReadableSize($this->rx);
}
public function humanReadableTx() {
return $this->humanReadableSize($this->tx);
}
}
......@@ -7,11 +7,13 @@ namespace App\Sensor;
*
* @author tibo
*/
class Inodes extends \App\AbstractSensor {
class Inodes extends \App\AbstractSensor
{
const REGEXP = "/\\n([A-z\/0-9:\\-\\.]+)\s*([0-9]+)\s*([0-9]+)\s*([0-9]+)\s*([0-9]+)%\s*([A-z\/0-9]+)/";
public function report() {
public function report()
{
$record = $this->getLastRecord("inodes");
if ($record == null) {
return "<p>No data availabe...</p>";
......@@ -21,14 +23,16 @@ class Inodes extends \App\AbstractSensor {
$return = "<table class='table table-sm'>";
$return .= "<tr><th></th><th></th><th>Usage</th></tr>";
foreach ($disks as $disk) {
$return .= "<tr><td>" . $disk->filesystem . "</td><td>" . $disk->mounted . "</td><td>" . $disk->usedPercent() . "%</td></tr>";
$return .= "<tr><td>" . $disk->filesystem . "</td><td>"
. $disk->mounted . "</td><td>" . $disk->usedPercent()
. "%</td></tr>";
}
$return .= "</table>";
return $return;
}
public function status() {
public function status()
{
$record = $this->getLastRecord("inodes");
if ($record == null) {
return self::STATUS_UNKNOWN;
......@@ -36,7 +40,7 @@ class Inodes extends \App\AbstractSensor {
$all_status = [];
foreach ($this->parse($record->inodes) as $disk) {
/* @var $disk DiskInodes */
/* @var $disk InodesDisk */
$status = self::STATUS_OK;
if ($disk->usedPercent() > 80) {
$status = self::STATUS_WARNING;
......@@ -49,7 +53,8 @@ class Inodes extends \App\AbstractSensor {
return max($all_status);
}
public function parse($string) {
public function parse($string)
{
$values = array();
preg_match_all(self::REGEXP, $string, $values);
$disks = array();
......@@ -60,7 +65,7 @@ class Inodes extends \App\AbstractSensor {
continue;
}
$disk = new DiskInodes();
$disk = new InodesDisk();
$disk->filesystem = $values[1][$i];
$disk->inodes = $values[2][$i];
$disk->used = $values[3][$i];
......@@ -70,14 +75,3 @@ class Inodes extends \App\AbstractSensor {
return $disks;
}
}
class DiskInodes {
public $filesystem = "";
public $inodes = 0;
public $used = 0;
public $mounted = "";
public function usedPercent() {
return round(100.0 * $this->used / $this->inodes);
}
}
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Sensor;
/**
* Description of InodesDisk
*
* @author tibo
*/
class InodesDisk
{
public $filesystem = "";
public $inodes = 0;
public $used = 0;
public $mounted = "";
public function usedPercent()
{
return round(100.0 * $this->used / $this->inodes);
}
}
......@@ -4,36 +4,42 @@ namespace App\Sensor;
use \App\AbstractSensor;
/**
* Description of LoadAvg
*
* @author tibo
*/
class LoadAvg extends AbstractSensor {
class LoadAvg extends AbstractSensor
{
public function report() {
public function report()
{
return view("agent.loadavg", [
"current_load" => $this->getLastValue(),
"server" => $this->getServer()]);
}
public function loadPoints() {
public function loadPoints()
{
$records = $this->getLastRecords("loadavg", 288);
$points = [];
foreach ($records as $record) {
$points[] = new Point(
$record->time * 1000, $this->parse($record->loadavg));
$record->time * 1000,
$this->parse($record->loadavg)
);
}
return $points;
}
public function status() {
public function status()
{
return self::STATUS_OK;
}
public function getLastValue() {
public function getLastValue()
{
$record = $this->getLastRecord("loadavg");
if ($record == null) {
return "no data...";
......@@ -42,7 +48,8 @@ class LoadAvg extends AbstractSensor {
return $this->parse($field);
}
function parse($string) {
public function parse($string)
{
return current(explode(" ", $string));
}
}
......@@ -9,40 +9,49 @@ use \App\AbstractSensor;
*
* @author tibo
*/
class MemInfo extends AbstractSensor {
class MemInfo extends AbstractSensor
{
public function report() {
public function report()
{
return view("agent.meminfo", ["server" => $this->getServer()]);
}
public function usedMemoryPoints() {
public function usedMemoryPoints()
{
$records = $this->getLastRecords("memory", 288);
$used = [];
foreach ($records as $record) {
$meminfo = $this->parseMeminfo($record->memory);
$used[] = new Point(
$record->time * 1000, $meminfo->used() / 1000);
$record->time * 1000,
$meminfo->used() / 1000
);
}
return $used;
}
public function cachedMemoryPoints() {
public function cachedMemoryPoints()
{
$records = $this->getLastRecords("memory", 288);
$points = [];
foreach ($records as $record) {
$meminfo = $this->parseMeminfo($record->memory);
$points[] = new Point(
$record->time * 1000, $meminfo->cached / 1000);
$record->time * 1000,
$meminfo->cached / 1000
);
}
return $points;
}
public function status() {
public function status()
{
return self::STATUS_OK;
}
......@@ -51,32 +60,19 @@ class MemInfo extends AbstractSensor {
const MEMFREE = "/^MemFree:\\s+([0-9]+) kB$/m";
const MEMCACHED = "/^Cached:\\s+([0-9]+) kB$/m";
public function parseMeminfo($string) {
public function parseMeminfo($string)
{
return new Memory(
$this->pregMatchOne(self::MEMTOTAL, $string),
$this->pregMatchOne(self::MEMFREE, $string),
$this->pregMatchOne(self::MEMCACHED, $string));
$this->pregMatchOne(self::MEMTOTAL, $string),
$this->pregMatchOne(self::MEMFREE, $string),
$this->pregMatchOne(self::MEMCACHED, $string)
);
}
public function pregMatchOne($pattern, $string) {
public function pregMatchOne($pattern, $string)
{
$matches = array();
preg_match($pattern, $string, $matches);
return $matches[1];
}
}
class Memory {
public $total;
public $free;
public $cached;
public function __construct($total, $free, $cached) {
$this->total = $total;
$this->free = $free;
$this->cached = $cached;
}
public function used() {
return $this->total - $this->free - $this->cached;
}
}
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Sensor;
/**
* Description of Memory
*
* @author tibo
*/
class Memory
{
public $total;
public $free;
public $cached;
public function __construct($total, $free, $cached)
{
$this->total = $total;
$this->free = $free;
$this->cached = $cached;
}
public function used()
{
return $this->total - $this->free - $this->cached;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment