__('Delete'),
2 => __('Disable'),
3 => __('Enable'),
4 => __('Change Device'),
5 => __('Reapply Suggested Names'),
);
$actions = api_plugin_hook_function('data_source_action_array', $actions);
/* set default action */
set_default_action();
validate_data_source_vars();
switch (get_request_var('action')) {
case 'save':
form_save();
break;
case 'actions':
form_actions();
break;
case 'rrd_add':
ds_rrd_add();
break;
case 'rrd_remove':
ds_rrd_remove();
break;
case 'data_edit':
top_header();
data_edit();
bottom_footer();
break;
case 'ds_disable':
ds_disable();
break;
case 'ds_enable':
ds_enable();
break;
case 'ds_remove':
ds_remove();
header('Location: data_sources.php');
break;
case 'ds_edit':
ds_edit();
break;
case 'ajax_hosts':
$sql_where = '';
if (get_request_var('site_id') > 0) {
$sql_where = 'site_id = ' . get_request_var('site_id');
}
get_allowed_ajax_hosts(true, 'applyFilter', $sql_where);
break;
case 'ajax_hosts_noany':
$sql_where = '';
if (get_request_var('site_id') > 0) {
$sql_where = 'site_id = ' . get_request_var('site_id');
}
get_allowed_ajax_hosts(false, 'applyFilter', $sql_where);
break;
default:
top_header();
ds();
bottom_footer();
break;
}
function form_save() {
if ((isset_request_var('save_component_data_source_new')) && (!isempty_request_var('data_template_id'))) {
$save['id'] = get_filter_request_var('local_data_id');
$save['host_id'] = get_filter_request_var('host_id');
$save['data_template_id'] = get_filter_request_var('data_template_id');
$local_data_id = sql_save($save, 'data_local');
change_data_template($local_data_id, get_request_var('data_template_id'));
/* update the title cache */
update_data_source_title_cache($local_data_id);
/* update host data */
if (!isempty_request_var('host_id')) {
push_out_host(get_request_var('host_id'), $local_data_id);
}
if (empty($save['id'])) {
/**
* Save the last time a data source was created/updated
* for Caching.
*/
set_config_option('time_last_change_data_source', time());
}
}
if ((isset_request_var('save_component_data')) && (!is_error_message())) {
/* ================= input validation ================= */
get_filter_request_var('data_template_data_id');
/* ==================================================== */
/* ok, first pull out all 'input' values so we know how much to save */
$input_fields = db_fetch_assoc_prepared("SELECT dtd.data_input_id, dl.host_id, dif.id, dif.input_output,
dif.data_name, dif.regexp_match, dif.allow_nulls, dif.type_code
FROM data_template_data AS dtd
LEFT JOIN data_input_fields AS dif
ON dif.data_input_id = dtd.data_input_id
LEFT JOIN data_local AS dl
ON dtd.local_data_id = dl.id
WHERE dtd.id = ?
AND dif.input_output='in'",
array(get_request_var('data_template_data_id')));
if (cacti_sizeof($input_fields)) {
foreach ($input_fields as $input_field) {
if (isset_request_var('value_' . $input_field['id'])) {
/* save the data into the 'data_input_data' table */
$form_value = get_nfilter_request_var('value_' . $input_field['id']);
/* we shouldn't enforce rules on fields the user cannot see (ie. templated ones) */
$data_template_id = db_fetch_cell_prepared('SELECT local_data_template_data_id
FROM data_template_data
WHERE id = ?',
array(get_request_var('data_template_data_id'))
);
$is_templated = db_fetch_cell_prepared('SELECT t_value
FROM data_input_data
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($input_field['id'], $data_template_id)
);
if ($is_templated == '') {
$allow_nulls = true;
} elseif ($input_field['allow_nulls'] == 'on') {
$allow_nulls = true;
} elseif (empty($input_field['allow_nulls'])) {
$allow_nulls = false;
}
/* run regexp match on input string */
$form_value = form_input_validate($form_value, 'value_' . $input_field['id'], $input_field['regexp_match'], $allow_nulls, 3);
if (!is_error_message()) {
db_execute_prepared("REPLACE INTO data_input_data
(data_input_field_id, data_template_data_id, t_value, value)
VALUES
(?, ?, '', ?)",
array($input_field['id'], get_request_var('data_template_data_id'), $form_value)
);
}
}
}
}
}
if ((isset_request_var('save_component_data_source')) && (!is_error_message())) {
/* ================= input validation ================= */
get_filter_request_var('current_rrd');
get_filter_request_var('rrd_step');
get_filter_request_var('data_input_id');
get_filter_request_var('data_source_profile_id');
get_filter_request_var('host_id');
get_filter_request_var('_host_id');
get_filter_request_var('_data_template_id');
/* ==================================================== */
$save1['id'] = get_filter_request_var('local_data_id');
$save1['data_template_id'] = get_filter_request_var('data_template_id');
$save1['host_id'] = get_filter_request_var('host_id');
$save2['id'] = get_filter_request_var('data_template_data_id');
$save2['local_data_template_data_id'] = get_filter_request_var('local_data_template_data_id');
$save2['data_template_id'] = get_filter_request_var('data_template_id');
$save2['data_input_id'] = form_input_validate(get_request_var('data_input_id'), 'data_input_id', '^[0-9]+$', true, 3);
$save2['name'] = form_input_validate(get_nfilter_request_var('name'), 'name', '', false, 3);
$save2['data_source_path'] = form_input_validate(get_nfilter_request_var('data_source_path'), 'data_source_path', '', true, 3);
$save2['active'] = form_input_validate((isset_request_var('active') ? get_nfilter_request_var('active') : ''), 'active', '', true, 3);
$save2['data_source_profile_id'] = form_input_validate(get_request_var('data_source_profile_id'), 'data_source_profile_id', '^[0-9]+$', false, 3);
$save2['rrd_step'] = form_input_validate(get_request_var('rrd_step'), 'rrd_step', '^[0-9]+$', false, 3);
if (!is_error_message()) {
$local_data_id = sql_save($save1, 'data_local');
$save2['local_data_id'] = $local_data_id;
$data_template_data_id = sql_save($save2, 'data_template_data');
if ($data_template_data_id) {
raise_message(1);
if (empty($save['id'])) {
/**
* Save the last time a data source was created/updated
* for Caching.
*/
set_config_option('time_last_change_data_source', time());
}
} else {
raise_message(2);
}
}
if (!is_error_message()) {
/* if this is a new data source and a template has been selected, skip item creation this time
otherwise it throws off the template creation because of the NULL data */
if (!isempty_request_var('local_data_id') || isempty_request_var('data_template_id')) {
/* if no template was set before the save, there will be only one data source item to save;
otherwise there might be >1 */
if (isempty_request_var('_data_template_id')) {
$rrds[0]['id'] = get_nfilter_request_var('current_rrd');
} else {
$rrds = db_fetch_assoc_prepared('SELECT id
FROM data_template_rrd
WHERE local_data_id = ?',
array(get_filter_request_var('local_data_id')));
}
if (cacti_sizeof($rrds)) {
foreach ($rrds as $rrd) {
if (isempty_request_var('_data_template_id')) {
$name_modifier = '';
} else {
$name_modifier = '_' . $rrd['id'];
}
$save3['id'] = $rrd['id'];
$save3['local_data_id'] = $local_data_id;
$save3['local_data_template_rrd_id'] = db_fetch_cell_prepared('SELECT local_data_template_rrd_id
FROM data_template_rrd
WHERE id = ?',
array($rrd['id']));
$save3['data_template_id'] = get_filter_request_var('data_template_id');
$save3['rrd_maximum'] = form_input_validate(get_nfilter_request_var("rrd_maximum$name_modifier"), "rrd_maximum$name_modifier", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$|\|query_ifSpeed\||\|query_ifHighSpeed\|", false, 3);
$save3['rrd_minimum'] = form_input_validate(get_nfilter_request_var("rrd_minimum$name_modifier"), "rrd_minimum$name_modifier", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$|\|query_ifSpeed\||\|query_ifHighSpeed\|", false, 3);
$save3['rrd_heartbeat'] = form_input_validate(get_nfilter_request_var("rrd_heartbeat$name_modifier"), "rrd_heartbeat$name_modifier", '^[0-9]+$', false, 3);
$save3['data_source_type_id'] = form_input_validate(get_nfilter_request_var("data_source_type_id$name_modifier"), "data_source_type_id$name_modifier", '^[0-9]+$', false, 3);
$save3['data_source_name'] = form_input_validate(get_nfilter_request_var("data_source_name$name_modifier"), "data_source_name$name_modifier", '^[a-zA-Z0-9_-]{1,19}$', false, 3);
$save3['data_input_field_id'] = form_input_validate((isset_request_var("data_input_field_id$name_modifier") ? get_nfilter_request_var("data_input_field_id$name_modifier") : '0'), "data_input_field_id$name_modifier", '', true, 3);
if ($save3['rrd_minimum'] != 'U' && $save3['rrd_maximum'] != 'U') {
if ($save3['rrd_minimum'] >= $save3['rrd_maximum']) {
raise_message(43);
$_SESSION[SESS_ERROR_FIELDS]['rrd_maximum'] = 'rrd_maximum';
header('Location: data_sources.php?action=ds_edit&id=' . (empty($local_data_id) ? get_filter_request_var('local_data_id') : $local_data_id) . '&host_id=' . get_request_var('host_id') . '&view_rrd=' . (isset_request_var('current_rrd') ? get_nfilter_request_var('current_rrd') : '0'));
exit;
}
}
$data_template_rrd_id = sql_save($save3, 'data_template_rrd');
if ($data_template_rrd_id) {
raise_message(1);
} else {
raise_message(2);
}
}
}
}
}
if (!is_error_message()) {
if (get_request_var('data_template_id') != get_request_var('_data_template_id')) {
/* update all necessary template information */
change_data_template($local_data_id, get_request_var('data_template_id'));
} elseif (!isempty_request_var('data_template_id')) {
update_data_source_data_query_cache($local_data_id);
}
if (get_request_var('host_id') != get_request_var('_host_id')) {
/* push out all necessary host information */
push_out_host(get_request_var('host_id'), $local_data_id);
/* reset current host for display purposes */
$_SESSION['sess_data_source_current_host_id'] = get_request_var('host_id');
}
/* if no data source path has been entered, generate one */
if (isempty_request_var('data_source_path')) {
generate_data_source_path($local_data_id);
}
/* update the title cache */
update_data_source_title_cache($local_data_id);
}
}
/* update the poller cache last to make sure everything is fresh */
if ((!is_error_message()) && (!empty($local_data_id))) {
update_poller_cache($local_data_id, true);
}
if (isset_request_var('save_component_data_source_new') && isempty_request_var('data_template_id')) {
header('Location: data_sources.php?action=ds_edit&host_id=' . get_request_var('host_id') . '&new=1');
} elseif ((is_error_message()) || (get_filter_request_var('data_template_id') != get_filter_request_var('_data_template_id')) || (get_filter_request_var('data_input_id') != get_filter_request_var('_data_input_id')) || (get_filter_request_var('host_id') != get_filter_request_var('_host_id'))) {
header('Location: data_sources.php?action=ds_edit&id=' . (empty($local_data_id) ? get_filter_request_var('local_data_id') : $local_data_id) . '&host_id=' . get_request_var('host_id') . '&view_rrd=' . (isset_request_var('current_rrd') ? get_nfilter_request_var('current_rrd') : '0'));
} else {
header('Location: data_sources.php');
}
}
function form_actions() {
global $actions;
/* ================= input validation ================= */
get_filter_request_var('drp_action', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z0-9_]+)$/')));
/* ==================================================== */
/* if we are to save this form, instead of display it */
if (isset_request_var('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
if (get_nfilter_request_var('drp_action') == '1') { /* delete */
if (!isset_request_var('delete_type')) {
set_request_var('delete_type', 1);
} else {
get_filter_request_var('delete_type');
}
switch (get_request_var('delete_type')) {
case '2': /* delete all graph items tied to this data source */
$data_template_rrds = array_rekey(db_fetch_assoc('SELECT id
FROM data_template_rrd
WHERE ' . array_to_sql_or($selected_items, 'local_data_id')), 'id', 'id');
$poller_ids = db_fetch_assoc('SELECT DISTINCT poller_id
FROM host AS h
INNER JOIN data_local AS dl
ON dl.host_id=h.id
WHERE poller_id > 1
AND id IN (' . implode(', ', $selected_items) . ')');
api_plugin_hook_function('graph_items_remove', $data_template_rrds);
/* loop through each data source item */
if (cacti_sizeof($data_template_rrds) > 0) {
db_execute('DELETE FROM graph_templates_item
WHERE task_item_id IN (' . implode(',', $data_template_rrds) . ')
AND local_graph_id > 0');
if (cacti_sizeof($poller_ids)) {
foreach ($poller_ids as $poller_id) {
if (($rcnn_id = poller_push_to_remote_db_connect($poller_id, true)) !== false) {
db_execute('DELETE FROM graph_templates_item
WHERE task_item_id IN (' . implode(',', $data_template_rrds) . ')
AND local_graph_id > 0', true, $rcnn_id);
}
}
}
}
break;
case '3': /* delete all graphs tied to this data source */
$graphs = array_rekey(db_fetch_assoc('SELECT
graph_templates_graph.local_graph_id
FROM (data_template_rrd,graph_templates_item,graph_templates_graph)
WHERE graph_templates_item.task_item_id=data_template_rrd.id
AND graph_templates_item.local_graph_id=graph_templates_graph.local_graph_id
AND ' . array_to_sql_or($selected_items, 'data_template_rrd.local_data_id') . '
AND graph_templates_graph.local_graph_id > 0
GROUP BY graph_templates_graph.local_graph_id'), 'local_graph_id', 'local_graph_id');
if (cacti_sizeof($graphs) > 0) {
api_graph_remove_multi($graphs);
}
break;
}
api_data_source_remove_multi($selected_items);
} elseif (get_nfilter_request_var('drp_action') == '2') { // data source disable
for ($i=0;($i < cacti_count($selected_items));$i++) {
api_data_source_disable($selected_items[$i]);
}
} elseif (get_nfilter_request_var('drp_action') == '3') { // data source enable
for ($i=0;($i < cacti_count($selected_items));$i++) {
api_data_source_enable($selected_items[$i]);
}
} elseif (get_nfilter_request_var('drp_action') == '4') { // change host
get_filter_request_var('host_id');
api_data_source_change_host($selected_items, get_request_var('host_id'));
} elseif (get_nfilter_request_var('drp_action') == '5') { // reapply suggested data source naming
for ($i=0;($i < cacti_count($selected_items));$i++) {
api_reapply_suggested_data_source_data($selected_items[$i]);
update_data_source_title_cache($selected_items[$i]);
}
} else {
api_plugin_hook_function('data_source_action_execute', get_nfilter_request_var('drp_action'));
}
}
/* update snmpcache */
snmpagent_data_source_action_bottom(array(get_nfilter_request_var('drp_action'), $selected_items));
api_plugin_hook_function('data_source_action_bottom', array(get_nfilter_request_var('drp_action'), $selected_items));
header('Location: data_sources.php');
exit;
} else {
$ilist = '';
$iarray = array();
/* some global defaults */
$graphs = array();
$flist = '';
$hosts = array_rekey(
db_fetch_assoc("SELECT id, CONCAT_WS('',description,' (',hostname,')') AS name
FROM host
ORDER BY description, hostname"),
'id', 'name'
);
/* loop through each of the graphs selected on the previous page and get more info about them */
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1], 'chk[1]');
/* ==================================================== */
$ilist .= '
' . html_escape(get_data_source_title($matches[1])) . '';
$iarray[] = $matches[1];
}
}
if (isset($iarray) && cacti_sizeof($iarray)) {
if (get_nfilter_request_var('drp_action') == '1') { /* delete */
$graphs = db_fetch_assoc('SELECT
graph_templates_graph.local_graph_id,
graph_templates_graph.title_cache
FROM (data_template_rrd,graph_templates_item,graph_templates_graph)
WHERE graph_templates_item.task_item_id=data_template_rrd.id
AND graph_templates_item.local_graph_id=graph_templates_graph.local_graph_id
AND ' . array_to_sql_or($iarray, 'data_template_rrd.local_data_id') . '
AND graph_templates_graph.local_graph_id > 0
GROUP BY graph_templates_graph.local_graph_id
ORDER BY graph_templates_graph.title_cache');
if (cacti_sizeof($graphs)) {
foreach($graphs as $g) {
$flist .= '' . html_escape($g['title_cache']) . '';
}
}
}
}
// For use by plugins
$save['drp_action'] = $drpval;
$save['ds_list'] = $ilist;
$save['ds_array'] = $iarray;
$form_data = array(
'general' => array(
'page' => 'data_sources.php',
'actions' => $actions,
'optvar' => 'drp_action',
'item_array' => $iarray,
'item_list' => $ilist
),
'options' => array(
1 => array(
'smessage' => __('Click \'Continue\' to Delete the following Data Source.'),
'pmessage' => __('Click \'Continue\' to Delete following Data Sources.'),
'scont' => __('Delete Data Source'),
'pcont' => __('Delete Data Sources'),
'flist' => $flist,
'sfmessage' => __n('The following Graph is using this Data Source.', 'The following Graphs are using this Data Source.', cacti_sizeof($graphs)),
'pfmessage' => __n('The following Graph is using these Data Sources.', 'The following Graphs are using these Data Sources.', cacti_sizeof($graphs)),
'extra' => array(
'delete_type' => array(
'method' => 'radio_button',
'options' => array(
'1' => array(
'default' => 3,
'title' => __n('Leave the Graph Untouched', 'Leave all Graphs untouched.', cacti_sizeof($graphs))
),
'2' => array(
'default' => 3,
'title' => __n('Delete all Graph Items that reference this Data Source.', 'Delete all Graph Items that reference these Data Sources', cacti_sizeof($iarray))
),
'3' => array(
'default' => 3,
'title' => __n('Delete all Graphs that reference this Data Source.', 'Delete all Graphs that reference these Data Sources', cacti_sizeof($iarray))
)
)
)
)
),
2 => array(
'smessage' => __('Click \'Continue\' to Disable the following Data Source.'),
'pmessage' => __('Click \'Continue\' to Disable following Data Sources.'),
'scont' => __('Disable Data Source'),
'pcont' => __('Disable Data Sources')
),
3 => array(
'smessage' => __('Click \'Continue\' to Enable the following Data Source.'),
'pmessage' => __('Click \'Continue\' to Enable following Data Sources.'),
'scont' => __('Enable Data Source'),
'pcont' => __('Enable Data Sources')
),
4 => array(
'smessage' => __('Click \'Continue\' to Change the Device for the following Data Source.'),
'pmessage' => __('Click \'Continue\' to Change the Device for the following Data Sources.'),
'scont' => __('Change Device for Data Source'),
'pcont' => __('Change Device for Data Sources'),
'extra' => array(
'host_id' => array(
'method' => 'drop_array',
'title' => __('New Device:'),
'default' => '',
'array' => $hosts
)
)
),
5 => array(
'smessage' => __('Click \'Continue\' to Reapply Suggested Names the following Data Source.'),
'pmessage' => __('Click \'Continue\' to Reapply Suggested Names for the following Data Sources.'),
'scont' => __('Reapply Suggested Names for Data Source'),
'pcont' => __('Reapply Suggested Names for Data Sources')
)
)
);
$form_data = api_plugin_hook_function('data_source_confirmation_form', $form_data);
form_continue_confirmation($form_data, 'data_source_action_prepare', $save);
}
}
function data_edit($incform = true) {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
if (!isempty_request_var('id')) {
$data = db_fetch_row_prepared('SELECT id, data_input_id, data_template_id, name, local_data_id
FROM data_template_data
WHERE local_data_id = ?',
array(get_request_var('id')));
$template_data = db_fetch_row_prepared('SELECT id, data_input_id
FROM data_template_data
WHERE data_template_id = ?
AND local_data_id = 0',
array($data['data_template_id']));
$host = db_fetch_row_prepared('SELECT host.id, host.hostname
FROM (data_local, host)
WHERE data_local.host_id = host.id
AND data_local.id = ?',
array(get_request_var('id')));
}
if ($incform) {
form_start('data_sources.php', 'data_source_edit');
}
$i = 0;
if (!empty($data['data_input_id'])) {
/* get each INPUT field for this data input source */
$fields = db_fetch_assoc_prepared('SELECT *
FROM data_input_fields
WHERE data_input_id = ?
AND input_output = "in"
ORDER BY name',
array($data['data_input_id'])
);
$data_input_name = db_fetch_cell_prepared('SELECT name
FROM data_input
WHERE id = ?',
array($data['data_input_id']));
html_start_box(__('Custom Data [data input: %s]', html_escape($data_input_name)), '100%', '', '3', 'center', '');
/* loop through each field found */
if (cacti_sizeof($fields) > 0) {
foreach ($fields as $field) {
$data_input_data = db_fetch_row_prepared('SELECT *
FROM data_input_data
WHERE data_template_data_id = ?
AND data_input_field_id = ?',
array($data['id'], $field['id'])
);
if (cacti_sizeof($data_input_data) > 0) {
$old_value = $data_input_data['value'];
} else {
$old_value = '';
}
/* if data template then get t_value FROM template, else always allow user input */
if (empty($data['data_template_id'])) {
$can_template = 'on';
} else {
$can_template = db_fetch_cell_prepared('SELECT t_value
FROM data_input_data
WHERE data_template_data_id = ?
AND data_input_field_id = ?',
array($template_data['id'], $field['id'])
);
}
form_alternate_row();
if ((!empty($host['id'])) && (preg_match('/^' . VALID_HOST_FIELDS . '$/i', $field['type_code']))) {
print "" . html_escape($field['name']) . ' ' . __('(From Device: %s)', html_escape($host['hostname'])) . ' | ';
print '' . html_escape($old_value) . ' | ';
} elseif (empty($can_template)) {
print "" . html_escape($field['name']) . ' ' . __('(From Data Template)') . ' | ';
print '' . (empty($old_value) ? __('Nothing Entered') : html_escape($old_value)) . ' | ';
} else {
print "" . html_escape($field['name']) . ' | ';
print '';
draw_custom_data_row('value_' . $field['id'], $field['id'], $data['id'], $old_value);
print ' | ';
}
print '';
$i++;
}
} else {
print '| ' . __('No Input Fields for the Selected Data Input Source') . ' |
';
}
html_end_box();
}
if ($incform) {
form_hidden_box('local_data_id', (isset($data) ? $data['local_data_id'] : '0'), '');
form_hidden_box('data_template_data_id', (isset($data) ? $data['id'] : '0'), '');
}
form_hidden_box('save_component_data', '1', '');
}
function ds_rrd_remove() {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
db_execute_prepared('DELETE FROM data_template_rrd
WHERE id = ?',
array(get_request_var('id')));
db_execute_prepared('UPDATE graph_templates_item
SET task_item_id = 0
WHERE task_item_id = ?',
array(get_request_var('id')));
header('Location: data_sources.php?action=ds_edit&id=' . get_request_var('local_data_id'));
}
function ds_rrd_add() {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
db_execute_prepared("INSERT INTO data_template_rrd
(local_data_id, rrd_maximum, rrd_minimum, rrd_heartbeat, data_source_type_id, data_source_name)
VALUES (?, 100, 0, 600, 1, 'ds')",
array(get_request_var('id')));
$data_template_rrd_id = db_fetch_insert_id();
header('Location: data_sources.php?action=ds_edit&id=' . get_request_var('id') . "&view_rrd=$data_template_rrd_id");
}
function ds_disable() {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
api_data_source_disable(get_request_var('id'));
header('Location: data_sources.php?action=ds_edit&id=' . get_request_var('id'));
}
function ds_enable() {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
api_data_source_enable(get_request_var('id'));
header('Location: data_sources.php?action=ds_edit&id=' . get_request_var('id'));
}
function ds_edit() {
global $struct_data_source, $struct_data_source_item;
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('host_id');
/* ==================================================== */
api_plugin_hook('data_source_edit_top');
$use_data_template = true;
$data_template = array();
if (!isempty_request_var('id')) {
$data_local = db_fetch_row_prepared('SELECT host_id, data_template_id
FROM data_local
WHERE id = ?',
array(get_request_var('id')));
$data = db_fetch_row_prepared('SELECT *
FROM data_template_data
WHERE local_data_id = ?',
array(get_request_var('id')));
if (isset($data_local['data_template_id']) && $data_local['data_template_id'] >= 0) {
$data_template = db_fetch_row_prepared('SELECT id, name
FROM data_template
WHERE id = ?',
array($data_local['data_template_id']));
$data_template_data = db_fetch_row_prepared('SELECT *
FROM data_template_data
WHERE data_template_id = ?
AND local_data_id = 0',
array($data_local['data_template_id']));
} else {
raise_message(11);
header('Location: data_sources.php');
exit;
}
$header_label = __esc('Data Template Selection [edit: %s]', get_data_source_title(get_request_var('id')));
if (empty($data_local['data_template_id'])) {
$use_data_template = false;
}
} else {
$header_label = __('Data Template Selection [new]');
$use_data_template = false;
}
/* handle debug mode */
if (isset_request_var('debug')) {
if (get_nfilter_request_var('debug') == '0') {
kill_session_var('ds_debug_mode');
} elseif (get_nfilter_request_var('debug') == '1') {
$_SESSION['ds_debug_mode'] = true;
}
}
/* handle debug mode */
if (isset_request_var('info')) {
if (get_nfilter_request_var('info') == '0') {
kill_session_var('ds_info_mode');
} elseif (get_nfilter_request_var('info') == '1') {
$_SESSION['ds_info_mode'] = true;
}
}
top_header();
if (!isempty_request_var('id')) {
$local_graph_ids = db_fetch_assoc_prepared('SELECT DISTINCT local_graph_id
FROM graph_templates_item AS gti
INNER JOIN data_template_rrd AS dtr
ON dtr.id = gti.task_item_id
WHERE local_data_id = ?',
array(get_request_var('id')));
?>
0) {
$hostDescription = db_fetch_cell_prepared('SELECT description
FROM host
WHERE id = ?',
array(get_request_var('host_id'))
);
} elseif (isset($data_local['host_id'])) {
$hostDescription = db_fetch_cell_prepared('SELECT description
FROM host
WHERE id = ?',
array($data_local['host_id'])
);
} else {
$hostDescription = '';
}
$form_array = array(
'data_template_id' => array(
'method' => 'drop_sql',
'friendly_name' => __('Selected Data Template'),
'description' => __('The name given to this data template. Please note that you may only change Graph Templates to a 100%$ compatible Graph Template, which means that it includes identical Data Sources.'),
'value' => (cacti_sizeof($data_template) ? $data_template['id'] : '0'),
'none_value' => (cacti_sizeof($data_template) ? '' : 'None'),
'sql' => $dtsql
),
'host_id' => array(
'method' => 'drop_callback',
'friendly_name' => __('Device'),
'description' => __('Choose the Device that this Data Source belongs to.'),
'none_value' => __('None'),
'sql' => 'SELECT id, description AS name FROM host ORDER BY name',
'action' => 'ajax_hosts_noany',
'id' => (isset($data_local['host_id']) ? $data_local['host_id'] : 0),
'value' => $hostDescription
),
'_data_template_id' => array(
'method' => 'hidden',
'value' => (isset($data_template['id']) ? $data_template['id'] : '0')
),
'_host_id' => array(
'method' => 'hidden',
'value' => (isset($data_local['host_id']) ? $data_local['host_id'] : '0')
),
'_data_input_id' => array(
'method' => 'hidden',
'value' => (isset($data['data_input_id']) ? $data['data_input_id'] : '0')
),
'data_template_data_id' => array(
'method' => 'hidden',
'value' => (isset($data['id']) ? $data['id'] : '0')
),
'local_data_template_data_id' => array(
'method' => 'hidden',
'value' => (isset($data['local_data_template_data_id']) ? $data['local_data_template_data_id'] : '0')
),
'local_data_id' => array(
'method' => 'hidden',
'value' => (isset($data['local_data_id']) ? $data['local_data_id'] : '0')
),
);
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => $form_array
)
);
html_end_box(true, true);
/* only display the "inputs" area if we are using a data template for this data source */
if (!empty($data['data_template_id'])) {
$template_data_rrds = db_fetch_assoc_prepared('SELECT *
FROM data_template_rrd
WHERE local_data_id = ?
ORDER BY data_source_name',
array(get_request_var('id')));
html_start_box(__('Supplemental Data Template Data'), '100%', true, '3', 'center', '');
draw_nontemplated_fields_data_source($data['data_template_id'], $data['local_data_id'], $data, '|field|', __('Data Source Fields'), true, true, 0);
draw_nontemplated_fields_data_source_item($data['data_template_id'], $template_data_rrds, '|field|_|id|', __('Data Source Item Fields'), true, true, true, 0);
draw_nontemplated_fields_custom_data($data['id'], 'value_|id|', __('Custom Data'), true, true, 0);
form_hidden_box('save_component_data','1','');
html_end_box(true, true);
}
if (((isset_request_var('id')) || (isset_request_var('new'))) && (empty($data['data_template_id']))) {
html_start_box(__('Data Source'), '100%', true, '3', 'center', '');
$form_array = array();
foreach ($struct_data_source as $field_name => $field_array) {
$form_array += array($field_name => $struct_data_source[$field_name]);
if (($field_array['method'] != 'header') && ($field_array['method'] != 'spacer')) {
if (!(($use_data_template == false) || (!empty($data_template_data['t_' . $field_name])) || ($field_array['flags'] == 'NOTEMPLATE'))) {
$form_array[$field_name]['description'] = '';
}
$form_array[$field_name]['value'] = (isset($data[$field_name]) ? $data[$field_name] : '');
$form_array[$field_name]['form_id'] = (empty($data['id']) ? '0' : $data['id']);
if (!(($use_data_template == false) || (!empty($data_template_data['t_' . $field_name])) || ($field_array['flags'] == 'NOTEMPLATE'))) {
$form_array[$field_name]['method'] = 'template_' . $form_array[$field_name]['method'];
}
}
}
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => inject_form_variables($form_array, (isset($data) ? $data : array()))
)
);
html_end_box(true, true);
/* fetch ALL rrd's for this data source */
if (!isempty_request_var('id')) {
$template_data_rrds = db_fetch_assoc_prepared('SELECT id, data_source_name
FROM data_template_rrd
WHERE local_data_id = ?
ORDER BY data_source_name',
array(get_request_var('id')));
}
/* select the first "rrd" of this data source by default */
if (isempty_request_var('view_rrd')) {
set_request_var('view_rrd', (isset($template_data_rrds[0]['id']) ? $template_data_rrds[0]['id'] : '0'));
}
/* get more information about the rrd we chose */
if (!isempty_request_var('view_rrd')) {
$local_data_template_rrd_id = db_fetch_cell_prepared('SELECT local_data_template_rrd_id
FROM data_template_rrd
WHERE id = ?',
array(get_request_var('view_rrd')));
$rrd = db_fetch_row_prepared('SELECT *
FROM data_template_rrd
WHERE id = ?',
array(get_request_var('view_rrd')));
$rrd_template = db_fetch_row_prepared('SELECT *
FROM data_template_rrd
WHERE id = ?',
array($local_data_template_rrd_id));
$header_label = __('[edit: %s]', $rrd['data_source_name']);
} else {
$header_label = '';
}
$i = 0;
if (isset($template_data_rrds)) {
if (cacti_sizeof($template_data_rrds) > 1) {
/* draw the data source tabs on the top of the page */
print " ";
foreach ($template_data_rrds as $template_data_rrd) {
$i++;
print ' |
| ';
print " | ";
}
print ' |
';
} elseif (cacti_sizeof($template_data_rrds) == 1) {
set_request_var('view_rrd', $template_data_rrds[0]['id']);
}
}
html_start_box('', '100%', true, '3', 'center', '');
print "';
/* data input fields list */
if ((empty($data['data_input_id'])) || (db_fetch_cell_prepared('SELECT type_id FROM data_input WHERE id = ?', array($data['data_input_id'])) > '1')) {
unset($struct_data_source_item['data_input_field_id']);
} else {
$struct_data_source_item['data_input_field_id']['sql'] = "SELECT id,CONCAT(data_name,' - ',name) as name FROM data_input_fields WHERE data_input_id=" . $data['data_input_id'] . " and input_output='out' and update_rra='on' order by data_name,name";
}
$form_array = array();
foreach ($struct_data_source_item as $field_name => $field_array) {
$form_array += array($field_name => $struct_data_source_item[$field_name]);
if (($field_array['method'] != 'header') && ($field_array['method'] != 'spacer')) {
if (!(($use_data_template == false) || ($rrd_template['t_' . $field_name] == 'on'))) {
$form_array[$field_name]['description'] = '';
}
$form_array[$field_name]['value'] = (isset($rrd) ? $rrd[$field_name] : '');
if (!(($use_data_template == false) || ($rrd_template['t_' . $field_name] == 'on'))) {
$form_array[$field_name]['method'] = 'template_' . $form_array[$field_name]['method'];
}
}
}
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => array(
'data_template_rrd_id' => array(
'method' => 'hidden',
'value' => (isset($rrd) ? $rrd['id'] : '0')
),
'local_data_template_rrd_id' => array(
'method' => 'hidden',
'value' => (isset($rrd) ? $rrd['local_data_template_rrd_id'] : '0')
)
) + $form_array
)
);
html_end_box(true, true);
/* data source data goes here */
data_edit(false);
form_hidden_box('current_rrd', get_request_var('view_rrd'), '0');
}
/* display the debug mode box if the user wants it */
if ((isset($_SESSION['ds_debug_mode'])) && (isset_request_var('id'))) {
?>
' . __('External') . '';
}
if ($seconds < 60) {
return '' . __('%d Seconds', $seconds) . '';
}
if ($seconds == 60) {
return __('1 Minute');
} else {
return '' . __('%d Minutes', ($seconds / 60)) . '';
}
}
function validate_data_source_vars() {
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'rfilter' => array(
'filter' => FILTER_VALIDATE_IS_REGEX,
'pageset' => true,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
),
'sort_column' => array(
'filter' => FILTER_CALLBACK,
'default' => 'name_cache',
'options' => array('options' => 'sanitize_search_string')
),
'sort_direction' => array(
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => array('options' => 'sanitize_search_string')
),
'host_id' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'site_id' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'status' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'template_id' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'profile' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'orphans' => array(
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_ds');
/* ================= input validation ================= */
}
function ds() {
global $actions, $item_rows, $sampling_intervals;
if (get_request_var('rows') == -1) {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
if (get_request_var('host_id') > 0) {
$host = db_fetch_row_prepared('SELECT hostname
FROM host
WHERE id = ?',
array(get_request_var('host_id')));
} else {
$host = array();
}
?>
0) {
$host_where = 'site_id = ' . get_request_var('site_id');
} else {
$host_where = '';
}
if (get_request_var('host_id') == -1) {
$header = __('Data Sources [ All Devices ]');
} elseif (get_request_var('host_id') == 0) {
$header = __('Data Sources [ Non Device Based ]');
} else {
$description = db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array(get_request_var('host_id')));
$header = __esc('Data Sources [ %s ]', $description);
}
html_start_box($header, '100%', '', '3', 'center', $add_url);
?>
|
|
0)';
}
$sql_order = get_order_string();
$sql_limit = ' LIMIT ' . ($rows * (get_request_var('page') - 1)) . ',' . $rows;
if (get_request_var('orphans') == 'true') {
$sql_where1 .= ($sql_where1 != '' ? ' AND ':'WHERE ') . '((dl.snmp_index = "" AND dl.snmp_query_id > 0) OR graph_items = 0 IS NULL OR dl.orphan = 1)';
$orphan_join = "LEFT JOIN (
SELECT dtr.local_data_id, COUNT(DISTINCT gti.task_item_id) AS graph_items
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON dtr.id = gti.task_item_id
INNER JOIN graph_local AS gl
ON gl.id = gti.local_graph_id
LEFT JOIN host AS h
ON h.id = gl.host_id
WHERE dtr.local_data_id > 0
$sql_where2
GROUP BY local_data_id
) AS dtr
ON dl.id = dtr.local_data_id";
$sql = "SELECT COUNT(*)
FROM data_local AS dl
INNER JOIN data_template_data AS dtd
ON dl.id = dtd.local_data_id
INNER JOIN data_template AS dt
ON dt.id = dl.data_template_id
LEFT JOIN host AS h
ON h.id = dl.host_id
$orphan_join
$sql_where1";
$total_rows = get_total_row_data($_SESSION[SESS_USER_ID], $sql, array(), 'data_source');
$data_sources = db_fetch_assoc("SELECT dtd.local_data_id,
dtd.name_cache, dtd.active, dtd.rrd_step, dt.name AS data_template_name,
dl.host_id, dtd.data_source_profile_id
FROM data_local AS dl
INNER JOIN data_template_data AS dtd
ON dl.id = dtd.local_data_id
INNER JOIN data_template AS dt
ON dt.id = dl.data_template_id
LEFT JOIN host AS h
ON h.id = dl.host_id
$orphan_join
$sql_where1
$sql_order
$sql_limit");
} else {
$sql = "SELECT COUNT(*)
FROM data_local AS dl
INNER JOIN data_template_data AS dtd
ON dl.id=dtd.local_data_id
LEFT JOIN data_template AS dt
ON dt.id = dl.data_template_id
LEFT JOIN host AS h
ON h.id = dl.host_id
$sql_where1";
$total_rows = get_total_row_data($_SESSION[SESS_USER_ID], $sql, array(), 'data_source');
$data_sources = db_fetch_assoc("SELECT dtd.local_data_id, dtd.name_cache, dtd.active,
dtd.rrd_step, dt.name AS data_template_name, dl.host_id, dtd.data_source_profile_id
FROM data_local AS dl
INNER JOIN data_template_data AS dtd
ON dl.id=dtd.local_data_id
LEFT JOIN data_template AS dt
ON dt.id = dl.data_template_id
LEFT JOIN host AS h
ON h.id = dl.host_id
$sql_where1
$sql_order
$sql_limit");
}
$nav = html_nav_bar('data_sources.php', MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 7, __('Data Sources'), 'page', 'main');
form_start('data_sources.php', 'chk');
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
$display_text = array(
'name_cache' => array(
'display' => __('Data Source Name'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The name of this Data Source. Generally programmatically generated from the Data Template definition.')
),
'local_data_id' => array(
'display' => __('ID'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The internal database ID for this Data Source. Useful when performing automation or debugging.')
),
'nosort0' => array(
'display' => __('Graphs'),
'align' => 'center',
'sort' => '',
'tip' => __('The number of Graphs and Aggregate Graphs that are using the Data Source.')
),
'nosort1' => array(
'display' => __('Poller Interval'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The frequency that data is collected for this Data Source.')
),
'nosort2' => array(
'display' => __('Deletable'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('If this Data Source is no long in use by Graphs, it can be Deleted.')
),
'active' => array(
'display' => __('Active'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('Whether or not data will be collected for this Data Source. Controlled at the Data Template level.')
),
'data_template_name' => array(
'display' => __('Template Name'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The Data Template that this Data Source was based upon.')
)
);
html_header_sort_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false);
$i = 0;
if (cacti_sizeof($data_sources)) {
foreach ($data_sources as $data_source) {
if (api_data_source_deletable($data_source['local_data_id'])) {
$disabled = false;
} else {
$disabled = true;
}
$data_source['data_template_name'] = html_escape($data_source['data_template_name']);
$data_name_cache = title_trim(html_escape($data_source['name_cache']), read_config_option('max_title_length'));
if (get_request_var('rfilter') != '') {
$data_name_cache = filter_value($data_name_cache, get_request_var('rfilter'));
}
/* keep copy of data source for comparison */
$data_source_orig = $data_source;
$data_source = api_plugin_hook_function('data_sources_table', $data_source);
/* we're escaping strings here, so no need to escape them on form_selectable_cell */
if (empty($data_source['data_template_name'])) {
$data_template_name = '' . __('None') . '';
} elseif ($data_source_orig['data_template_name'] != $data_source['data_template_name']) {
/* was changed by plugin, plugin has to take care for html-escaping */
$data_template_name = $data_source['data_template_name'];
} elseif (get_request_var('rfilter') != '') {
$data_template_name = filter_value($data_source['data_template_name'], get_request_var('rfilter'));
} else {
$data_template_name = html_escape($data_source['data_template_name']);
}
$graphs_aggregates_url = get_graphs_aggregates_url($data_source['local_data_id']);
if ($data_source['name_cache'] == '') {
$name = __('Damaged Data Source Name');
} else {
$name = $data_source['name_cache'];
}
form_alternate_row('line' . $data_source['local_data_id'], true, $disabled);
form_selectable_cell(filter_value(title_trim($name, read_config_option('max_title_length')), get_request_var('rfilter'), 'data_sources.php?action=ds_edit&id=' . $data_source['local_data_id']), $data_source['local_data_id']);
form_selectable_cell($data_source['local_data_id'], $data_source['local_data_id'], '', 'right');
// Show link to Graphs and Aggregates
form_selectable_cell($graphs_aggregates_url, $data_source['local_data_id'], '', 'center');
form_selectable_cell(get_poller_interval($data_source['rrd_step'], $data_source['data_source_profile_id']), $data_source['local_data_id']);
form_selectable_cell(api_data_source_deletable($data_source['local_data_id']) ? __('Yes') : __('No'), $data_source['local_data_id']);
form_selectable_cell(($data_source['active'] == 'on' ? __('Yes'):__('No')), $data_source['local_data_id']);
form_selectable_cell($data_template_name, $data_source['local_data_id']);
form_checkbox_cell($name, $data_source['local_data_id'], $disabled);
form_end_row();
}
} else {
print "| " . __('No Data Sources Found') . ' |
';
}
html_end_box(false);
if (cacti_sizeof($data_sources)) {
print $nav;
}
/* draw the dropdown containing a list of available actions for this form */
draw_actions_dropdown($actions);
form_end();
}
function get_graphs_aggregates_url($local_data_id) {
$graphs = db_fetch_row_prepared('SELECT GROUP_CONCAT(DISTINCT gl.id) AS graphs, COUNT(DISTINCT gl.id) AS total
FROM data_local AS dl
INNER JOIN data_template_rrd AS dtr
ON dl.id = dtr.local_data_id
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
LEFT JOIN aggregate_graphs AS ag
ON ag.local_graph_id = gl.id
WHERE dl.id = ?
AND ag.local_graph_id IS NULL',
array($local_data_id));
$aggregates = db_fetch_row_prepared('SELECT GROUP_CONCAT(DISTINCT gl.id) AS graphs, COUNT(DISTINCT gl.id) AS total
FROM data_local AS dl
INNER JOIN data_template_rrd AS dtr
ON dl.id = dtr.local_data_id
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
INNER JOIN aggregate_graphs AS ag
ON ag.local_graph_id = gl.id
WHERE dl.id = ?',
array($local_data_id));
$url = '';
if (cacti_sizeof($graphs) && $graphs['total'] > 0) {
$url .= '' . $graphs['total'] . '';
} else {
$url .= '0';
}
if (cacti_sizeof($aggregates) && $aggregates['total'] > 0) {
$url .= ' / ' . $aggregates['total'] . '';
} else {
$url .= ' / 0';
}
return $url;
}