Example #1
0
File: host.php Project: MrWnn/cacti
function form_actions()
{
    global $device_actions, $fields_host_edit;
    /* ================= input validation ================= */
    input_validate_input_regex(get_request_var_post('drp_action'), '^([a-zA-Z0-9_]+)$');
    /* ==================================================== */
    /* if we are to save this form, instead of display it */
    if (isset($_POST['selected_items'])) {
        $selected_items = unserialize(stripslashes($_POST['selected_items']));
        if ($_POST['drp_action'] == '2') {
            /* Enable Selected Devices */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                db_execute_prepared("UPDATE host SET disabled = '' WHERE id = ?", array($selected_items[$i]));
                /* update poller cache */
                $data_sources = db_fetch_assoc_prepared('SELECT id FROM data_local WHERE host_id = ?', array($selected_items[$i]));
                $poller_items = $local_data_ids = array();
                if (sizeof($data_sources) > 0) {
                    foreach ($data_sources as $data_source) {
                        $local_data_ids[] = $data_source['id'];
                        $poller_items = array_merge($poller_items, update_poller_cache($data_source['id']));
                    }
                }
                if (sizeof($local_data_ids)) {
                    poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
                }
            }
        } elseif ($_POST['drp_action'] == '3') {
            /* Disable Selected Devices */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                db_execute_prepared("UPDATE host SET disabled='on' WHERE id = ?", array($selected_items[$i]));
                /* update poller cache */
                db_execute_prepared('DELETE FROM poller_item WHERE host_id = ?', array($selected_items[$i]));
                db_execute_prepared('DELETE FROM poller_reindex WHERE host_id = ?', array($selected_items[$i]));
            }
        } elseif ($_POST['drp_action'] == '4') {
            /* change snmp options */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                reset($fields_host_edit);
                while (list($field_name, $field_array) = each($fields_host_edit)) {
                    if (isset($_POST["t_{$field_name}"])) {
                        db_execute_prepared("UPDATE host SET {$field_name} = ? WHERE id = ?", array($_POST[$field_name], $selected_items[$i]));
                    }
                }
                push_out_host($selected_items[$i]);
            }
        } elseif ($_POST['drp_action'] == '5') {
            /* Clear Statisitics for Selected Devices */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                db_execute_prepared("UPDATE host SET min_time = '9.99999', max_time = '0', cur_time = '0', avg_time = '0',\n\t\t\t\t\t\ttotal_polls = '0', failed_polls = '0',\tavailability = '100.00'\n\t\t\t\t\t\twhere id = ?", array($selected_items[$i]));
            }
        } elseif ($_POST['drp_action'] == '6') {
            /* change availability options */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                reset($fields_host_edit);
                while (list($field_name, $field_array) = each($fields_host_edit)) {
                    if (isset($_POST["t_{$field_name}"])) {
                        db_execute_prepared("UPDATE host SET {$field_name} = ? WHERE id = ?", array($_POST[$field_name], $selected_items[$i]));
                    }
                }
                push_out_host($selected_items[$i]);
            }
        } elseif ($_POST['drp_action'] == '1') {
            /* delete */
            if (!isset($_POST['delete_type'])) {
                $_POST['delete_type'] = 2;
            }
            $data_sources_to_act_on = array();
            $graphs_to_act_on = array();
            $devices_to_act_on = array();
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                $data_sources = db_fetch_assoc('SELECT
					data_local.id AS local_data_id
					FROM data_local
					WHERE ' . array_to_sql_or($selected_items, 'data_local.host_id'));
                if (sizeof($data_sources) > 0) {
                    foreach ($data_sources as $data_source) {
                        $data_sources_to_act_on[] = $data_source['local_data_id'];
                    }
                }
                if ($_POST['delete_type'] == 2) {
                    $graphs = db_fetch_assoc('SELECT
						graph_local.id AS local_graph_id
						FROM graph_local
						WHERE ' . array_to_sql_or($selected_items, 'graph_local.host_id'));
                    if (sizeof($graphs) > 0) {
                        foreach ($graphs as $graph) {
                            $graphs_to_act_on[] = $graph['local_graph_id'];
                        }
                    }
                }
                $devices_to_act_on[] = $selected_items[$i];
            }
            switch ($_POST['delete_type']) {
                case '1':
                    /* leave graphs and data_sources in place, but disable the data sources */
                    api_data_source_disable_multi($data_sources_to_act_on);
                    api_plugin_hook_function('data_source_remove', $data_sources_to_act_on);
                    break;
                case '2':
                    /* delete graphs/data sources tied to this device */
                    api_data_source_remove_multi($data_sources_to_act_on);
                    api_graph_remove_multi($graphs_to_act_on);
                    api_plugin_hook_function('graphs_remove', $graphs_to_act_on);
                    break;
            }
            api_device_remove_multi($devices_to_act_on);
            api_plugin_hook_function('device_remove', $devices_to_act_on);
        } elseif (preg_match('/^tr_([0-9]+)$/', $_POST['drp_action'], $matches)) {
            /* place on tree */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                input_validate_input_number(get_request_var_post('tree_id'));
                input_validate_input_number(get_request_var_post('tree_item_id'));
                /* ==================================================== */
                api_tree_item_save(0, $_POST['tree_id'], TREE_ITEM_TYPE_HOST, $_POST['tree_item_id'], '', 0, read_graph_config_option('default_rra_id'), $selected_items[$i], 1, 1, false);
            }
        } else {
            api_plugin_hook_function('device_action_execute', $_POST['drp_action']);
        }
        /* update snmpcache */
        snmpagent_device_action_bottom(array($_POST['drp_action'], $selected_items));
        api_plugin_hook_function('device_action_bottom', array($_POST['drp_action'], $selected_items));
        header('Location: host.php');
        exit;
    }
    /* setup some variables */
    $host_list = '';
    $i = 0;
    /* loop through each of the host templates selected on the previous page and get more info about them */
    while (list($var, $val) = each($_POST)) {
        if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
            /* ================= input validation ================= */
            input_validate_input_number($matches[1]);
            /* ==================================================== */
            $host_list .= '<li>' . htmlspecialchars(db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array($matches[1]))) . '<br>';
            $host_array[$i] = $matches[1];
            $i++;
        }
    }
    top_header();
    /* add a list of tree names to the actions dropdown */
    add_tree_names_to_actions_array();
    html_start_box('<strong>' . $device_actions[get_request_var_post('drp_action')] . '</strong>', '60%', '', '3', 'center', '');
    print "<form action='host.php' autocomplete='off' method='post'>\n";
    if (isset($host_array) && sizeof($host_array)) {
        if ($_POST['drp_action'] == '2') {
            /* Enable Devices */
            print "\t<tr>\n\t\t\t\t\t<td colspan='2' class='textArea'>\n\t\t\t\t\t\t<p>To enable the following Device(s), click \"Continue\".</p>\n\t\t\t\t\t\t<p><ul>" . $host_list . "</ul></p>\n\t\t\t\t\t</td>\n\t\t\t\t\t</tr>";
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Enable Device(s)'>";
        } elseif ($_POST['drp_action'] == '3') {
            /* Disable Devices */
            print "\t<tr>\n\t\t\t\t\t<td colspan='2' class='textArea'>\n\t\t\t\t\t\t<p>To disable the following Device(s), click \"Continue\".</p>\n\t\t\t\t\t\t<p><ul>" . $host_list . '</ul></p>
					</td>
					</tr>';
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Disable Device(s)'>";
        } elseif ($_POST['drp_action'] == '4') {
            /* change snmp options */
            print "\t<tr>\n\t\t\t\t\t<td colspan='2' class='textArea'>\n\t\t\t\t\t\t<p>To change SNMP parameters for the following Device(s), check the box next to the fields\n\t\t\t\t\t\tyou want to update, fill in the new value, and click \"Continue\".</p>\n\t\t\t\t\t\t<p><ul>" . $host_list . '</ul></p>
					</td>
					</tr>';
            $form_array = array();
            while (list($field_name, $field_array) = each($fields_host_edit)) {
                if (preg_match('/^snmp_/', $field_name) || $field_name == 'max_oids') {
                    $form_array += array($field_name => $fields_host_edit[$field_name]);
                    $form_array[$field_name]['value'] = '';
                    $form_array[$field_name]['description'] = '';
                    $form_array[$field_name]['form_id'] = 0;
                    $form_array[$field_name]['sub_checkbox'] = array('name' => 't_' . $field_name, 'friendly_name' => 'Update this Field', 'value' => '');
                }
            }
            draw_edit_form(array('config' => array('no_form_tag' => true), 'fields' => $form_array));
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Change Device(s) SNMP Options'>";
        } elseif ($_POST['drp_action'] == '6') {
            /* change availability options */
            print "\t<tr>\n\t\t\t\t\t<td colspan='2' class='textArea'>\n\t\t\t\t\t\t<p>To change Availability parameters for the following Device(s), check the box next to the fields\n\t\t\t\t\t\tyou want to update, fill in the new value, and click \"Continue\".</p>\n\t\t\t\t\t\t<p><ul>" . $host_list . '</ul></p>
					</td>
					</tr>';
            $form_array = array();
            while (list($field_name, $field_array) = each($fields_host_edit)) {
                if (preg_match('/(availability_method|ping_method|ping_port)/', $field_name)) {
                    $form_array += array($field_name => $fields_host_edit[$field_name]);
                    $form_array[$field_name]['value'] = '';
                    $form_array[$field_name]['description'] = '';
                    $form_array[$field_name]['form_id'] = 0;
                    $form_array[$field_name]['sub_checkbox'] = array('name' => 't_' . $field_name, 'friendly_name' => 'Update this Field', 'value' => '');
                }
            }
            draw_edit_form(array('config' => array('no_form_tag' => true), 'fields' => $form_array));
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Change Device(s) Availability Options'>";
        } elseif ($_POST['drp_action'] == '5') {
            /* Clear Statisitics for Selected Devices */
            print "\t<tr>\n\t\t\t\t\t<td colspan='2' class='textArea'>\n\t\t\t\t\t\t<p>To clear the counters for the following Device(s), press the \"Continue\" button below.</p>\n\t\t\t\t\t\t<p><ul>" . $host_list . '</ul></p>
					</td>
					</tr>';
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Clear Statistics on Device(s)'>";
        } elseif ($_POST['drp_action'] == '1') {
            /* delete */
            print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>When you click \"Continue\" the following Device(s) will be deleted.</p>\n\t\t\t\t\t\t<p><ul>" . $host_list . '</ul></p>';
            form_radio_button('delete_type', '2', '1', 'Leave all Graph(s) and Data Source(s) untouched.  Data Source(s) will be disabled however.', '1');
            print '<br>';
            form_radio_button('delete_type', '2', '2', 'Delete all associated <strong>Graph(s)</strong> and <strong>Data Source(s)</strong>.', '1');
            print '<br>';
            print "</td></tr>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t";
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Delete Device(s)'>";
        } elseif (preg_match('/^tr_([0-9]+)$/', $_POST['drp_action'], $matches)) {
            /* place on tree */
            print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Device(s) will be placed under the branch selected\n\t\t\t\t\t\tbelow.</p>\n\t\t\t\t\t\t<p><ul>" . $host_list . '</ul></p>
						<p><strong>Destination Branch:</strong><br>';
            grow_dropdown_tree($matches[1], '0', 'tree_item_id', '0');
            print "</p>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n\n\t\t\t\t";
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Place Device(s) on Tree'>";
        } else {
            $save['drp_action'] = $_POST['drp_action'];
            $save['host_list'] = $host_list;
            $save['host_array'] = isset($host_array) ? $host_array : array();
            api_plugin_hook_function('device_action_prepare', $save);
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue'>";
        }
    } else {
        print "<tr><td class='even'><span class='textError'>You must select at least one device.</span></td></tr>\n";
        $save_html = "<input type='button' value='Return' onClick='window.history.back()'>";
    }
    print "\t<tr>\n\t\t\t<td colspan='2' align='right' class='saveRow'>\n\t\t\t\t<input type='hidden' name='action' value='actions'>\n\t\t\t\t<input type='hidden' name='selected_items' value='" . (isset($host_array) ? serialize($host_array) : '') . "'>\n\t\t\t\t<input type='hidden' name='drp_action' value='" . $_POST['drp_action'] . "'>\n\t\t\t\t{$save_html}\n\t\t\t</td>\n\t\t</tr>\n\t\t";
    html_end_box();
    bottom_footer();
}
Example #2
0
/* issue warnings and start message if applicable */
print "WARNING: Do not interrupt this script.  Rebuilding the Poller Cache can take quite some time\n";
debug("There are '" . sizeof($poller_data) . "' data source elements to update.");
/* start rebuilding the poller cache */
if (sizeof($poller_data) > 0) {
    foreach ($poller_data as $data) {
        if (!$debug) {
            print ".";
        }
        $local_data_ids[] = $data["id"];
        $poller_items = array_merge($poller_items, update_poller_cache($data["id"]));
        debug("Data Source Item '{$current_ds}' of '{$total_ds}' updated");
        $current_ds++;
    }
    if (sizeof($local_data_ids)) {
        poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
    }
}
if (!$debug) {
    print "\n";
}
/* poller cache rebuilt, restore runtime parameters */
ini_set("max_execution_time", $max_execution);
/*	display_help - displays the usage of the function */
function display_help()
{
    $version = db_fetch_cell('SELECT cacti FROM version');
    echo "Rebuild Poller Cache Utility, Version {$version}, " . COPYRIGHT_YEARS . "\n\n";
    echo "usage: rebuild_poller_cache.php [--host-id=ID] [-d | --debug] [-h | --help | -v | --version]\n\n";
    echo "-d | --debug  - Display verbose output during execution\n";
    echo "-v --version  - Display this help message\n";
Example #3
0
function push_out_device($device_id, $local_data_id = 0, $data_template_id = 0) {
	/* ok here's the deal: first we need to find every data source that uses this device.
	then we go through each of those data sources, finding each one using a data input method
	with "special fields". if we find one, fill it will the data here from this device */
	/* setup the poller items array */
	$poller_items   = array();
	$local_data_ids = array();
	$devices          = array();
	$sql_where      = "";

	/* setup the sql where, and if using a device, get it's device information */
	if ($device_id != 0) {
		/* get all information about this device so we can write it to the data source */
		$devices[$device_id] = db_fetch_row("select * from device where id=$device_id");

		$sql_where .= " AND data_local.device_id=$device_id";
	}

	/* sql where fom local_data_id */
	if ($local_data_id != 0) {
		$sql_where .= " AND data_local.id=$local_data_id";
	}

	/* sql where fom data_template_id */
	if ($data_template_id != 0) {
		$sql_where .= " AND data_template_data.data_template_id=$data_template_id";
	}

	$data_sources = db_fetch_assoc("SELECT
		data_template_data.id,
		data_template_data.data_input_id,
		data_template_data.local_data_id,
		data_template_data.local_data_template_data_id,
		data_local.device_id
		FROM (data_local, data_template_data)
		WHERE data_local.id=data_template_data.local_data_id
		AND data_template_data.data_input_id>0
		$sql_where");

	/* loop through each matching data source */
	if (sizeof($data_sources) > 0) {
	foreach ($data_sources as $data_source) {
		/* set the device information */
		if (!isset($devices[$data_source["device_id"]])) {
			$devices[$data_source["device_id"]] = db_fetch_row("select * from device where id=" . $data_source["device_id"]);
		}
		$device = $devices[$data_source["device_id"]];

		/* get field information from the data template */
		if (!isset($template_fields{$data_source["local_data_template_data_id"]})) {
			$template_fields{$data_source["local_data_template_data_id"]} = db_fetch_assoc("select
				data_input_data.value,
				data_input_data.t_value,
				data_input_fields.id,
				data_input_fields.type_code
				from data_input_fields left join data_input_data
				on (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_source["local_data_template_data_id"] . ")
				where data_input_fields.data_input_id=" . $data_source["data_input_id"] . "
				and (data_input_data.t_value='' or data_input_data.t_value is null)
				and data_input_fields.input_output='in'");
		}

		reset($template_fields{$data_source["local_data_template_data_id"]});

		/* loop through each field contained in the data template and push out a device value if:
		 - the field is a valid "device field"
		 - the value of the field is empty
		 - the field is set to 'templated' */
		if (sizeof($template_fields{$data_source["local_data_template_data_id"]})) {
		foreach ($template_fields{$data_source["local_data_template_data_id"]} as $template_field) {
			if ((preg_match('/^' . VALID_HOST_FIELDS . '$/i', $template_field["type_code"])) && ($template_field["value"] == "") && ($template_field["t_value"] == "")) {
				db_execute("replace into data_input_data (data_input_field_id,data_template_data_id,value) values (" . $template_field["id"] . "," . $data_source["id"] . ",'" . $device{$template_field["type_code"]} . "')");
			}
		}
		}

		/* flag an update to the poller cache as well */
		$local_data_ids[] = $data_source["local_data_id"];
		$poller_items     = array_merge($poller_items, update_poller_cache($data_source["local_data_id"]));
	}
	}

	if (sizeof($local_data_ids)) {
		poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
	}
}
Example #4
0
function form_actions()
{
    /* modify for multi user start */
    if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
        $rows = db_fetch_assoc("\r\n            SELECT host.id FROM host\r\n                INNER JOIN user_auth_perms ON host.id = user_auth_perms.item_id AND user_auth_perms.user_id = '" . $_SESSION["sess_user_id"] . "' AND user_auth_perms.type = '3'");
        foreach ($rows as $row) {
            $hosts[] = $row["id"];
        }
    }
    /* modify for multi user end */
    global $colors, $device_actions, $fields_host_edit;
    /* if we are to save this form, instead of display it */
    if (isset($_POST["selected_items"])) {
        $selected_items = unserialize(stripslashes($_POST["selected_items"]));
        /* modify for multi user start */
        if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
            for ($i = 0; $i < count($selected_items); $i++) {
                if (!in_array($selected_items[$i], $hosts)) {
                    access_denied();
                }
            }
        }
        /* modify for multi user end */
        if ($_POST["drp_action"] == "2") {
            /* Enable Selected Devices */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                /* modify for multi user start */
                if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
                    if (db_fetch_cell("SELECT id FROM host WHERE id = '" . $selected_items[$i] . "' AND disabled = 'ps'")) {
                        continue;
                    }
                }
                /* modify for multi user end */
                db_execute("update host set disabled='' where id='" . $selected_items[$i] . "'");
                /* update poller cache */
                $data_sources = db_fetch_assoc("select id from data_local where host_id='" . $selected_items[$i] . "'");
                $poller_items = array();
                if (sizeof($data_sources) > 0) {
                    foreach ($data_sources as $data_source) {
                        $local_data_ids[] = $data_source["id"];
                        $poller_items = array_merge($poller_items, update_poller_cache($data_source["id"]));
                    }
                }
                poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
            }
        } elseif ($_POST["drp_action"] == "3") {
            /* Disable Selected Devices */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                /* modify for multi user start */
                if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
                    if (db_fetch_cell("SELECT id FROM host WHERE id = '" . $selected_items[$i] . "' AND disabled = 'ps'")) {
                        continue;
                    }
                }
                /* modify for multi user end */
                db_execute("update host set disabled='on' where id='" . $selected_items[$i] . "'");
                /* update poller cache */
                db_execute("delete from poller_item where host_id='" . $selected_items[$i] . "'");
                db_execute("delete from poller_reindex where host_id='" . $selected_items[$i] . "'");
            }
        } elseif ($_POST["drp_action"] == "4") {
            /* change snmp options */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                reset($fields_host_edit);
                while (list($field_name, $field_array) = each($fields_host_edit)) {
                    if (isset($_POST["t_{$field_name}"])) {
                        db_execute("update host set {$field_name} = '" . $_POST[$field_name] . "' where id='" . $selected_items[$i] . "'");
                    }
                }
                push_out_host($selected_items[$i]);
            }
        } elseif ($_POST["drp_action"] == "5") {
            /* Clear Statisitics for Selected Devices */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                db_execute("update host set min_time = '9.99999', max_time = '0', cur_time = '0',\tavg_time = '0',\r\n\t\t\t\t\t\ttotal_polls = '0', failed_polls = '0',\tavailability = '100.00'\r\n\t\t\t\t\t\twhere id = '" . $selected_items[$i] . "'");
            }
        } elseif ($_POST["drp_action"] == "6") {
            /* change availability options */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                reset($fields_host_edit);
                while (list($field_name, $field_array) = each($fields_host_edit)) {
                    if (isset($_POST["t_{$field_name}"])) {
                        db_execute("update host set {$field_name} = '" . $_POST[$field_name] . "' where id='" . $selected_items[$i] . "'");
                    }
                }
                push_out_host($selected_items[$i]);
            }
        } elseif ($_POST["drp_action"] == "1") {
            /* delete */
            if (!isset($_POST["delete_type"])) {
                $_POST["delete_type"] = 2;
            }
            $data_sources_to_act_on = array();
            $graphs_to_act_on = array();
            $devices_to_act_on = array();
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                /* ==================================================== */
                $data_sources = db_fetch_assoc("select\r\n\t\t\t\t\tdata_local.id as local_data_id\r\n\t\t\t\t\tfrom data_local\r\n\t\t\t\t\twhere " . array_to_sql_or($selected_items, "data_local.host_id"));
                if (sizeof($data_sources) > 0) {
                    foreach ($data_sources as $data_source) {
                        $data_sources_to_act_on[] = $data_source["local_data_id"];
                    }
                }
                if ($_POST["delete_type"] == 2) {
                    $graphs = db_fetch_assoc("select\r\n\t\t\t\t\t\tgraph_local.id as local_graph_id\r\n\t\t\t\t\t\tfrom graph_local\r\n\t\t\t\t\t\twhere " . array_to_sql_or($selected_items, "graph_local.host_id"));
                    if (sizeof($graphs) > 0) {
                        foreach ($graphs as $graph) {
                            $graphs_to_act_on[] = $graph["local_graph_id"];
                        }
                    }
                }
                $devices_to_act_on[] = $selected_items[$i];
            }
            switch ($_POST["delete_type"]) {
                case '1':
                    /* leave graphs and data_sources in place, but disable the data sources */
                    api_data_source_disable_multi($data_sources_to_act_on);
                    api_plugin_hook_function('data_source_remove', $data_sources_to_act_on);
                    break;
                case '2':
                    /* delete graphs/data sources tied to this device */
                    api_data_source_remove_multi($data_sources_to_act_on);
                    api_graph_remove_multi($graphs_to_act_on);
                    api_plugin_hook_function('graphs_remove', $graphs_to_act_on);
                    break;
            }
            api_device_remove_multi($devices_to_act_on);
            /* modify for multi user start */
            if ($_SESSION["permission"] <= ACCESS_ADMINISTRATOR) {
                foreach ($devices_to_act_on as $device_id) {
                    db_execute("DELETE FROM user_auth_perms WHERE type = 3 AND user_id = '" . $_SESSION["sess_user_id"] . "' AND item_id = '{$device_id}'");
                }
            }
            /* modify for multi user end */
            api_plugin_hook_function('device_remove', $devices_to_act_on);
        } elseif (preg_match("/^tr_([0-9]+)\$/", $_POST["drp_action"], $matches)) {
            /* place on tree */
            /* modify for multi user start */
            if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
                input_validate_input_number(get_request_var_post("tree_id"));
                if ($_POST["tree_id"] != $_SESSION["public_tree_id"]) {
                    access_denied();
                }
            }
            /* modify for multi user end */
            for ($i = 0; $i < count($selected_items); $i++) {
                /* ================= input validation ================= */
                input_validate_input_number($selected_items[$i]);
                input_validate_input_number(get_request_var_post("tree_id"));
                input_validate_input_number(get_request_var_post("tree_item_id"));
                /* ==================================================== */
                api_tree_item_save(0, $_POST["tree_id"], TREE_ITEM_TYPE_HOST, $_POST["tree_item_id"], "", 0, read_graph_config_option("default_rra_id"), $selected_items[$i], 1, 1, false);
            }
            /* modify for multi user start */
            if (isset($_SESSION['dhtml_tree'])) {
                unset($_SESSION['dhtml_tree']);
            }
            /* modify for multi user end */
        } else {
            api_plugin_hook_function('device_action_execute', $_POST['drp_action']);
        }
        header("Location: host.php");
        exit;
    }
    /* setup some variables */
    $host_list = "";
    $i = 0;
    /* loop through each of the host templates selected on the previous page and get more info about them */
    while (list($var, $val) = each($_POST)) {
        if (preg_match("/^chk_([0-9]+)\$/", $var, $matches)) {
            /* ================= input validation ================= */
            input_validate_input_number($matches[1]);
            /* ==================================================== */
            /* modify for multi user start */
            if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
                if (!in_array($matches[1], $hosts)) {
                    access_denied();
                }
            }
            /* modify for multi user start */
            $host_list .= "<li>" . htmlspecialchars(db_fetch_cell("select description from host where id=" . $matches[1])) . "<br>";
            $host_array[$i] = $matches[1];
            $i++;
        }
    }
    include_once "./include/top_header.php";
    /* add a list of tree names to the actions dropdown */
    add_tree_names_to_actions_array();
    html_start_box("<strong>" . $device_actions[get_request_var_post("drp_action")] . "</strong>", "60%", $colors["header_panel"], "3", "center", "");
    print "<form action='host.php' autocomplete='off' method='post'>\n";
    if (isset($host_array) && sizeof($host_array)) {
        if ($_POST["drp_action"] == "2") {
            /* Enable Devices */
            print "\t<tr>\r\n\t\t\t\t\t<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>To enable the following Device(s), click \"Continue\".</p>\r\n\t\t\t\t\t\t<p><ul>" . $host_list . "</ul></p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>";
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Enable Device(s)'>";
        } elseif ($_POST["drp_action"] == "3") {
            /* Disable Devices */
            print "\t<tr>\r\n\t\t\t\t\t<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>To disable the following Device(s), click \"Continue\".</p>\r\n\t\t\t\t\t\t<p><ul>" . $host_list . "</ul></p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>";
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Disable Device(s)'>";
        } elseif ($_POST["drp_action"] == "4") {
            /* change snmp options */
            print "\t<tr>\r\n\t\t\t\t\t<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>To change SNMP parameters for the following Device(s), check the box next to the fields\r\n\t\t\t\t\t\tyou want to update, fill in the new value, and click \"Continue\".</p>\r\n\t\t\t\t\t\t<p><ul>" . $host_list . "</ul></p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>";
            $form_array = array();
            while (list($field_name, $field_array) = each($fields_host_edit)) {
                if (preg_match("/^snmp_/", $field_name) || $field_name == "max_oids") {
                    $form_array += array($field_name => $fields_host_edit[$field_name]);
                    $form_array[$field_name]["value"] = "";
                    $form_array[$field_name]["description"] = "";
                    $form_array[$field_name]["form_id"] = 0;
                    $form_array[$field_name]["sub_checkbox"] = array("name" => "t_" . $field_name, "friendly_name" => "Update this Field", "value" => "");
                }
            }
            /* modify for multi user start */
            if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
                $form_array["snmp_timeout"]["method"] = "hidden";
                $form_array["max_oids"]["method"] = "hidden";
            }
            /* modify for multi user end */
            draw_edit_form(array("config" => array("no_form_tag" => true), "fields" => $form_array));
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Change Device(s) SNMP Options'>";
        } elseif ($_POST["drp_action"] == "6") {
            /* change availability options */
            print "\t<tr>\r\n\t\t\t\t\t<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>To change Availability parameters for the following Device(s), check the box next to the fields\r\n\t\t\t\t\t\tyou want to update, fill in the new value, and click \"Continue\".</p>\r\n\t\t\t\t\t\t<p><ul>" . $host_list . "</ul></p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>";
            $form_array = array();
            while (list($field_name, $field_array) = each($fields_host_edit)) {
                if (preg_match("/(availability_method|ping_method|ping_port)/", $field_name)) {
                    $form_array += array($field_name => $fields_host_edit[$field_name]);
                    $form_array[$field_name]["value"] = "";
                    $form_array[$field_name]["description"] = "";
                    $form_array[$field_name]["form_id"] = 0;
                    $form_array[$field_name]["sub_checkbox"] = array("name" => "t_" . $field_name, "friendly_name" => "Update this Field", "value" => "");
                }
            }
            draw_edit_form(array("config" => array("no_form_tag" => true), "fields" => $form_array));
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Change Device(s) Availability Options'>";
        } elseif ($_POST["drp_action"] == "5") {
            /* Clear Statisitics for Selected Devices */
            print "\t<tr>\r\n\t\t\t\t\t<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>To clear the counters for the following Device(s), press the \"Continue\" button below.</p>\r\n\t\t\t\t\t\t<p><ul>" . $host_list . "</ul></p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>";
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Clear Statistics on Device(s)'>";
        } elseif ($_POST["drp_action"] == "1") {
            /* delete */
            print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>When you click \"Continue\" the following Device(s) will be deleted.</p>\r\n\t\t\t\t\t\t<p><ul>" . $host_list . "</ul></p>";
            /* modify for multi user start */
            if ($_SESSION["permission"] == ACCESS_ADMINISTRATOR) {
                form_radio_button("delete_type", "2", "1", "Leave all Graph(s) and Data Source(s) untouched.  Data Source(s) will be disabled however.", "1");
                print "<br>";
            }
            /* modify for multi user end */
            form_radio_button("delete_type", "2", "2", "Delete all associated <strong>Graph(s)</strong> and <strong>Data Source(s)</strong>.", "1");
            print "<br>";
            print "</td></tr>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t";
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Delete Device(s)'>";
        } elseif (preg_match("/^tr_([0-9]+)\$/", $_POST["drp_action"], $matches)) {
            /* place on tree */
            print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Device(s) will be placed under the branch selected\r\n\t\t\t\t\t\tbelow.</p>\r\n\t\t\t\t\t\t<p><ul>" . $host_list . "</ul></p>\r\n\t\t\t\t\t\t<p><strong>Destination Branch:</strong><br>";
            grow_dropdown_tree($matches[1], "tree_item_id", "0");
            print "</p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n\r\n\t\t\t\t";
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Place Device(s) on Tree'>";
        } else {
            $save['drp_action'] = $_POST['drp_action'];
            $save['host_list'] = $host_list;
            $save['host_array'] = isset($host_array) ? $host_array : array();
            api_plugin_hook_function('device_action_prepare', $save);
            $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue'>";
        }
    } else {
        print "<tr><td bgcolor='#" . $colors["form_alternate1"] . "'><span class='textError'>You must select at least one device.</span></td></tr>\n";
        $save_html = "<input type='button' value='Return' onClick='window.history.back()'>";
    }
    print "\t<tr>\r\n\t\t\t<td colspan='2' align='right' bgcolor='#eaeaea'>\r\n\t\t\t\t<input type='hidden' name='action' value='actions'>\r\n\t\t\t\t<input type='hidden' name='selected_items' value='" . (isset($host_array) ? serialize($host_array) : '') . "'>\r\n\t\t\t\t<input type='hidden' name='drp_action' value='" . $_POST["drp_action"] . "'>\r\n\t\t\t\t{$save_html}\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t\t";
    html_end_box();
    include_once "./include/bottom_footer.php";
}
Example #5
0
function form_actions() {
	global $colors, $device_actions, $fields_host_edit;

	/* if we are to save this form, instead of display it */
	if (isset($_POST["selected_items"])) {
		$selected_items = unserialize(stripslashes($_POST["selected_items"]));

		if ($_POST["drp_action"] == "2") { /* Enable Selected Devices */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				db_execute("update host set disabled='' where id='" . $selected_items[$i] . "'");

				/* update poller cache */
				$data_sources = db_fetch_assoc("select id from data_local where host_id='" . $selected_items[$i] . "'");
				$poller_items = array();

				if (sizeof($data_sources) > 0) {
					foreach ($data_sources as $data_source) {
						$local_data_ids[] = $data_source["id"];
						$poller_items     = array_merge($poller_items, update_poller_cache($data_source["id"]));
					}
				}

				poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
			}
		}elseif ($_POST["drp_action"] == "3") { /* Disable Selected Devices */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				db_execute("update host set disabled='on' where id='" . $selected_items[$i] . "'");

				/* update poller cache */
				db_execute("delete from poller_item where host_id='" . $selected_items[$i] . "'");
				db_execute("delete from poller_reindex where host_id='" . $selected_items[$i] . "'");
			}
		}elseif ($_POST["drp_action"] == "4") { /* change snmp options */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				reset($fields_host_edit);
				while (list($field_name, $field_array) = each($fields_host_edit)) {
					if (isset($_POST["t_$field_name"])) {
						db_execute("update host set $field_name = '" . $_POST[$field_name] . "' where id='" . $selected_items[$i] . "'");
					}
				}

				push_out_host($selected_items[$i]);
			}
		}elseif ($_POST["drp_action"] == "5") { /* Clear Statisitics for Selected Devices */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				db_execute("update host set min_time = '9.99999', max_time = '0', cur_time = '0',	avg_time = '0',
						total_polls = '0', failed_polls = '0',	availability = '100.00'
						where id = '" . $selected_items[$i] . "'");
			}
		}elseif ($_POST["drp_action"] == "6") { /* change availability options */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				reset($fields_host_edit);
				while (list($field_name, $field_array) = each($fields_host_edit)) {
					if (isset($_POST["t_$field_name"])) {
						db_execute("update host set $field_name = '" . $_POST[$field_name] . "' where id='" . $selected_items[$i] . "'");
					}
				}

				push_out_host($selected_items[$i]);
			}
		}elseif ($_POST["drp_action"] == "1") { /* delete */
			if (!isset($_POST["delete_type"])) { $_POST["delete_type"] = 2; }

			$data_sources_to_act_on = array();
			$graphs_to_act_on       = array();
			$devices_to_act_on      = array();

			for ($i=0; $i<count($selected_items); $i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				$data_sources = db_fetch_assoc("select
					data_local.id as local_data_id
					from data_local
					where " . array_to_sql_or($selected_items, "data_local.host_id"));

				if (sizeof($data_sources) > 0) {
				foreach ($data_sources as $data_source) {
					$data_sources_to_act_on[] = $data_source["local_data_id"];
				}
				}

				if ($_POST["delete_type"] == 2) {
					$graphs = db_fetch_assoc("select
						graph_local.id as local_graph_id
						from graph_local
						where " . array_to_sql_or($selected_items, "graph_local.host_id"));

					if (sizeof($graphs) > 0) {
					foreach ($graphs as $graph) {
						$graphs_to_act_on[] = $graph["local_graph_id"];
					}
					}
				}

				$devices_to_act_on[] = $selected_items[$i];
			}

			switch ($_POST["delete_type"]) {
				case '1': /* leave graphs and data_sources in place, but disable the data sources */
					api_data_source_disable_multi($data_sources_to_act_on);

					break;
				case '2': /* delete graphs/data sources tied to this device */
					api_data_source_remove_multi($data_sources_to_act_on);

					api_graph_remove_multi($graphs_to_act_on);

					break;
			}

			api_device_remove_multi($devices_to_act_on);
		}elseif (ereg("^tr_([0-9]+)$", $_POST["drp_action"], $matches)) { /* place on tree */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				input_validate_input_number(get_request_var_post("tree_id"));
				input_validate_input_number(get_request_var_post("tree_item_id"));
				/* ==================================================== */

				api_tree_item_save(0, $_POST["tree_id"], TREE_ITEM_TYPE_HOST, $_POST["tree_item_id"], "", 0, read_graph_config_option("default_rra_id"), $selected_items[$i], 1, 1, false);
			}
		} else {
			api_plugin_hook_function('device_action_execute', $_POST['drp_action']); 
		}

		header("Location: host.php");
		exit;
	}

	/* setup some variables */
	$host_list = ""; $i = 0;

	/* loop through each of the host templates selected on the previous page and get more info about them */
	while (list($var,$val) = each($_POST)) {
		if (ereg("^chk_([0-9]+)$", $var, $matches)) {
			/* ================= input validation ================= */
			input_validate_input_number($matches[1]);
			/* ==================================================== */

			$host_list .= "<li>" . db_fetch_cell("select description from host where id=" . $matches[1]) . "<br>";
			$host_array[$i] = $matches[1];
		}

		$i++;
	}

	include_once("./include/top_header.php");

	/* add a list of tree names to the actions dropdown */
	add_tree_names_to_actions_array();

	html_start_box("<strong>" . $device_actions{$_POST["drp_action"]} . "</strong>", "60%", $colors["header_panel"], "3", "center", "");

	print "<form action='host.php' method='post'>\n";

	if ($_POST["drp_action"] == "2") { /* Enable Devices */
		print "	<tr>
				<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
					<p>To enable the following devices, press the \"yes\" button below.</p>
					<p>$host_list</p>
				</td>
				</tr>";
	}elseif ($_POST["drp_action"] == "3") { /* Disable Devices */
		print "	<tr>
				<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
					<p>To disable the following devices, press the \"yes\" button below.</p>
					<p>$host_list</p>
				</td>
				</tr>";
	}elseif ($_POST["drp_action"] == "4") { /* change snmp options */
		print "	<tr>
				<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
					<p>To change SNMP parameters for the following devices, check the box next to the fields
					you want to update, fill in the new value, and click Save.</p>
					<p>$host_list</p>
				</td>
				</tr>";
				$form_array = array();
				while (list($field_name, $field_array) = each($fields_host_edit)) {
					if ((ereg("^snmp_", $field_name)) ||
						($field_name == "max_oids")) {
						$form_array += array($field_name => $fields_host_edit[$field_name]);

						$form_array[$field_name]["value"] = "";
						$form_array[$field_name]["description"] = "";
						$form_array[$field_name]["form_id"] = 0;
						$form_array[$field_name]["sub_checkbox"] = array(
							"name" => "t_" . $field_name,
							"friendly_name" => "Update this Field",
							"value" => ""
							);
					}
				}

				draw_edit_form(
					array(
						"config" => array("no_form_tag" => true),
						"fields" => $form_array
						)
					);
	}elseif ($_POST["drp_action"] == "6") { /* change availability options */
		print "	<tr>
				<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
					<p>To change SNMP parameters for the following devices, check the box next to the fields
					you want to update, fill in the new value, and click Save.</p>
					<p>$host_list</p>
				</td>
				</tr>";
				$form_array = array();
				while (list($field_name, $field_array) = each($fields_host_edit)) {
					if (ereg("(availability_method|ping_method|ping_port)", $field_name)) {
						$form_array += array($field_name => $fields_host_edit[$field_name]);

						$form_array[$field_name]["value"] = "";
						$form_array[$field_name]["description"] = "";
						$form_array[$field_name]["form_id"] = 0;
						$form_array[$field_name]["sub_checkbox"] = array(
							"name" => "t_" . $field_name,
							"friendly_name" => "Update this Field",
							"value" => ""
							);
					}
				}

				draw_edit_form(
					array(
						"config" => array("no_form_tag" => true),
						"fields" => $form_array
						)
					);
	}elseif ($_POST["drp_action"] == "5") { /* Clear Statisitics for Selected Devices */
		print "	<tr>
				<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
					<p>To clear the counters for the following devices, press the \"yes\" button below.</p>
					<p>$host_list</p>
				</td>
				</tr>";
	}elseif ($_POST["drp_action"] == "1") { /* delete */
		print "	<tr>
				<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
					<p>Are you sure you want to delete the following devices?</p>
					<p>$host_list</p>";
					form_radio_button("delete_type", "2", "1", "Leave all graphs and data sources untouched.  Data sources will be disabled however.", "1"); print "<br>";
					form_radio_button("delete_type", "2", "2", "Delete all associated <strong>graphs</strong> and <strong>data sources</strong>.", "1"); print "<br>";
					print "</td></tr>
				</td>
			</tr>\n
			";
	}elseif (ereg("^tr_([0-9]+)$", $_POST["drp_action"], $matches)) { /* place on tree */
		print "	<tr>
				<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
					<p>When you click save, the following hosts will be placed under the branch selected
					below.</p>
					<p>$host_list</p>
					<p><strong>Destination Branch:</strong><br>"; grow_dropdown_tree($matches[1], "tree_item_id", "0"); print "</p>
				</td>
			</tr>\n
			<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n
			";
	} else {
		$save['drp_action'] = $_POST['drp_action'];
		$save['host_list'] = $host_list;
		$save['host_array'] = (isset($host_array)? $host_array : array());
		api_plugin_hook_function('device_action_prepare', $save);
	}

	if (!isset($host_array)) {
		print "<tr><td bgcolor='#" . $colors["form_alternate1"]. "'><span class='textError'>You must select at least one device.</span></td></tr>\n";
		$save_html = "";
	}else{
		$save_html = "<input type='image' src='images/button_yes.gif' alt='Save' align='absmiddle'>";
	}

	print "	<tr>
			<td colspan='2' align='right' bgcolor='#eaeaea'>
				<input type='hidden' name='action' value='actions'>
				<input type='hidden' name='selected_items' value='" . (isset($host_array) ? serialize($host_array) : '') . "'>
				<input type='hidden' name='drp_action' value='" . $_POST["drp_action"] . "'>
				<a href='host.php'><img src='images/button_no.gif' alt='Cancel' align='absmiddle' border='0'></a>
				$save_html
			</td>
		</tr>
		";

	html_end_box();

	include_once("./include/bottom_footer.php");
}
Example #6
0
/** for a given data template, update all input data and the poller cache
 * @param int $host_id - id of host, if any
 * @param int $local_data_id - id of a single data source, if any
 * @param int $data_template_id - id of data template
 * works on table data_input_data and poller cache
 */
