From 58e9f160d129c48ca8949230e79567f8b15b04ff Mon Sep 17 00:00:00 2001 From: ChrisSantiago82 <43000085+ChrisSantiago82@users.noreply.github.com> Date: Sun, 28 Feb 2021 15:03:42 -0400 Subject: [PATCH] Add Funtionality --- config/config.php | 14 +++++-- src/Monitor.php | 8 ---- src/MonitorClass.php | 67 ++++++++++++++++++++++++++++++++++ src/MonitorController.php | 33 +++++++++++++++++ src/MonitorFacade.php | 2 +- src/MonitorServiceProvider.php | 44 ++++------------------ src/routes.php | 4 ++ 7 files changed, 122 insertions(+), 50 deletions(-) delete mode 100644 src/Monitor.php create mode 100644 src/MonitorClass.php create mode 100644 src/MonitorController.php create mode 100644 src/routes.php diff --git a/config/config.php b/config/config.php index 48d0ee9..45c483f 100644 --- a/config/config.php +++ b/config/config.php @@ -1,8 +1,14 @@ env('MONITOR_KEY'), + + +]; diff --git a/src/Monitor.php b/src/Monitor.php deleted file mode 100644 index b6868b4..0000000 --- a/src/Monitor.php +++ /dev/null @@ -1,8 +0,0 @@ -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; + } + + +} diff --git a/src/MonitorController.php b/src/MonitorController.php new file mode 100644 index 0000000..d93a7d3 --- /dev/null +++ b/src/MonitorController.php @@ -0,0 +1,33 @@ +json($monitorClass->results); + + } + +} diff --git a/src/MonitorFacade.php b/src/MonitorFacade.php index 118ddf8..996974e 100644 --- a/src/MonitorFacade.php +++ b/src/MonitorFacade.php @@ -1,6 +1,6 @@ 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'); } /** @@ -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; }); + */ } } diff --git a/src/routes.php b/src/routes.php new file mode 100644 index 0000000..a80ab51 --- /dev/null +++ b/src/routes.php @@ -0,0 +1,4 @@ +