phantomjs performance

This commit is contained in:
daniel 2020-10-13 18:24:10 +02:00 committed by Daniel Rodriguez
parent 1134113cf3
commit 148384f8fb
7 changed files with 186 additions and 35 deletions

View File

@ -272,6 +272,12 @@ if ($install_package) {
"Update version: $version of ".$product_name.' by '.$config['id_user']
);
// An update have been applied, clean phantomjs cache.
config_update_value(
'clean_phantomjs_cache',
1
);
$return['status'] = 'success';
echo json_encode($return);
return;

View File

@ -41,6 +41,17 @@ require_once $config['homedir'].'/include/functions_modules.php';
require_once $config['homedir'].'/include/functions_agents.php';
require_once $config['homedir'].'/include/functions_tags.php';
$data_raw = get_parameter('data');
$data_decoded = json_decode(base64_decode($data_raw), true);
if (json_last_error() === JSON_ERROR_NONE) {
$data = urldecode($data_decoded['data']);
$session_id = urldecode($data_decoded['session_id']);
$data_combined = urldecode($data_decoded['data_combined']);
$data_module_list = urldecode($data_decoded['data_module_list']);
$type_graph_pdf = urldecode($data_decoded['type_graph_pdf']);
$viewport_width = urldecode($data_decoded['viewport_width']);
}
/**
* Echo to stdout a PhantomJS callback call.
@ -69,7 +80,7 @@ function echoPhantomCallback()
global $config;
// Try to initialize session using existing php session id.
$user = new PandoraFMS\User(['phpsessionid' => $_REQUEST['session_id']]);
$user = new PandoraFMS\User(['phpsessionid' => $session_id]);
if (check_login(false) === false) {
// Error handler.
?>
@ -97,12 +108,12 @@ if (check_login(false) === false) {
}
// Access granted.
$params = json_decode($_REQUEST['data'], true);
$params = json_decode($data, true);
// Metaconsole connection to the node.
$server_id = $params['server_id'];
if (is_metaconsole() && !empty($server_id)) {
if (is_metaconsole() === true && empty($server_id) === false) {
$server = metaconsole_get_connection_by_id($server_id);
// Error connecting.
if (metaconsole_connect($server) !== NOERR) {
@ -165,12 +176,12 @@ if (file_exists('languages/'.$user_language.'.mo') === true) {
$params['only_image'] = false;
$params['menu'] = false;
$params_combined = json_decode($_REQUEST['data_combined'], true);
$module_list = json_decode($_REQUEST['data_module_list'], true);
$type_graph_pdf = $_REQUEST['type_graph_pdf'];
$params_combined = json_decode($data_combined, true);
$module_list = json_decode($data_module_list, true);
$type_graph_pdf = $type_graph_pdf;
if (isset($params['vconsole']) === false || $params['vconsole'] === false) {
$params['width'] = (int) $_REQUEST['viewport_width'];
$params['width'] = (int) $viewport_width;
if ((isset($params['width']) === false
|| ($params['width'] <= 0))
) {

View File

@ -107,13 +107,13 @@ class ConsoleSupervisor
$this->verbose = $verbose;
if ($source === false) {
$this->enabled = false;
$this->notificationsEnabled = false;
$this->sourceId = null;
$this->targetGroups = null;
$this->targetUsers = null;
} else {
$this->enabled = (bool) $source['enabled'];
$this->notificationsEnabled = (bool) $source['enabled'];
$this->sourceId = $source['id'];
// Assign targets.
@ -251,8 +251,10 @@ class ConsoleSupervisor
{
global $config;
if ($this->enabled === false) {
// Feature not enabled.
$this->maintenanceOperations();
if ($this->notificationsEnabled === false) {
// Notifications not enabled.
return;
}
@ -490,6 +492,21 @@ class ConsoleSupervisor
}
/**
* Executes console maintenance operations. Executed ALWAYS through CRON.
*
* @return void
*/
public function maintenanceOperations()
{
/*
* Process cache clean if needed.
*/
$this->checkCleanPhantomCache();
}
/**
* Update targets for given notification using object targets.
*
@ -2597,4 +2614,30 @@ class ConsoleSupervisor
}
/**
* Clean Phantom cache if needed.
*
* @return void
*/
public function checkCleanPhantomCache()
{
global $config;
if ((int) $config['clean_phantomjs_cache'] !== 1) {
return;
}
$cache_dir = $config['homedir'].'/attachment/cache';
if (is_dir($cache_dir) === true) {
rrmdir($cache_dir);
}
// Clean process has ended.
config_update_value(
'clean_phantomjs_cache',
0
);
}
}

View File

