Merge branch 'ent-10979-plugin-grafana' into 'develop'
Ent 10979 plugin grafana See merge request artica/pandorafms!6007
This commit is contained in:
commit
08b00dba7b
|
@ -0,0 +1,7 @@
|
|||
# /etc/httpd/conf/httpd.conf -> AllowOverride All -> service httpd restart
|
||||
|
||||
RewriteEngine on
|
||||
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteCond %{REQUEST_FILENAME}\.php -f
|
||||
RewriteRule ^(.*)$ $1.php
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
// Allow Grafana proxy
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, X-Grafana-Org-Id, X-Grafana-NoCache, X-DS-Authorization');
|
||||
|
||||
// Get all request headers
|
||||
$headers = apache_request_headers();
|
||||
|
||||
// Check if user and password has been sent
|
||||
if ($headers['Authorization']) {
|
||||
$headers['X-DS-Authorization'] = $headers['Authorization'];
|
||||
}
|
||||
|
||||
if ($headers['X-DS-Authorization']) {
|
||||
include_once '../../include/config.php';
|
||||
|
||||
global $config;
|
||||
|
||||
include_once $config['homedir'].'/include/functions_config.php';
|
||||
include_once $config['homedir'].'/include/functions.php';
|
||||
|
||||
list($user, $password) = explode(':', base64_decode($headers['X-DS-Authorization']));
|
||||
|
||||
// Check user login
|
||||
$user_in_db = process_user_login($user, $password, true);
|
||||
|
||||
if ($user_in_db !== false) {
|
||||
// Check user ACL
|
||||
if (check_acl($user_in_db, 0, 'AR')) {
|
||||
$result_array = [
|
||||
'code' => 200,
|
||||
'message' => 'Access granted',
|
||||
];
|
||||
} else {
|
||||
$result_array = [
|
||||
'code' => 403,
|
||||
'message' => 'Access forbidden',
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$result_array = [
|
||||
'code' => 401,
|
||||
'message' => 'Unauthorized',
|
||||
];
|
||||
}
|
||||
} else {
|
||||
// OPTIONS request automatically works
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
|
||||
$result_array = [
|
||||
'code' => 200,
|
||||
'message' => 'Options request accepted',
|
||||
];
|
||||
} else {
|
||||
$result_array = [
|
||||
'code' => 401,
|
||||
'message' => 'Unauthorized',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Numeric data in array must be numeric data in json (not text)
|
||||
$result = json_encode($result_array, JSON_NUMERIC_CHECK);
|
||||
|
||||
echo $result;
|
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
// Allow Grafana proxy.
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, X-Grafana-Org-Id, X-Grafana-NoCache, X-DS-Authorization');
|
||||
|
||||
// Get all request headers.
|
||||
$headers = apache_request_headers();
|
||||
|
||||
$result_array = [];
|
||||
|
||||
// Check if user and password has been sent.
|
||||
if ($headers['Authorization']) {
|
||||
// Get all POST data sent.
|
||||
$payload = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
include_once '../../include/config.php';
|
||||
|
||||
global $config;
|
||||
|
||||
include_once $config['homedir'].'/include/functions_config.php';
|
||||
include_once $config['homedir'].'/include/functions.php';
|
||||
|
||||
list($user, $password) = explode(':', base64_decode($headers['Authorization']));
|
||||
|
||||
// Check user login
|
||||
$user_in_db = process_user_login($user, $password, true);
|
||||
|
||||
if ($user_in_db !== false) {
|
||||
// Check user ACL
|
||||
if (check_acl($user_in_db, 0, 'AR')) {
|
||||
include_once $config['homedir'].'/include/functions_db.php';
|
||||
include_once $config['homedir'].'/include/functions_graph.php';
|
||||
|
||||
// Get graph data for each Grafana target
|
||||
foreach ($payload['targets'] as $target) {
|
||||
$sql_results = [];
|
||||
$result_data = [];
|
||||
|
||||
// Decode target data sent by datasource plugin in Grafana
|
||||
$target_data = json_decode($target['target'], true);
|
||||
|
||||
if ($target_data['module']) {
|
||||
// Get module name as target if not defined in Grafana.
|
||||
if (!$target_data['target']) {
|
||||
$target_data['target'] = io_safe_output(db_get_value_sql('SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = '.$target_data['module']));
|
||||
}
|
||||
|
||||
$target_data['interval'] = db_get_value_sql('SELECT module_interval FROM tagente_modulo WHERE id_agente_modulo = '.$target_data['module']);
|
||||
|
||||
$params = [
|
||||
'agent_module_id' => $target_data['module'],
|
||||
'period' => (strtotime($payload['range']['to']) - strtotime($payload['range']['from'])),
|
||||
'date' => strtotime($payload['range']['to']),
|
||||
'return_data' => 1,
|
||||
'show_unknown' => true,
|
||||
'fullscale' => (bool) $target_data['tip'],
|
||||
'time_interval' => $target_data['interval'],
|
||||
];
|
||||
|
||||
// Get all data.
|
||||
$data = grafico_modulo_sparse($params);
|
||||
|
||||
$unknown_timestamps = [];
|
||||
|
||||
// Set unknown data as null.
|
||||
foreach ($data['unknown1']['data'] as $d) {
|
||||
if (($d[1] == 1 && !$params['fullscale']) || ($d[1] == 0 && $params['fullscale'])) {
|
||||
$result_data[] = [
|
||||
null,
|
||||
$d[0],
|
||||
];
|
||||
}
|
||||
|
||||
$unknown_timestamps[] = $d[0];
|
||||
}
|
||||
|
||||
// Get each data if not in unknown timestamps
|
||||
foreach ($data['sum1']['data'] as $d) {
|
||||
if ($d[1] != false && !in_array($d[0], $unknown_timestamps)) {
|
||||
$result_data[] = [
|
||||
$d[1],
|
||||
$d[0],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Sort all data by utimestamp (Grafana needs it).
|
||||
usort(
|
||||
$result_data,
|
||||
function ($a, $b) {
|
||||
return $a[1] > $b[1] ? 1 : -1;
|
||||
}
|
||||
);
|
||||
|
||||
$rows = [];
|
||||
|
||||
foreach ($result_data as $k => $v) {
|
||||
if (($result_data[$k][0] !== $result_data[($k - 1)][0]
|
||||
|| $result_data[$k][0] !== $result_data[($k + 1)][0])
|
||||
|| ($result_data[($k - 1)][0] === null
|
||||
&& $result_data[$k][0] !== null
|
||||
&& $result_data[$k][1] != (strtotime($payload['range']['to']) * 1000))
|
||||
|| ($result_data[($k - 1)][0] === $result_data[$k][0] && $result_data[$k][1] == (strtotime($payload['range']['to']) * 1000))
|
||||
) {
|
||||
$rows[] = $result_data[$k];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$params['fullscale']) {
|
||||
$target_data['target'] .= ' (avg)';
|
||||
}
|
||||
|
||||
// Set all target info and data
|
||||
$result_array[] = [
|
||||
'type' => 'table',
|
||||
'target' => $target_data['target'],
|
||||
'refId' => $target_data['target'],
|
||||
'columns' => [
|
||||
[
|
||||
'text' => $target_data['target'],
|
||||
],
|
||||
[
|
||||
'text' => 'Time',
|
||||
'type' => 'time',
|
||||
],
|
||||
],
|
||||
'datapoints' => array_values($rows),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Numeric data in array must be numeric data in json (not text).
|
||||
$result = json_encode($result_array, JSON_NUMERIC_CHECK);
|
||||
|
||||
echo $result;
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
// Allow Grafana proxy
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, X-Grafana-Org-Id, X-Grafana-NoCache, X-DS-Authorization');
|
||||
|
||||
// Get all request headers
|
||||
$headers = apache_request_headers();
|
||||
|
||||
$result_array = [];
|
||||
|
||||
// Check if user and password has been sent
|
||||
if ($headers['Authorization']) {
|
||||
// Get all POST data sent
|
||||
$payload = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
include_once '../../include/config.php';
|
||||
|
||||
global $config;
|
||||
|
||||
include_once $config['homedir'].'/include/functions_config.php';
|
||||
include_once $config['homedir'].'/include/functions.php';
|
||||
|
||||
list($user, $password) = explode(':', base64_decode($headers['Authorization']));
|
||||
|
||||
// Check user login
|
||||
$user_in_db = process_user_login($user, $password, true);
|
||||
|
||||
if ($user_in_db !== false) {
|
||||
// Check user ACL
|
||||
if (check_acl($user_in_db, 0, 'AR')) {
|
||||
include_once $config['homedir'].'/include/functions_db.php';
|
||||
|
||||
// If search is for groups
|
||||
if ($payload['type'] == 'group') {
|
||||
// Include group ALL
|
||||
$result_array[] = [
|
||||
'value' => 0,
|
||||
'text' => 'All',
|
||||
];
|
||||
|
||||
// Get groups that match the search
|
||||
$sql = 'SELECT nombre, id_grupo id FROM tgrupo WHERE LOWER(nombre) LIKE LOWER("%'.io_safe_input($payload['search']).'%")';
|
||||
|
||||
// If search is for agents
|
||||
} else if ($payload['type'] == 'agent') {
|
||||
// Get agents that match the search
|
||||
$sql = 'SELECT a.alias nombre, a.id_agente id FROM tagente a, tgrupo g WHERE a.disabled = 0 AND a.id_grupo = g.id_grupo AND LOWER(a.alias) LIKE LOWER("%'.io_safe_input($payload['search']).'%")';
|
||||
|
||||
// If search group is not all, add extra filter
|
||||
if ($payload['extra'] != 0) {
|
||||
$sql .= ' AND g.id_grupo = "'.io_safe_input($payload['extra']).'"';
|
||||
}
|
||||
|
||||
// If search is for modules
|
||||
} else if ($payload['type'] == 'module') {
|
||||
// Get modules that match the search (not string)
|
||||
$sql = 'SELECT m.nombre nombre, m.id_agente_modulo id FROM tagente_modulo m, tagente a, ttipo_modulo t WHERE m.disabled = 0 AND m.id_agente = a.id_agente AND t.id_tipo = m.id_tipo_modulo AND a.id_agente = "'.io_safe_input($payload['extra']).'" AND LOWER(m.nombre) LIKE LOWER("%'.io_safe_input($payload['search']).'%") AND t.nombre NOT LIKE "%string"';
|
||||
}
|
||||
|
||||
// Run query
|
||||
$sql_results = db_get_all_rows_sql($sql);
|
||||
|
||||
foreach ($sql_results as $sql_result) {
|
||||
// If search is for groups, only add those with permissions
|
||||
if ($payload['type'] == 'group') {
|
||||
if (check_acl($user_in_db, $sql_result['id'], 'AR')) {
|
||||
$result_array[] = [
|
||||
'value' => $sql_result['id'],
|
||||
'text' => io_safe_output($sql_result['nombre']),
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$result_array[] = [
|
||||
'value' => $sql_result['id'],
|
||||
'text' => io_safe_output($sql_result['nombre']),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result = json_encode($result_array, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
echo $result;
|
Loading…
Reference in New Issue