259 lines
7.5 KiB
PHP
Executable File
259 lines
7.5 KiB
PHP
Executable File
<?php declare(strict_types=1); namespace IR\App\Controllers; if (!defined('IR_START')) exit('<pre>No direct script access allowed</pre>');
|
|
/**
|
|
* @framework Wevads Framework
|
|
* @version 1.0
|
|
* @author Amine Idrissi <contact@iresponse.tech>
|
|
* @date 2019
|
|
* @name Api.php
|
|
*/
|
|
|
|
# core
|
|
use IR\Core\Application as Application;
|
|
|
|
# mvc
|
|
use IR\Mvc\Controller as Controller;
|
|
|
|
# http
|
|
use IR\Http\Request as Request;
|
|
|
|
# helpers
|
|
use IR\App\Helpers\Page as Page;
|
|
|
|
/**
|
|
* @name Api
|
|
* @description Api Controller
|
|
*/
|
|
class Api extends Controller
|
|
{
|
|
/**
|
|
* @app
|
|
* @readwrite
|
|
*/
|
|
protected $app;
|
|
|
|
/**
|
|
* @name init
|
|
* @description initializing process before the action method executed
|
|
* @once
|
|
* @protected
|
|
*/
|
|
public function init()
|
|
{
|
|
# set the current application to a local variable
|
|
$this->app = Application::getCurrent();
|
|
|
|
# connect to the database
|
|
$this->app->database('system')->connect();
|
|
|
|
# prevent layout
|
|
$this->showMasterView = false;
|
|
}
|
|
|
|
/**
|
|
* @name main
|
|
* @description the main action
|
|
* @before init
|
|
* @after closeConnections
|
|
*/
|
|
public function main()
|
|
{
|
|
# get post data
|
|
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
|
|
|
|
if(count($data) == 0)
|
|
{
|
|
Page::printApiResults(500,'Our system detected no parameters passed !');
|
|
}
|
|
|
|
try
|
|
{
|
|
$controller = $this->app->utils->arrays->get($data,'controller');
|
|
$action = $this->app->utils->arrays->get($data,'action');
|
|
$parameters = $this->app->utils->arrays->get($data,'parameters',[]);
|
|
|
|
if($controller == null || strlen($controller) == 0)
|
|
{
|
|
@file_put_contents('/tmp/api_404.log',date('H:i:s').' MISSING: '.$controller.'/'.$action."
|
|
",FILE_APPEND); Page::printApiResults(404,'Webservice controller not found !');
|
|
}
|
|
|
|
$fileName = ($this->app->utils->strings->contains($controller,'-')) ? str_replace(' ','',ucwords(str_replace('-',' ',$controller))) : ucfirst($controller);
|
|
|
|
$class = FW_ABBR . ANS . 'App' . ANS . 'Webservices' . ANS . $fileName;
|
|
|
|
# check if the controller exists
|
|
if(!file_exists(BASE_PATH . DS . 'app' . DS . 'webservices' . DS . $fileName . ".php"))
|
|
{
|
|
@file_put_contents('/tmp/api_404.log',date('H:i:s').' MISSING: '.$controller.'/'.$action."
|
|
",FILE_APPEND); Page::printApiResults(404,'Webservice controller not found !');
|
|
}
|
|
|
|
# loading the controller
|
|
$instance = new $class();
|
|
|
|
if($instance != null)
|
|
{
|
|
if($action == null || !method_exists($instance,strval($action)))
|
|
{
|
|
@file_put_contents('/tmp/api_404.log',date('H:i:s').' ACTION_NOT_FOUND: '.$controller.'/'.strval($action)."
|
|
",FILE_APPEND); Page::printApiResults(404,'webservice action not found !');
|
|
}
|
|
|
|
$methodMeta = $this->app->utils->inspector->methodMeta($class,$action);
|
|
|
|
if (!empty($methodMeta["@protected"]) || !empty($methodMeta["@private"]))
|
|
{
|
|
Page::printApiResults(403,"Action {$action} is not accessible !");
|
|
}
|
|
|
|
$hooks = function($meta, $type) use ($class,$instance)
|
|
{
|
|
if (isset($meta[$type]))
|
|
{
|
|
$run = [];
|
|
|
|
foreach ($meta[$type] as $method)
|
|
{
|
|
$hookMeta = $this->app->utils->inspector->methodMeta($class,$method);
|
|
|
|
if (in_array($method, $run) && !empty($hookMeta["@once"]))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
$instance->$method();
|
|
$run[] = $method;
|
|
}
|
|
}
|
|
};
|
|
|
|
# calling "before" hook function
|
|
$hooks($methodMeta, "@before");
|
|
|
|
# executing the main action requested from the url
|
|
$instance->{$action}($parameters);
|
|
|
|
# calling "after" hook function
|
|
$hooks($methodMeta, "@after");
|
|
}
|
|
}
|
|
catch (\Throwable $e)
|
|
{
|
|
Page::printApiResults(500,'Internal server error !',[],$e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @name systemMetrics
|
|
* @description get system metrics (CPU, RAM, Storage)
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function systemMetrics()
|
|
{
|
|
try
|
|
{
|
|
# get CPU usage
|
|
$cpuUsage = $this->getCpuUsage();
|
|
|
|
# get RAM usage
|
|
$ramUsage = $this->getRamUsage();
|
|
|
|
# get Storage usage
|
|
$storageUsage = $this->getStorageUsage();
|
|
|
|
# return metrics as JSON
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'cpu' => $cpuUsage,
|
|
'ram' => $ramUsage,
|
|
'storage' => $storageUsage
|
|
]);
|
|
exit();
|
|
}
|
|
catch(Exception $exception)
|
|
{
|
|
header('Content-Type: application/json');
|
|
http_response_code(500);
|
|
echo json_encode(['error' => $exception->getMessage()]);
|
|
exit();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @name getCpuUsage
|
|
* @description get CPU usage percentage
|
|
* @access private
|
|
* @return float
|
|
*/
|
|
private function getCpuUsage()
|
|
{
|
|
$load = sys_getloadavg();
|
|
$cpuCount = intval(trim(shell_exec("nproc")));
|
|
|
|
if ($cpuCount > 0) {
|
|
$cpuUsage = ($load[0] / $cpuCount) * 100;
|
|
return min(100, max(0, round($cpuUsage, 1)));
|
|
}
|
|
|
|
return 0.0;
|
|
}
|
|
|
|
/**
|
|
* @name getRamUsage
|
|
* @description get RAM usage percentage
|
|
* @access private
|
|
* @return float
|
|
*/
|
|
private function getRamUsage()
|
|
{
|
|
$memInfo = file_get_contents('/proc/meminfo');
|
|
preg_match('/MemTotal:\s+(\d+)/', $memInfo, $memTotal);
|
|
preg_match('/MemAvailable:\s+(\d+)/', $memInfo, $memAvailable);
|
|
|
|
if (isset($memTotal[1]) && isset($memAvailable[1])) {
|
|
$total = intval($memTotal[1]);
|
|
$available = intval($memAvailable[1]);
|
|
$used = $total - $available;
|
|
|
|
return round(($used / $total) * 100, 1);
|
|
}
|
|
|
|
return 0.0;
|
|
}
|
|
|
|
/**
|
|
* @name getStorageUsage
|
|
* @description get Storage usage percentage
|
|
* @access private
|
|
* @return float
|
|
*/
|
|
private function getStorageUsage()
|
|
{
|
|
$diskTotal = disk_total_space('/');
|
|
$diskFree = disk_free_space('/');
|
|
|
|
if ($diskTotal && $diskFree) {
|
|
$diskUsed = $diskTotal - $diskFree;
|
|
return round(($diskUsed / $diskTotal) * 100, 1);
|
|
}
|
|
|
|
return 0.0;
|
|
}
|
|
|
|
/**
|
|
* @name closeConnections
|
|
* @description close all connections
|
|
* @once
|
|
* @protected
|
|
*/
|
|
public function closeConnections()
|
|
{
|
|
# connect to the database
|
|
$this->app->database('system')->disconnect();
|
|
$this->app->database('clients')->disconnect();
|
|
}
|
|
}
|
|
|
|
|