From 564466a72b0ec2c8b00be83d2e3429f5ae971574 Mon Sep 17 00:00:00 2001 From: Nikolay Beketov Date: Tue, 14 May 2024 17:00:31 +0700 Subject: [PATCH] Add getDMI and getHypervisor methods for metrika sollection --- .../Lib/License/SendMetricsAction.php | 19 +++++++ .../Lib/Sysinfo/GetDMIInfoAction.php | 53 ++++++++++++++++++ .../Lib/Sysinfo/GetHypervisorInfoAction.php | 54 +++++++++++++++++++ src/PBXCoreREST/Lib/Sysinfo/GetInfoAction.php | 29 ++++++++-- .../Lib/SysinfoManagementProcessor.php | 8 +++ 5 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 src/PBXCoreREST/Lib/Sysinfo/GetDMIInfoAction.php create mode 100644 src/PBXCoreREST/Lib/Sysinfo/GetHypervisorInfoAction.php diff --git a/src/PBXCoreREST/Lib/License/SendMetricsAction.php b/src/PBXCoreREST/Lib/License/SendMetricsAction.php index 019314895..47308c593 100644 --- a/src/PBXCoreREST/Lib/License/SendMetricsAction.php +++ b/src/PBXCoreREST/Lib/License/SendMetricsAction.php @@ -24,6 +24,7 @@ use MikoPBX\Common\Models\PbxSettingsConstants; use MikoPBX\Common\Providers\ManagedCacheProvider; use MikoPBX\Common\Providers\MarketPlaceProvider; +use MikoPBX\Common\Providers\PBXCoreRESTClientProvider; use MikoPBX\PBXCoreREST\Lib\PBXApiResult; use Phalcon\Di; @@ -80,6 +81,24 @@ public static function main(): PBXApiResult // Virtual Hardware Type $dataMetrics['VirtualHardwareType'] = PbxSettings::getValueByKey(PbxSettingsConstants::VIRTUAL_HARDWARE_TYPE); + // Hypervisor + $restAnswer = $di->get(PBXCoreRESTClientProvider::SERVICE_NAME, [ + '/pbxcore/api/sysinfo/getHypervisorInfo', + PBXCoreRESTClientProvider::HTTP_METHOD_GET + ]); + if ($restAnswer->success){ + $dataMetrics['Hypervisor'] = $restAnswer->data['Hypervisor']; + } + + // DMI + $restAnswer = $di->get(PBXCoreRESTClientProvider::SERVICE_NAME, [ + '/pbxcore/api/sysinfo/getDMIInfo', + PBXCoreRESTClientProvider::HTTP_METHOD_GET + ]); + if ($restAnswer->success){ + $dataMetrics['DMI'] = $restAnswer->data['DMI']; + } + $license = $di->get(MarketPlaceProvider::SERVICE_NAME); $license->sendLicenseMetrics($licenseKey, $dataMetrics); } diff --git a/src/PBXCoreREST/Lib/Sysinfo/GetDMIInfoAction.php b/src/PBXCoreREST/Lib/Sysinfo/GetDMIInfoAction.php new file mode 100644 index 000000000..d45fa9945 --- /dev/null +++ b/src/PBXCoreREST/Lib/Sysinfo/GetDMIInfoAction.php @@ -0,0 +1,53 @@ +. + */ + +namespace MikoPBX\PBXCoreREST\Lib\Sysinfo; + +use MikoPBX\Core\System\Util; +use MikoPBX\PBXCoreREST\Lib\PBXApiResult; + +/** + * Returns DMI information + * + * @package MikoPBX\PBXCoreREST\Lib\Sysinfo + */ +class GetDMIInfoAction extends \Phalcon\Di\Injectable +{ + /** + * Returns DMI information + * + * @return PBXApiResult An object containing the result of the API call. + */ + public static function main(): PBXApiResult + { + $res = new PBXApiResult(); + $res->processor = __METHOD__; + + $dmesg = Util::which('dmesg'); + $grep = Util::which('grep'); + $awk = Util::which('awk'); + $result = shell_exec("$dmesg | $grep DMI | $awk -F 'DMI: ' '{ print $2}'"); + $result = trim($result); + $res->data = ['DMI'=>$result]; + if ($result){ + $res->success = true; + } + return $res; + } +} \ No newline at end of file diff --git a/src/PBXCoreREST/Lib/Sysinfo/GetHypervisorInfoAction.php b/src/PBXCoreREST/Lib/Sysinfo/GetHypervisorInfoAction.php new file mode 100644 index 000000000..446e20b61 --- /dev/null +++ b/src/PBXCoreREST/Lib/Sysinfo/GetHypervisorInfoAction.php @@ -0,0 +1,54 @@ +. + */ + +namespace MikoPBX\PBXCoreREST\Lib\Sysinfo; + +use MikoPBX\Core\System\Util; +use MikoPBX\PBXCoreREST\Lib\PBXApiResult; + +/** + * Returns hypervisor information + * + * @package MikoPBX\PBXCoreREST\Lib\Sysinfo + */ +class GetHypervisorInfoAction extends \Phalcon\Di\Injectable +{ + /** + * Returns hypervisor information + * + * @return PBXApiResult An object containing the result of the API call. + */ + public static function main(): PBXApiResult + { + $res = new PBXApiResult(); + $res->processor = __METHOD__; + + $dmesg = Util::which('dmesg'); + $grep = Util::which('grep'); + $awk = Util::which('awk'); + $result = shell_exec("$dmesg | $grep 'Hypervisor detected' | $awk -F 'Hypervisor detected: ' '{ print $2}'"); + $result = trim($result); + $res->data =['Hypervisor'=>$result]; + if ($result){ + $res->success = true; + } + return $res; + } + +} \ No newline at end of file diff --git a/src/PBXCoreREST/Lib/Sysinfo/GetInfoAction.php b/src/PBXCoreREST/Lib/Sysinfo/GetInfoAction.php index e86fb4fe5..17caf348d 100644 --- a/src/PBXCoreREST/Lib/Sysinfo/GetInfoAction.php +++ b/src/PBXCoreREST/Lib/Sysinfo/GetInfoAction.php @@ -68,6 +68,7 @@ public static function main(): PBXApiResult public static function prepareSysyinfoContent(): string { $content = self::getPBXVersion(); + $content .= self::getEnvironment(); $content .= self::getDate(); $content .= self::getUpTime(); $content .= self::getCpu(); @@ -113,13 +114,9 @@ public static function getDate(): string private static function getPBXVersion(): string { $version = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_VERSION); - $installationType = PbxSettings::getValueByKey(PbxSettingsConstants::VIRTUAL_HARDWARE_TYPE); $content = '─────────────────────────────────────── PBXVersion ───────────────────────────────────────'; $content .= PHP_EOL . PHP_EOL; $content .= $version . PHP_EOL; - if ($installationType){ - $content .= 'Machine environment: '.$installationType . PHP_EOL; - } $content .= PHP_EOL . PHP_EOL; return $content; } @@ -414,4 +411,28 @@ private static function getCorruptedFiles(): string return $content; } + /** + * Prepare system environment information + * @return string + */ + private static function getEnvironment(): string + { + $content = '───────────────────────────────────────── Environment ─────────────────────────────────────────'; + $content .= PHP_EOL . PHP_EOL; + $installationType = PbxSettings::getValueByKey(PbxSettingsConstants::VIRTUAL_HARDWARE_TYPE); + if ($installationType){ + $content .= 'Machine hardware: '.$installationType . PHP_EOL; + } + $hyperVisor = GetHypervisorInfoAction::main(); + if ($hyperVisor->success) { + $content .= "Hypervisor: {$hyperVisor->data['Hypervisor']}" . PHP_EOL; + } + $dmi = GetDMIInfoAction::main(); + if ($dmi->success) { + $content .= "DMI: {$dmi->data['DMI']}" . PHP_EOL; + } + $content .= PHP_EOL . PHP_EOL; + return $content; + } + } \ No newline at end of file diff --git a/src/PBXCoreREST/Lib/SysinfoManagementProcessor.php b/src/PBXCoreREST/Lib/SysinfoManagementProcessor.php index e8884ff2e..8b88b0c1b 100644 --- a/src/PBXCoreREST/Lib/SysinfoManagementProcessor.php +++ b/src/PBXCoreREST/Lib/SysinfoManagementProcessor.php @@ -19,7 +19,9 @@ namespace MikoPBX\PBXCoreREST\Lib; +use MikoPBX\PBXCoreREST\Lib\Sysinfo\GetDMIInfoAction; use MikoPBX\PBXCoreREST\Lib\Sysinfo\GetExternalIpInfoAction; +use MikoPBX\PBXCoreREST\Lib\Sysinfo\GetHypervisorInfoAction; use MikoPBX\PBXCoreREST\Lib\Sysinfo\GetInfoAction; use Phalcon\Di\Injectable; @@ -50,6 +52,12 @@ public static function callBack(array $request): PBXApiResult case 'getExternalIpInfo': $res = GetExternalIpInfoAction::main(); break; + case 'getHypervisorInfo': + $res = GetHypervisorInfoAction::main(); + break; + case 'getDMIInfo': + $res = GetDMIInfoAction::main(); + break; default: $res->messages['error'][] = "Unknown action - $action in ".__CLASS__; }