Login   Register  
PHP Classes

File: examples/case-studies/system/dashboard/system_alert_dashboard.php

Recommend this page to a friend!
  Classes of ASCOOS CMS   Ascoos OS   examples/case-studies/system/dashboard/system_alert_dashboard.php   Download  
File: examples/case-studies/system/dashboard/system_alert_dashboard.php
Role: Example script
Content typex: text/plain
Description: Example script
Class: Ascoos OS
A PHP Web 5.0 Kernel for decentralized web and IoT
Author: By
Last change: Update of examples/case-studies/system/dashboard/system_alert_dashboard.php
Date: 1 month ago
Size: 4,097 bytes
 

Contents

Class file image Download
<?php
/**
 * @ASCOOS-NAME : Ascoos OS
 * @ASCOOS-VERSION : 26.0.0
 * @ASCOOS-SUPPORT : [email protected]
 * @ASCOOS-BUGS : https://issues.ascoos.com
 *
 * @desc <English> Real-time dashboard for monitoring system and Apache resources, triggering alerts, logging events, and generating visual reports.
 * @desc <Greek> ??????? ?????????????? ?? ?????????? ????? ??? ?????? ?????????? ??? Apache, ?? ????????????, ????????? ??? ??????? ????????.
 *
 * @since PHP 8.2.0
 */
declare(strict_types=1);

use
ASCOOS\OS\Kernel\Systems\TCoreSystemHandler;
use
ASCOOS\OS\Kernel\Apache\TApacheHandler;
use
ASCOOS\OS\Kernel\Arrays\Events\TEventHandler;
use
ASCOOS\OS\Extras\Arrays\Graphs\TArrayGraphHandler;
use
ASCOOS\OS\Kernel\Dates\TDatesHandler;

global
$AOS_LOGS_PATH, $AOS_TMP_DATA_PATH, $AOS_FONTS_PATH;

// <English> Define configuration for logging, graphing, and thresholds
// <Greek> ??????? ????????? ??? ?????????, ????????? ??? ???? ??????????
$properties = [
   
'logs' => [
       
'useLogger' => true,
       
'dir' => $AOS_LOGS_PATH . '/',
       
'file' => 'system_alerts.log'
   
],
   
'graph' => [
       
'fontPath' => $AOS_FONTS_PATH . '/Murecho/Murecho-Regular.ttf',
       
'width' => 800,
       
'height' => 600
   
],
   
'thresholds' => [
       
'cpu' => 85,
       
'memory' => 80
   
]
];

// <English> Initialize handlers
// <Greek> ???????????? ?????????
$system = new TCoreSystemHandler($properties);
$apache = TApacheHandler::getInstance([], $properties);
$eventHandler = new TEventHandler([], $properties);
$graphHandler = new TArrayGraphHandler([], $properties['graph']);
$datesHandler = new TDatesHandler('Europe/Athens', $properties);

// <English> Register alert events
// <Greek> ?????????? ????????? ??????????
$eventHandler->register('alerts', 'cpu.high', fn($data) => $eventHandler->logger->log("High CPU usage: {$data['cpu']}%", $eventHandler::DEBUG_LEVEL_WARNING));
$eventHandler->register('alerts', 'memory.high', fn($data) => $eventHandler->logger->log("High memory usage: {$data['memory']}%", $eventHandler::DEBUG_LEVEL_WARNING));
$eventHandler->register('alerts', 'apache.down', fn() => $eventHandler->logger->log("Apache server is not running", $eventHandler::DEBUG_LEVEL_CRITICAL));

// <English> Monitor system resources
// <Greek> ????????????? ????? ??????????
$cpu = $system->get_cpu_load();
$memoryStats = $system->get_memory_stats();
$memory = $memoryStats['percent'];
$apacheRunning = $apache->isServerRunning();

// <English> Trigger alerts based on thresholds
// <Greek> ???????????? ?????????? ????? ?????
if ($cpu > $properties['thresholds']['cpu']) {
   
$eventHandler->trigger('alerts', 'cpu.high', ['cpu' => $cpu]);
}
if (
$memory > $properties['thresholds']['memory']) {
   
$eventHandler->trigger('alerts', 'memory.high', ['memory' => $memory]);
}
if (!
$apacheRunning) {
   
$eventHandler->trigger('alerts', 'apache.down');
}

// <English> Generate graph of current metrics
// <Greek> ?????????? ?????????? ?? ??? ????????? ?????????
$graphHandler->setArray([$cpu, $memory]);
$graphPath = $AOS_TMP_DATA_PATH . '/reports/system_metrics_' . $datesHandler->getCurrentDate('Ymd') . '.png';
$graphHandler->createGaugeChart($graphPath);

// <English> Output summary
// <Greek> ???????? ???????
echo json_encode([
   
'cpu' => $cpu,
   
'memory' => $memory,
   
'apache_running' => $apacheRunning,
   
'graph' => $graphPath
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

// <English> Free resources
// <Greek> ???????????? ?????
$system->Free($system);
$apache->Free($apache);
$eventHandler->Free($eventHandler);
$graphHandler->Free($graphHandler);
$datesHandler->Free($datesHandler);