MetricService.php in Drupalmonitor 8
File
src/MetricService.php
View source
<?php
namespace Drupal\drupalmonitor;
use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\Yaml\Yaml;
class MetricService {
protected $database;
protected $moduleHandler;
public function __construct(Connection $database, ModuleHandlerInterface $module_handler) {
$this->database = $database;
$this->moduleHandler = $module_handler;
}
public function getUserCount() {
$query = "SELECT count(*) FROM {users}";
return (int) $this->database
->query($query)
->fetchField();
}
public function getActiveSessionsCount() {
$query = "SELECT count(*) FROM {users_field_data} u WHERE NOW()-300 > u.access";
return (int) $this->database
->query($query)
->fetchField();
}
public function getLoggedInSessionsCount() {
$query = "SELECT count(*) FROM {users_field_data} u WHERE NOW()-300 > u.access AND u.uid > 0";
return (int) $this->database
->query($query)
->fetchField();
}
public function getFilesCount() {
$query = "SELECT count(*) FROM {file_managed}";
return (int) $this->database
->query($query)
->fetchField();
}
public function getTotalFilesSize() {
$query = "SELECT sum(filesize) FROM {file_managed}";
return (int) $this->database
->query($query)
->fetchField();
}
public function getNodeContentTypes() {
$query = "SELECT n.type, count(*) as counter FROM {node} n GROUP BY n.type";
return $this->database
->query($query)
->fetchAllKeyed();
}
public function getModuleList() {
$modules_list = [];
foreach ($this->moduleHandler
->getModuleList() as $module_name => $module_info) {
$modules_list[$module_name] = Yaml::parseFile($module_info
->getPathname());
}
return $modules_list;
}
public function getConfigList() {
$query = "SELECT * FROM {config}";
$result = $this->database
->query($query);
$data = [];
foreach ($result as $record) {
$data[$record->name] = unserialize($record->data);
}
return $data;
}
public function getServerData() {
$info = [];
$info['php_uname'] = php_uname('s');
$info['system_load'] = sys_getloadavg();
$info['php_version'] = phpversion();
$info['getrusage'] = getrusage();
$info['sys_get_temp_dir'] = sys_get_temp_dir();
$info['PHP_SERVER'] = $_SERVER;
$info['phpinfo'] = $this
->getPhpInfoAsArray();
return $info;
}
public function getPhpInfoAsArray() {
ob_start();
phpinfo();
$info_arr = [];
$info_lines = explode("\n", strip_tags(ob_get_clean(), "<tr><td><h2>"));
$cat = "General";
foreach ($info_lines as $line) {
preg_match("~<h2>(.*)</h2>~", $line, $title) ? $cat = $title[1] : NULL;
if (preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val)) {
$info_arr[$cat][$val[1]] = $val[2];
}
elseif (preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val)) {
$info_arr[$cat][$val[1]] = [
"local" => $val[2],
"master" => $val[3],
];
}
}
return $info_arr;
}
}