"; print_r($g);print ""; if (isset($_SESSION['sess_config_array']['thold_draw_vrules']) && $_SESSION['sess_config_array']['thold_draw_vrules'] == 'on') { $end = $g['end']; if ($end < 0) { $end = time() + $end; } $end++; $start = $g['start']; if ($start < 0) { $start = $end + $start; } $start--; if ($id) { $rows = db_fetch_assoc_prepared('SELECT time, status FROM plugin_thold_log WHERE local_graph_id = ? AND type = 0 AND time > ? AND time < ?', [$id, $start, $end]); if (cacti_sizeof($rows)) { $color = ''; foreach ($rows as $row) { switch($row['status']) { case '3': $color = '#CC6600'; break; case '4': $color = '#FF0000'; break; case '5': $color = '#00FF00'; break; default: $color = ''; break; } if ($color != '') { $g['graph_defs'] .= 'VRULE:' . $row['time'] . $color . ' \\' . "\n"; } } } } } $tholds_w_hrule = db_fetch_assoc_prepared('SELECT * FROM thold_data WHERE thold_enabled = 1 AND data_type IN (0, 1, 2) AND (thold_hrule_alert > 0 || thold_hrule_warning > 0) && local_graph_id = ?', [$id]); $thold_id = 0; $txt_graph_items = ''; if (cacti_sizeof($tholds_w_hrule)) { foreach ($tholds_w_hrule as $t) { // Adjust number for graph thold_modify_values_by_cdef($t); $baseu = db_fetch_cell_prepared('SELECT base_value FROM graph_templates_graph WHERE local_graph_id = ?', [$t['local_graph_id']]); if ($t['data_type'] == 2) { $suffix = false; } else { $suffix = true; } $show_units = ($t['show_units'] ? true : false); $skipscale = ($t['skipscale'] == 'on' ? ':skipscale' : ''); switch($t['data_type']) { case '0': // Exact value case '1': // CDEF case '3': // Upper+Lower if ($t['thold_hrule_alert'] > 0) { $color = db_fetch_cell_prepared('SELECT hex FROM colors WHERE id = ?', [$t['thold_hrule_alert']]); switch($t['thold_type']) { case '0': // Hi / Low if ($t['thold_hi'] != '') { $txt_graph_items .= 'LINE1:' . $t['thold_hi'] . '#' . $color . ':' . thold_prep_rrd_string(__esc('Alert Hi for %s (%s)', $t['name_cache'], thold_format_number($t['thold_hi'], 2, $baseu, $suffix, $show_units), 'thold')) . $skipscale . ' \\' . "\n"; } if ($t['thold_low'] != '') { $txt_graph_items .= 'LINE1:' . $t['thold_low'] . '#' . $color . ':' . thold_prep_rrd_string(__esc('Alert Low for %s (%s)', $t['name_cache'], thold_format_number($t['thold_low'], 2, $baseu, $suffix, $show_units), 'thold')) . $skipscale . ' \\' . "\n"; } break; case '2': // Time Based if ($t['time_hi'] != '') { $txt_graph_items .= 'LINE1:' . $t['time_hi'] . '#' . $color . ':' . thold_prep_rrd_string(__esc('Alert Hi for %s (%s)', $t['name_cache'], thold_format_number($t['time_hi'], 2, $baseu, $suffix, $show_units), 'thold')) . $skipscale . ' \\' . "\n"; } if ($t['time_low'] != '') { $txt_graph_items .= 'LINE1:' . $t['time_low'] . '#' . $color . ':' . thold_prep_rrd_string(__esc('Alert Low for %s (%s)', $t['name_cache'], thold_format_number($t['time_low'], 2, $baseu, $suffix, $show_units), 'thold')) . $skipscale . ' \\' . "\n"; } break; } } if ($t['thold_hrule_warning'] > 0) { $color = db_fetch_cell_prepared('SELECT hex FROM colors WHERE id = ?', [$t['thold_hrule_warning']]); switch($t['thold_type']) { case '0': // Hi / Low if ($t['thold_warning_hi'] != '') { $txt_graph_items .= 'LINE1:' . $t['thold_warning_hi'] . '#' . $color . ':' . thold_prep_rrd_string(__esc('Warning Hi for %s (%s)', $t['name_cache'], thold_format_number($t['thold_warning_hi'], 2, $baseu, $suffix, $show_units), 'thold')) . $skipscale . ' \\' . "\n"; } if ($t['thold_warning_low'] != '') { $txt_graph_items .= 'LINE1:' . $t['thold_warning_low'] . '#' . $color . ':' . thold_prep_rrd_string(__esc('Warning Low for %s (%s)', $t['name_cache'], thold_format_number($t['thold_warning_low'], 2, $baseu, $suffix, $show_units), 'thold')) . $skipscale . ' \\' . "\n"; } break; case '2': // Time Based if ($t['time_warning_hi'] != '') { $txt_graph_items .= 'LINE1:' . $t['time_warning_hi'] . '#' . $color . ':' . thold_prep_rrd_string(__esc('Warning Hi for %s (%s)', $t['name_cache'], thold_format_number($t['time_warning_hi'], 2, $baseu, $suffix, $show_units), 'thold')) . $skipscale . ' \\' . "\n"; } if ($t['time_warning_low'] != '') { $txt_graph_items .= 'LINE1:' . $t['time_warning_low'] . '#' . $color . ':' . thold_prep_rrd_string(__esc('Warning Low for %s (%s)', $t['name_cache'], thold_format_number($t['time_warning_low'], 2, $baseu, $suffix, $show_units), 'thold')) . $skipscale . ' \\' . "\n"; } break; } } break; case '2': // Percentage if (isset($data_defs[$t['percent_ds']])) { if ($t['thold_hrule_alert'] > 0) { $color = db_fetch_cell_prepared('SELECT hex FROM colors WHERE id = ?', [$t['thold_hrule_alert']]); switch($t['thold_type']) { case '0': // Hi / Low if ($t['thold_hi'] != '') { $g['graph_defs'] .= 'CDEF:th' . $thold_id . 'ahi=' . $data_defs[$t['percent_ds']] . ',' . $t['thold_hi'] . ',100,/,* \\' . "\n"; $txt_graph_items .= 'LINE1:th' . $thold_id . 'ahi#' . $color . ':' . thold_prep_rrd_string(__esc('Alert Hi for %s (%s %%%)', $t['name_cache'], number_format_i18n($t['thold_hi']), 'thold')) . $skipscale . ' \\' . "\n"; $thold_id++; } if ($t['thold_low'] != '') { $g['graph_defs'] .= 'CDEF:th' . $thold_id . 'alow=' . $data_defs[$t['percent_ds']] . ',' . $t['thold_low'] . ',100,/,* \\' . "\n"; $txt_graph_items .= 'LINE1:th' . $thold_id . 'alow#' . $color . ':' . thold_prep_rrd_string(__esc('Alert Low for %s (%s %%%)', $t['name_cache'], number_format_i18n($t['thold_low']), 'thold')) . $skipscale . ' \\' . "\n"; $thold_id++; } break; case '2': // Time Based if ($t['time_hi'] != '') { $g['graph_defs'] .= 'CDEF:th' . $thold_id . 'ahi=' . $data_defs[$t['percent_ds']] . ',' . $t['time_hi'] . ',100,/,* \\' . "\n"; $txt_graph_items .= 'LINE1:th' . $thold_id . 'ahi#' . $color . ':' . thold_prep_rrd_string(__esc('Alert Hi for %s (%s %%%)', $t['name_cache'], number_format_i18n($t['time_hi']), 'thold')) . $skipscale . ' \\' . "\n"; $thold_id++; } if ($t['time_low'] != '') { $g['graph_defs'] .= 'CDEF:th' . $thold_id . 'alow=' . $data_defs[$t['percent_ds']] . ',' . $t['time_low'] . ',100,/,* \\' . "\n"; $txt_graph_items .= 'LINE1:th' . $thold_id . 'alow#' . $color . ':' . thold_prep_rrd_string(__esc('Alert Low for %s (%s %%%)', $t['name_cache'], number_format_i18n($t['time_low']), 'thold')) . $skipscale . ' \\' . "\n"; $thold_id++; } break; } } if ($t['thold_hrule_warning'] > 0) { $color = db_fetch_cell_prepared('SELECT hex FROM colors WHERE id = ?', [$t['thold_hrule_warning']]); switch($t['thold_type']) { case '0': // Hi / Low if ($t['thold_warning_hi'] != '') { $g['graph_defs'] .= 'CDEF:th' . $thold_id . 'whi=' . $data_defs[$t['percent_ds']] . ',' . $t['thold_warning_hi'] . ',100,/,* \\' . "\n"; $txt_graph_items .= 'LINE1:th' . $thold_id . 'whi#' . $color . ':' . thold_prep_rrd_string(__esc('Warning Hi for %s (%s %%%)', $t['name_cache'], number_format_i18n($t['thold_warning_hi']), 'thold')) . $skipscale . ' \\' . "\n"; $thold_id++; } if ($t['thold_warning_low'] != '') { $g['graph_defs'] .= 'CDEF:th' . $thold_id . 'wlow=' . $data_defs[$t['percent_ds']] . ',' . $t['thold_warning_low'] . ',100,/,* \\' . "\n"; $txt_graph_items .= 'LINE1:th' . $thold_id . 'wlow#' . $color . ':' . thold_prep_rrd_string(__esc('Warning Low for %s (%s %%%)', $t['name_cache'], number_format_i18n($t['thold_warning_low']), 'thold')) . $skipscale . ' \\' . "\n"; $thold_id++; } break; case '2': // Time Based if ($t['time_warning_hi'] != '') { $g['graph_defs'] .= 'CDEF:th' . $thold_id . 'whi=' . $data_defs[$t['percent_ds']] . ',' . $t['time_warning_hi'] . ',100,/,* \\' . "\n"; $txt_graph_items .= 'LINE1:th' . $thold_id . 'whi#' . $color . ':' . thold_prep_rrd_string(__esc('Warning Hi for %s (%s %%%)', $t['name_cache'], number_format_i18n($t['time_warning_hi']), 'thold')) . $skipscale . ' \\' . "\n"; $thold_id++; } if ($t['time_warning_low'] != '') { $g['graph_defs'] .= 'CDEF:th' . $thold_id . 'wlow=' . $data_defs[$t['percent_ds']] . ',' . $t['time_warning_low'] . ',100,/,* \\' . "\n"; $txt_graph_items .= 'LINE1:th' . $thold_id . 'wlow#' . $color . ':' . thold_prep_rrd_string(__esc('Warning Low for %s (%s %%%)', $t['name_cache'], number_format_i18n($t['time_warning_low']), 'thold')) . $skipscale . ' \\' . "\n"; $thold_id++; } break; } } } } } } if ($txt_graph_items) { $g['txt_graph_items'] .= ' \\' . "\n" . 'COMMENT:\' ' . '\\n' . '\' \\' . "\n" . 'COMMENT:\'' . __('Threshold Alert/Warning Values', 'thold') . '' . '\\n' . '\' \\' . "\n" . $txt_graph_items; } return $g; } /** * Prepares a string value for safe embedding in an RRDtool command * line argument by shell-escaping it, then RRDtool-escaping it, then * wrapping it in single quotes. * * @param string $string The raw string value to prepare. * * @return string The quoted, escaped string ready for RRDtool command * construction. */ function thold_prep_rrd_string($string) { return '\'' . trim(cacti_escapeshellarg(rrdtool_escape_string($string)), "'") . '\''; } /** * Executes the "Apply Thresholds" bulk device action: auto-creates * thresholds for each selected device using its device template's * associated threshold templates. Registered as the * 'device_action_execute' Cacti hook; a no-op passthrough for any * other action. * * @param string $action The bulk action identifier being executed. * * @return string The unmodified $action, for hook chaining. * * @global array $config Cacti global configuration array; used to * locate the library file to include. */ function thold_device_action_execute($action) { global $config; if ($action != 'thold') { return $action; } include_once($config['base_path'] . '/plugins/thold/includes/functions.php'); $selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items')); if ($selected_items != false) { for ($i = 0; ($i < cacti_sizeof($selected_items)); $i++) { autocreate($selected_items[$i]); } } return $action; } /** * Auto-creates thresholds for a newly created device when the global * thold_autocreate setting is enabled. Registered as the * 'api_device_new' Cacti hook. * * @param array $save The newly created device's saved field data, * including its 'id'. * * @return array The unmodified $save array, for hook chaining. * * @global array $config Cacti global configuration array; used to * locate the library file to include. */ function thold_api_device_new($save) { global $config; include_once($config['base_path'] . '/plugins/thold/includes/functions.php'); if (read_config_option('thold_autocreate') == 'on') { if (!empty($save['id'])) { autocreate($save['id']); } } return $save; } /** * Renders the confirmation box listing the devices selected for the * "Apply Thresholds" bulk device action. Registered as the * 'device_action_prepare' Cacti hook; a no-op passthrough for any * other action. * * @param array $save The bulk-action form submission data, including * 'drp_action' and a pre-built 'host_list' HTML * fragment. * * @return array The unmodified $save array, for hook chaining. * * @global array $host_list Reserved/declared for parity with other * functions in this file; not used directly * here. */ function thold_device_action_prepare($save) { global $host_list; if ($save['drp_action'] != 'thold') { return $save; } print "
" . __('Click \'Continue\' to apply all appropriate Thresholds to these Device(s).', 'thold') . "
' . __('The following Data Sources have no Threshold Templates associated with them', 'thold') . '
'; print '' . __('Click \'Continue\' to create Thresholds for these Data Sources?', 'thold') . '
| ";
$form_array = [
'general_header' => [
'friendly_name' => __('Available Threshold Templates', 'thold'),
'method' => 'spacer',
],
'thold_template_id' => [
'method' => 'drop_sql',
'friendly_name' => __('Select a Threshold Template', 'thold'),
'description' => '',
'none_value' => __('None', 'thold'),
'value' => __('None', 'thold'),
'sql' => $sql
]
];
draw_edit_form(
[
'config' => ['no_form_tag' => true],
'fields' => $form_array
]
);
} else {
if (strlen($not_found)) {
print ' ' . __('There are no Threshold Templates associated with the following Data Sources', 'thold') . ' '; print '
|
| ';
} else {
return $save;
}
}
/**
* Adds the "Create Threshold from Template" entry to the data source
* management bulk-actions dropdown. Registered as the
* 'data_source_action_array' Cacti hook.
*
* @param array $action The existing bulk-action label map, keyed by
* action id.
*
* @return array The action map with this plugin's entry added.
*/
function thold_data_source_action_array($action) {
$action['plugin_thold_create'] = __('Create Threshold from Template', 'thold');
return $action;
}
/**
* Executes the "Create Threshold from Template" bulk graph action:
* applies the selected threshold template to each selected graph's
* matching data source(s), creating a new threshold for each one that
* doesn't already have one from that template. Registered as the
* 'graphs_action_execute' Cacti hook; a no-op passthrough for any
* other action.
*
* @param string $action The bulk action identifier being executed.
*
* @return string The unmodified $action, for hook chaining.
*
* @global array $config Cacti global configuration array; used to
* locate the library file to include.
* @global array $form_array Reserved/declared for parity with other
* functions in this file; not used directly
* here.
*/
function thold_graphs_action_execute($action) {
global $config, $form_array;
include_once($config['base_path'] . '/plugins/thold/includes/functions.php');
if ($action == 'plugin_thold_create') {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
$message = '';
$created = 0;
get_filter_request_var('thold_template_id');
$template = db_fetch_row_prepared('SELECT *
FROM thold_template
WHERE id = ?',
[get_request_var('thold_template_id')]);
if (cacti_sizeof($template)) {
foreach ($selected_items as $local_graph_id) {
$data_sources = db_fetch_assoc_prepared('SELECT DISTINCT
dtr.id, gl.id AS local_graph_id, dtr.local_data_id
FROM data_template_rrd AS dtr
INNER JOIN graph_templates_item AS gti
ON gti.task_item_id=dtr.id
INNER JOIN graph_local AS gl
ON gl.id=gti.local_graph_id
WHERE gl.id = ?
AND dtr.data_source_name = ?',
[$local_graph_id, $template['data_source_name']]);
if (cacti_sizeof($data_sources)) {
foreach ($data_sources as $data_source) {
$local_data_id = $data_source['local_data_id'];
$local_graph_id = $data_source['local_graph_id'];
$data_template_rrd_id = $data_source['id'];
if (thold_create_from_template($local_data_id, $local_graph_id, $data_template_rrd_id, $template, $message)) {
$created++;
}
}
}
}
if (strlen($message)) {
thold_raise_message(__('Created %s thresholds', $created) . ' ' . $message, MESSAGE_LEVEL_INFO); } else { thold_raise_message(__('No Threshold(s) Created. Either they already exist, or no suitable matches found.', 'thold'), MESSAGE_LEVEL_INFO); } } else { thold_raise_message(__('No Threshold(s) Created. Threshold(s) Template not found.', 'thold'), MESSAGE_LEVEL_ERROR); } } } return $action; } /** * Renders the confirmation page for the "Create Threshold from * Template" bulk graph action, listing which selected graphs have at * least one applicable threshold template (and offering a template * selection), while filtering out any that don't. Registered as the * 'graphs_action_prepare' Cacti hook; a no-op passthrough for any * other action. * * @param array $save The bulk-action form submission data, including * 'drp_action'. * * @return array|void The unmodified $save array (for hook chaining) * when the action isn't 'plugin_thold_create'; * otherwise this renders the confirmation UI * directly and returns nothing. * * @global array $config Cacti global configuration array (declared but * not used directly here). */ function thold_graphs_action_prepare($save) { global $config; if ($save['drp_action'] == 'plugin_thold_create') { /* get the valid thold templates * remove those hosts that do not have any valid templates */ $found_list = ''; $not_found = ''; $template_ids = []; if (cacti_sizeof($save['graph_array'])) { foreach ($save['graph_array'] as $item) { $item_found = false; $data_template_ids = db_fetch_assoc_prepared('SELECT DISTINCT dtr.data_template_id FROM data_template_rrd AS dtr LEFT JOIN graph_templates_item AS gti ON gti.task_item_id=dtr.id LEFT JOIN graph_local AS gl ON gl.id=gti.local_graph_id WHERE gl.id = ?', [$item]); if (cacti_sizeof($data_template_ids)) { foreach ($data_template_ids as $i => $data_template_rec) { $data_template_id = $data_template_rec['data_template_id']; $templates = db_fetch_assoc_prepared('SELECT id FROM thold_template WHERE data_template_id = ?', [$data_template_id]); if (cacti_sizeof($templates)) { $item_found = true; $template_ids[] = $data_template_id; } } } if (!$item_found) { $not_found .= ' | |
| ";
if (strlen($found_list)) {
if (strlen($not_found)) {
print ' ' . __('The following Graphs have no Threshold Templates associated with them', 'thold') . ' '; print '
' . __('Press \'Continue\' if you wish to create Threshold(s) for these Graph(s)', 'thold') . '
|
|
| ";
$form_array = [
'general_header' => [
'friendly_name' => __('Available Threshold Templates', 'thold'),
'method' => 'spacer',
],
'thold_template_id' => [
'method' => 'drop_sql',
'friendly_name' => __('Select a Threshold Template', 'thold'),
'description' => '',
'value' => __('None', 'thold'),
'sql' => $sql
]
];
draw_edit_form(
[
'config' => ['no_form_tag' => true],
'fields' => $form_array
]
);
} else {
if (strlen($not_found)) {
print ' ' . __('There are no Threshold Templates associated with the following Graphs', 'thold') . ' '; print '
|
| '; } else { return $save; } } /** * Adds the "Create Threshold from Template" entry to the graph * management bulk-actions dropdown. Registered as the * 'graphs_action_array' Cacti hook. * * @param array $action The existing bulk-action label map, keyed by * action id. * * @return array The action map with this plugin's entry added. */ function thold_graphs_action_array($action) { $action['plugin_thold_create'] = __('Create Threshold from Template', 'thold'); return $action; } /** * Emits the JavaScript that shows/hides the notification-list dropdown * on the device edit form based on the selected "Threshold Up/Down * Email Notification" mode. Registered as the 'host_edit_bottom' and * 'device_change_javascript' Cacti hooks. * * @return void */ function thold_host_edit_bottom() { // This hook can fire on core pages that have not loaded includes/functions.php, where plugin_thold_csp_nonce() lives. require_once(__DIR__ . '/includes/functions.php'); ?> install($config['base_path'] . '/plugins/thold/CACTI-THOLD-MIB', true); } } /** * Removes this plugin's CACTI-THOLD-MIB definitions from Cacti's SNMP * agent cache, when the SNMP agent's MibCache class is available. * Invoked during plugin uninstallation. * * @return void * * @global array $config Cacti global configuration array; used to * locate the MIB definition file. */ function thold_snmpagent_cache_uninstall() { global $config; if (class_exists('MibCache')) { $mc = new MibCache('CACTI-THOLD-MIB'); $mc->uninstall(); } } /** * Emits this plugin's theme-specific stylesheet link (when one exists) * and a JavaScript handler that rebinds the threshold-VRULE toggle * link's click behavior to an AJAX partial-page refresh after each AJAX * completion. Registered as the 'page_head' Cacti hook. * * @return void * * @global array $config Cacti global configuration array; used to * check for a theme-specific stylesheet and build * asset URLs. */ function thold_page_head() { global $config; // This hook can fire on core pages that have not loaded includes/functions.php, where plugin_thold_csp_nonce() lives. require_once(__DIR__ . '/includes/functions.php'); if (file_exists($config['base_path'] . '/plugins/thold/css/' . get_selected_theme() . '.css')) { print get_md5_include_css('plugins/thold/css/' . get_selected_theme() . '.css'); } print get_md5_include_css('plugins/thold/css/pills.css'); ?> | ) | ' href=''> | ' . __('No Associated Threshold Templates.', 'thold') . ' | ||
|
|||||
|
' |
|||||
| ' onClick='$("#cdialog").dialog("close")' name='cancel'> ' name='continue' title=''> | ) | ' href=''> | ' . __('No Associated Threshold Templates.', 'thold') . ' | '; } $unmapped = db_fetch_assoc_prepared('SELECT DISTINCT tt.id, tt.name FROM thold_template AS tt LEFT JOIN plugin_thold_host_template AS ptdt ON tt.id = ptdt.thold_template_id AND ptdt.host_template_id = ? WHERE ptdt.host_template_id IS NULL OR ptdt.host_template_id != ? ORDER BY tt.name', [get_request_var('id'), get_request_var('id')]); if (cacti_sizeof($unmapped)) { ?>||
|
|||||
|
' |
|||||
| ' onClick='$("#cdialog").dialog("close")' name='cancel'> ' name='continue' title=''> | |||||