Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
fix(pendo): use api to get pendo information (#10349)
Browse files Browse the repository at this point in the history
Refs: MON-10926
  • Loading branch information
kduret authored Oct 21, 2021
1 parent ad4ad49 commit ccb8638
Show file tree
Hide file tree
Showing 7 changed files with 459 additions and 213 deletions.
5 changes: 4 additions & 1 deletion config/packages/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,7 @@ services:

Centreon\Domain\Log\ContactForDebug:
class: Centreon\Domain\Log\ContactForDebug
arguments: ['%env(DEBUG_CONTACT)%']
arguments: ['%env(DEBUG_CONTACT)%']

Centreon\Domain\Log\LegacyLogger:
public: true
115 changes: 115 additions & 0 deletions src/Centreon/Domain/Log/LegacyLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/*
* Copyright 2005 - 2021 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Centreon\Domain\Log;

use Psr\Log\LoggerInterface;
use Centreon\Domain\Log\LoggerTrait;

/**
* This class is designed to be used in legacy codebase to use a logger
*
* @package Centreon\Domain\Log
*/
class LegacyLogger implements LoggerInterface
{
use LoggerTrait {
emergency as traitEmergency;
alert as traitAlert;
critical as traitCritical;
error as traitError;
warning as traitWarning;
notice as traitNotice;
info as traitInfo;
debug as traitDebug;
log as traitLog;
}

public function emergency($message, array $context = [])
{
$this->traitEmergency($message, $context);
}

/**
* @inheritDoc
*/
public function alert($message, array $context = [])
{
$this->traitAlert($message, $context);
}

/**
* @inheritDoc
*/
public function critical($message, array $context = [])
{
$this->traitCritical($message, $context);
}

/**
* @inheritDoc
*/
public function error($message, array $context = [])
{
$this->traitError($message, $context);
}

/**
* @inheritDoc
*/
public function warning($message, array $context = [])
{
$this->traitWarning($message, $context);
}

/**
* @inheritDoc
*/
public function notice($message, array $context = [])
{
$this->traitNotice($message, $context);
}

/**
* @inheritDoc
*/
public function info($message, array $context = [])
{
$this->traitInfo($message, $context);
}

/**
* @inheritDoc
*/
public function debug($message, array $context = [])
{
$this->traitDebug($message, $context);
}

/**
* @inheritDoc
*/
public function log($level, $message, array $context = [])
{
$this->traitLog($level, $message, $context);
}
}
267 changes: 267 additions & 0 deletions www/api/class/centreon_ceip.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
<?php

/*
* Copyright 2005 - 2021 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

require_once __DIR__ . '/../../../bootstrap.php';
require_once __DIR__ . '/../../class/centreonDB.class.php';
require_once __DIR__ . '/../../class/centreonUUID.class.php';
require_once __DIR__ . '/../../class/centreonStatistics.class.php';
require_once __DIR__ . '/webService.class.php';

class CentreonCeip extends CentreonWebService
{
/**
* @var string
*/
private $uuid;

/**
* @var \CentreonUser
*/
private $user;

/**
* @var \Centreon\Domain\Log\LegacyLogger
*/
private $logger;

/**
* Constructor
*/
public function __construct()
{
parent::__construct();

global $centreon;

$this->user = $centreon->user;

// Generate UUID
$this->uuid = (new CentreonUUID($this->pearDB))->getUUID();

$kernel = \App\Kernel::createForWeb();
$this->logger = $kernel->getContainer()->get(
\Centreon\Domain\Log\LegacyLogger::class
);
}

/**
* Get CEIP Account and User info
*
* @return array<string,mixed> with Account/User info
*/
public function getCeipInfo(): array
{
// Don't compute data is CEIP is disabled
if (!$this->isCeipActive()) {
return [
'ceip' => false
];
}

return [
'visitor' => $this->getVisitorInformation(),
'account' => $this->getAccountInformation(),
'excludeAllText' => true,
'ceip' => true
];
}

/**
* Get the type of the Centreon server
*
* @return string the type of the server (central|remote)
*/
private function getServerType(): string
{
$instanceType = 'central';

$result = $this->pearDB->query(
"SELECT `value` FROM `informations` WHERE `key` = 'isRemote'"
);
if ($row = $result->fetch()) {
$instanceType = $row['value'] === 'yes' ? 'remote' : 'central';
}

return $instanceType;
}

/**
* Get visitor information
*
* @return array<string,mixed> with visitor information
* @throws \PDOException
*/
private function getVisitorInformation(): array
{
$locale = $this->user->lang === 'browser'
? null
: $this->user->lang;

$role = $this->user->admin
? "admin"
: "user";

if (strcmp($role, 'admin') != 0) {
$stmt = $this->pearDB->prepare('
SELECT COUNT(*) AS countAcl
FROM acl_actions_rules AS aar
INNER JOIN acl_actions AS aa ON (aa.acl_action_id = aar.acl_action_rule_id)
INNER JOIN acl_group_actions_relations AS agar ON (agar.acl_action_id = aar.acl_action_rule_id)
INNER JOIN acl_group_contacts_relations AS agcr ON (agcr.acl_group_id = agar.acl_group_id)
INNER JOIN acl_group_contactgroups_relations AS agcgr ON (agcgr.acl_group_id = agar.acl_group_id)
WHERE aar.acl_action_name LIKE "service\_%" OR aar.acl_action_name LIKE "host\_%"
AND agcr.contact_contact_id = :contact_id
OR agcgr.acl_group_id IN (
SELECT contactgroup_cg_id
FROM contactgroup_contact_relation
WHERE contact_contact_id = :contact_id
)
');
$stmt->bindValue(':contact_id', $this->user->user_id, PDO::PARAM_INT);
$stmt->execute();
if (($row = $stmt->fetch()) && $row['countAcl'] > 0) {
$role = 'operator';
}
}

return [
'id' => substr($this->uuid, 0, 6) . '-' . $this->user->user_id,
'locale' => $locale,
'role' => $role
];
}

/**
* Get account information
*
* @return array<string,mixed> with account information
*/
private function getAccountInformation(): array
{
// Get Centreon statistics
$centreonStats = new CentreonStatistics($this->logger);
$configUsage = $centreonStats->getPlatformInfo();

// Get Licences information
$licenseInfo = $this->getLicenseInformation();

// Get Version of Centreon
$centreonVersion = $this->getCentreonVersion();

return [
'id' => $this->uuid,
'name' => $licenseInfo['companyName'],
'serverType' => $this->getServerType(),
'licenseType' => $licenseInfo['licenseType'],
'versionMajor' => $centreonVersion['major'],
'versionMinor' => $centreonVersion['minor'],
'nb_hosts' => (int) $configUsage['nb_hosts'],
'nb_services' => (int) $configUsage['nb_services'],
'nb_servers' => $configUsage['nb_central'] + $configUsage['nb_remotes'] + $configUsage['nb_pollers']
];
}

/**
* Get license information such as company name and license type
*
* @return array<string,string> with license info
*/
private function getLicenseInformation(): array
{
/**
* Getting License informations.
*/
$dependencyInjector = \Centreon\LegacyContainer::getInstance();
$productLicense = 'Open Source';
$licenseClientName = '';
try {
$centreonModules = ['epp', 'bam', 'map', 'mbi'];
$licenseObject = $dependencyInjector['lm.license'];
$licenseInformation = [];
foreach ($centreonModules as $module) {
$licenseObject->setProduct($module);
$isLicenseValid = $licenseObject->validate(false);
if ($isLicenseValid && !empty($licenseObject->getData())) {
$licenseInformation[$module] = $licenseObject->getData();
$licenseClientName = $licenseInformation[$module]['client']['name'];
if ($module === 'epp') {
$productLicense = 'IT Edition';
}
if (in_array($module, ['mbi', 'bam', 'map'])) {
$productLicense = 'Business Edition';
break;
}
}
}
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage, ['context' => $exception]);
}

return [
'companyName' => $licenseClientName,
'licenseType' => $productLicense
];
}

/**
* Get the major and minor versions of Centreon web
*
* @return array<string,string|null> with major and minor versions
* @throws \PDOException
*/
private function getCentreonVersion(): array
{
$major = null;
$minor = null;

$result = $this->pearDB->query(
"SELECT informations.value FROM informations WHERE informations.key = 'version'"
);
if ($row = $result->fetch()) {
$minor = $row['value'];
$major = substr($minor, 0, strrpos($minor, '.', 0));
}

return [
'major' => $major,
'minor' => $minor
];
}

/**
* Get CEIP status
*
* @return bool the status of CEIP
* @throws \PDOException
*/
private function isCeipActive(): bool
{
$result = $this->pearDB->query(
"SELECT `value` FROM `options` WHERE `key` = 'send_statistics' LIMIT 1"
);

if (($sendStatisticsResult = $result->fetch()) && $sendStatisticsResult["value"] === "1") {
return true;
} else {
return false;
}
}
}
Loading

0 comments on commit ccb8638

Please sign in to comment.