function api_data_template_save($data_template_id, $_fields_data_source) { require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php"); require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_info.php"); require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_push.php"); /* sanity checks */ validate_id_die($data_template_id, "data_template_id", true); /* make sure that there is at least one field to save */ if (sizeof($_fields_data_source) == 0) { return false; } /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $data_template_id); /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_data_source, api_data_template_form_list()); $_fields += sql_get_database_field_array($_fields_data_source, api_data_source_form_list()); if (db_replace("data_template", $_fields, array("id"))) { if (empty($data_template_id)) { $data_template_id = db_fetch_insert_id(); }else{ /* push out data template fields */ api_data_template_propagate($data_template_id); } return $data_template_id; }else{ return false; } }
function api_data_source_save($data_source_id, &$_fields_data_source, $skip_cache_update = false) { require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php"); /* sanity checks */ validate_id_die($data_source_id, "data_source_id", true); /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $data_source_id); /* field: data_template_id */ if (isset($_fields_data_source["data_template_id"])) { $_fields["data_template_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_data_source["data_template_id"]); } /* field: host_id */ if (isset($_fields_data_source["host_id"])) { $_fields["host_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_data_source["host_id"]); } /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_data_source, api_data_source_form_list()); /* check for an empty field list */ if (sizeof($_fields) == 1) { return true; } if (db_replace("data_source", $_fields, array("id"))) { if (empty($data_source_id)) { $data_source_id = db_fetch_insert_id(); } if ($skip_cache_update == false) { /* update data source title cache */ api_data_source_title_cache_update($data_source_id); } /* keep the poller cache up to date */ update_poller_cache($data_source_id, true); return $data_source_id; }else{ return false; } }
function api_data_source_total_get($filter_array = "") { require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_form.php"); $sql_where = ""; /* validation and setup for the WHERE clause */ if ((is_array($filter_array)) && (sizeof($filter_array) > 0)) { /* validate each field against the known master field list */ $_sv_arr = array(); $field_errors = api_data_source_fields_validate(sql_filter_array_to_field_array($filter_array), $_sv_arr); /* if a field input error has occured, register the error in the session and return */ if (sizeof($field_errors) > 0) { field_register_error($field_errors); return false; /* otherwise, form an SQL WHERE string using the filter fields */ }else{ $sql_where = sql_filter_array_to_where_string($filter_array, api_data_source_form_list(), true); } } return db_fetch_cell("select count(*) from data_source $sql_where"); }
function api_data_source_fields_validate(&$_fields_data_source, &$_fields_suggested_values, $data_source_field_name_format = "|field|", $suggested_values_field_name_format = "") { require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php"); if (sizeof($_fields_data_source) == 0) { return array(); } /* array containing errored fields */ $error_fields = array(); /* get a complete field list */ $fields_data_source = api_data_source_form_list(); /* base fields */ while (list($_field_name, $_field_array) = each($fields_data_source)) { if ((isset($_fields_data_source[$_field_name])) && (isset($_field_array["validate_regexp"])) && (isset($_field_array["validate_empty"]))) { $form_field_name = str_replace("|field|", $_field_name, $data_source_field_name_format); if (!form_input_validate($_fields_data_source[$_field_name], $form_field_name, $_field_array["validate_regexp"], $_field_array["validate_empty"])) { $error_fields[] = $form_field_name; } } } /* suggested values */ while (list($_field_name, $_sv_array) = each($_fields_suggested_values)) { if ((isset($fields_data_source[$_field_name])) && (isset($fields_data_source[$_field_name]["validate_regexp"])) && (isset($fields_data_source[$_field_name]["validate_empty"]))) { while (list($_sv_seq, $_sv_arr) = each($_sv_array)) { $form_field_name = str_replace("|field|", $_field_name, str_replace("|id|", $_sv_arr["id"], $suggested_values_field_name_format)); if (!form_input_validate($_sv_arr["value"], $form_field_name, $fields_data_source[$_field_name]["validate_regexp"], $fields_data_source[$_field_name]["validate_empty"])) { $error_fields[] = $form_field_name; } } } } return $error_fields; }
function package_data_template_import(&$xml_array, $package_id, $object_hash) { require_once(CACTI_BASE_PATH . "/lib/data_preset/data_preset_rra_info.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"); require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_update.php"); $save_fields = array(); /* tag the graph template as a package member */ $save_fields["package_id"] = $package_id; /* * XML Tag: <template> */ /* obtain a list of all data template specific fields */ $data_template_fields = api_data_template_form_list(); if (isset($xml_array["template"])) { /* get the base fields from the xml */ foreach (array_keys($data_template_fields) as $field_name) { if (isset($xml_array["template"][$field_name])) { $save_fields[$field_name] = xml_character_decode($xml_array["template"][$field_name]); } } } /* * XML Tag: <data_source> */ /* obtain a list of all data source specific fields */ $data_source_fields = api_data_source_form_list(); if (isset($xml_array["data_source"])) { /* get the base fields from the xml */ foreach (array_keys($data_source_fields) as $field_name) { if (isset($xml_array["data_source"][$field_name])) { $save_fields[$field_name] = xml_character_decode($xml_array["data_source"][$field_name]); } } } /* make sure we got the required information before trying to save */ if ((isset($xml_array["template"])) && (isset($xml_array["data_source"]))) { /* save the data template field to the database and register its new id */ $data_template_id = package_hash_update($object_hash, api_data_template_save(0, $save_fields)); } /* make sure the save completed successfully */ if ($data_template_id === false) { return; } /* * XML Tag: <items> */ /* obtain a list of all data source item specific fields */ $data_source_item_fields = api_data_source_item_form_list(); if ((isset($xml_array["items"])) && (is_array($xml_array["items"]))) { $save_fields = array(); /* get the base fields from the xml */ foreach ($xml_array["items"] as $data_template_item_hash => $data_template_item) { $save_fields = array(); /* make sure that each field is associated with the new data template */ $save_fields["data_template_id"] = $data_template_id; /* get the base fields from the xml */ foreach (array_keys($data_source_item_fields) as $field_name) { if (isset($data_template_item[$field_name])) { $save_fields[$field_name] = xml_character_decode($data_template_item[$field_name]); } } /* save the data template item to the database and register its new id */ package_hash_update($data_template_item_hash, api_data_template_item_save(0, $save_fields)); } } /* * XML Tag: <fields> */ if (isset($xml_array["fields"])) { $save_fields = array(); /* get the base fields from the xml */ foreach ($xml_array["fields"] as $field_array) { if ((isset($field_array["name"])) && (isset($field_array["t_value"])) && (isset($field_array["value"]))) { /* build an array containing each data input field */ if ($field_array["name"] == "data_query_id") { $field_value = package_hash_resolve($field_array["value"]); }else if ($field_array["name"] == "script_id") { $field_value = package_hash_resolve($field_array["value"]); }else{ $field_value = xml_character_decode($field_array["value"]); } $save_fields{$field_array["name"]} = array("t_value" => $field_array["t_value"], "value" => $field_value); } } /* save the fields to the database */ api_data_template_input_fields_save($data_template_id, $save_fields); } /* * XML Tag: <rra_items> */ /* obtain a list of all rra item preset specific fields */ $rra_items_fields = api_data_preset_rra_item_form_list(); if ((isset($xml_array["rra_items"])) && (is_array($xml_array["rra_items"]))) { $save_fields = array(); /* get the base fields from the xml */ foreach ($xml_array["rra_items"] as $rra_item) { $save_fields = array(); /* make sure that each field is associated with the new data template */ $save_fields["data_template_id"] = $data_template_id; /* get the base fields from the xml */ foreach (array_keys($rra_items_fields) as $field_name) { if (isset($rra_item[$field_name])) { $save_fields[$field_name] = xml_character_decode($rra_item[$field_name]); } } /* save the rra item to the database and register its new id */ api_data_template_rra_item_save(0, $save_fields); } } /* * XML Tag: <suggested_values> */ if (isset($xml_array["suggested_values"])) { $save_fields = array(); /* get the base fields from the xml */ foreach ($xml_array["suggested_values"] as $field_array) { if ((isset($field_array["field_name"])) && (isset($field_array["sequence"])) && (isset($field_array["value"]))) { /* build an array containing each data input field */ $save_fields{$field_array["field_name"]}[] = array("id" => "0", "value" => xml_character_decode($field_array["value"])); } } /* save the suggested values to the database */ api_data_template_suggested_values_save($data_template_id, $save_fields); } }
function api_data_template_propagate($data_template_id) { require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php"); require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_info.php"); /* get information about this data template */ $data_template = api_data_template_get($data_template_id); /* must be a valid data template */ if ($data_template === false) { return false; } /* retrieve a list of data source fields */ $data_source_fields = api_data_source_form_list(); $ds_fields = array(); /* loop through each data source column name (from the above array) */ foreach ($data_source_fields as $field_name => $field_array) { /* are we allowed to push out the column? */ if ((isset($data_template["t_$field_name"])) && (isset($data_template[$field_name])) && ($data_template["t_$field_name"] == "0")) { $ds_fields[$field_name] = array("type" => $field_array["data_type"], "value" => $data_template[$field_name]); } } if (sizeof($ds_fields) > 0) { $ds_fields["data_template_id"] = array("type" => DB_TYPE_INTEGER, "value" => $data_template_id); return db_update("data_source", $ds_fields, array("data_template_id")); } return true; }
function &package_data_template_export($data_template_id, $indent = 3) { require_once(CACTI_BASE_PATH . "/lib/data_preset/data_preset_rra_info.php"); require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_info.php"); require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php"); $xml = ""; /* * XML Tag: <template> */ /* obtain a list of all data template specific fields */ $data_template_fields = api_data_template_form_list(); /* obtain a copy of this specfic data template */ $data_template = api_data_template_get($data_template_id); $_xml = ""; foreach (array_keys($data_template_fields) as $field_name) { /* create an XML key for each data template field */ $_xml .= package_xml_tag_get($field_name, xml_character_encode($data_template[$field_name]), $indent + 2); } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("template", $_xml, $indent + 1, true); /* * XML Tag: <data_source> */ /* obtain a list of all data source specific fields */ $data_source_fields = api_data_source_form_list(); $_xml = ""; foreach (array_keys($data_source_fields) as $field_name) { /* check because the 'name' column does not exist */ if (isset($data_template[$field_name])) { /* create an XML key for each data source field */ $_xml .= package_xml_tag_get($field_name, xml_character_encode($data_template[$field_name]), $indent + 2); } /* check because the 't_data_input_type' and 't_rrd_path' columns do not exist */ if (isset($data_template{"t_" . $field_name})) { /* create an XML key for each "template" data source field */ $_xml .= package_xml_tag_get("t_" . $field_name, xml_character_encode($data_template{"t_" . $field_name}), $indent + 2); } } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("data_source", $_xml, $indent + 1, true); /* * XML Tag: <items> */ /* obtain a list of all data source item specific fields */ $data_source_items_fields = api_data_source_item_form_list(); /* obtain a list of all data template items associated with this data template */ $data_template_items = api_data_template_item_list($data_template_id); $_xml = ""; if (sizeof($data_template_items) > 0) { foreach ($data_template_items as $data_template_item) { $__xml = ""; foreach (array_keys($data_source_items_fields) as $field_name) { /* create an XML key for each data template item field */ $__xml .= package_xml_tag_get($field_name, xml_character_encode($data_template_item[$field_name]), $indent + 3); } /* append the result onto a temporary XML string */ $_xml .= package_xml_tag_get(package_hash_get($data_template_item["id"], "data_template_item"), $__xml, $indent + 2, true); } } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("items", $_xml, $indent + 1, true); /* * XML Tag: <fields> */ /* obtain a list of all data template input fields associated with this data template */ $data_template_input_fields = api_data_template_input_field_list($data_template_id); $_xml = ""; if (sizeof($data_template_input_fields) > 0) { $i = 0; foreach ($data_template_input_fields as $data_template_input_field_name => $data_template_input_field) { $__xml = ""; /* create an XML key for each suggested value field */ $__xml .= package_xml_tag_get("name", xml_character_encode($data_template_input_field_name), $indent + 3); $__xml .= package_xml_tag_get("t_value", xml_character_encode($data_template_input_field["t_value"]), $indent + 3); /* make sure to resolve internal ID's for specific fields */ if ($data_template_input_field_name == "data_query_id") { $__xml .= package_xml_tag_get("value", xml_character_encode(package_hash_get($data_template_input_field["value"], "data_query")), $indent + 3); }else if ($data_template_input_field_name == "script_id") { $__xml .= package_xml_tag_get("value", xml_character_encode(package_hash_get($data_template_input_field["value"], "script")), $indent + 3); }else{ $__xml .= package_xml_tag_get("value", xml_character_encode($data_template_input_field["value"]), $indent + 3); } /* break out each row into its own key */ $_xml .= package_xml_tag_get("item_" . str_pad($i, 5, "0", STR_PAD_LEFT), $__xml, $indent + 2, true); $i++; } } /* * XML Tag: <rra_items> */ /* obtain a list of all rra item preset specific fields */ $rra_items_fields = api_data_preset_rra_item_form_list(); /* obtain a list of all rra items associated with this data template */ $rra_items = api_data_template_rra_item_list($data_template_id); $_xml = ""; if (is_array($rra_items)) { $i = 0; foreach ($rra_items as $rra_item) { $__xml = ""; foreach (array_keys($rra_items_fields) as $field_name) { /* create an XML key for each rra item field */ $__xml .= package_xml_tag_get($field_name, xml_character_encode($rra_item[$field_name]), $indent + 3); } /* break out each row into its own key */ $_xml .= package_xml_tag_get("item_" . str_pad($i, 5, "0", STR_PAD_LEFT), $__xml, $indent + 2, true); $i++; } } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("rra_items", $_xml, $indent + 1, true); /* * XML Tag: <suggested_values> */ /* obtain a list of all suggested values associated with this data template */ $data_template_suggested_values = api_data_template_suggested_values_list($data_template_id); $_xml = ""; if (sizeof($data_template_suggested_values) > 0) { $i = 0; foreach ($data_template_suggested_values as $data_template_suggested_value) { $__xml = ""; /* create an XML key for each suggested value field */ $__xml .= package_xml_tag_get("field_name", xml_character_encode($data_template_suggested_value["field_name"]), $indent + 3); $__xml .= package_xml_tag_get("sequence", xml_character_encode($data_template_suggested_value["sequence"]), $indent + 3); $__xml .= package_xml_tag_get("value", xml_character_encode($data_template_suggested_value["value"]), $indent + 3); /* break out each row into its own key */ $_xml .= package_xml_tag_get("item_" . str_pad($i, 5, "0", STR_PAD_LEFT), $__xml, $indent + 2, true); $i++; } } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("suggested_values", $_xml, $indent + 1, true); /* wrap the whole XML string into a 'data_template' tag and return it */ $xml = package_xml_tag_get(package_hash_get($data_template_id, "data_template"), $xml, $indent, true); return $xml; }
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}"); } } } } }