diff --git a/README.md b/README.md index 88af0c6..d740d5c 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ The sylog plugin has been in development for well over a decade with increasing ## ChangeLog --- develop --- +* issue#90: Can not show correct info when choose device filter in Syslog - Alert Log page * issue#91: Page become blank after collecting multiple host syslog info * issue#94: Stored XSS in syslog_removal.php diff --git a/functions.php b/functions.php index 3ed13fa..b5d909c 100644 --- a/functions.php +++ b/functions.php @@ -582,6 +582,7 @@ function sql_hosts_where($tab) { $hostfilter = ''; $hostfilter_log = ''; + $hosts_array = array(); if (!isempty_request_var('host') && get_nfilter_request_var('host') != 'null') { $hostarray = explode(',', trim(get_nfilter_request_var('host'))); @@ -596,11 +597,15 @@ function sql_hosts_where($tab) { array($host_id)); if (!empty($log_host)) { - $hostfilter_log .= ($hostfilter_log != '' ? ' AND ':'') . 'host = ' . db_qstr($log_host); + $hosts_array[] = db_qstr($log_host); } } } + if (sizeof($hosts_array)) { + $hostfilter_log = ' host IN(' . implode(',', $hosts_array) . ')'; + } + $hostfilter .= (strlen($hostfilter) ? ' AND ':'') . ' host_id IN(' . implode(',', $hostarray) . ')'; } } diff --git a/setup.php b/setup.php index 78ed9e1..081c621 100644 --- a/setup.php +++ b/setup.php @@ -1176,13 +1176,34 @@ function syslog_utilities_action($action) { if ($action == 'purge_syslog_hosts') { $records = 0; - syslog_db_execute('DELETE FROM syslog_hosts WHERE host_id NOT IN (SELECT DISTINCT host_id FROM syslog UNION SELECT DISTINCT host_id FROM syslog_removed)'); + syslog_db_execute('DELETE FROM syslog_hosts + WHERE host_id NOT IN ( + SELECT DISTINCT host_id + FROM syslog + UNION + SELECT DISTINCT host_id + FROM syslog_removed + )'); $records += db_affected_rows($syslog_cnn); - syslog_db_execute('DELETE FROM syslog_host_facilities WHERE host_id NOT IN (SELECT DISTINCT host_id FROM syslog UNION SELECT DISTINCT host_id FROM syslog_removed)'); + syslog_db_execute('DELETE FROM syslog_host_facilities + WHERE host_id NOT IN ( + SELECT DISTINCT host_id + FROM syslog + UNION + SELECT DISTINCT host_id + FROM syslog_removed + )'); $records += db_affected_rows($syslog_cnn); - syslog_db_execute('DELETE FROM syslog_statistics WHERE host_id NOT IN (SELECT DISTINCT host_id FROM syslog UNION SELECT DISTINCT host_id FROM syslog_removed)'); + syslog_db_execute('DELETE FROM syslog_statistics + WHERE host_id NOT IN ( + SELECT DISTINCT host_id + FROM syslog + UNION + SELECT DISTINCT host_id + FROM syslog_removed + )'); $records += db_affected_rows($syslog_cnn); raise_message('syslog_info', __('There were %s Device records removed from the Syslog database', $records, 'syslog'), MESSAGE_LEVEL_INFO); diff --git a/syslog.php b/syslog.php index b79727a..d8b9802 100644 --- a/syslog.php +++ b/syslog.php @@ -774,31 +774,40 @@ function get_syslog_messages(&$sql_where, $rows, $tab) { $sql_where = ''; - if (get_request_var('host') == 0 && $tab != 'syslog') { - // Show all hosts - } elseif (strpos(get_request_var('host'), '-1') !== false && $tab != 'syslog') { - // Show threshold type only plus matching hosts if any - $hosts = explode(',', get_request_var('host')); + if ($tab == 'alerts') { + if (get_request_var('host') == 0) { + // Show all hosts + } else { + $hosts = explode(',', get_request_var('host')); - if (cacti_sizeof($hosts) > 1) { - sql_hosts_where($tab); + $thold_pos = array_search('-1', $hosts, true); - if (strlen($hostfilter_log)) { - $sql_where .= 'WHERE ' . $hostfilter_log; + if ($thold_pos !== false) { + unset($hosts[$thold_pos]); } - } - $ids = array_rekey( - syslog_db_fetch_assoc('SELECT id - FROM syslog_alert - WHERE method = 1'), - 'id', 'id' - ); + if (sizeof($hosts)) { + sql_hosts_where($tab); - if (cacti_sizeof($ids)) { - $sql_where .= ($sql_where == '' ? 'WHERE ':' AND ') . 'alert_id IN (' . implode(', ', $ids) . ')'; - } else { - $sql_where .= ($sql_where == '' ? 'WHERE ':' AND ') . '0 = 1'; + if (strlen($hostfilter_log)) { + $sql_where .= 'WHERE ' . $hostfilter_log; + } + } + + if ($thold_pos !== false) { + $ids = array_rekey( + syslog_db_fetch_assoc('SELECT id + FROM syslog_alert + WHERE method = 1'), + 'id', 'id' + ); + + if (cacti_sizeof($ids)) { + $sql_where .= ($sql_where == '' ? 'WHERE ':' OR ') . 'alert_id IN (' . implode(', ', $ids) . ')'; + } elseif ($sql_where == '') { + $sql_where .= 'WHERE 0 = 1'; + } + } } } elseif ($tab == 'syslog') { if (!isempty_request_var('host')) { @@ -808,8 +817,6 @@ function get_syslog_messages(&$sql_where, $rows, $tab) { $sql_where .= 'WHERE ' . $hostfilter; } } - } elseif (!isempty_request_var('host') && $hostfilter_log != '') { - $sql_where .= 'WHERE ' . $hostfilter_log; } $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . @@ -823,18 +830,18 @@ function get_syslog_messages(&$sql_where, $rows, $tab) { if (!isempty_request_var('filter')) { if ($tab == 'syslog') { - $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . "message LIKE " . db_qstr('%' . get_request_var('filter') . '%'); + $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'message LIKE ' . db_qstr('%' . get_request_var('filter') . '%'); } else { - $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . "logmsg LIKE " . db_qstr('%' . get_request_var('filter') . '%'); + $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'logmsg LIKE ' . db_qstr('%' . get_request_var('filter') . '%'); } } if (get_request_var('eprogram') != '-1') { - $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . "syslog.program_id = " . db_qstr(get_request_var('eprogram')); + $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'syslog.program_id = ' . db_qstr(get_request_var('eprogram')); } if (get_request_var('efacility') != '-1') { - $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . "syslog.facility_id = " . db_qstr(get_request_var('efacility')); + $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'syslog.facility_id = ' . db_qstr(get_request_var('efacility')); } if (isset_request_var('epriority') && get_request_var('epriority') != '-1') { diff --git a/syslog_process.php b/syslog_process.php index 1c8209a..2e2c46f 100644 --- a/syslog_process.php +++ b/syslog_process.php @@ -164,7 +164,10 @@ /* get a uniqueID to allow moving of records to done table */ while (1) { $uniqueID = rand(1, 127); - $count = syslog_db_fetch_cell('SELECT count(*) FROM `' . $syslogdb_default . '`.`syslog_incoming` WHERE status=' . $uniqueID); + + $count = syslog_db_fetch_cell('SELECT count(*) + FROM `' . $syslogdb_default . '`.`syslog_incoming` + WHERE status=' . $uniqueID); if ($count == 0) { break;