From d30f649c18dff4b7125c4d073ad3d0c369d1e9b1 Mon Sep 17 00:00:00 2001 From: Laurent Pinsivy Date: Tue, 9 Apr 2019 11:05:01 +0200 Subject: [PATCH] enh(ceip): Add additional statistics including modules if present (#7328) * enh(ceip): Add additional statistics including modules if present * enh(doc): add statistics modules & widgets usage --- cron/centreon-send-stats.php | 4 +- doc/en/ceip.rst | 2 +- doc/fr/ceip.rst | 2 +- www/class/centreonStatistics.class.php | 25 +++++ www/class/centreonStatsModules.class.php | 118 +++++++++++++++++++++++ www/class/centreonVersion.class.php | 45 +++++++++ 6 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 www/class/centreonStatsModules.class.php diff --git a/cron/centreon-send-stats.php b/cron/centreon-send-stats.php index cfbce6dcd4f..62e2a3ecf0c 100644 --- a/cron/centreon-send-stats.php +++ b/cron/centreon-send-stats.php @@ -30,6 +30,7 @@ $versions = $oStatistics->getVersion(); $infos = $oStatistics->getPlatformInfo(); $timez = $oStatistics->getPlatformTimezone(); + $additional = $oStatistics->getAdditionalData(); // Construct the object gathering datas $data = array( @@ -37,7 +38,8 @@ 'UUID' => $UUID, 'versions' => $versions, 'infos' => $infos, - 'timezone' => $timez + 'timezone' => $timez, + 'additional' => $additional ); try { diff --git a/doc/en/ceip.rst b/doc/en/ceip.rst index b27bb1febd3..971278a5442 100644 --- a/doc/en/ceip.rst +++ b/doc/en/ceip.rst @@ -19,7 +19,7 @@ voluntary and anonymous. Customers who choose to participate agree to share: * Information such as the operating system version of the Centreon main server of the platform as well as the name and version of the DBMS. * Centreon product information such as number of servers and version numbers of components installed on the Centreon main server (modules & widgets). -* Centreon information such as number of hosts, services, groups of hosts & services. +* Centreon information such as number of hosts, services, groups of hosts & services, and usage of modules and widgets. * The timezone of the Centreon server. During the installation or upgrade processus, you will be asked to participate in diff --git a/doc/fr/ceip.rst b/doc/fr/ceip.rst index 79b44dc7de4..79504eb24a4 100644 --- a/doc/fr/ceip.rst +++ b/doc/fr/ceip.rst @@ -21,7 +21,7 @@ choisissent de participer acceptent de partager : * Informations telles que la version du système d'exploitation du serveur principal Centreon de la plate-forme, ainsi que le nom et la version du SGBD * Informations sur les produits Centreon, telles que le nombre de serveurs et le numéro de version des composants installés sur le serveur principal de Centreon (modules et widgets) -* Informations sur la configuration Centreon tels que le nombre d'hôtes, de services, de groupes d'hôtes et les services +* Informations sur la configuration Centreon tels que le nombre d'hôtes, de services, de groupes d'hôtes et les services, ainsi que l'usage des modules et widgets * Le fuseau horaire du serveur Centreon Au cours du processus d'installation ou de mise à jour, il vous sera demandé de diff --git a/www/class/centreonStatistics.class.php b/www/class/centreonStatistics.class.php index 00a24493215..bf7014a1346 100644 --- a/www/class/centreonStatistics.class.php +++ b/www/class/centreonStatistics.class.php @@ -37,6 +37,7 @@ require_once _CENTREON_PATH_ . "/www/class/centreonGMT.class.php"; require_once _CENTREON_PATH_ . "/www/class/centreonVersion.class.php"; require_once _CENTREON_PATH_ . "/www/class/centreonDB.class.php"; +require_once _CENTREON_PATH_ . "/www/class/centreonStatsModules.class.php"; class CentreonStatistics { @@ -126,4 +127,28 @@ public function getPlatformTimezone() 'timezone' => $timezone ); } + + /** + * Get Additional data + * + * @return array + */ + public function getAdditionalData() + { + $centreonVersion = new CentreonVersion($this->dbConfig); + + $data = array( + 'extension' => array( + 'widgets' => $centreonVersion->getWidgetsUsage() + ), + ); + + $oModulesStats = new CentreonStatsModules(); + $modulesData = $oModulesStats->getModulesStatistics(); + foreach ($modulesData as $moduleData) { + $data['extension'] = array_merge($data['extension'], $moduleData); + } + + return $data; + } } diff --git a/www/class/centreonStatsModules.class.php b/www/class/centreonStatsModules.class.php new file mode 100644 index 00000000000..ff3fbfba4c5 --- /dev/null +++ b/www/class/centreonStatsModules.class.php @@ -0,0 +1,118 @@ +. + * + * Linking this program statically or dynamically with other modules is making a + * combined work based on this program. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this program give Centreon + * permission to link this program with independent modules to produce an executable, + * regardless of the license terms of these independent modules, and to copy and + * distribute the resulting executable under terms of Centreon choice, provided that + * Centreon also meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module which is not + * derived from this program. If you modify this program, you may extend this + * exception to your version of the program, but you are not obliged to do so. If you + * do not wish to do so, delete this exception statement from your version. + * + * For more information : contact@centreon.com + * + */ + +require_once _CENTREON_PATH_ . "/www/class/centreonDB.class.php"; + +class CentreonStatsModules +{ + /** + * @var \CentreonDB + */ + private $db; + + /** + * Constructor + */ + public function __construct() + { + $this->db = new centreonDB(); + } + + /** + * Get list of installed modules + * + * @return array Return the names of installed modules [['name' => string], ...] + */ + private function getInstalledModules() + { + $installedModules = array(); + $stmt = $this->db->prepare("SELECT name FROM modules_informations"); + $stmt->execute(); + foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $value) { + $installedModules[] = $value['name']; + } + + return $installedModules; + } + + /** + * Get statistics module objects + * + * @param array $installedModules Names of installed modules for which you want + * to retrieve statistics module [['name' => string], ...] + * + * @return array Return a list of statistics module found + * @see CentreonStatsModules::getInstalledModules() + */ + private function getModuleObjects(array $installedModules) + { + $moduleObjects = array(); + + foreach ($installedModules as $module) { + if ($files = glob(_CENTREON_PATH_ . 'www/modules/' . $module . '/statistics/*.class.php')) { + foreach ($files as $full_file) { + include_once $full_file; + $file_name = str_replace('.class.php', '', basename($full_file)); + if (class_exists(ucfirst($file_name))) { + $moduleObjects[] = ucfirst($file_name); + } + } + } + } + + return $moduleObjects; + } + + /** + * Get statistics from module + * + * @return array The statistics of each module + */ + public function getModulesStatistics() + { + $data = array(); + $moduleObjects = $this->getModuleObjects( + $this->getInstalledModules() + ); + + if (is_array($moduleObjects)) { + foreach ($moduleObjects as $moduleObject) { + $oModuleObject = new $moduleObject(); + $data[] = $oModuleObject->getStats(); + } + } + return $data; + } +} diff --git a/www/class/centreonVersion.class.php b/www/class/centreonVersion.class.php index 2f070e9a721..70e6e7fe505 100644 --- a/www/class/centreonVersion.class.php +++ b/www/class/centreonVersion.class.php @@ -144,6 +144,51 @@ public function getSystem() $data['mysql'] = $row['Value']; } + return array_merge($data, $this->getVersionSystem()); + } + + /** + * get system information + * + * @return array $data An array composed with the name and version of the OS + * @throws Exception + */ + public function getVersionSystem() + { + $data = array(); + + // Get OS version + if (function_exists("shell_exec") && is_readable("/etc/os-release")) { + $os = shell_exec('cat /etc/os-release'); + if (preg_match_all('/ID=?"(.*)?"/', $os, $matches)) { + $data['OS_name'] = $matches[1][0]; + $data['OS_version'] = $matches[1][1]; + } + } + return $data; + } + + /** + * Get all Centreon widgets + * + * @return array $data Widgets statistics + */ + public function getWidgetsUsage() + { + $data = array(); + + $query = 'SELECT wm.title AS name, version, COUNT(widget_id) AS count + FROM widgets AS w + INNER JOIN widget_models AS wm ON (w.widget_model_id = wm.widget_model_id) + GROUP BY name'; + $result = $this->db->query($query); + while ($row = $result->fetch()) { + $data[] = array( + 'name' => $row['name'], + 'version' => $row['version'], + 'used' => $row['count'] + ); + } return $data; } }