Skip to content

Commit

Permalink
Add Funtionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSantiago82 committed Feb 28, 2021
1 parent 8e63bb9 commit 58e9f16
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 50 deletions.
14 changes: 10 additions & 4 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?php

/*
* You can place your custom package configuration in here.
*/
return [

];
/*
|--------------------------------------------------------------------------
| Monitoring Configuration File
|--------------------------------------------------------------------------
*/

'key' => env('MONITOR_KEY'),


];
8 changes: 0 additions & 8 deletions src/Monitor.php

This file was deleted.

67 changes: 67 additions & 0 deletions src/MonitorClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace chrissantiago82\monitor;


class MonitorClass
{

public $results = [];

function __construct()
{
$this->getDiskSpace();
$this->getMemory();
$this->getCpuLoad();
}


protected function getDiskSpace()
{
$this->results['freeDiskPro'] = round(100/(disk_total_space("/") / disk_free_space("/")), 1);
$this->results['freeDiskGB'] = round(disk_free_space("/") / (1000 * 1000 * 1000), 1);
}

protected function getMemory()
{
//RAM usage
$free = shell_exec('free');
$free = (string) trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
$usedmem = $mem[2];
$usedmemInGB = number_format($usedmem / 1048576, 2) . ' GB';
$memory1 = $mem[2] / $mem[1] * 100;
$memory = round($memory1, 1);
$fh = fopen('/proc/meminfo', 'r');
$mem = 0;
while ($line = fgets($fh)) {
$pieces = array();
if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
$mem = $pieces[1];
break;
}
}
fclose($fh);
$totalram = number_format($mem / 1048576, 2) . ' GB';

$this->results['memoryLoad'] = $memory;


}

protected function getCpuLoad()
{
$load2 = sys_getloadavg();

$command = "cat /proc/cpuinfo | grep processor | wc -l";
$coreCount = (int) shell_exec($command);
$load2 = round($load2[2] / $coreCount, 2);

$this->results['cpuLoad'] = $load2;
}


}
33 changes: 33 additions & 0 deletions src/MonitorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace chrissantiago82\monitor;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class MonitorController extends Controller
{

public function monitor($key)
{

if (config('krebsmonitor.key') === null) {
return;
}

if (config('krebsmonitor.key') != $key) {
return;
}

//$monitorClass = new Monitor();

$monitorClass = new MonitorClass();


return response()->json($monitorClass->results);

}

}
2 changes: 1 addition & 1 deletion src/MonitorFacade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Chrissantiago82\Monitor;
namespace chrissantiago82\monitor;

use Illuminate\Support\Facades\Facade;

Expand Down
44 changes: 7 additions & 37 deletions src/MonitorServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Chrissantiago82\Monitor;
namespace chrissantiago82\monitor;

use Illuminate\Support\ServiceProvider;

Expand All @@ -11,41 +11,7 @@ class MonitorServiceProvider extends ServiceProvider
*/
public function boot()
{
/*
* Optional methods to load your package assets
*/
// $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'monitor');
// $this->loadViewsFrom(__DIR__.'/../resources/views', 'monitor');
// $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
// $this->loadRoutesFrom(__DIR__.'/routes.php');





if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('monitor.php'),
], 'config');

// Publishing the views.
/*$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/monitor'),
], 'views');*/

// Publishing assets.
/*$this->publishes([
__DIR__.'/../resources/assets' => public_path('vendor/monitor'),
], 'assets');*/

// Publishing the translation files.
/*$this->publishes([
__DIR__.'/../resources/lang' => resource_path('lang/vendor/monitor'),
], 'lang');*/

// Registering package commands.
// $this->commands([]);
}
$this->loadRoutesFrom(__DIR__.'/routes.php');
}

/**
Expand All @@ -54,11 +20,15 @@ public function boot()
public function register()
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'monitor');
//$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'monitor');
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'krebsmonitor');
$this->app->make('chrissantiago82\monitor\MonitorController');

/*
// Register the main class to use with the facade
$this->app->singleton('monitor', function () {
return new Monitor;
});
*/
}
}
4 changes: 4 additions & 0 deletions src/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php


Route::GET('/krebscmonitor/{key}', 'chrissantiago82\monitor\MonitorController@monitor');

0 comments on commit 58e9f16

Please sign in to comment.