Skip to content

Commit

Permalink
Add getDMI and getHypervisor methods for metrika sollection
Browse files Browse the repository at this point in the history
  • Loading branch information
jorikfon committed May 14, 2024
1 parent 0ac6aeb commit 564466a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/PBXCoreREST/Lib/License/SendMetricsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
53 changes: 53 additions & 0 deletions src/PBXCoreREST/Lib/Sysinfo/GetDMIInfoAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/*
* MikoPBX - free phone system for small business
* Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

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;
}
}
54 changes: 54 additions & 0 deletions src/PBXCoreREST/Lib/Sysinfo/GetHypervisorInfoAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
* MikoPBX - free phone system for small business
* Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

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;
}

}
29 changes: 25 additions & 4 deletions src/PBXCoreREST/Lib/Sysinfo/GetInfoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

}
8 changes: 8 additions & 0 deletions src/PBXCoreREST/Lib/SysinfoManagementProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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__;
}
Expand Down

0 comments on commit 564466a

Please sign in to comment.