Example #1
0
function generate_complete_graph($graph_template_id, $host_id = 0, $data_query_id = 0, $data_query_index = "") {
	require_once(CACTI_BASE_PATH . "/lib/graph/graph_update.php");
	require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_push.php");
	require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_info.php");

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

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

	$data_templates = get_data_templates_from_graph_template($graph_template_id);

	/* decide which data sources we will need to create */
	$create_data_templates_list = array();

	if (sizeof($data_templates) > 0) {
		foreach ($data_templates as $item) {
			if (($item["data_input_type"] == DATA_INPUT_TYPE_DATA_QUERY) && (isset($form_data_input_fields["data_query_id"]))) {
				if (db_fetch_cell("select value from data_template_field where data_template_id = " . $item["id"] . " and name = 'data_query_id'") == $form_data_input_fields["data_query_id"]) {
					$create_data_templates_list[] = $item["id"];
				}
			}else{
				$create_data_templates_list[] = $item["id"];
			}
		}
	}

	/* no data templates have been marked for creation */
	if (sizeof($create_data_templates_list) == 0) {
		return false;
	}

	$dti_to_dsi = array();

	foreach ($create_data_templates_list as $data_template_id) {
		$data_source_id = copy_data_template_to_data_source($data_template_id, $host_id, $data_query_id, $data_query_index);

		if ($data_source_id === false) {
			log_save("Error generating data source from data template [ID#$data_template_id]", SEV_ERROR);
		}else{
			$dti_to_dsi[$data_template_id] = $data_source_id;
		}
	}

	/* create the actual graph from the chosen graph template */
	$graph_id = copy_graph_template_to_graph($graph_template_id, $host_id, $data_query_id, $data_query_index);

	if ($graph_id === false) {
		log_save("Error generating graph from graph template [ID#$graph_template_id]", SEV_ERROR);
	}else{
		/* fetch a list graph template items and their associated data template items */
		$data_template_items = get_data_template_items_from_graph_template($graph_template_id);

		if (sizeof($data_template_items) > 0) {
			foreach ($data_template_items as $item) {
				/* write out the graph->data source item mapping here. note, that we are only able to use 'sequence'
				 * as a primary key here because the graph was just created above and therefore must be accurate */
				db_update("graph_item",
					array(
						"id" => array("type" => DB_TYPE_INTEGER, "value" => db_fetch_cell("select id from graph_item where graph_id = " . sql_sanitize($graph_id) . " and graph_template_item_id = " . sql_sanitize($item["graph_template_item_id"]))),
						"data_source_item_id" => array("type" => DB_TYPE_INTEGER, "value" => db_fetch_cell("select id from data_source_item where data_source_name = '" . sql_sanitize($item["data_source_name"]) . "' and data_source_id = " . sql_sanitize($dti_to_dsi{$item["data_template_id"]})))
						),
					array("id"));
			}
		}

		/* make sure the graph title is up to date */
		api_graph_title_cache_update($graph_id);

		return array("graph" => array($graph_template_id => $graph_id), "data_source" => $dti_to_dsi);
	}

	return false;
}
Example #2
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}");
                }
            }
        }
    }
}