Example #1
0
function copy_data_template_to_data_source($data_template_id, $host_id = 0, $data_query_id = 0, $data_query_index = "") {
	require_once(CACTI_BASE_PATH . "/lib/sys/variable.php");
	require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_update.php");
	require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php");
	require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_info.php");

	/* sanity check for $data_template_id */
	if ((!is_numeric($data_template_id)) || (empty($data_template_id))) {
		return false;
	}

	/* sanity check for $host_id */
	if (!is_numeric($host_id)) {
		return false;
	}

	/* fetch field lists */
	$fields_data_source = api_data_source_form_list();
	$fields_data_source_item = api_data_source_item_form_list();

	/* fetch information from that data template */
	$data_template = api_data_template_get($data_template_id);
	$_data_template_input_fields = api_data_template_input_field_list($data_template_id);

	if (sizeof($data_template) > 0) {
		/* copy down per-data source only fields */
		$_fields = array();
		$_fields["id"] = "0";
		$_fields["data_template_id"] = $data_template_id;
		$_fields["host_id"] = $host_id;

		/* evaluate suggested values: data query-based graphs */
		if ((!empty($data_query_id)) && ($data_query_index != "")) {
			$_fields["name"] = evaluate_data_query_suggested_values($host_id, $data_query_id, $data_query_index, "data_template_suggested_value", "data_template_id = " . sql_sanitize($data_template_id) . " and field_name = 'name'", 0);
		/* evaluate suggested values: non-data query-based graphs */
		}else{
			$_fields["name"] = db_fetch_cell("select value from data_template_suggested_value where data_template_id = " . sql_sanitize($data_template_id) . " and field_name = 'name' order by sequence limit 1");
		}

		/* copy down all visible fields */
		foreach (array_keys($fields_data_source) as $field_name) {
			if (isset($data_template[$field_name])) {
				$_fields[$field_name] = $data_template[$field_name];
			}
		}

		$data_source_id = api_data_source_save(0, $_fields, true);

		if ($data_source_id) {
			log_save("Cloning data source [ID#$data_source_id] from template [ID#$data_template_id]", SEV_DEBUG);

			/* reformat the $_data_template_input_fields to be more compatible with api_data_source_save() */
			$data_template_input_fields = array();
			foreach (array_keys($_data_template_input_fields) as $field_name) {
				$data_template_input_fields[$field_name] = $_data_template_input_fields[$field_name]["value"];
			}

			/* handle data source custom fields */
			api_data_source_fields_save($data_source_id, $data_template_input_fields);

			/* handle associated rra items */
			api_data_source_data_template_rra_item_copy($data_source_id, $data_template_id);

			/* move onto the data source items */
			$data_template_items = api_data_template_item_list($data_template_id);

			if (sizeof($data_template_items) > 0) {
				foreach ($data_template_items as $data_template_item) {
					/* copy down per-data source only fields */
					$_fields = array();
					$_fields["id"] = "0";
					$_fields["data_source_id"] = $data_source_id;
					$_fields["data_template_item_id"] = $data_template_item["id"];
					$_fields["field_input_value"] = $data_template_item["field_input_value"];

					/* copy down all visible fields */
					foreach (array_keys($fields_data_source_item) as $field_name) {
						$_fields[$field_name] = $data_template_item[$field_name];
					}

					if (!api_data_source_item_save(0, $_fields)) {
						log_save("Save error in api_data_source_item_save()", SEV_ERROR);
					}
				}
			}

			return $data_source_id;
		}else{
			log_save("Save error in api_data_source_save()", SEV_ERROR, FACIL_WEBUI);

			return false;
		}
	}

	return false;
}
Example #2
0
function host_new_graphs_save()
{
    $validation_array = array();
    $selected_graphs_array = unserialize(stripslashes($_POST["selected_graphs_array"]));
    $map_id_to_index_array = unserialize(stripslashes($_POST["map_id_to_index_array"]));
    /* form an array that contains all of the data on the previous form */
    while (list($var, $val) = each($_POST)) {
        if (preg_match("/^g_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) {
            /* 1: data_query_id, 2: graph_template_id, 3: field_name */
            foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) {
                $selected_graphs_array[$uniq_id]["graph_template"][$matches[3]] = $val;
            }
            $validation_array["graph_template"][$matches[3]][$var] = $val;
        } elseif (preg_match("/^gi_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) {
            /* 1: data_query_id, 2: graph_template_id, 3: graph_template_input_id, 4: field_name */
            foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) {
                $selected_graphs_array[$uniq_id]["graph_template_item"][$matches[2]][$matches[3]] = $val;
            }
            $validation_array["graph_template_item"][$matches[4]][$var] = $val;
        } elseif (preg_match("/^d_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) {
            /* 1: data_query_id, 2: graph_template_id, 3: data_template_id, 4: field_name */
            foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) {
                $selected_graphs_array[$uniq_id]["data_template"][$matches[3]][$matches[4]] = $val;
            }
            $validation_array["data_template"][$matches[4]][$var] = $val;
        } elseif (preg_match("/^c_(\\d+)_(\\d+)_(\\d+)_(\\d+)/", $var, $matches)) {
            /* 1: data_query_id, 2: graph_template_id, 3: data_template_id, 4: data_input_field_name */
            foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) {
                $selected_graphs_array[$uniq_id]["custom_data"][$matches[3]][$matches[4]] = $val;
            }
            $validation_array["custom_data"][$matches[4]][$var] = $val;
        } elseif (preg_match("/^di_(\\d+)_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) {
            /* 1: data_query_id, 2: graph_template_id, 3: data_template_id, 4: data_template_item_id, 5: field_name */
            foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) {
                $selected_graphs_array[$uniq_id]["data_template_item"][$matches[3]][$matches[4]][$matches[5]] = $val;
            }
            $validation_array["data_template_item"][$matches[5]][$var] = $val;
        }
    }
    /* first pass: form validation */
    while (list($type, $type_array) = each($validation_array)) {
        while (list($field_name, $field_array) = each($type_array)) {
            while (list($form_field_name, $value) = each($field_array)) {
                $_v_arr = array($field_name => $value);
                if ($type == "data_template") {
                    $_sv_arr = array();
                    field_register_error(api_data_source_fields_validate($_v_arr, $_sv_arr, $form_field_name, ""));
                } else {
                    if ($type == "custom_data") {
                        field_register_error(api_data_source_input_fields_validate($_v_arr, $form_field_name));
                    } else {
                        if ($type == "data_template_item") {
                            $_v_arr["id"] = 0;
                            field_register_error(api_data_source_item_fields_validate($_v_arr, $form_field_name));
                        } else {
                            if ($type == "graph_template") {
                                $_sv_arr = array();
                                field_register_error(api_graph_fields_validate($_v_arr, $_sv_arr, $form_field_name, ""));
                            } else {
                                if ($type == "graph_template_item") {
                                    field_register_error(api_graph_item_fields_validate($_v_arr, $form_field_name));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    /* form validation failed: redirect back */
    if (is_error_message()) {
        /* cache all post field values */
        init_post_field_cache();
        host_new_graphs($selected_graphs_array, $map_id_to_index_array);
        /* form validation passed: save the data on the form */
    } else {
        debug_log_clear("new_graphs");
        foreach ($selected_graphs_array as $uniq_id => $skel) {
            if (isset($skel["custom_data"]["all_dq"])) {
                $is_data_query_graph = true;
                $data_query_id = isset($skel["custom_data"]["all_dq"]["data_query_id"]) ? $skel["custom_data"]["all_dq"]["data_query_id"] : 0;
                /* decode the data query index into its literal form */
                $data_query_index = decode_data_query_index(isset($skel["custom_data"]["all_dq"]["data_query_index"]) ? $skel["custom_data"]["all_dq"]["data_query_index"] : 0, get_data_query_indexes($data_query_id, $_POST["host_id"]));
            } else {
                $is_data_query_graph = false;
                $data_query_id = 0;
                $data_query_index = "";
            }
            $create_info = generate_complete_graph($skel["graph_template_id"], $_POST["host_id"], $data_query_id, $data_query_index);
            /* set the appropriate 'custom_data' keys */
            if ($is_data_query_graph == true) {
                /* pick the data query field name to index on */
                $data_query_field_name = get_best_data_query_index_type($_POST["host_id"], $data_query_id);
                /* get a list of all data templates associated with this graph template that are of type DATA_INPUT_TYPE_DATA_QUERY */
                $dq_data_templates = get_data_templates_from_graph_template($skel["graph_template_id"], DATA_INPUT_TYPE_DATA_QUERY);
                if (sizeof($dq_data_templates) > 0) {
                    foreach ($dq_data_templates as $data_template) {
                        $skel["custom_data"][$data_template["id"]]["data_query_id"] = $data_query_id;
                        $skel["custom_data"][$data_template["id"]]["data_query_index"] = $data_query_index;
                        $skel["custom_data"][$data_template["id"]]["data_query_field_name"] = $data_query_field_name;
                        $skel["custom_data"][$data_template["id"]]["data_query_field_value"] = get_data_query_row_value($data_query_id, $_POST["host_id"], $data_query_field_name, $data_query_index);
                    }
                }
            }
            /* update user specified data: data source-specific fields */
            foreach (array_keys($create_info["data_source"]) as $data_template_id) {
                if (isset($skel["data_template"][$data_template_id])) {
                    if (!api_data_source_save($create_info["data_source"][$data_template_id], $skel["data_template"][$data_template_id])) {
                        log_save("Problems updating new data source [ID#" . $create_info["data_source"][$data_template_id] . "], data template [ID#{$data_template_id}] from user data", SEV_ERROR);
                    }
                }
                if (isset($skel["data_template_item"][$data_template_id])) {
                    foreach ($skel["data_template_item"][$data_template_id] as $data_source_item_id => $data_template_item_array) {
                        if (!api_data_source_item_save($data_source_item_id, $data_template_item_array)) {
                            log_save("Problems updating new data source [item] [ID#" . $create_info["data_source"][$data_template_id] . "], data template [ID#{$data_template_id}] from user data", SEV_ERROR);
                        }
                    }
                }
                if (isset($skel["custom_data"][$data_template_id])) {
                    if (!api_data_source_fields_save($create_info["data_source"][$data_template_id], $skel["custom_data"][$data_template_id])) {
                        log_save("Problems updating new data source (fields) [ID#" . $create_info["data_source"][$data_template_id] . "], data template [ID#{$data_template_id}] from user data", SEV_ERROR);
                    }
                }
                /* update the title cache */
                api_data_source_title_cache_update($create_info["data_source"][$data_template_id]);
                /* update poller cache */
                update_poller_cache($create_info["data_source"][$data_template_id]);
            }
            /* update user specified data: graph-specific fields */
            foreach (array_keys($create_info["graph"]) as $graph_template_id) {
                if (isset($skel["graph_template"][$graph_template_id])) {
                    if (!api_graph_save($create_info["graph"][$graph_template_id], $skel["graph_template"][$graph_template_id])) {
                        log_save("Problems updating new graph [ID#" . $create_info["graph"][$graph_template_id] . "], graph template [ID#{$graph_template_id}] from user data", SEV_ERROR);
                    }
                }
                if (isset($skel["graph_template_item"][$graph_template_id])) {
                    foreach ($skel["graph_template_item"][$graph_template_id] as $graph_template_item_input_id => $value) {
                        if (!api_graph_template_item_input_propagate($graph_template_item_input_id, $value)) {
                            log_save("Problems updating new graph [item] [ID#" . $create_info["graph"][$graph_template_id] . "], graph template [ID#{$graph_template_id}] from user data", SEV_ERROR);
                        }
                    }
                }
                /* update the title cache */
                api_graph_title_cache_update($create_info["graph"][$graph_template_id]);
            }
            debug_log_insert("new_graphs", _("Created graph: ") . api_graph_title_get($create_info["graph"][$skel["graph_template_id"]]));
        }
        /* lastly push host-specific information to our data sources */
        //push_out_host($_POST["host_id"], 0);
    }
}
Example #3
0
function form_post()
{
    if ($_POST["action_post"] == "data_source_add") {
        if (!empty($_POST["data_template_id"])) {
            $data_source_id = copy_data_template_to_data_source($_POST["data_template_id"], $_POST["host_id"]);
            if ($data_source_id) {
                api_data_source_title_cache_update($data_source_id);
            } else {
                log_save("Error pushing data template [ID#" . $_POST["data_template_id"] . "] down to a data source.", SEV_ERROR);
            }
        }
        header("Location: data_sources.php?action=edit" . (empty($data_source_id) ? "" : "&id={$data_source_id}") . (!isset($_POST["host_id"]) ? "" : "&host_id=" . $_POST["host_id"]) . (!isset($_POST["data_template_id"]) ? "" : "&data_template_id=" . $_POST["data_template_id"]));
    } else {
        if ($_POST["action_post"] == "data_source_edit") {
            /* fetch some cache variables */
            if (empty($_POST["data_source_id"])) {
                $_data_template_id = 0;
            } else {
                $_data_template_id = db_fetch_cell("select data_template_id from data_source where id = " . $_POST["data_source_id"]);
            }
            /* cache all post field values */
            init_post_field_cache();
            $data_source_fields = array();
            $data_source_item_fields = array();
            $data_input_fields = array();
            $rra_item_fields = array();
            /* parse out form values that we care about (data source / data source item fields) */
            reset($_POST);
            while (list($name, $value) = each($_POST)) {
                if (substr($name, 0, 4) == "dsi|") {
                    $matches = explode("|", $name);
                    $data_source_item_fields[$matches[2]][$matches[1]] = $value;
                } else {
                    if (substr($name, 0, 4) == "dif_") {
                        $data_input_fields[substr($name, 4)] = $value;
                    } else {
                        if (substr($name, 0, 3) == "ds|") {
                            $matches = explode("|", $name);
                            $data_source_fields[$matches[1]] = $value;
                        } else {
                            if (substr($name, 0, 5) == "rrai|") {
                                $matches = explode("|", $name);
                                $rra_item_fields[$matches[2]][$matches[1]] = $value;
                            }
                        }
                    }
                }
            }
            /* add any unchecked checkbox fields */
            $data_source_fields += field_register_html_checkboxes(api_data_source_form_list(), "ds||field|");
            /* step #2: field validation */
            $suggested_value_fields = array();
            /* placeholder */
            field_register_error(api_data_source_fields_validate($data_source_fields, $suggested_value_fields, "ds||field|", ""));
            field_register_error(api_data_source_input_fields_validate($data_input_fields, "dif_|field|"));
            foreach ($data_source_item_fields as $data_source_item_id => $data_source_item) {
                field_register_error(api_data_source_item_fields_validate($data_source_item, "dsi||field||{$data_source_item_id}"));
            }
            /* step #3: field save */
            if (is_error_message()) {
                log_save("User input validation error for data source [ID#" . $_POST["data_source_id"] . "]", SEV_DEBUG);
            } else {
                /* save data source data */
                $data_source_id = api_data_source_save($_POST["data_source_id"], $data_source_fields);
                if ($data_source_id) {
                    /* treat this as a regular form with rra presets if there is no template selected */
                    if (empty($_data_template_id)) {
                        if (isset($_POST["preset_rra_id"])) {
                            /* copy down the selected rra preset into the data template if a preset is selected */
                            api_data_source_rra_item_clear($data_source_id);
                            /* copy down associated rra items from the preset */
                            api_data_source_preset_rra_item_copy($data_source_id, $_POST["preset_rra_id"]);
                        }
                        /* rra items for templated forms ALWAYS come from the template */
                    } else {
                        /* ONLY copy down rra items from the template if the data source is new */
                        if (empty($_POST["data_source_id"])) {
                            if (!api_data_source_data_template_rra_item_copy($data_source_id, $_POST["data_template_id"])) {
                                log_save("Copy error for data template [ID#" . $_POST["data_template_id"] . "], data source [ID#" . $data_source_id . "]", SEV_ERROR);
                            }
                        }
                    }
                    /* save data source input fields */
                    if (!api_data_source_fields_save($data_source_id, $data_input_fields)) {
                        log_save("Save error for data input fields, data source [ID#" . $data_source_id . "]", SEV_ERROR);
                    }
                    /* save data source item data */
                    foreach ($data_source_item_fields as $data_source_item_id => $data_source_item) {
                        /* required fields */
                        $data_source_item["data_source_id"] = $data_source_id;
                        if (!api_data_source_item_save($data_source_item_id, $data_source_item)) {
                            log_save("Save error for data source item [ID#" . $data_source_item_id . "], data source [ID#" . $data_source_id . "]", SEV_ERROR);
                        }
                    }
                } else {
                    log_save("Save error for data source [ID#" . $_POST["data_source_id"] . "]", SEV_ERROR);
                }
            }
            if (is_error_message() || $_POST["data_template_id"] != $_data_template_id) {
                if (isset($_POST["redirect_item_add"])) {
                    $action = "item_add";
                } else {
                    $action = "edit";
                }
                header("Location: data_sources.php?action={$action}" . (empty($_POST["data_source_id"]) ? "" : "&id=" . $_POST["data_source_id"]) . (!isset($_POST["host_id"]) ? "" : "&host_id=" . $_POST["host_id"]) . (!isset($_POST["data_template_id"]) ? "" : "&data_template_id=" . $_POST["data_template_id"]) . (isset($_POST["data_input_type"]) ? "&data_input_type=" . $_POST["data_input_type"] : "") . (isset($_POST["dif_script_id"]) ? "&script_id=" . $_POST["dif_script_id"] : "") . (isset($_POST["dif_data_query_id"]) ? "&data_query_id=" . $_POST["dif_data_query_id"] : ""));
            } else {
                header("Location: data_sources.php");
            }
            /* submit button on the actions area page */
        } else {
            if ($_POST["action_post"] == "box-1") {
                $selected_rows = explode(":", $_POST["box-1-action-area-selected-rows"]);
                if ($_POST["box-1-action-area-type"] == "search") {
                    $get_string = "";
                    if ($_POST["box-1-search_device"] != "-1") {
                        $get_string .= ($get_string == "" ? "?" : "&") . "search_device=" . urlencode($_POST["box-1-search_device"]);
                    }
                    if (trim($_POST["box-1-search_filter"]) != "") {
                        $get_string .= ($get_string == "" ? "?" : "&") . "search_filter=" . urlencode($_POST["box-1-search_filter"]);
                    }
                    header("Location: data_sources.php{$get_string}");
                    exit;
                } else {
                    if ($_POST["box-1-action-area-type"] == "remove") {
                        foreach ($selected_rows as $data_source_id) {
                            api_data_source_remove($data_source_id);
                        }
                    } else {
                        if ($_POST["box-1-action-area-type"] == "duplicate") {
                            // not yet coded
                        } else {
                            if ($_POST["box-1-action-area-type"] == "enable") {
                                foreach ($selected_rows as $data_source_id) {
                                    api_data_source_enable($data_source_id);
                                }
                            } else {
                                if ($_POST["box-1-action-area-type"] == "disable") {
                                    foreach ($selected_rows as $data_source_id) {
                                        api_data_source_disable($data_source_id);
                                    }
                                } else {
                                    if ($_POST["box-1-action-area-type"] == "change_data_template") {
                                        // note yet coded
                                    } else {
                                        if ($_POST["box-1-action-area-type"] == "change_host") {
                                            foreach ($selected_rows as $data_source_id) {
                                                api_data_source_host_update($data_source_id, $_POST["box-1-change_device"]);
                                            }
                                        } else {
                                            if ($_POST["box-1-action-area-type"] == "convert_data_template") {
                                                // note yet coded
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                header("Location: data_sources.php");
                /* 'filter' area at the bottom of the box */
            } else {
                if ($_POST["action_post"] == "data_source_list") {
                    $get_string = "";
                    /* the 'clear' button wasn't pressed, so we should filter */
                    if (!isset($_POST["box-1-action-clear-button"])) {
                        if (trim($_POST["box-1-search_filter"]) != "") {
                            $get_string = ($get_string == "" ? "?" : "&") . "search_filter=" . urlencode($_POST["box-1-search_filter"]);
                        }
                    }
                    header("Location: data_sources.php{$get_string}");
                }
            }
        }
    }
}