__('Delete'), 2 => __('Disable'), 3 => __('Enable'), ]; $mactions = [ 1 => __('Disable'), 2 => __('Enable') ]; $tabs_manager_edit = [ 'general' => __('General'), 'notifications' => __('Notifications'), 'logs' => __('Logs'), ]; // set default action set_default_action(); gfrv('tab', FILTER_CALLBACK, ['options' => 'sanitize_search_string']); switch (grv('action')) { case 'save': form_save(); break; case 'actions': form_actions(); break; case 'edit': top_header(); manager_edit(); bottom_footer(); break; default: top_header(); manager(); bottom_footer(); break; } function manager() : void { global $actions, $item_rows; // create the page filter $pageFilter = new CactiTableFilter(__('SNMP Notification Receivers'), 'managers.php', 'form_snmpagent_managers', 'sess_snmp_mgr', 'managers.php?action=edit'); $pageFilter->rows_label = __('Receivers'); $pageFilter->set_sort_array('hostname', 'ASC'); $pageFilter->render(); if (grv('rows') == '-1') { $rows = read_config_option('num_rows_table'); } else { $rows = grv('rows'); } // form the 'where' clause for our main sql query $sql_where = 'WHERE ( sm.hostname LIKE ' . db_qstr('%' . grv('filter') . '%') . ' OR sm.description LIKE ' . db_qstr('%' . grv('filter') . '%') . ')'; $total_rows = db_fetch_cell("SELECT COUNT(sm.id) FROM snmpagent_managers AS sm $sql_where"); $sql_order = get_order_string(); $sql_limit = ' LIMIT ' . ($rows * (grv('page') - 1)) . ',' . $rows; $managers = db_fetch_assoc("SELECT sm.id, sm.description, sm.hostname, sm.disabled, smn.count_notify, snl.count_log FROM snmpagent_managers AS sm LEFT JOIN ( SELECT COUNT(*) as count_notify, manager_id FROM snmpagent_managers_notifications GROUP BY manager_id ) AS smn ON smn.manager_id = sm.id LEFT JOIN ( SELECT COUNT(*) as count_log, manager_id FROM snmpagent_notifications_log GROUP BY manager_id ) AS snl ON snl.manager_id = sm.id $sql_where $sql_order $sql_limit"); $display_text = [ 'description' => [ __('Description'), 'ASC'], 'id' => [ __('Id'), 'ASC'], 'disabled' => [ __('Status'), 'ASC'], 'hostname' => [ __('Hostname'), 'ASC'], 'count_notify' => [ __('Notifications'), 'ASC'], 'count_log' => [ __('Logs'), 'ASC'] ]; // generate page list $nav = html_nav_bar('managers.php?filter=' . grv('filter'), MAX_DISPLAY_PAGES, grv('page'), $rows, $total_rows, 11, __('Receivers'), 'page', 'main'); form_start('managers.php', 'chk'); print $nav; html_start_box('', '100%', false, 3, 'center', ''); html_header_sort_checkbox($display_text, grv('sort_column'), grv('sort_direction'), false); if (cacti_sizeof($managers)) { foreach ($managers as $item) { $description = filter_value($item['description'], grv('filter')); $hostname = filter_value($item['hostname'], grv('filter')); $url = 'managers.php?action=edit&id=' . $item['id']; $url1 = 'managers.php?action=edit&tab=notifications&id=' . $item['id']; $url2 = 'managers.php?action=edit&tab=logs&id=' . $item['id']; form_alternate_row('line' . $item['id'], false); form_selectable_cell(filter_value($description, '', $url), $item['id']); form_selectable_cell($item['id'], $item['id']); form_selectable_cell($item['disabled'] ? '' . __('Disabled') . '' : '' . __('Enabled') . '', $item['id']); form_selectable_ecell($hostname, $item['id']); form_selectable_cell(filter_value($item['count_notify'] ? $item['count_notify'] : 0, '', $url1), $item['id']); form_selectable_cell(filter_value($item['count_log'] ? $item['count_log'] : 0, '', $url2), $item['id']); form_checkbox_cell($item['description'], $item['id']); form_end_row(); } } else { print '' . __('No SNMP Notification Receivers') . ''; } html_end_box(false); if (cacti_sizeof($managers)) { print $nav; } form_hidden_box('action_receivers', '1', ''); draw_actions_dropdown($actions); form_end(); } function manager_edit() : void { global $snmp_auth_protocols, $snmp_priv_protocols, $snmp_versions, $tabs_manager_edit, $fields_manager_edit, $mactions; // ================= input validation ================= gfrv('id'); // ==================================================== if (!isrv('tab')) { srv('tab', 'general'); } $id = (isrv('id') ? grv('id') : '0'); if ($id) { $manager = db_fetch_row_prepared('SELECT * FROM snmpagent_managers WHERE id = ?', [grv('id')]); $header_label = __esc('SNMP Notification Receiver [edit: %s]', $manager['description']); } else { $header_label = __('SNMP Notification Receiver [new]'); } if (cacti_sizeof($tabs_manager_edit) && isrv('id')) { $i = 0; // draw the tabs print "
'; if (read_config_option('legacy_menu_nav') != 'on') { ?> ['no_form_tag' => true], 'fields' => inject_form_variables($fields_manager_edit, (isset($manager) ? $manager : [])) ] ); html_end_box(true, true); form_save_button('managers.php', 'return'); ?> [ [ 'filter' => [ 'method' => 'textbox', 'friendly_name' => __('Search'), 'filter' => FILTER_DEFAULT, 'placeholder' => __('Enter a search term'), 'size' => '30', 'default' => '', 'pageset' => true, 'max_length' => '120', 'value' => '' ], 'mib' => [ 'method' => 'drop_array', 'friendly_name' => __('MIB'), 'filter' => FILTER_CALLBACK, 'filter_options' => ['options' => 'sanitize_search_string'], 'default' => 'any', 'pageset' => true, 'array' => $mibs, 'value' => 'any' ], 'rows' => [ 'method' => 'drop_array', 'friendly_name' => __('Entries'), 'filter' => FILTER_VALIDATE_INT, 'default' => '-1', 'pageset' => true, 'array' => $item_rows, 'value' => '-1' ] ] ], 'buttons' => [ 'go' => [ 'method' => 'submit', 'display' => __('Go'), 'title' => __('Apply Filter to Table'), ], 'clear' => [ 'method' => 'button', 'display' => __('Clear'), 'title' => __('Reset Filter to Default Values'), ] ] ]; } function draw_manager_notification_filter(bool $render = false, string $header_label = '') : void { $filters = create_manager_notification_filter(); // create the page filter $pageFilter = new CactiTableFilter($header_label, 'managers.php?action=edit&tab=notifications&id=' . gfrv('id'), 'form_snmpagent_managers', 'sess_snmp_cache'); $pageFilter->rows_label = __('OIDs'); $pageFilter->set_filter_array($filters); if ($render) { $pageFilter->render(); } else { $pageFilter->sanitize(); } } function manager_notifications(int $id, string $header_label) : void { global $item_rows, $mactions; draw_manager_notification_filter(true, $header_label); if (grv('rows') == '-1') { $rows = read_config_option('num_rows_table'); } else { $rows = grv('rows'); } html_start_box($header_label, '100%', false, 3, 'center', ''); $sql_where = "WHERE `kind`='Notification'"; $sql_params = []; // filter by host if (grv('mib') != 'any' && grv('mib') != '-1') { $sql_where .= ($sql_where != '' ? ' AND ' : 'WHERE ') . ' snmpagent_cache.mib = ?'; $sql_params[] = grv('mib'); } // filter by search string if (grv('filter') != 'any') { $sql_where .= ($sql_where != '' ? ' AND ' : 'WHERE ') . ' (`oid` LIKE ? OR `name` LIKE ? OR `mib` LIKE ?)'; $sql_params[] = '%' . grv('filter') . '%'; $sql_params[] = '%' . grv('filter') . '%'; $sql_params[] = '%' . grv('filter') . '%'; } $sql_order = ' ORDER by `oid`'; form_start('managers.php', 'chk'); $total_rows = db_fetch_cell_prepared("SELECT COUNT(*) FROM snmpagent_cache $sql_where", $sql_params); $snmp_cache_sql = "SELECT * FROM snmpagent_cache $sql_where $sql_order LIMIT " . ($rows * (grv('page') - 1)) . ',' . $rows; $snmp_cache = db_fetch_assoc_prepared($snmp_cache_sql, $sql_params); $registered_notifications = db_fetch_assoc_prepared('SELECT notification, mib FROM snmpagent_managers_notifications WHERE manager_id = ?', [$id]); $notifications = []; if ($registered_notifications && cacti_sizeof($registered_notifications) > 0) { foreach ($registered_notifications as $registered_notification) { $notifications[$registered_notification['mib']][$registered_notification['notification']] = 1; } } $display_text = [ __('Name'), __('OID'), __('MIB'), __('Kind'), __('Max-Access'), __('Monitored') ]; // generate page list $nav = html_nav_bar('managers.php?action=edit&id=' . $id . '&tab=notifications&mib=' . grv('mib') . '&filter=' . grv('filter'), MAX_DISPLAY_PAGES, grv('page'), $rows, $total_rows, cacti_sizeof($display_text) + 1, __('Notifications'), 'page', 'main'); print $nav; html_start_box('', '100%', false, 3, 'center', ''); html_header_checkbox($display_text, true, 'managers.php?action=edit&tab=notifications&id=' . $id); if (cacti_sizeof($snmp_cache)) { foreach ($snmp_cache as $item) { $row_id = $item['mib'] . '__' . $item['name']; $oid = filter_value($item['oid'], grv('filter')); $name = filter_value($item['name'], grv('filter')); $mib = filter_value($item['mib'], grv('filter')); form_alternate_row('line' . $row_id, false); if ($item['description']) { form_selectable_cell(filter_value($name, '', '#', $item['description']), $row_id); } else { form_selectable_cell($name, $row_id); } form_selectable_cell($oid, $row_id); form_selectable_cell($mib, $row_id); form_selectable_ecell($item['kind'], $row_id); form_selectable_cell($item['max-access'],$row_id); form_selectable_cell(((isset($notifications[$item['mib']]) && isset($notifications[$item['mib']][$item['name']])) ? '' . __('Enabled') : '' . __('Disabled')) . '', $row_id); form_checkbox_cell($item['oid'], $row_id); form_end_row(); } } else { print '' . __('No SNMP Notifications') . ''; } form_hidden_box('id', grv('id'), ''); html_end_box(false); if (cacti_sizeof($snmp_cache)) { print $nav; } draw_actions_dropdown($mactions); form_end(); } function create_manager_log_filter(array $severity_levels) : array { global $item_rows; $all = ['-1' => __('All')]; $severity_levels = $all + $severity_levels; return [ 'rows' => [ [ 'filter' => [ 'method' => 'textbox', 'friendly_name' => __('Search'), 'filter' => FILTER_DEFAULT, 'placeholder' => __('Enter a search term'), 'size' => '30', 'default' => '', 'pageset' => true, 'max_length' => '120', 'value' => '' ], 'severity' => [ 'method' => 'drop_array', 'friendly_name' => __('Severity'), 'filter' => FILTER_VALIDATE_INT, 'default' => '-1', 'pageset' => true, 'array' => $severity_levels, 'value' => '-1' ], 'rows' => [ 'method' => 'drop_array', 'friendly_name' => __('Entries'), 'filter' => FILTER_VALIDATE_INT, 'default' => '-1', 'pageset' => true, 'array' => $item_rows, 'value' => '-1' ] ] ], 'buttons' => [ 'go' => [ 'method' => 'submit', 'display' => __('Go'), 'title' => __('Apply Filter to Table'), ], 'clear' => [ 'method' => 'button', 'display' => __('Clear'), 'title' => __('Reset Filter to Default Values'), ], 'purge' => [ 'method' => 'button', 'display' => __('Purge'), 'title' => __('Purge the Notification Receiver Log'), ] ] ]; } function draw_manager_log_filter(bool $render = false, array $severity_levels = [], string $header_label = '') : void { $filters = create_manager_log_filter($severity_levels); // create the page filter $pageFilter = new CactiTableFilter($header_label, 'managers.php?action=edit&tab=logs&id=' . gfrv('id'), 'form_log', 'sess_snmp_log'); $pageFilter->rows_label = __('Entries'); $pageFilter->set_filter_array($filters); if ($render) { $pageFilter->render(); } else { $pageFilter->sanitize(); } } function manager_logs(int $id, string $header_label) : void { $severity_levels = [ SNMPAGENT_EVENT_SEVERITY_LOW => 'LOW', SNMPAGENT_EVENT_SEVERITY_MEDIUM => 'MEDIUM', SNMPAGENT_EVENT_SEVERITY_HIGH => 'HIGH', SNMPAGENT_EVENT_SEVERITY_CRITICAL => 'CRITICAL' ]; $severity_colors = [ SNMPAGENT_EVENT_SEVERITY_LOW => '#00FF00', SNMPAGENT_EVENT_SEVERITY_MEDIUM => '#FFFF00', SNMPAGENT_EVENT_SEVERITY_HIGH => '#FF0000', SNMPAGENT_EVENT_SEVERITY_CRITICAL => '#FF00FF' ]; if (grv('action') == 'purge') { db_execute_prepared('DELETE FROM snmpagent_notifications_log WHERE manager_id = ?', [$id]); srv('clear', true); } draw_manager_log_filter(true, $severity_levels, $header_label); // ==================================================== if (grv('rows') == '-1') { $rows = read_config_option('num_rows_table'); } else { $rows = grv('rows'); } $sql_params = []; $sql_where = 'WHERE snl.manager_id = ?'; $sql_params[] = $id; // filter by severity if (grv('severity') > 0) { $sql_where .= ' AND snl.severity = ?'; $sql_params[] = grv('severity'); } // filter by search string if (grv('filter') != '') { $sql_where .= ' AND (`varbinds` LIKE ?)'; $sql_params[] = '%' . grv('severity') . '%'; } $sql_order = ' ORDER by `id` DESC'; $sql_query = "SELECT snl.*, sc.description FROM snmpagent_notifications_log AS snl LEFT JOIN snmpagent_cache AS sc ON sc.name = snl.notification $sql_where $sql_order LIMIT " . ($rows * (grv('page') - 1)) . ',' . $rows; form_start('managers.php', 'chk'); $total_rows = db_fetch_cell_prepared("SELECT COUNT(*) FROM snmpagent_notifications_log AS snl $sql_where", $sql_params); $logs = db_fetch_assoc_prepared($sql_query, $sql_params); $display_text = [ __('Data'), __('Time'), __('Notification'), __('Varbinds') ]; $nav = html_nav_bar('managers.php?action=exit&id=' . $id . '&tab=logs&mib=' . grv('mib') . '&filter=' . grv('filter'), MAX_DISPLAY_PAGES, grv('page'), $rows, $total_rows, cacti_sizeof($display_text), __('Receivers'), 'page', 'main'); print $nav; html_start_box('', '100%', false, 3, 'center', ''); html_header($display_text); if (cacti_sizeof($logs)) { foreach ($logs as $item) { $varbinds = filter_value($item['varbinds'], grv('filter')); form_alternate_row('line' . $item['id'], true); form_selectable_cell(filter_value('', '', '#', __esc('Severity Level') . ': ' . $severity_levels[$item['severity']]), $item['id'], '', 'width:10px;background-color:' . $severity_colors[$item['severity']] . ';border-top:1px solid white;border-bottom:1px solid white;'); form_selectable_cell(date('Y/m/d H:i:s', $item['time']), $item['id']); if ($item['description']) { $description = ''; $lines = preg_split('/\r\n|\r|\n/', $item['description']); foreach (($lines ?: []) as $line) { $description .= htmle(trim($line)) . '
'; } form_selectable_cell(filter_value($item['notification'], '', '#', $item['notification'] . $description), $item['id']); } else { form_selectable_ecell($item['notification'], $item['id']); } form_selectable_cell($varbinds, $item['id']); form_end_row(); } } else { print '' . __('No SNMP Notification Log Entries') . ''; } html_end_box(); if (cacti_sizeof($logs)) { print $nav; } ?> '> $notifications) { foreach ($notifications as $notification => $state) { db_execute_prepared('DELETE FROM snmpagent_managers_notifications WHERE `manager_id` = ? AND `mib` = ? AND `notification` = ? LIMIT 1', [gnrv('id'), $mib, $notification]); } } } elseif (gnrv('drp_action') == '2') { // enable foreach ($selected_items as $mib => $notifications) { foreach ($notifications as $notification => $state) { db_execute_prepared('INSERT IGNORE INTO snmpagent_managers_notifications (`manager_id`, `notification`, `mib`) VALUES (?, ?, ?)', [gnrv('id'), $notification, $mib]); } } } } header('Location: managers.php?action=edit&id=' . gnrv('id') . '&tab=notifications'); exit; } } elseif (isrv('action_receivers')) { $ilist = ''; $iarray = []; foreach ($_POST as $key => $value) { if (strstr($key, 'chk_')) { // grep manager's id $id = substr($key, 4); // ================= input validation ================= input_validate_input_number($id, 'id'); // ==================================================== $ilist .= '
  • ' . htmle(db_fetch_cell_prepared('SELECT description FROM snmpagent_managers WHERE id = ?', [$id])) . '
  • '; $iarray[] = $id; } } $form_data = [ 'general' => [ 'page' => 'managers.php', 'actions' => $actions, 'eaction' => 'action_receivers', 'optvar' => 'drp_action', 'item_array' => $iarray, 'item_list' => $ilist ], 'options' => [ 1 => [ 'smessage' => __('Click \'Continue\' to Delete the following Notification Receiver.'), 'pmessage' => __('Click \'Continue\' to Delete following Notification Receivers.'), 'scont' => __('Delete Notification Receiver'), 'pcont' => __('Delete Notification Receivers') ], 2 => [ 'smessage' => __('Click \'Continue\' to Disable the following Notification Receiver.'), 'pmessage' => __('Click \'Continue\' to Disable following Notification Receivers.'), 'scont' => __('Disable Notification Receiver'), 'pcont' => __('Disable Notification Receivers') ], 3 => [ 'smessage' => __('Click \'Continue\' to Enable the following Notification Receiver.'), 'pmessage' => __('Click \'Continue\' to Enable following Notification Receivers.'), 'scont' => __('Enable Notification Receiver'), 'pcont' => __('Enable Notification Receivers'), ] ] ]; form_continue_confirmation($form_data); } else { $ilist = ''; $iarray = []; // ================= input validation ================= gfrv('id'); // ==================================================== foreach ($_POST as $key => $value) { if (strstr($key, 'chk_')) { // grep mib and notification name $row_id = substr($key, 4); [$mib, $name] = explode('__', $row_id); $ilist .= '
  • ' . htmle($name) . ' (' . htmle($mib) . ')
  • '; $iarray[$mib][$name] = 1; } } $form_data = [ 'general' => [ 'page' => 'managers.php?action=edit&tab=notifications&id=' . grv('id'), 'actions' => $mactions, 'eaction' => 'action_receiver_notifications', 'optvar' => 'drp_action', 'item_array' => $iarray, 'item_list' => $ilist ], 'options' => [ 1 => [ 'smessage' => __('Click \'Continue\' to Disable Forwarding the following Notification Object the following Notification Receiver.'), 'pmessage' => __('Click \'Continue\' to Disable Forwarding the following Notification Objects to the following Notification Receiver.'), 'scont' => __('Disable Forwarding Object'), 'pcont' => __('Disable Forwarding Objects') ], 2 => [ 'smessage' => __('Click \'Continue\' to Enable Forwarding the following Notification Object to this Notification Receiver.'), 'pmessage' => __('Click \'Continue\' to Enable Forwarding the following Notification Objects Notification Receivers.'), 'scont' => __('Enable Forwarding Object'), 'pcont' => __('Enable Forwarding Objects') ] ] ]; form_continue_confirmation($form_data); } }