sintonia/legacy/application/models/Systemstatus.php

234 lines
7.6 KiB
PHP
Raw Permalink Normal View History

<?php
class Application_Model_Systemstatus
{
public static function GetMonitStatus($p_ip)
{
$CC_CONFIG = Config::getConfig();
2021-10-12 14:05:55 +02:00
// $monit_user = $CC_CONFIG['monit_user'];
// $monit_password = $CC_CONFIG['monit_password'];
2021-10-11 16:10:47 +02:00
$url = "http://{$p_ip}:2812/_status?format=xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
2021-10-12 14:05:55 +02:00
// curl_setopt($ch, CURLOPT_USERPWD, "$monit_user:$monit_password");
// wait a max of 3 seconds before aborting connection attempt
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$docRoot = null;
2021-10-11 16:10:47 +02:00
if ($result !== false && $info['http_code'] === 200) {
if ($result != '') {
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($result);
$docRoot = $xmlDoc->documentElement;
}
}
return $docRoot;
}
public static function ExtractServiceInformation($p_docRoot, $p_serviceName)
{
2021-10-11 16:10:47 +02:00
$starting = [
'name' => '',
'process_id' => 'STARTING...',
'uptime_seconds' => '-1',
'status' => 0,
'memory_perc' => '0%',
'memory_kb' => '0',
2021-10-12 14:05:55 +02:00
'cpu_perc' => '0%',
];
2021-10-11 16:10:47 +02:00
$notMonitored = [
'name' => $p_serviceName,
'process_id' => 'NOT MONITORED',
'uptime_seconds' => '1',
'status' => 1,
'memory_perc' => '0%',
'memory_kb' => '0',
'cpu_perc' => '0%',
];
$notRunning = [
'name' => $p_serviceName,
'process_id' => 'FAILED',
'uptime_seconds' => '-1',
'status' => 0,
'memory_perc' => '0%',
'memory_kb' => '0',
'cpu_perc' => '0%',
];
$data = $notRunning;
if (!is_null($p_docRoot)) {
2021-10-11 16:10:47 +02:00
foreach ($p_docRoot->getElementsByTagName('service') as $item) {
if ($item->getElementsByTagName('name')->item(0)->nodeValue == $p_serviceName) {
$monitor = $item->getElementsByTagName('monitor');
if ($monitor->length > 0) {
$status = $monitor->item(0)->nodeValue;
2021-10-11 16:10:47 +02:00
if ($status == '2') {
$data = $starting;
} elseif ($status == 1) {
// is monitored, but is it running?
2021-10-11 16:10:47 +02:00
$pid = $item->getElementsByTagName('pid');
if ($pid->length == 0) {
$data = $notRunning;
}
2022-09-12 13:16:14 +02:00
}
// running!
elseif ($status == 0) {
$data = $notMonitored;
}
}
2021-10-11 16:10:47 +02:00
$process_id = $item->getElementsByTagName('name');
if ($process_id->length > 0) {
2021-10-11 16:10:47 +02:00
$data['name'] = $process_id->item(0)->nodeValue;
}
2021-10-11 16:10:47 +02:00
$process_id = $item->getElementsByTagName('pid');
if ($process_id->length > 0) {
2021-10-11 16:10:47 +02:00
$data['process_id'] = $process_id->item(0)->nodeValue;
$data['status'] = 0;
}
2021-10-11 16:10:47 +02:00
$uptime = $item->getElementsByTagName('uptime');
if ($uptime->length > 0) {
2021-10-11 16:10:47 +02:00
$data['uptime_seconds'] = $uptime->item(0)->nodeValue;
}
2021-10-11 16:10:47 +02:00
$memory = $item->getElementsByTagName('memory');
if ($memory->length > 0) {
2021-10-11 16:10:47 +02:00
$data['memory_perc'] = $memory->item(0)->getElementsByTagName('percenttotal')->item(0)->nodeValue . '%';
$data['memory_kb'] = $memory->item(0)->getElementsByTagName('kilobytetotal')->item(0)->nodeValue;
}
2021-10-11 16:10:47 +02:00
$cpu = $item->getElementsByTagName('cpu');
if ($cpu->length > 0) {
2021-10-11 16:10:47 +02:00
$data['cpu_perc'] = $cpu->item(0)->getElementsByTagName('percent')->item(0)->nodeValue . '%';
}
2021-10-11 16:10:47 +02:00
break;
}
}
}
return $data;
}
public static function GetPlatformInfo()
{
2021-10-11 16:10:47 +02:00
$keys = ['release', 'machine', 'memory', 'swap'];
$data = [];
foreach ($keys as $key) {
2021-10-11 16:10:47 +02:00
$data[$key] = 'UNKNOWN';
}
2021-10-11 16:10:47 +02:00
$docRoot = self::GetMonitStatus('localhost');
if (!is_null($docRoot)) {
2021-10-11 16:10:47 +02:00
foreach ($docRoot->getElementsByTagName('platform') as $item) {
foreach ($keys as $key) {
$keyElement = $item->getElementsByTagName($key);
if ($keyElement->length > 0) {
$data[$key] = $keyElement->item(0)->nodeValue;
}
}
}
}
return $data;
}
public static function GetPypoStatus()
{
2021-10-11 16:10:47 +02:00
$component = CcServiceRegisterQuery::create()->findOneByDbName('pypo');
if (is_null($component)) {
return null;
2021-10-11 16:10:47 +02:00
}
$ip = $component->getDbIp();
2021-10-11 16:10:47 +02:00
$docRoot = self::GetMonitStatus($ip);
2021-10-12 14:05:55 +02:00
return self::ExtractServiceInformation($docRoot, 'libretime-playout');
}
public static function GetLiquidsoapStatus()
{
2021-10-11 16:10:47 +02:00
$component = CcServiceRegisterQuery::create()->findOneByDbName('pypo');
if (is_null($component)) {
return null;
2021-10-11 16:10:47 +02:00
}
$ip = $component->getDbIp();
2021-10-11 16:10:47 +02:00
$docRoot = self::GetMonitStatus($ip);
2021-10-12 14:05:55 +02:00
return self::ExtractServiceInformation($docRoot, 'libretime-liquidsoap');
}
public static function GetMediaMonitorStatus()
{
2021-10-11 16:10:47 +02:00
$component = CcServiceRegisterQuery::create()->findOneByDbName('media-monitor');
if (is_null($component)) {
return null;
2021-10-11 16:10:47 +02:00
}
$ip = $component->getDbIp();
2021-10-11 16:10:47 +02:00
$docRoot = self::GetMonitStatus($ip);
2021-10-11 16:10:47 +02:00
return self::ExtractServiceInformation($docRoot, 'airtime-analyzer');
}
public static function GetIcecastStatus()
{
2021-10-11 16:10:47 +02:00
$docRoot = self::GetMonitStatus('localhost');
2021-10-11 16:10:47 +02:00
return self::ExtractServiceInformation($docRoot, 'icecast2');
}
public static function GetRabbitMqStatus()
{
2021-10-11 16:10:47 +02:00
if (isset($_SERVER['RABBITMQ_HOST'])) {
$rabbitmq_host = $_SERVER['RABBITMQ_HOST'];
} else {
2021-10-11 16:10:47 +02:00
$rabbitmq_host = 'localhost';
}
$docRoot = self::GetMonitStatus($rabbitmq_host);
2021-10-11 16:10:47 +02:00
$data = self::ExtractServiceInformation($docRoot, 'rabbitmq-server');
return $data;
}
public static function GetDiskInfo()
{
$storagePath = Config::getStoragePath();
$totalSpace = disk_total_space($storagePath);
2021-10-11 16:10:47 +02:00
$partitions = [];
$partitions[$totalSpace] = new stdClass();
$partitions[$totalSpace]->totalSpace = $totalSpace;
$partitions[$totalSpace]->totalFreeSpace = disk_free_space($storagePath);
$partitions[$totalSpace]->usedSpace = $totalSpace - $partitions[$totalSpace]->totalFreeSpace;
$partitions[$totalSpace]->dirs[] = $storagePath;
2021-10-11 16:10:47 +02:00
return array_values($partitions);
}
public static function isDiskOverQuota()
{
$diskInfo = self::GetDiskInfo();
$diskInfo = $diskInfo[0];
$diskUsage = $diskInfo->totalSpace - $diskInfo->totalFreeSpace;
2014-05-05 17:52:13 +02:00
if ($diskUsage > 0 && $diskUsage >= $diskInfo->totalSpace) {
return true;
}
return false;
}
}