@ -3973,8 +3973,22 @@ function series_type_graph_array($data, $show_elements_graph)
}
function generator_chart_to_pdf($type_graph_pdf, $params, $params_combined=false, $module_list=false)
{
/**
* Draw chart pdf.
*
* @param string $type_graph_pdf Type graph.
* @param array $params Params.
* @param boolean $params_combined Params only charts combined.
* @param boolean $module_list Array modules.
*
* @return string Img or base64.
*/
function generator_chart_to_pdf(
$type_graph_pdf,
$params,
$params_combined=false,
$module_list=false
) {
global $config;
if (is_metaconsole()) {
@ -4016,8 +4030,18 @@ function generator_chart_to_pdf($type_graph_pdf, $params, $params_combined=false
}
$session_id = session_id();
$cache_dir = $config['homedir'].'/attachment/cache';
$cmd = '"'.io_safe_output($config['phantomjs_bin']).DIRECTORY_SEPARATOR.'phantomjs" --ssl-protocol=any --ignore-ssl-errors=true "'.$file_js.'" '.' "'.$url.'"'.' "'.$type_graph_pdf.'"'.' "'.$params_encode_json.'"'.' "'.$params_combined.'"'.' "'.$module_list.'"'.' "'.$img_path.'"'.' "'.$width_img.'"'.' "'.$height_img.'"'.' "'.$session_id.'"'.' "'.$params['return_img_base_64'].'"';
$cmd = '"'.io_safe_output($config['phantomjs_bin']);
$cmd .= DIRECTORY_SEPARATOR.'phantomjs" ';
$cmd .= ' --disk-cache=true --disk-cache-path="'.$cache_dir.'"';
$cmd .= ' --max-disk-cache-size=10000 ';
$cmd .= ' --ssl-protocol=any --ignore-ssl-errors=true ';
$cmd .= '"'.$file_js.'" "'.$url.'" "'.$type_graph_pdf.'"';
$cmd .= ' "'.$params_encode_json.'" "'.$params_combined.'"';
$cmd .= ' "'.$module_list.'" "'.$img_path.'"';
$cmd .= ' "'.$width_img.'" "'.$height_img.'"';
$cmd .= ' "'.$session_id.'" "'.$params['return_img_base_64'].'"';
$result = null;
$retcode = null;
@ -4029,7 +4053,7 @@ function generator_chart_to_pdf($type_graph_pdf, $params, $params_combined=false
// To be used in alerts.
return $img_content;
} else {
// to be used in PDF files.
// To be used in PDF files.
$config['temp_images'][] = $img_path;
return '<img src="'.$img_url.'" />';
}

View File

@ -1349,7 +1349,7 @@ function graphic_combined_module(
if ($count_modules > 0) {
foreach ($module_list as $key => $value) {
$sources[$key]['id_server'] = (isset($value['id_server']) === true) ? $value['id_server'] : $params['server_id'];
$sources[$key]['id_agent_module'] = $value['module'];
$sources[$key]['id_agent_module'] = (isset($value['module']) === true) ? $value['module'] : $value;
$sources[$key]['weight'] = $weights[$key];
$sources[$key]['label'] = $params_combined['labels'];
}

View File

@ -1753,6 +1753,12 @@ function update_manager_extract_package()
return false;
}
// An update have been applied, clean phantomjs cache.
config_update_value(
'clean_phantomjs_cache',
1
);
db_process_sql_update(
'tconfig',
['value' => 50],

View File

@ -27,27 +27,79 @@ if (!viewport_height) {
}
if (type_graph_pdf == "combined") {
post_data =
"data=" +
url_params +
"&data_combined=" +
url_params_comb +
"&data_module_list=" +
url_module_list +
"&type_graph_pdf=" +
type_graph_pdf +
"&session_id=" +
session_id;
post_data = {
data: url_params,
session_id: session_id,
type_graph_pdf: type_graph_pdf,
data_module_list: url_module_list,
data_combined: url_params_comb
};
} else {
post_data =
"data=" +
url_params +
"&type_graph_pdf=" +
type_graph_pdf +
"&session_id=" +
session_id;
post_data = {
data: url_params,
session_id: session_id,
type_graph_pdf: type_graph_pdf
};
}
/* DEBUG
page.onAlert = function() {
console.log("onAlert");
};
page.onCallback = function() {
console.log("onCallback");
};
page.onClosing = function() {
console.log("onClosing");
};
page.onConfirm = function() {
console.log("onConfirm");
};
page.onConsoleMessage = function() {
console.log("onConsoleMessage");
};
page.onError = function() {
console.log("onError");
};
page.onFilePicker = function() {
console.log("onFilePicker");
};
page.onInitialized = function() {
console.log("onInitialized");
};
page.onLoadFinished = function() {
console.log("onLoadFinished");
};
page.onLoadStarted = function() {
console.log("onLoadStarted");
};
page.onNavigationRequested = function() {
console.log("onNavigationRequested");
};
page.onPageCreated = function() {
console.log("onPageCreated");
};
page.onPrompt = function() {
console.log("onPrompt");
};
page.onResourceError = function() {
console.log("onResourceError");
};
page.onResourceReceived = function(res) {
console.log("onResourceReceived" + ";" + res.url + ";" + res.status);
};
page.onResourceRequested = function(res) {
console.log("onResourceRequested" + ";" + res.url);
};
page.onResourceTimeout = function() {
console.log("onResourceTimeout");
};
page.onUrlChanged = function(url) {
console.log("onUrlChanged" + ";" + url);
};
*/
var page = require("webpage").create();
page.onResourceError = function(resourceError) {
@ -80,8 +132,15 @@ page.viewportSize = {
page.zoomFactor = 1;
page.settings.userAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36";
page.settings.resourceTimeout = 2000;
page.settings.localToRemoteUrlAccessEnabled = true;
page.onConsoleMessage = function(msg) {
console.log(msg);
page.close();
phantom.exit();
};
page.onError = function(msg) {
@ -102,7 +161,9 @@ page.onCallback = function() {
phantom.exit();
};
page.open(url, "POST", post_data, function(status) {
page.open(url, "POST", "data=" + btoa(JSON.stringify(post_data)), function(
status
) {
if (status == "fail") {
console.out("Failed to generate chart.");
phantom.exit();