Skip to content

Commit

Permalink
poller optimalization (thold), fix few bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
xmacan committed Sep 16, 2020
1 parent 518f9fd commit 4f3758c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 46 deletions.
2 changes: 1 addition & 1 deletion INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[info]
name = intropage
version = 2.0.2
version = 2.0.3
longname = Intropage/Dashboard
author = Petr Macek
email = [email protected]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Tomas Macek, Peter Michael Calum, Trevor Leadley, Earendil
## Changelog
2.0.3 ---
Poller speed up
Fix a lot of small bugs

2.0.2 ---
All panels with user permission
Expand Down
47 changes: 18 additions & 29 deletions include/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1391,27 +1391,23 @@ function trend($display=false, $update=false, $force_update=false) {
foreach ($users as $user) {
/*
old fast code
$x = 0;
$allowed = get_allowed_devices('','null',-1,$x,$user['id']);
if (count($allowed) > 0) {
$allowed_hosts = implode(',', array_column($allowed, 'id'));
} else {
$allowed_hosts = false;
}
if ($allowed_hosts) {
*/
if ($_SESSION['allowed_hosts'][$user['id']]) {


db_execute_prepared("REPLACE INTO plugin_intropage_trends
(name,value,user_id)
SELECT 'thold', COUNT(*),?
FROM thold_data
WHERE thold_data.host_id in (" . $allowed_hosts . ") AND thold_data.thold_alert!=0
OR thold_data.bl_fail_count >= thold_data.bl_fail_trigger",
WHERE thold_data.host_id in (" . $_SESSION['allowed_hosts'][$user['id']] . ")
AND thold_data.thold_enabled = 'on'
AND (((thold_data.thold_alert != 0 AND thold_data.thold_fail_count >= thold_data.thold_fail_trigger)
OR (thold_data.bl_alert > 0 AND thold_data.bl_fail_count >= thold_data.bl_fail_trigger)))",
array($user['id']));

/*
new code from thold plugin
*/
include_once($config['base_path'] . '/plugins/thold/thold_functions.php');
Expand All @@ -1426,21 +1422,14 @@ function trend($display=false, $update=false, $force_update=false) {
(name,value,user_id)
VALUES ('thold', ?,?)",
array($t_trig,$user['id']));

*/

db_execute_prepared("REPLACE INTO plugin_intropage_trends
(name,value,user_id)
SELECT 'host', COUNT(*),?
FROM host
WHERE id in (" . $_SESSION['allowed_hosts'][$user['id']] . ") AND status='1' AND disabled=''",
array($user['id']));
//echo "\n\n";
//echo "resim usera: " . $user['id'] . "\n";
//print_r($_SESSION['allowed_hosts'][$user['id']]);
//echo "\n\n";

/*
old code
}
else {
db_execute_prepared("REPLACE INTO plugin_intropage_trends
Expand All @@ -1450,7 +1439,7 @@ function trend($display=false, $update=false, $force_update=false) {
(name,value,user_id) values ('host,0,?)",
array($user['id']));
}
*/

}
}

Expand Down Expand Up @@ -3041,8 +3030,9 @@ function graph_thold($display=false, $update=false, $force_update=false) {
$result['data'] = __('You don\'t have plugin permission', 'intropage');
} else {
/*
// old code, wrong counts
// old code, faster
https://github.com/Cacti/plugin_thold/issues/440
*/
// need for thold - isn't any better solution?
$current_user = db_fetch_row('SELECT * FROM user_auth WHERE id=' . $user['id']);
$sql_where = get_graph_permissions_sql($current_user['policy_graphs'], $current_user['policy_hosts'], $current_user['policy_graph_templates']);
Expand All @@ -3051,16 +3041,15 @@ function graph_thold($display=false, $update=false, $force_update=false) {
(thold_data.host_id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id= ' . $user['id'] . ') OR
(thold_data.graph_template_id=user_auth_perms.item_id AND user_auth_perms.type=4 AND user_auth_perms.user_id= ' . $user['id'] . '))';

$t_all = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE $sql_where");
$t_brea = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE (thold_data.thold_alert!=0 OR thold_data.bl_alert>0) AND $sql_where");
$t_trig = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE (thold_data.thold_alert!=0 OR thold_data.bl_fail_count >= thold_data.bl_fail_trigger) AND $sql_where");
$t_trig = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE ((thold_data.thold_alert!=0 AND thold_data.thold_fail_count >= thold_data.thold_fail_trigger) OR (thold_data.bl_alert>0 AND thold_data.bl_fail_count >= thold_data.bl_fail_trigger)) AND $sql_where");
$t_all = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE thold_data.thold_enabled = 'on' AND $sql_where");
$t_brea = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE thold_data.thold_enabled = 'on' AND (thold_data.thold_alert!=0 OR thold_data.bl_alert>0) AND $sql_where");
$t_trig = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE thold_data.thold_enabled = 'on' AND ((thold_data.thold_alert!=0 AND thold_data.thold_fail_count >= thold_data.thold_fail_trigger) OR (thold_data.bl_alert > 0 AND thold_data.bl_fail_count >= thold_data.bl_fail_trigger)) AND $sql_where");
$t_disa = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE thold_data.thold_enabled='off' AND $sql_where");

$count = $t_all + $t_brea + $t_trig + $t_disa;

*/

/*
// right way but it is slow
include_once($config['base_path'] . '/plugins/thold/thold_functions.php');
Expand All @@ -3076,7 +3065,7 @@ function graph_thold($display=false, $update=false, $force_update=false) {
$x = get_allowed_thresholds($sql_where, 'null', 1, $t_trig, $user['id']);
$sql_where = "td.thold_enabled = 'off'";
$x = get_allowed_thresholds($sql_where, 'null', 1, $t_disa, $user['id']);

*/
$has_access = db_fetch_cell('SELECT COUNT(*) FROM user_auth_realm WHERE user_id = '.$user['id']." AND realm_id IN (SELECT id + 100 FROM plugin_realms WHERE file LIKE '%thold_graph.php%')");
$url_prefix = $has_access ? '<a href="' . html_escape($config['url_path'] . 'plugins/thold/thold_graph.php?tab=thold&triggered=%s') . '">' : '';
$url_suffix = $has_access ? '</a>' : '';
Expand Down
36 changes: 20 additions & 16 deletions include/data_detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ function graph_thold_detail() {
} elseif (!db_fetch_cell('SELECT DISTINCT user_id FROM user_auth_realm WHERE user_id = ' . $_SESSION['sess_user_id'] . " AND realm_id IN (SELECT id + 100 FROM plugin_realms WHERE file LIKE '%thold%')")) {
$result['detail'] = __('You don\'t have permission', 'intropage');
} else {
/* better code, slower
include_once($config['base_path'] . '/plugins/thold/thold_functions.php');
// right way but it is slow
Expand All @@ -835,9 +836,10 @@ function graph_thold_detail() {
$t_trig_result = get_allowed_thresholds($sql_where, 'null', '', $t_trig, $_SESSION['sess_user_id']);
$sql_where = "td.thold_enabled = 'off'";
$x = get_allowed_thresholds($sql_where, 'null', 1, $t_disa, $_SESSION['sess_user_id']);

*/
/* old fast code, but wrong counts
https://github.com/Cacti/plugin_thold/issues/440
*/
// need for thold - isn't any better solution?
$current_user = db_fetch_row('SELECT * FROM user_auth WHERE id=' . $_SESSION['sess_user_id']);
$sql_where = get_graph_permissions_sql($current_user['policy_graphs'], $current_user['policy_hosts'], $current_user['policy_graph_templates']);
Expand All @@ -846,16 +848,14 @@ function graph_thold_detail() {
(thold_data.host_id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id= ' . $_SESSION['sess_user_id'] . ') OR
(thold_data.graph_template_id=user_auth_perms.item_id AND user_auth_perms.type=4 AND user_auth_perms.user_id= ' . $_SESSION['sess_user_id'] . '))';

$t_all = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE $sql_where");
$t_brea = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE (thold_data.thold_alert!=0 OR thold_data.bl_alert>0) AND $sql_where");
$t_trig = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE (thold_data.thold_alert!=0 OR thold_data.bl_fail_count >= thold_data.bl_fail_trigger) AND $sql_where");
$t_trig = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE ((thold_data.thold_alert!=0 AND
thold_data.thold_fail_count >= thold_data.thold_fail_trigger) OR (thold_data.bl_alert>0 AND thold_data.bl_fail_count >= thold_data.bl_fail_trigger)) AND $sql_where");
$t_all = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE $sql_where AND thold_data.thold_enabled='on' ");
$t_brea = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE thold_data.thold_enabled='on' AND (thold_data.thold_alert!=0 OR thold_data.bl_alert>0) AND $sql_where");
$t_trig = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE thold_data.thold_enabled='on' AND ((thold_data.thold_alert!=0 AND
thold_data.thold_fail_count >= thold_data.thold_fail_trigger) OR (thold_data.bl_alert>0 AND thold_data.bl_fail_count >= thold_data.bl_fail_trigger)) AND $sql_where");
$t_disa = db_fetch_cell("SELECT COUNT(*) FROM thold_data $sql_join WHERE thold_data.thold_enabled='off' AND $sql_where");

$count = $t_all + $t_brea + $t_trig + $t_disa;
*/


$has_access = db_fetch_cell('SELECT COUNT(*) FROM user_auth_realm WHERE user_id = '.$_SESSION['sess_user_id']." AND realm_id IN (SELECT id + 100 FROM plugin_realms WHERE file LIKE '%thold_graph.php%')");
$url_prefix = $has_access ? '<a href="' . html_escape($config['url_path'] . 'plugins/thold/thold_graph.php?tab=thold&triggered=%s') . '">' : '';
Expand All @@ -869,19 +869,22 @@ function graph_thold_detail() {
// alarms and details
if ($t_brea > 0) {
$result['alarm'] = 'yellow';
// $hosts = db_fetch_assoc("SELECT description FROM thold_data $sql_join WHERE (thold_data.thold_alert!=0 OR thold_data.bl_alert>0) AND $sql_where");
$hosts = db_fetch_assoc("SELECT description FROM thold_data $sql_join WHERE thold_data.thold_enabled='on' AND (thold_data.thold_alert!=0 OR thold_data.bl_alert>0) AND $sql_where");
$result['detail'] .= '<b>' . __('BREACHED', 'intropage') . ':</b><br/>';
foreach ($t_brea_result as $host) {
// foreach ($t_brea_result as $host) {
foreach ($hosts as $host) {
$result['detail'] .= html_escape($host['name_cache']) . '<br/>';
}
$result['detail'] .= '<br/><br/>';
}

if ($t_trig > 0) {
$result['alarm'] = 'red';
// $hosts = db_fetch_assoc("SELECT description FROM thold_data $sql_join WHERE (thold_data.thold_alert!=0 OR thold_data.bl_fail_count >= thold_data.bl_fail_trigger) AND $sql_where");
$hosts = db_fetch_assoc("SELECT description FROM thold_data $sql_join WHERE thold_data.thold_enabled = 'on' AND ((thold_data.thold_alert!=0 AND
thold_data.thold_fail_count >= thold_data.thold_fail_trigger) OR (thold_data.bl_alert > 0 AND thold_data.bl_fail_count >= thold_data.bl_fail_trigger)) AND $sql_where");
$result['detail'] .= '<b>' . __('TRIGGERED', 'intropage') .':</b><br/>';
foreach ($t_trig_result as $host) {
foreach ($hosts as $host) {
// foreach ($t_trig_result as $host) {
$result['detail'] .= html_escape($host['name_cache']) . '<br/>';
}
$result['detail'] .= '<br/><br/>';
Expand Down Expand Up @@ -1012,9 +1015,10 @@ function thold_event_detail() {
$result['alarm'] = 'yellow';
$result['detail'] = __('Plugin Thold isn\'t installed or started', 'intropage');
} else {
include_once($config['base_path'] . '/plugins/thold/thold_functions.php');
// include_once($config['base_path'] . '/plugins/thold/thold_functions.php');

/* old faster code
*/
$sql_result = db_fetch_assoc('SELECT tl.description as description,tl.time as time,
tl.status as status, uap0.user_id AS user0, uap1.user_id AS user1, uap2.user_id AS user2
FROM plugin_thold_log AS tl
Expand All @@ -1037,9 +1041,9 @@ function thold_event_detail() {
WHERE td.host_id in (' . $_SESSION['allowed_hosts'] . ')
HAVING (user0 IS NULL OR (user1 IS NULL OR user2 IS NULL))
ORDER BY `time` DESC
LIMIT 20');
*/
$sql_result = get_allowed_threshold_logs('','tl.time desc', 30);
LIMIT 30');

// $sql_result = get_allowed_threshold_logs('','tl.time desc', 30);

if (cacti_sizeof($sql_result)) {
foreach ($sql_result as $row) {
Expand Down

0 comments on commit 4f3758c

Please sign in to comment.