function push_out_host($host_id, $local_data_id = 0, $data_template_id = 0)
{
    /* ok here's the deal: first we need to find every data source that uses this host.
    	then we go through each of those data sources, finding each one using a data input method
    	with "special fields". if we find one, fill it will the data here FROM this host */
    /* setup the poller items array */
    $poller_items = array();
    $local_data_ids = array();
    $hosts = array();
    $sql_where = "";
    /* setup the sql where, and if using a host, get it's host information */
    if ($host_id != 0) {
        /* get all information about this host so we can write it to the data source */
        $hosts[$host_id] = db_fetch_row("SELECT id AS host_id, host.* FROM host WHERE id={$host_id}");
        $sql_where .= " AND data_local.host_id={$host_id}";
    }
    /* sql WHERE for local_data_id */
    if ($local_data_id != 0) {
        $sql_where .= " AND data_local.id={$local_data_id}";
    }
    /* sql WHERE for data_template_id */
    if ($data_template_id != 0) {
        $sql_where .= " AND data_template_data.data_template_id={$data_template_id}";
    }
    $data_sources = db_fetch_assoc("SELECT\n\t\tdata_template_data.id,\n\t\tdata_template_data.data_input_id,\n\t\tdata_template_data.local_data_id,\n\t\tdata_template_data.local_data_template_data_id,\n\t\tdata_local.host_id\n\t\tFROM (data_local, data_template_data)\n\t\tWHERE data_local.id=data_template_data.local_data_id\n\t\tAND data_template_data.data_input_id>0\n\t\t{$sql_where}");
    /* loop through each matching data source */
    if (sizeof($data_sources) > 0) {
        foreach ($data_sources as $data_source) {
            /* set the host information */
            if (!isset($hosts[$data_source["host_id"]])) {
                $hosts[$data_source["host_id"]] = db_fetch_row("SELECT * FROM host WHERE id=" . $data_source["host_id"]);
            }
            $host = $hosts[$data_source["host_id"]];
            /* get field information FROM the data template */
            if (!isset($template_fields[$data_source["local_data_template_data_id"]])) {
                $template_fields[$data_source["local_data_template_data_id"]] = db_fetch_assoc("SELECT\n\t\t\t\tdata_input_data.value,\n\t\t\t\tdata_input_data.t_value,\n\t\t\t\tdata_input_fields.id,\n\t\t\t\tdata_input_fields.type_code\n                FROM data_input_fields \n                LEFT JOIN data_input_data\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id AND data_input_data.data_template_data_id=" . $data_source["local_data_template_data_id"] . ")\n\t\t\t\tWHERE data_input_fields.data_input_id=" . $data_source["data_input_id"] . "\n\t\t\t\tAND (data_input_data.t_value='' OR data_input_data.t_value is null)\n\t\t\t\tAND data_input_fields.input_output='in'");
            }
            reset($template_fields[$data_source["local_data_template_data_id"]]);
            /* loop through each field contained in the data template and push out a host value if:
            		 - the field is a valid "host field"
            		 - the value of the field is empty
            		 - the field is set to 'templated' */
            if (sizeof($template_fields[$data_source["local_data_template_data_id"]])) {
                foreach ($template_fields[$data_source["local_data_template_data_id"]] as $template_field) {
                    if (preg_match('/^' . VALID_HOST_FIELDS . '$/i', $template_field["type_code"]) && $template_field["value"] == "" && $template_field["t_value"] == "") {
                        db_execute_prepared('REPLACE INTO data_input_data (data_input_field_id, data_template_data_id, value) VALUES (?, ?, ?)', array($template_field['id'], $data_source['id'], $host[$template_field['type_code']]));
                    }
                }
            }
            /* flag an update to the poller cache as well */
            $local_data_ids[] = $data_source["local_data_id"];
            $poller_items = array_merge($poller_items, update_poller_cache($data_source["local_data_id"]));
        }
    }
    if (sizeof($local_data_ids)) {
        poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
    }
}
Example #7
0
function update_host_disabled($host_id, $disabled)
{
    db_execute("UPDATE host SET disabled = '{$disabled}' WHERE id = '{$host_id}'");
    /* update poller cache */
    if ($disabled === "") {
        $data_sources = db_fetch_assoc("SELECT id FROM data_local WHERE host_id = '{$host_id}'");
        $poller_items = array();
        include dirname(__FILE__) . "/include/global.php";
        include_once $config["base_path"] . "/lib/utility.php";
        if (sizeof($data_sources) > 0) {
            foreach ($data_sources as $data_source) {
                $local_data_ids[] = $data_source["id"];
                $poller_items = array_merge($poller_items, update_poller_cache($data_source["id"]));
            }
        }
        poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
    } else {
        db_execute("DELETE FROM poller_item WHERE host_id = '{$host_id}'");
        db_execute("DELETE FROM poller_reindex WHERE host_id = '{$host_id}'");
    }
}
Example #8
0
function api_device_form_actions() {
	global $colors;
	require_once(CACTI_BASE_PATH . "/lib/device/device_info.php");
	require(CACTI_BASE_PATH . "/include/graph_tree/graph_tree_arrays.php");

	$fields_device_edit = device_form_list();
	$fields_device_edit_availability = device_availability_form_list();
	/* if we are to save this form, instead of display it */
	if (isset($_POST["selected_items"])) {
		$selected_items = unserialize(stripslashes($_POST["selected_items"]));

		if (get_request_var_post("drp_action") === DEVICE_ACTION_ENABLE) { /* Enable Selected Devices */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				db_execute("update device set disabled='' where id='" . $selected_items[$i] . "'");

				/* update poller cache */
				$data_sources = db_fetch_assoc("select id from data_local where device_id='" . $selected_items[$i] . "'");
				$poller_items = array();

				if (sizeof($data_sources) > 0) {
					foreach ($data_sources as $data_source) {
						$local_data_ids[] = $data_source["id"];
						$poller_items     = array_merge($poller_items, update_poller_cache($data_source["id"]));
					}
				}

				poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
			}
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_DISABLE) { /* Disable Selected Devices */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				db_execute("update device set disabled='on' where id='" . $selected_items[$i] . "'");

				/* update poller cache */
				db_execute("delete from poller_item where device_id='" . $selected_items[$i] . "'");
				db_execute("delete from poller_reindex where device_id='" . $selected_items[$i] . "'");
			}
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CHANGE_SNMP_OPTIONS) { /* change snmp options */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				reset($fields_device_edit);
				while (list($field_name, $field_array) = each($fields_device_edit)) {
					if (isset($_POST["t_$field_name"])) {
						db_execute("update device set $field_name = '" . $_POST[$field_name] . "' where id='" . $selected_items[$i] . "'");
					}
				}

				push_out_device($selected_items[$i]);
			}
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CLEAR_STATISTICS) { /* Clear Statisitics for Selected Devices */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				db_execute("update device set min_time = '9.99999', max_time = '0', cur_time = '0',	avg_time = '0',
						total_polls = '0', failed_polls = '0',	availability = '100.00'
						where id = '" . $selected_items[$i] . "'");
			}
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CHANGE_AVAILABILITY_OPTIONS) { /* change availability options */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				reset($fields_device_edit);
				while (list($field_name, $field_array) = each($fields_device_edit)) {
					if (isset($_POST["t_$field_name"])) {
						db_execute("update device set $field_name = '" . $_POST[$field_name] . "' where id='" . $selected_items[$i] . "'");
					}
				}

				push_out_device($selected_items[$i]);
			}
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CHANGE_POLLER) { /* change poller */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				reset($fields_device_edit);
				while (list($field_name, $field_array) = each($fields_device_edit)) {
					if (isset($_POST["$field_name"])) {
						db_execute("update device set $field_name = '" . $_POST[$field_name] . "' where id='" . $selected_items[$i] . "'");
					}
				}

				push_out_device($selected_items[$i]);
			}
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CHANGE_SITE) { /* change site */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				reset($fields_device_edit);
				while (list($field_name, $field_array) = each($fields_device_edit)) {
					if (isset($_POST["$field_name"])) {
						db_execute("update device set $field_name = '" . $_POST[$field_name] . "' where id='" . $selected_items[$i] . "'");
					}
				}

				push_out_device($selected_items[$i]);
			}
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_DELETE) { /* delete */
			if (!isset($_POST["delete_type"])) { $_POST["delete_type"] = 2; }

			$data_sources_to_act_on = array();
			$graphs_to_act_on       = array();
			$devices_to_act_on      = array();

			for ($i=0; $i<count($selected_items); $i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				/* ==================================================== */

				$data_sources = db_fetch_assoc("select
					data_local.id as local_data_id
					from data_local
					where " . array_to_sql_or($selected_items, "data_local.device_id"));

				if (sizeof($data_sources) > 0) {
				foreach ($data_sources as $data_source) {
					$data_sources_to_act_on[] = $data_source["local_data_id"];
				}
				}

				if (get_request_var_post("delete_type") == 2) {
					$graphs = db_fetch_assoc("select
						graph_local.id as local_graph_id
						from graph_local
						where " . array_to_sql_or($selected_items, "graph_local.device_id"));

					if (sizeof($graphs) > 0) {
					foreach ($graphs as $graph) {
						$graphs_to_act_on[] = $graph["local_graph_id"];
					}
					}
				}

				$devices_to_act_on[] = $selected_items[$i];
			}

			switch (get_request_var_post("delete_type")) {
				case '1': /* leave graphs and data_sources in place, but disable the data sources */
					api_data_source_disable_multi($data_sources_to_act_on);

					break;
				case '2': /* delete graphs/data sources tied to this device */
					api_data_source_remove_multi($data_sources_to_act_on);

					api_graph_remove_multi($graphs_to_act_on);

					break;
			}

			api_device_remove_multi($devices_to_act_on);
		}elseif (preg_match("/^tr_([0-9]+)$/", get_request_var_post("drp_action"), $matches)) { /* place on tree */
			for ($i=0;($i<count($selected_items));$i++) {
				/* ================= input validation ================= */
				input_validate_input_number($selected_items[$i]);
				input_validate_input_number(get_request_var_post("tree_id"));
				input_validate_input_number(get_request_var_post("tree_item_id"));
				/* ==================================================== */

				api_tree_item_save(0, get_request_var_post("tree_id"), TREE_ITEM_TYPE_DEVICE, get_request_var_post("tree_item_id"), "", 0, read_graph_config_option("default_rra_id"), $selected_items[$i], 1, 1, false);
			}
		} else {
			api_plugin_hook_function('device_action_execute', get_request_var_post('drp_action'));
		}

		header("Location: devices.php");
		exit;
	}

	/* setup some variables */
	$device_list = ""; $i = 0; $device_array = array();

	/* loop through each of the device templates selected on the previous page and get more info about them */
	while (list($var,$val) = each($_POST)) {
		if (preg_match("/^chk_([0-9]+)$/", $var, $matches)) {
			/* ================= input validation ================= */
			input_validate_input_number($matches[1]);
			/* ==================================================== */

			$device_list .= "<li>" . db_fetch_cell("select description from device where id=" . $matches[1]) . "<br>";
			$device_array[$i] = $matches[1];
		}

		$i++;
	}

	include_once(CACTI_BASE_PATH . "/include/top_header.php");

	/* add a list of tree names to the actions dropdown */
	$device_actions = array_merge($device_actions, api_tree_add_tree_names_to_actions_array());

	$device_actions[ACTION_NONE] = __("None");

	print "<form method='post' action='" .  basename($_SERVER["PHP_SELF"]) . "' name='device_edit_actions'>\n";
	html_start_box("<strong>" . $device_actions{get_request_var_post("drp_action")} . "</strong>", "60", $colors["header_panel"], "3", "center", "");

	if (sizeof($device_array)) {
		if (get_request_var_post("drp_action") === ACTION_NONE) { /* NONE */
			print "	<tr>
						<td class='textArea'>
							<p>" . __("You did not select a valid action. Please select 'Return' to return to the previous menu.") . "</p>
						</td>
					</tr>\n";
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_ENABLE) { /* Enable Devices */
			print "	<tr>
					<td colspan='2' class='textArea'>
						<p>" . __("To enable the following devices, press the \"yes\" button below.") . "</p>
						<p>$device_list</p>
					</td>
					</tr>";
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_DISABLE) { /* Disable Devices */
			print "	<tr>
					<td colspan='2' class='textArea'>
						<p>" . __("To disable the following devices, press the \"yes\" button below.") . "</p>
						<p>$device_list</p>
					</td>
					</tr>";
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CHANGE_SNMP_OPTIONS) { /* change snmp options */
			print "	<tr>
					<td colspan='2' class='textArea'>
						<p>" . __("To change SNMP parameters for the following devices, check the box next to the fields you want to update, fill in the new value, and click \"yes\".") . "</p>
						<p>$device_list</p>
					</td>
					</tr>";

			$form_array = array();
			while (list($field_name, $field_array) = each($fields_device_edit_availability)) {
				if (preg_match("/(^snmp_|max_oids)/", $field_name)) {
					$form_array += array($field_name => $fields_device_edit_availability[$field_name]);

					$form_array[$field_name]["value"] = "";
					$form_array[$field_name]["form_id"] = 0;
					$form_array[$field_name]["sub_checkbox"] = array(
						"name" => "t_" . $field_name,
						"friendly_name" => __("Update this Field"),
						"value" => ""
						);
				}
			}

			draw_edit_form(
				array(
					"config" => array("no_form_tag" => true),
					"fields" => $form_array
					)
				);
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CHANGE_AVAILABILITY_OPTIONS) { /* change availability options */
			print "	<tr>
					<td colspan='2' class='textArea'>
						<p>" . __("To change availability parameters for the following devices, check the box next to the fields you want to update, fill in the new value, and click yes.") . "</p>
						<p>$device_list</p>
					</td>
					</tr>";

			$form_array = array();
			while (list($field_name, $field_array) = each($fields_device_edit_availability)) {
				if (!preg_match("/(^snmp_|max_oids)/", $field_name)) {
					$form_array += array($field_name => $fields_device_edit_availability[$field_name]);

					$form_array[$field_name]["value"] = "";
					$form_array[$field_name]["form_id"] = 0;
					$form_array[$field_name]["sub_checkbox"] = array(
						"name" => "t_" . $field_name,
						"friendly_name" => __("Update this Field"),
						"value" => ""
						);
				}
			}

			draw_edit_form(
				array(
					"config" => array("no_form_tag" => true),
					"fields" => $form_array
					)
				);
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CLEAR_STATISTICS) { /* Clear Statisitics for Selected Devices */
			print "	<tr>
					<td colspan='2' class='textArea'>
						<p>" . __("To clear the counters for the following devices, press the \"yes\" button below.") . "</p>
						<p>$device_list</p>
					</td>
					</tr>";
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_DELETE) { /* delete */
			print "	<tr>
					<td class='textArea'>
						<p>" . __("Are you sure you want to delete the following devices?") . "</p>
						<p>$device_list</p>";
						form_radio_button("delete_type", "2", "1", __("Leave all graphs and data sources untouched.  Data sources will be disabled however."), "1"); print "<br>";
						form_radio_button("delete_type", "2", "2", __("Delete all associated <strong>graphs</strong> and <strong>data sources</strong>."), "1"); print "<br>";
						print "</td></tr>
					</td>
				</tr>\n
				";
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CHANGE_POLLER) { /* Change Poller */
			print "	<tr>
					<td colspan='2' class='textArea'>
						<p>" . __("Select the new poller below for the devices(s) below and select 'yes' to continue, or 'no' to return.") . "</p>
						<p>$device_list</p>
					</td>
					</tr>";

			$form_array = array();
			$field_name = "poller_id";
			$form_array += array($field_name => $fields_device_edit["poller_id"]);
			$form_array[$field_name]["description"] = __("Please select the new poller for the selected device(s).");

			draw_edit_form(
				array(
					"config" => array("no_form_tag" => true),
					"fields" => $form_array
					)
				);
		}elseif (get_request_var_post("drp_action") === DEVICE_ACTION_CHANGE_SITE) { /* Change Site */
			print "	<tr>
					<td colspan='2' class='textArea'>
						<p>" . __("Select the new site for the devices(s) below and select 'yes' to continue, or 'no' to return.") . "</p>
						<p>$device_list</p>
					</td>
					</tr>";

			$form_array = array();
			$field_name = "site_id";
			$form_array += array($field_name => $fields_device_edit["site_id"]);
			$form_array[$field_name]["description"] = __("Please select the new site for the selected device(s).");

			draw_edit_form(
				array(
					"config" => array("no_form_tag" => true),
					"fields" => $form_array
					)
				);
		}elseif (preg_match("/^tr_([0-9]+)$/", get_request_var_post("drp_action"), $matches)) { /* place on tree */
			print "	<tr>
					<td class='textArea'>
						<p>" . __("When you click save, the following devices will be placed under the branch selected below.") . "</p>
						<p>$device_list</p>
						<p><strong>" . __("Destination Branch:") . "</strong><br>"; grow_dropdown_tree($matches[1], "tree_item_id", "0"); print "</p>
					</td>
				</tr>\n
				<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n
				";
		} else {
			$save['drp_action'] = $_POST['drp_action'];
			$save['device_list'] = $device_list;
			$save['device_array'] = (isset($device_array)? $device_array : array());
			api_plugin_hook_function('device_action_prepare', $save);
		}
	} else {
		print "	<tr>
				<td class='textArea'>
					<p>" . __("You must first select a Device.  Please select 'Return' to return to the previous menu.") . "</p>
				</td>
			</tr>\n";
	}

	if (!sizeof($device_array) || get_request_var_post("drp_action") === ACTION_NONE) {
		form_return_button_alt();
	}else{
		form_yesno_button_alt(serialize($device_array), get_request_var_post("drp_action"));
	}

	html_end_box();

	include_once(CACTI_BASE_PATH . "/include/bottom_footer.php");
}