function api_data_preset_gprint_save($data_preset_gprint_id, $_fields_data_preset_gprint) { require_once(CACTI_BASE_PATH . "/lib/data_preset/data_preset_gprint_info.php"); /* sanity checks */ validate_id_die($data_preset_gprint_id, "data_preset_gprint_id", true); /* make sure that there is at least one field to save */ if (sizeof($_fields_data_preset_gprint) == 0) { return false; } /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $data_preset_gprint_id); /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_data_preset_gprint, api_data_preset_gprint_form_list()); if (db_replace("preset_gprint", $_fields, array("id"))) { if (empty($data_preset_gprint_id)) { $data_preset_gprint_id = db_fetch_insert_id(); } return $data_preset_gprint_id; }else{ return false; } }
function api_device_save($device_id, &$_fields_device) { require_once(CACTI_BASE_PATH . "/lib/device/device_info.php"); /* sanity checks */ validate_id_die($device_id, "device_id", true); /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $device_id); /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_device, api_device_form_list()); /* check for an empty field list */ if (sizeof($_fields) == 1) { return true; } if (db_replace("host", $_fields, array("id"))) { if (empty($device_id)) { return db_fetch_insert_id(); }else{ return $device_id; } }else{ return false; } }
function api_graph_save($graph_id, &$_fields_graph, $skip_cache_update = false) { require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); /* sanity check for $graph_id */ if (!is_numeric($graph_id)) { return false; } /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $graph_id); /* field: graph_template_id */ if (isset($_fields_graph["graph_template_id"])) { $_fields["graph_template_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph["graph_template_id"]); } /* field: host_id */ if (isset($_fields_graph["host_id"])) { $_fields["host_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph["host_id"]); } /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_graph, api_graph_form_list()); /* check for an empty field list */ if (sizeof($_fields) == 1) { return true; } if (db_replace("graph", $_fields, array("id"))) { $graph_id = db_fetch_insert_id(); if ($skip_cache_update == false) { api_graph_title_cache_update($graph_id); } return true; }else{ return false; } }
function api_user_save($array) { if (db_replace("user_auth", $array)) { /* logging */ if (empty($array["id"])) { /* New user */ $user_id = db_fetch_insert_id(); log_save(sprintf(_("USER_ADMIN: User id '%s' added"), $user_id), SEV_NOTICE, FACIL_AUTH); }else{ /* existing user */ $user_id = $array["id"]["value"]; log_save(sprintf(_("USER_ADMIN: User id '%s' updated"), $user_id), SEV_NOTICE, FACIL_AUTH); } return $user_id; } else { log_save(sprintf(_("USER_ADMIN: Error saving user id '%s' "), $user_id), SEV_ERROR, FACIL_AUTH); return 0; } }
function api_package_metadata_save($package_metadata_id, &$_fields_package_metadata) { require_once(CACTI_BASE_PATH . "/lib/package/package_info.php"); /* sanity checks */ validate_id_die($package_metadata_id, "package_metadata_id", true); /* sanity check for $package_id */ if ((empty($package_metadata_id)) && (empty($_fields_package_metadata["package_id"]))) { log_save("Required package_id when package_metadata_id = 0", SEV_ERROR); return false; } else if ((isset($_fields_package_metadata["package_id"])) && (!is_numeric($_fields_package_metadata["package_id"]))) { return false; } /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $package_metadata_id); /* field: package_id */ if (!empty($_fields_package_metadata["package_id"])) { $_fields["package_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_package_metadata["package_id"]); } /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_package_metadata, api_package_metadata_form_list()); /* check for an empty field list */ if (sizeof($_fields) == 1) { return true; } if (db_replace("package_metadata", $_fields, array("id"))) { if (empty($package_metadata_id)) { return db_fetch_insert_id(); }else{ return $package_metadata_id; } }else{ return false; } }
function api_data_preset_rra_item_save($data_preset_rra_item_id, $_fields_data_preset_rra_item) { require_once(CACTI_BASE_PATH . "/lib/data_preset/data_preset_rra_info.php"); /* sanity checks */ validate_id_die($data_preset_rra_item_id, "data_preset_rra_item_id", true); /* make sure that there is at least one field to save */ if (sizeof($_fields_data_preset_rra_item) == 0) { return false; } /* sanity check for $preset_rra_id */ if ((empty($data_preset_rra_item_id)) && (empty($_fields_data_preset_rra_item["preset_rra_id"]))) { log_save("Required preset_rra_id when data_preset_rra_item_id = 0", SEV_ERROR); return false; } else if ((isset($_fields_data_preset_rra_item["preset_rra_id"])) && (!db_integer_validate($_fields_data_preset_rra_item["preset_rra_id"]))) { return false; } /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $data_preset_rra_item_id); /* field: preset_rra_id */ if (!empty($_fields_data_preset_rra_item["preset_rra_id"])) { $_fields["preset_rra_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_data_preset_rra_item["preset_rra_id"]); } /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_data_preset_rra_item, api_data_preset_rra_item_form_list()); if (db_replace("preset_rra_item", $_fields, array("id"))) { if (empty($data_preset_rra_item_id)) { $data_preset_rra_item_id = db_fetch_insert_id(); } return $data_preset_rra_item_id; }else{ return false; } }
function copy_graph_template_to_graph($graph_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/graph/graph_update.php"); require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_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; } /* fetch field lists */ $fields_graph = api_graph_form_list(); $fields_graph_item = api_graph_item_form_list(); $graph_template = api_graph_template_get($graph_template_id); if (sizeof($graph_template) > 0) { /* copy down per-graph only fields */ $_fields = array(); $_fields["id"] = "0"; $_fields["graph_template_id"] = $graph_template_id; $_fields["host_id"] = $host_id; /* evaluate suggested values: data query-based graphs */ if ((!empty($data_query_id)) && ($data_query_index != "")) { $_fields["title"] = evaluate_data_query_suggested_values($host_id, $data_query_id, $data_query_index, "graph_template_suggested_value", "graph_template_id = " . sql_sanitize($graph_template_id) . " and field_name = 'title'", 0); /* evaluate suggested values: non-data query-based graphs */ }else{ $_fields["title"] = db_fetch_cell("select value from graph_template_suggested_value where graph_template_id = " . sql_sanitize($graph_template_id) . " and field_name = 'title' order by sequence limit 1"); } /* copy down all visible fields */ foreach (array_keys($fields_graph) as $field_name) { if (isset($graph_template[$field_name])) { $_fields[$field_name] = $graph_template[$field_name]; } } if (api_graph_save(0, $_fields, true)) { $graph_id = db_fetch_insert_id(); log_save("Cloning graph [ID#$graph_id] from template [ID#$graph_template_id]", SEV_DEBUG); /* move onto the graph items */ $graph_template_items = api_graph_template_item_list($graph_template_id); if (sizeof($graph_template_items) > 0) { foreach ($graph_template_items as $graph_template_item) { /* copy down per-graph only fields */ $_fields = array(); $_fields["id"] = "0"; $_fields["graph_id"] = $graph_id; $_fields["graph_template_item_id"] = $graph_template_item["id"]; /* this allows us to connect the dots later */ foreach (array_keys($fields_graph_item) as $field_name) { if (isset($graph_template_item[$field_name])) { $_fields[$field_name] = $graph_template_item[$field_name]; } } if (!api_graph_item_save(0, $_fields)) { log_save("Save error in api_graph_item_save()", SEV_ERROR); } } } return $graph_id; }else{ log_save("Save error in api_graph_save()", SEV_ERROR); return false; } } return false; }
function form_save() { require(CACTI_BASE_PATH . "/include/graph/graph_arrays.php"); if (isset($_POST["save_component_item"])) { /* ================= input validation ================= */ input_validate_input_number(get_request_var_post("graph_template_id")); input_validate_input_number(get_request_var_post("task_item_id")); /* ==================================================== */ $items[0] = array(); if (get_request_var_post("graph_type_id") == GRAPH_ITEM_TYPE_LEGEND) { /* this can be a major time saver when creating lots of graphs with the typical GPRINT LAST/AVERAGE/MAX legends */ $items = array( 0 => array( "color_id" => "0", "graph_type_id" => GRAPH_ITEM_TYPE_GPRINT_LAST, "consolidation_function_id" => RRA_CF_TYPE_AVERAGE, "text_format" => __("Current:"), "hard_return" => "" ), 1 => array( "color_id" => "0", "graph_type_id" => GRAPH_ITEM_TYPE_GPRINT_AVERAGE, "consolidation_function_id" => RRA_CF_TYPE_AVERAGE, "text_format" => __("Average:"), "hard_return" => "" ), 2 => array( "color_id" => "0", "graph_type_id" => GRAPH_ITEM_TYPE_GPRINT_MAX, "consolidation_function_id" => RRA_CF_TYPE_AVERAGE, "text_format" => __("Maximum:"), "hard_return" => CHECKED )); } if ($_POST["graph_type_id"] == GRAPH_ITEM_TYPE_CUSTOM_LEGEND) { /* this can be a major time saver when creating lots of graphs with the typical VDEFs */ $items = array( 0 => array( "color_id" => "0", "graph_type_id" => read_config_option("cl1_gt_id"), "consolidation_function_id" => read_config_option("cl1_cf_id"), "vdef_id" => read_config_option("cl1_vdef_id"), "text_format" => read_config_option("cl1_text_format"), "hard_return" => read_config_option("cl1_hard_return") ), 1 => array( "color_id" => "0", "graph_type_id" => read_config_option("cl2_gt_id"), "consolidation_function_id" => read_config_option("cl2_cf_id"), "vdef_id" => read_config_option("cl2_vdef_id"), "text_format" => read_config_option("cl2_text_format"), "hard_return" => read_config_option("cl2_hard_return") ), 2 => array( "color_id" => "0", "graph_type_id" => read_config_option("cl3_gt_id"), "consolidation_function_id" => read_config_option("cl3_cf_id"), "vdef_id" => read_config_option("cl3_vdef_id"), "text_format" => read_config_option("cl3_text_format"), "hard_return" => read_config_option("cl3_hard_return") ), 3 => array( "color_id" => "0", "graph_type_id" => read_config_option("cl4_gt_id"), "consolidation_function_id" => read_config_option("cl4_cf_id"), "vdef_id" => read_config_option("cl4_vdef_id"), "text_format" => read_config_option("cl4_text_format"), "hard_return" => read_config_option("cl4_hard_return") ), ); foreach ($items as $key => $item) { #drop "empty" custom legend items if (empty($item["text_format"])) unset($items[$key]); } } foreach ($items as $item) { $save["id"] = form_input_validate($_POST["graph_template_item_id"], "graph_template_item_id", "^[0-9]+$", false, 3); $save["hash"] = get_hash_graph_template($_POST["graph_template_item_id"], "graph_template_item"); $save["graph_template_id"] = form_input_validate($_POST["graph_template_id"], "graph_template_id", "^[0-9]+$", false, 3); $save["local_graph_id"] = 0; $save["task_item_id"] = form_input_validate(((isset($item["task_item_id"]) ? $item["task_item_id"] : (isset($_POST["task_item_id"]) ? $_POST["task_item_id"] : 0))), "task_item_id", "^[0-9]+$", true, 3); $save["color_id"] = form_input_validate(((isset($item["color_id"]) ? $item["color_id"] : (isset($_POST["color_id"]) ? $_POST["color_id"] : 0))), "color_id", "^[0-9]+$", true, 3); $save["alpha"] = form_input_validate(((isset($item["alpha"]) ? $item["alpha"] : (isset($_POST["alpha"]) ? $_POST["alpha"] : "FF"))), "alpha", "^[a-fA-F0-9]+$", true, 3); $save["graph_type_id"] = form_input_validate(((isset($item["graph_type_id"]) ? $item["graph_type_id"] : (isset($_POST["graph_type_id"]) ? $_POST["graph_type_id"] : 0))), "graph_type_id", "^[0-9]+$", true, 3); if (isset($_POST["line_width"]) || isset($item["line_width"])) { $save["line_width"] = form_input_validate((isset($item["line_width"]) ? $item["line_width"] : $_POST["line_width"]), "line_width", "^[0-9]+[\.,]+[0-9]+$", true, 3); }else { # make sure to transfer old LINEx style into line_width on save switch ($save["graph_type_id"]) { case GRAPH_ITEM_TYPE_LINE1: $save["line_width"] = 1; break; case GRAPH_ITEM_TYPE_LINE2: $save["line_width"] = 2; break; case GRAPH_ITEM_TYPE_LINE3: $save["line_width"] = 3; break; default: $save["line_width"] = 0; } } $save["dashes"] = form_input_validate((isset($_POST["dashes"]) ? $_POST["dashes"] : ""), "dashes", "^[0-9]+[,0-9]*$", true, 3); $save["dash_offset"] = form_input_validate((isset($_POST["dash_offset"]) ? $_POST["dash_offset"] : ""), "dash_offset", "^[0-9]+$", true, 3); $save["cdef_id"] = form_input_validate(((isset($item["cdef_id"]) ? $item["cdef_id"] : (isset($_POST["cdef_id"]) ? $_POST["cdef_id"] : 0))), "cdef_id", "^[0-9]+$", true, 3); $save["vdef_id"] = form_input_validate(((isset($item["vdef_id"]) ? $item["vdef_id"] : (isset($_POST["vdef_id"]) ? $_POST["vdef_id"] : 0))), "vdef_id", "^[0-9]+$", true, 3); $save["shift"] = form_input_validate((isset($_POST["shift"]) ? $_POST["shift"] : ""), "shift", "^((on)|)$", true, 3); $save["consolidation_function_id"] = form_input_validate(((isset($item["consolidation_function_id"]) ? $item["consolidation_function_id"] : (isset($_POST["consolidation_function_id"]) ? $_POST["consolidation_function_id"] : 0))), "consolidation_function_id", "^[0-9]+$", true, 3); $save["textalign"] = form_input_validate((isset($_POST["textalign"]) ? $_POST["textalign"] : ""), "textalign", "^[a-z]+$", true, 3); $save["text_format"] = form_input_validate(((isset($item["text_format"]) ? $item["text_format"] : (isset($_POST["text_format"]) ? $_POST["text_format"] : ""))), "text_format", "", true, 3); $save["value"] = form_input_validate((isset($_POST["value"]) ? $_POST["value"] : ""), "value", "", true, 3); $save["hard_return"] = form_input_validate(((isset($item["hard_return"]) ? $item["hard_return"] : (isset($_POST["hard_return"]) ? $_POST["hard_return"] : ""))), "hard_return", "", true, 3); $save["gprint_id"] = form_input_validate(((isset($item["gprint_id"]) ? $item["gprint_id"] : (isset($_POST["gprint_id"]) ? $_POST["gprint_id"] : 0))), "gprint_id", "^[0-9]+$", true, 3); /* generate a new sequence if needed */ if (empty($_POST["sequence"])) { $_POST["sequence"] = get_sequence($_POST["sequence"], "sequence", "graph_templates_item", "graph_template_id=" . $_POST["graph_template_id"] . " and local_graph_id=0"); } $save["sequence"] = form_input_validate($_POST["sequence"], "sequence", "^[0-9]+$", false, 3); if (!is_error_message()) { /* Before we save the item, let's get a look at task_item_id <-> input associations */ $orig_data_source_graph_inputs = db_fetch_assoc("select graph_template_input.id, graph_template_input.name, graph_templates_item.task_item_id from (graph_template_input,graph_template_input_defs,graph_templates_item) where graph_template_input.id=graph_template_input_defs.graph_template_input_id and graph_template_input_defs.graph_template_item_id=graph_templates_item.id and graph_template_input.graph_template_id=" . $save["graph_template_id"] . " and graph_template_input.column_name='task_item_id' group by graph_templates_item.task_item_id"); $orig_data_source_to_input = array_rekey($orig_data_source_graph_inputs, "task_item_id", "id"); $graph_template_item_id = sql_save($save, "graph_templates_item"); if ($graph_template_item_id) { raise_message(1); if (!empty($save["task_item_id"])) { /* old item clean-up. Don't delete anything if the item <-> task_item_id association remains the same. */ if ($_POST["hidden_task_item_id"] != $_POST["task_item_id"]) { /* It changed. Delete any old associations */ db_execute("delete from graph_template_input_defs where graph_template_item_id=$graph_template_item_id"); /* Input for current data source exists and has changed. Update the association */ if (isset($orig_data_source_to_input{$save["task_item_id"]})) { db_execute("REPLACE INTO graph_template_input_defs " . "(graph_template_input_id, graph_template_item_id) " . "VALUES (" . $orig_data_source_to_input{$save["task_item_id"]} . "," . $graph_template_item_id . ")"); } } /* an input for the current data source does NOT currently exist, let's create one */ if (!isset($orig_data_source_to_input{$save["task_item_id"]})) { $ds_name = db_fetch_cell("select data_source_name from data_template_rrd where id=" . $_POST["task_item_id"]); db_execute("REPLACE INTO graph_template_input " . "(hash,graph_template_id,name,column_name) " . "VALUES ('" . get_hash_graph_template(0, "graph_template_input") . "'," . $save["graph_template_id"] . "," . "'Data Source [" . $ds_name . "]'," . 'task_item_id' . ")"); $graph_template_input_id = db_fetch_insert_id(); $graph_items = db_fetch_assoc("select id from graph_templates_item where graph_template_id=" . $save["graph_template_id"] . " and task_item_id=" . $_POST["task_item_id"]); if (sizeof($graph_items) > 0) { foreach ($graph_items as $graph_item) { db_execute("REPLACE INTO graph_template_input_defs " . "(graph_template_input_id,graph_template_item_id) " . "VALUES (" . $graph_template_input_id . "," . $graph_item["id"] . ")"); } } } } push_out_graph_item($graph_template_item_id); if (isset($_POST["task_item_id"]) && isset($orig_data_source_to_input{$_POST["task_item_id"]})) { /* make sure all current graphs using this graph input are aware of this change */ push_out_graph_input($orig_data_source_to_input{$_POST["task_item_id"]}, $graph_template_item_id, array($graph_template_item_id => $graph_template_item_id)); } }else{ raise_message(2); } } $_POST["sequence"] = 0; } if (is_error_message()) { header("Location: graph_templates_items.php?action=item_edit&graph_template_item_id=" . (empty($graph_template_item_id) ? $_POST["graph_template_item_id"] : $graph_template_item_id) . "&id=" . $_POST["graph_template_id"]); }else{ header("Location: graph_templates.php?action=template_edit&id=" . $_POST["graph_template_id"] . "&template_id=" . $_POST["graph_template_id"] . "&tab=items"); } exit; } }
function template_rrd_add() { /* ================= input validation ================= */ input_validate_input_number(get_request_var("id")); input_validate_input_number(get_request_var("local_data_id")); /* ==================================================== */ $hash = get_hash_data_template(0, "data_template_item"); db_execute("insert into data_template_rrd (hash,data_template_id,rrd_maximum,rrd_minimum,rrd_heartbeat,data_source_type_id,\n\t\tdata_source_name) values ('{$hash}'," . $_GET["id"] . ",100,0,600,1,'ds')"); $data_template_rrd_id = db_fetch_insert_id(); /* add this data template item to each data source using this data template */ $children = db_fetch_assoc("select local_data_id from data_template_data where data_template_id=" . $_GET["id"] . " and local_data_id>0"); if (sizeof($children) > 0) { foreach ($children as $item) { db_execute("insert into data_template_rrd (local_data_template_rrd_id,local_data_id,data_template_id,rrd_maximum,rrd_minimum,rrd_heartbeat,data_source_type_id,\n\t\t\tdata_source_name) values ({$data_template_rrd_id}," . $item["local_data_id"] . "," . $_GET["id"] . ",100,0,600,1,'ds')"); } } header("Location: data_templates.php?action=template_edit&id=" . $_GET["id"] . "&view_rrd={$data_template_rrd_id}"); }
function form_save() { if (isset($_POST["save_component_item"])) { /* ================= input validation ================= */ input_validate_input_number(get_request_var_post("graph_template_id")); input_validate_input_number(get_request_var_post("task_item_id")); /* ==================================================== */ global $graph_item_types; $items[0] = array(); if ($graph_item_types{$_POST["graph_type_id"]} == "LEGEND") { /* this can be a major time saver when creating lots of graphs with the typical GPRINT LAST/AVERAGE/MAX legends */ $items = array( 0 => array( "color_id" => "0", "graph_type_id" => "9", "consolidation_function_id" => "4", "text_format" => "Current:", "hard_return" => "" ), 1 => array( "color_id" => "0", "graph_type_id" => "9", "consolidation_function_id" => "1", "text_format" => "Average:", "hard_return" => "" ), 2 => array( "color_id" => "0", "graph_type_id" => "9", "consolidation_function_id" => "3", "text_format" => "Maximum:", "hard_return" => "on" )); } foreach ($items as $item) { /* generate a new sequence if needed */ if (empty($_POST["sequence"])) { $_POST["sequence"] = get_sequence($_POST["sequence"], "sequence", "graph_templates_item", "graph_template_id=" . $_POST["graph_template_id"] . " and local_graph_id=0"); } $save["id"] = $_POST["graph_template_item_id"]; $save["hash"] = get_hash_graph_template($_POST["graph_template_item_id"], "graph_template_item"); $save["graph_template_id"] = $_POST["graph_template_id"]; $save["local_graph_id"] = 0; $save["task_item_id"] = form_input_validate($_POST["task_item_id"], "task_item_id", "", true, 3); $save["color_id"] = form_input_validate((isset($item["color_id"]) ? $item["color_id"] : $_POST["color_id"]), "color_id", "", true, 3); /* if alpha is disabled, use invisible_alpha instead */ if (!isset($_POST["alpha"])) {$_POST["alpha"] = $_POST["invisible_alpha"];} $save["alpha"] = form_input_validate((isset($item["alpha"]) ? $item["alpha"] : $_POST["alpha"]), "alpha", "", true, 3); $save["graph_type_id"] = form_input_validate((isset($item["graph_type_id"]) ? $item["graph_type_id"] : $_POST["graph_type_id"]), "graph_type_id", "", true, 3); $save["cdef_id"] = form_input_validate($_POST["cdef_id"], "cdef_id", "", true, 3); $save["consolidation_function_id"] = form_input_validate((isset($item["consolidation_function_id"]) ? $item["consolidation_function_id"] : $_POST["consolidation_function_id"]), "consolidation_function_id", "", true, 3); $save["text_format"] = form_input_validate((isset($item["text_format"]) ? $item["text_format"] : $_POST["text_format"]), "text_format", "", true, 3); $save["value"] = form_input_validate($_POST["value"], "value", "", true, 3); $save["hard_return"] = form_input_validate(((isset($item["hard_return"]) ? $item["hard_return"] : (isset($_POST["hard_return"]) ? $_POST["hard_return"] : ""))), "hard_return", "", true, 3); $save["gprint_id"] = form_input_validate($_POST["gprint_id"], "gprint_id", "", true, 3); $save["sequence"] = $_POST["sequence"]; if (!is_error_message()) { /* Before we save the item, let's get a look at task_item_id <-> input associations */ $orig_data_source_graph_inputs = db_fetch_assoc("select graph_template_input.id, graph_template_input.name, graph_templates_item.task_item_id from (graph_template_input,graph_template_input_defs,graph_templates_item) where graph_template_input.id=graph_template_input_defs.graph_template_input_id and graph_template_input_defs.graph_template_item_id=graph_templates_item.id and graph_template_input.graph_template_id=" . $save["graph_template_id"] . " and graph_template_input.column_name='task_item_id' group by graph_templates_item.task_item_id"); $orig_data_source_to_input = array_rekey($orig_data_source_graph_inputs, "task_item_id", "id"); $graph_template_item_id = sql_save($save, "graph_templates_item"); if ($graph_template_item_id) { raise_message(1); if (!empty($save["task_item_id"])) { /* old item clean-up. Don't delete anything if the item <-> task_item_id association remains the same. */ if ($_POST["_task_item_id"] != $_POST["task_item_id"]) { /* It changed. Delete any old associations */ db_execute("delete from graph_template_input_defs where graph_template_item_id=$graph_template_item_id"); /* Input for current data source exists and has changed. Update the association */ if (isset($orig_data_source_to_input{$save["task_item_id"]})) { db_execute("replace into graph_template_input_defs (graph_template_input_id, graph_template_item_id) values (" . $orig_data_source_to_input{$save["task_item_id"]} . ",$graph_template_item_id)"); } } /* an input for the current data source does NOT currently exist, let's create one */ if (!isset($orig_data_source_to_input{$save["task_item_id"]})) { $ds_name = db_fetch_cell("select data_source_name from data_template_rrd where id=" . $_POST["task_item_id"]); db_execute("replace into graph_template_input (hash,graph_template_id,name,column_name) values ( '" . get_hash_graph_template(0, "graph_template_input") . "'," . $save["graph_template_id"] . ", 'Data Source [$ds_name]','task_item_id')"); $graph_template_input_id = db_fetch_insert_id(); $graph_items = db_fetch_assoc("select id from graph_templates_item where graph_template_id=" . $save["graph_template_id"] . " and task_item_id=" . $_POST["task_item_id"]); if (sizeof($graph_items) > 0) { foreach ($graph_items as $graph_item) { db_execute("replace into graph_template_input_defs (graph_template_input_id,graph_template_item_id) values ($graph_template_input_id," . $graph_item["id"] . ")"); } } } } push_out_graph_item($graph_template_item_id); if (isset($orig_data_source_to_input{$_POST["task_item_id"]})) { /* make sure all current graphs using this graph input are aware of this change */ push_out_graph_input($orig_data_source_to_input{$_POST["task_item_id"]}, $graph_template_item_id, array($graph_template_item_id => $graph_template_item_id)); } }else{ raise_message(2); } } $_POST["sequence"] = 0; } if (is_error_message()) { header("Location: graph_templates_items.php?action=item_edit&graph_template_item_id=" . (empty($graph_template_item_id) ? $_POST["graph_template_item_id"] : $graph_template_item_id) . "&id=" . $_POST["graph_template_id"]); exit; }else{ header("Location: graph_templates.php?action=template_edit&id=" . $_POST["graph_template_id"]); exit; } } }
function ds_rrd_add() { /* ================= input validation ================= */ input_validate_input_number(get_request_var("id")); /* ==================================================== */ db_execute("insert into data_template_rrd (local_data_id,rrd_maximum,rrd_minimum,rrd_heartbeat,data_source_type_id,\n\t\tdata_source_name) values (" . $_GET["id"] . ",100,0,600,1,'ds')"); $data_template_rrd_id = db_fetch_insert_id(); header("Location: data_sources.php?action=ds_edit&id=" . $_GET["id"] . "&view_rrd={$data_template_rrd_id}"); }
function item_group() { if (ereg("&group_item_id=([0-9]+)\$", $_SESSION["sess_field_values"]["cacti_js_dropdown_redirect"], $matches)) { $graph_template_item_id = $matches[1]; $selected_items = array(); /* list each selected item */ while (list($name, $value) = each($_SESSION["sess_field_values"])) { if (substr($name, 0, 9) == "gi_value_" && $value == "1") { $selected_items[substr($name, 9)] = 1; } } /* get an official list of items to compare against */ $graph_template_items = db_fetch_assoc("select id from graph_template_item where graph_template_id = " . $_SESSION["sess_field_values"]["id"] . " order by sequence"); /* find out which items were selected for grouping by the user */ $_group = array(); $keep_this_group = false; if (sizeof($graph_template_items) > 0) { foreach ($graph_template_items as $item) { if (isset($selected_items[$item["id"]])) { $_group[] = $item["id"]; if ($graph_template_item_id == $item["id"]) { $keep_this_group = true; } } else { if ($keep_this_group == true) { break; } $_group = array(); } } } if (sizeof($_group) > 1) { db_execute("insert into graph_template_item_group (id,hash,graph_template_id) values (0,''," . $_SESSION["sess_field_values"]["id"] . ")"); $graph_template_item_group_id = db_fetch_insert_id(); if ($graph_template_item_group_id) { for ($i = 0; $i < sizeof($_group); $i++) { db_execute("insert into graph_template_item_group_item (graph_template_item_group_id,graph_template_item_id) values ({$graph_template_item_group_id}," . $_group[$i] . ")"); } } } } }
function ds_rrd_add() { db_execute("insert into data_template_rrd (local_data_id,rrd_maximum,rrd_minimum,rrd_heartbeat,data_source_type_id, data_source_name) values (" . $_GET["id"] . ",100,0,600,1,'ds')"); $data_template_rrd_id = db_fetch_insert_id(); header("Location: data_sources.php?action=ds_edit&id=" . $_GET["id"] . "&view_rrd=$data_template_rrd_id"); }
function template_rrd_add() { /* ================= input validation ================= */ input_validate_input_number(get_request_var_request('id')); input_validate_input_number(get_request_var_request('local_data_id')); /* ==================================================== */ $hash = get_hash_data_template(0, 'data_template_item'); db_execute_prepared("INSERT INTO data_template_rrd (hash, data_template_id, rrd_maximum, rrd_minimum, rrd_heartbeat, data_source_type_id, data_source_name) \n\t\t VALUES (?, ?, 0, 0, 600, 1, 'ds')", array($hash, $_REQUEST['id'])); $data_template_rrd_id = db_fetch_insert_id(); /* add this data template item to each data source using this data template */ $children = db_fetch_assoc_prepared('SELECT local_data_id FROM data_template_data WHERE data_template_id = ? AND local_data_id > 0', array($_REQUEST['id'])); if (sizeof($children) > 0) { foreach ($children as $item) { db_execute_prepared("INSERT INTO data_template_rrd (local_data_template_rrd_id, local_data_id, data_template_id, rrd_maximum, rrd_minimum, rrd_heartbeat, data_source_type_id, data_source_name) \n\t\t\t\t VALUES (?, ?, ?, 0, 0, 600, 1, 'ds')", array($data_template_rrd_id, $item['local_data_id'], $_REQUEST['id'])); } } header('Location: data_templates.php?action=template_edit&id=' . $_REQUEST['id'] . "&view_rrd={$data_template_rrd_id}"); }
function api_graph_tree_item_save($graph_tree_item_id, &$_fields_graph_tree_item) { require_once(CACTI_BASE_PATH . "/include/graph_tree/graph_tree_constants.php"); require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_utility.php"); require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_info.php"); /* sanity checks */ validate_id_die($graph_tree_item_id, "graph_tree_item_id", true); /* sanity check for $graph_tree_id */ if ((empty($graph_tree_item_id)) && (empty($_fields_graph_tree_item["graph_tree_id"]))) { log_save("Required graph_tree_id when graph_tree_item_id = 0", SEV_ERROR); return false; }else if ((isset($_fields_graph_tree_item["graph_tree_id"])) && (!db_integer_validate($_fields_graph_tree_item["graph_tree_id"]))) { return false; } /* sanity check for $item_type */ if ((!isset($_fields_graph_tree_item["item_type"])) || (!db_integer_validate($_fields_graph_tree_item["item_type"]))) { log_save("Missing required item_type", SEV_ERROR); return false; } /* sanity check for $item_value */ if ((empty($graph_tree_item_id)) && (empty($_fields_graph_tree_item["item_value"]))) { log_save("Required item_value when graph_tree_item_id = 0", SEV_ERROR); return false; }else if ((isset($_fields_graph_tree_item["item_value"])) && ( (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_GRAPH) || ($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HOST)) && (!db_integer_validate($_fields_graph_tree_item["item_value"])) )) { return false; } /* sanity check for $parent_item_id */ if ((!isset($_fields_graph_tree_item["parent_item_id"])) || (!db_integer_validate($_fields_graph_tree_item["parent_item_id"], true))) { log_save("Missing required parent_item_id", SEV_ERROR); return false; } /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $graph_tree_item_id); /* field: graph_tree_id */ if (isset($_fields_graph_tree_item["graph_tree_id"])) { $_fields["graph_tree_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph_tree_item["graph_tree_id"]); } /* get a copy of the parent tree item id */ if ($_fields_graph_tree_item["parent_item_id"] == "0") { $parent_order_key = ""; $parent_sort_type = TREE_ORDERING_NONE; }else{ $parent_graph_tree_item = api_graph_tree_item_get($_fields_graph_tree_item["parent_item_id"]); $parent_order_key = $parent_graph_tree_item["order_key"]; $parent_sort_type = $parent_graph_tree_item["sort_children_type"]; } /* generate a new order key if this is a new graph tree item */ if (empty($graph_tree_item_id)) { $_fields["order_key"] = array("type" => DB_TYPE_STRING, "value" => api_graph_tree_item_available_order_key_get($_fields_graph_tree_item["graph_tree_id"], $parent_order_key)); }else{ $graph_tree_item = api_graph_tree_item_get($graph_tree_item_id); $_fields["order_key"] = array("type" => DB_TYPE_STRING, "value" => $graph_tree_item["order_key"]); } /* if this item is a graph, make sure it is not being added to the same branch twice */ $search_key = substr($parent_order_key, 0, (api_graph_tree_item_depth_get($parent_order_key) * CHARS_PER_TIER)); if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_GRAPH) && (sizeof(db_fetch_assoc("select id from graph_tree_items where item_value = " . $_fields_graph_tree_item["item_value"] . " and item_type = " . TREE_ITEM_TYPE_GRAPH . " and graph_tree_id = " . $_fields_graph_tree_item["graph_tree_id"] . " and order_key like '$search_key" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', (MAX_TREE_DEPTH * CHARS_PER_TIER) - (strlen($search_key) + CHARS_PER_TIER)) . "'")) > 0)) { return true; } /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_graph_tree_item, api_graph_tree_item_form_list()); /* check for an empty field list */ if (sizeof($_fields) == 1) { return true; } if (db_replace("graph_tree_items", $_fields, array("id"))) { if (empty($graph_tree_item_id)) { $graph_tree_item_id = db_fetch_insert_id(); } /* re-parent the branch if the parent item has changed */ if ($_fields_graph_tree_item["parent_item_id"] != api_graph_tree_item_parent_get_bykey($_fields["order_key"]["value"], $_fields_graph_tree_item["graph_tree_id"])) { api_graph_tree_item_reparent($graph_tree_item_id, $_fields_graph_tree_item["parent_item_id"]); } $parent_tree = api_graph_tree_get($_fields_graph_tree_item["graph_tree_id"]); /* tree item ordering */ if ($parent_tree["sort_type"] == TREE_ORDERING_NONE) { /* resort our parent */ if ($parent_sort_type != TREE_ORDERING_NONE) { echo $parent_sort_type; api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $_fields_graph_tree_item["parent_item_id"], $parent_sort_type); } /* if this is a header, sort direct children */ if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HEADER) && ($parent_sort_type != TREE_ORDERING_NONE)) { api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $graph_tree_item_id, $parent_sort_type); } /* tree ordering */ }else{ /* potential speed savings for large trees */ if (api_graph_tree_item_depth_get($_fields["order_key"]["value"]) == 1) { api_graph_tree_item_sort(SORT_TYPE_TREE, $_fields_graph_tree_item["graph_tree_id"], $parent_tree["sort_type"]); }else{ api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $_fields_graph_tree_item["parent_item_id"], $parent_tree["sort_type"]); } } /* if the user checked the 'Propagate Changes' box */ if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HEADER) && (isset($_fields_graph_tree_item["sort_children_type"])) && (!empty($_fields_graph_tree_item["propagate_changes"]))) { $graph_tree_items = api_graph_tree_item_list($_fields_graph_tree_item["graph_tree_id"], array("item_type" => TREE_ITEM_TYPE_HEADER), $graph_tree_item_id, false, false); if (is_array($graph_tree_items) > 0) { foreach ($graph_tree_items as $graph_tree_item) { db_update("graph_tree_items", array( "id" => array("type" => DB_TYPE_INTEGER, "value" => $graph_tree_item["id"]), "sort_children_type" => array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph_tree_item["sort_children_type"]) ), array("id")); if ($_fields_graph_tree_item["sort_children_type"] != TREE_ORDERING_NONE) { api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $graph_tree_item["id"], $_fields_graph_tree_item["sort_children_type"]); } } } } return $graph_tree_item_id; }else{ return false; } }
function form_save() { if (isset($_POST['save_component_item'])) { /* ================= input validation ================= */ input_validate_input_number(get_request_var_post('graph_template_id')); input_validate_input_number(get_request_var_post('graph_template_item_id')); input_validate_input_number(get_request_var_post('task_item_id')); /* ==================================================== */ global $graph_item_types; $items[0] = array(); if ($graph_item_types[$_POST['graph_type_id']] == 'LEGEND') { /* this can be a major time saver when creating lots of graphs with the typical GPRINT LAST/AVERAGE/MAX legends */ $items = array(0 => array('color_id' => '0', 'graph_type_id' => '9', 'consolidation_function_id' => '4', 'text_format' => 'Current:', 'hard_return' => ''), 1 => array('color_id' => '0', 'graph_type_id' => '9', 'consolidation_function_id' => '1', 'text_format' => 'Average:', 'hard_return' => ''), 2 => array('color_id' => '0', 'graph_type_id' => '9', 'consolidation_function_id' => '3', 'text_format' => 'Maximum:', 'hard_return' => 'on')); } foreach ($items as $item) { /* generate a new sequence if needed */ if (empty($_POST['sequence'])) { $_POST['sequence'] = get_sequence($_POST['sequence'], 'sequence', 'graph_templates_item', 'graph_template_id=' . $_POST['graph_template_id'] . ' AND local_graph_id=0'); } $save['id'] = $_POST['graph_template_item_id']; $save['hash'] = get_hash_graph_template($_POST['graph_template_item_id'], 'graph_template_item'); $save['graph_template_id'] = $_POST['graph_template_id']; $save['local_graph_id'] = 0; $save['task_item_id'] = form_input_validate($_POST['task_item_id'], 'task_item_id', '', true, 3); $save['color_id'] = form_input_validate(isset($item['color_id']) ? $item['color_id'] : $_POST['color_id'], 'color_id', '', true, 3); /* if alpha is disabled, use invisible_alpha instead */ if (!isset($_POST['alpha'])) { $_POST['alpha'] = $_POST['invisible_alpha']; } $save['alpha'] = form_input_validate(isset($item['alpha']) ? $item['alpha'] : $_POST['alpha'], 'alpha', '', true, 3); $save['graph_type_id'] = form_input_validate(isset($item['graph_type_id']) ? $item['graph_type_id'] : $_POST['graph_type_id'], 'graph_type_id', '', true, 3); $save['cdef_id'] = form_input_validate($_POST['cdef_id'], 'cdef_id', '', true, 3); $save['consolidation_function_id'] = form_input_validate(isset($item['consolidation_function_id']) ? $item['consolidation_function_id'] : $_POST['consolidation_function_id'], 'consolidation_function_id', '', true, 3); $save['text_format'] = form_input_validate(isset($item['text_format']) ? $item['text_format'] : $_POST['text_format'], 'text_format', '', true, 3); $save['value'] = form_input_validate($_POST['value'], 'value', '', true, 3); $save['hard_return'] = form_input_validate(isset($item['hard_return']) ? $item['hard_return'] : (isset($_POST['hard_return']) ? $_POST['hard_return'] : ''), 'hard_return', '', true, 3); $save['gprint_id'] = form_input_validate($_POST['gprint_id'], 'gprint_id', '', true, 3); $save['sequence'] = $_POST['sequence']; if (!is_error_message()) { /* Before we save the item, let's get a look at task_item_id <-> input associations */ $orig_data_source_graph_inputs = db_fetch_assoc_prepared("SELECT\n\t\t\t\t\tgraph_template_input.id,\n\t\t\t\t\tgraph_template_input.name,\n\t\t\t\t\tgraph_templates_item.task_item_id\n\t\t\t\t\tFROM (graph_template_input, graph_template_input_defs, graph_templates_item)\n\t\t\t\t\tWHERE graph_template_input.id = graph_template_input_defs.graph_template_input_id\n\t\t\t\t\tAND graph_template_input_defs.graph_template_item_id = graph_templates_item.id\n\t\t\t\t\tAND graph_template_input.graph_template_id = ?\n\t\t\t\t\tAND graph_template_input.column_name = 'task_item_id'\n\t\t\t\t\tGROUP BY graph_templates_item.task_item_id", array($save['graph_template_id'])); $orig_data_source_to_input = array_rekey($orig_data_source_graph_inputs, 'task_item_id', 'id'); $graph_template_item_id = sql_save($save, 'graph_templates_item'); if ($graph_template_item_id) { raise_message(1); if (!empty($save['task_item_id'])) { /* old item clean-up. Don't delete anything if the item <-> task_item_id association remains the same. */ if ($_POST['_task_item_id'] != $_POST['task_item_id']) { /* It changed. Delete any old associations */ db_execute_prepared('DELETE FROM graph_template_input_defs WHERE graph_template_item_id = ?', array($graph_template_item_id)); /* Input for current data source exists and has changed. Update the association */ if (isset($orig_data_source_to_input[$save['task_item_id']])) { db_execute_prepared('REPLACE INTO graph_template_input_defs (graph_template_input_id, graph_template_item_id) values (?, ?)', array($orig_data_source_to_input[$save['task_item_id']], $graph_template_item_id)); } } /* an input for the current data source does NOT currently exist, let's create one */ if (!isset($orig_data_source_to_input[$save['task_item_id']])) { $ds_name = db_fetch_cell_prepared('SELECT data_source_name FROM data_template_rrd WHERE id = ?', array($_POST['task_item_id'])); db_execute_prepared("REPLACE INTO graph_template_input (hash, graph_template_id, name, column_name) VALUES (?, ?, ?, 'task_item_id')", array(get_hash_graph_template(0, 'graph_template_input'), $save['graph_template_id'], 'Data Source [$ds_name]')); $graph_template_input_id = db_fetch_insert_id(); $graph_items = db_fetch_assoc_prepared('SELECT id FROM graph_templates_item WHERE graph_template_id = ? AND task_item_id = ?', array($save['graph_template_id'], $_POST['task_item_id'])); if (sizeof($graph_items) > 0) { foreach ($graph_items as $graph_item) { db_execute_prepared('REPLACE INTO graph_template_input_defs (graph_template_input_id, graph_template_item_id) VALUES (?, ?)', array($graph_template_input_id, $graph_item['id'])); } } } } push_out_graph_item($graph_template_item_id); if (isset($orig_data_source_to_input[$_POST['task_item_id']])) { /* make sure all current graphs using this graph input are aware of this change */ push_out_graph_input($orig_data_source_to_input[$_POST['task_item_id']], $graph_template_item_id, array($graph_template_item_id => $graph_template_item_id)); } } else { raise_message(2); } } $_POST['sequence'] = 0; } if (is_error_message()) { header('Location: graph_templates_items.php?action=item_edit&graph_template_item_id=' . (empty($graph_template_item_id) ? $_POST['graph_template_item_id'] : $graph_template_item_id) . '&id=' . $_POST['graph_template_id']); exit; } else { header('Location: graph_templates.php?action=template_edit&id=' . $_POST['graph_template_id']); exit; } } }
function sql_save($array_items, $table_name, $key_cols = 'id', $autoinc = TRUE, $db_conn = FALSE) { global $database_sessions, $database_default; /* check for a connection being passed, if not use legacy behavior */ if (!$db_conn) { $db_conn = $database_sessions[$database_default]; } if (read_config_option('log_verbosity') == POLLER_VERBOSITY_DEVDBG) { cacti_log("DEVEL: SQL Save on table '{$table_name}': \"" . serialize($array_items) . '"', FALSE); } while (list($key, $value) = each($array_items)) { $array_items[$key] = '"' . sql_sanitize($value) . '"'; } $replace_result = _db_replace($db_conn, $table_name, $array_items, $key_cols, $autoinc); if ($replace_result === false) { cacti_log("ERROR: SQL Save Command Failed for Table '{$table_name}'. Error was '" . mysql_error($db_conn) . "'", false); return FALSE; } /* get the last AUTO_ID and return it */ if (!$replace_result || db_fetch_insert_id($db_conn) == '0') { if (!is_array($key_cols)) { if (isset($array_items[$key_cols])) { return str_replace('"', '', $array_items[$key_cols]); } } return FALSE; } else { return $replace_result; } }
function &xml_to_graph_template($hash, &$xml_array, &$hash_cache) { global $struct_graph, $struct_graph_item, $fields_graph_template_input_edit, $hash_version_codes; /* import into: graph_templates */ $_graph_template_id = db_fetch_cell("select id from graph_templates where hash='$hash'"); $save["id"] = (empty($_graph_template_id) ? 0 : $_graph_template_id); $save["hash"] = $hash; $save["name"] = $xml_array["name"]; $graph_template_id = sql_save($save, "graph_templates"); $hash_cache["graph_template"][$hash] = $graph_template_id; /* import into: graph_templates_graph */ unset($save); $save["id"] = (empty($_graph_template_id) ? 0 : db_fetch_cell("select graph_templates_graph.id from (graph_templates cross join graph_templates_graph) where graph_templates.id=graph_templates_graph.graph_template_id and graph_templates.id=$graph_template_id and graph_templates_graph.local_graph_id=0")); $save["graph_template_id"] = $graph_template_id; /* parse information from the hash */ $parsed_hash = parse_xml_hash($hash); reset($struct_graph); while (list($field_name, $field_array) = each($struct_graph)) { /* make sure this field exists in the xml array first */ if (isset($xml_array["graph"]{"t_" . $field_name})) { $save{"t_" . $field_name} = $xml_array["graph"]{"t_" . $field_name}; } /* make sure this field exists in the xml array first */ if (isset($xml_array["graph"][$field_name])) { if (($field_name == "unit_exponent_value") && (get_version_index($parsed_hash["version"]) < get_version_index("0.8.5")) && ($xml_array["graph"][$field_name] == "0")) { /* backwards compatability */ $save[$field_name] = ""; }else{ $save[$field_name] = addslashes(xml_character_decode($xml_array["graph"][$field_name])); } } } $graph_template_graph_id = sql_save($save, "graph_templates_graph"); /* import into: graph_templates_item */ if (is_array($xml_array["items"])) { while (list($item_hash, $item_array) = each($xml_array["items"])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_graph_template_item_id = db_fetch_cell("select id from graph_templates_item where hash='" . $parsed_hash["hash"] . "' and graph_template_id=$graph_template_id and local_graph_id=0"); $save["id"] = (empty($_graph_template_item_id) ? "0" : $_graph_template_item_id); $save["hash"] = $parsed_hash["hash"]; $save["graph_template_id"] = $graph_template_id; reset($struct_graph_item); while (list($field_name, $field_array) = each($struct_graph_item)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { /* is the value of this field a hash or not? */ if (ereg("hash_([a-f0-9]{2})([a-f0-9]{4})([a-f0-9]{32})", $item_array[$field_name])) { $save[$field_name] = resolve_hash_to_id($item_array[$field_name], $hash_cache); }elseif (($field_name == "color_id") && (ereg("^[a-fA-F0-9]{6}$", $item_array[$field_name])) && (get_version_index($parsed_hash["version"]) >= get_version_index("0.8.5"))) { /* treat the 'color' field differently */ $color_id = db_fetch_cell("select id from colors where hex='" . $item_array[$field_name] . "'"); if (empty($color_id)) { db_execute("insert into colors (hex) values ('" . $item_array[$field_name] . "')"); $color_id = db_fetch_insert_id(); } $save[$field_name] = $color_id; }else{ $save[$field_name] = addslashes(xml_character_decode($item_array[$field_name])); } } } $graph_template_item_id = sql_save($save, "graph_templates_item"); $hash_cache["graph_template_item"]{$parsed_hash["hash"]} = $graph_template_item_id; } } /* import into: graph_template_input */ if (is_array($xml_array["inputs"])) { while (list($item_hash, $item_array) = each($xml_array["inputs"])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_graph_template_input_id = db_fetch_cell("select id from graph_template_input where hash='" . $parsed_hash["hash"] . "' and graph_template_id=$graph_template_id"); $save["id"] = (empty($_graph_template_input_id) ? "0" : $_graph_template_input_id); $save["hash"] = $parsed_hash["hash"]; $save["graph_template_id"] = $graph_template_id; reset($fields_graph_template_input_edit); while (list($field_name, $field_array) = each($fields_graph_template_input_edit)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { $save[$field_name] = addslashes(xml_character_decode($item_array[$field_name])); } } $graph_template_input_id = sql_save($save, "graph_template_input"); $hash_cache["graph_template_input"]{$parsed_hash["hash"]} = $graph_template_input_id; /* import into: graph_template_input_defs */ $hash_items = explode("|", $item_array["items"]); if (!empty($hash_items[0])) { for ($i=0; $i<count($hash_items); $i++) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($hash_items[$i]); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } if (isset($hash_cache["graph_template_item"]{$parsed_hash["hash"]})) { /* db_replace() doesn't replace primary key, delete then insert */ db_execute("delete from graph_template_input_defs where graph_template_input_id=$graph_template_input_id and graph_template_item_id = ".$hash_cache["graph_template_item"]{$parsed_hash["hash"]}); db_execute("insert into graph_template_input_defs (graph_template_input_id,graph_template_item_id) values($graph_template_input_id,".$hash_cache["graph_template_item"]{$parsed_hash["hash"]}.")"); } } } } } /* status information that will be presented to the user */ $_SESSION["import_debug_info"]["type"] = (empty($_graph_template_id) ? "new" : "update"); $_SESSION["import_debug_info"]["title"] = $xml_array["name"]; $_SESSION["import_debug_info"]["result"] = (empty($graph_template_id) ? "fail" : "success"); return $hash_cache; }
function ds_rrd_add() { /* ================= input validation ================= */ input_validate_input_number(get_request_var_request('id')); /* ==================================================== */ db_execute_prepared("INSERT INTO data_template_rrd (local_data_id, rrd_maximum, rrd_minimum, rrd_heartbeat, data_source_type_id, data_source_name) VALUES (?, 100, 0, 600, 1, 'ds')", array($_REQUEST['id'])); $data_template_rrd_id = db_fetch_insert_id(); header('Location: data_sources.php?action=ds_edit&id=' . $_REQUEST['id'] . "&view_rrd={$data_template_rrd_id}"); }
function &xml_to_graph_template($hash, &$xml_array, &$hash_cache, $hash_version) { global $struct_graph, $struct_graph_item, $fields_graph_template_input_edit, $hash_version_codes; /* import into: graph_templates */ $_graph_template_id = db_fetch_cell("select id from graph_templates where hash='{$hash}'"); $save["id"] = empty($_graph_template_id) ? "0" : $_graph_template_id; $save["hash"] = $hash; $save["name"] = $xml_array["name"]; $graph_template_id = sql_save($save, "graph_templates"); $hash_cache["graph_template"][$hash] = $graph_template_id; /* import into: graph_templates_graph */ unset($save); $save["id"] = empty($_graph_template_id) ? "0" : db_fetch_cell("select graph_templates_graph.id from (graph_templates,graph_templates_graph) where graph_templates.id=graph_templates_graph.graph_template_id and graph_templates.id={$graph_template_id} and graph_templates_graph.local_graph_id=0"); $save["graph_template_id"] = $graph_template_id; reset($struct_graph); while (list($field_name, $field_array) = each($struct_graph)) { /* make sure this field exists in the xml array first */ if (isset($xml_array["graph"]["t_" . $field_name])) { $save["t_" . $field_name] = $xml_array["graph"]["t_" . $field_name]; } /* make sure this field exists in the xml array first */ if (isset($xml_array["graph"][$field_name])) { /* Cacti pre 0.8.5 did handle a unit_exponent=0 differently * so we need to know the version of the current hash code we're just working on */ if ($field_name == "unit_exponent_value" && get_version_index($hash_version) < get_version_index("0.8.5") && $xml_array["graph"][$field_name] == "0") { /* backwards compatability */ $save[$field_name] = ""; } else { $save[$field_name] = addslashes(xml_character_decode($xml_array["graph"][$field_name])); } } } $graph_template_graph_id = sql_save($save, "graph_templates_graph"); /* import into: graph_templates_item */ if (is_array($xml_array["items"])) { while (list($item_hash, $item_array) = each($xml_array["items"])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_graph_template_item_id = db_fetch_cell("select id from graph_templates_item where hash='" . $parsed_hash["hash"] . "' and graph_template_id={$graph_template_id} and local_graph_id=0"); $save["id"] = empty($_graph_template_item_id) ? "0" : $_graph_template_item_id; $save["hash"] = $parsed_hash["hash"]; $save["graph_template_id"] = $graph_template_id; reset($struct_graph_item); while (list($field_name, $field_array) = each($struct_graph_item)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { /* is the value of this field a hash or not? */ if (preg_match("/hash_([a-f0-9]{2})([a-f0-9]{4})([a-f0-9]{32})/", $item_array[$field_name])) { $save[$field_name] = resolve_hash_to_id($item_array[$field_name], $hash_cache); } elseif ($field_name == "color_id" && preg_match("/^[a-fA-F0-9]{6}\$/", $item_array[$field_name]) && get_version_index($parsed_hash["version"]) >= get_version_index("0.8.5")) { /* treat the 'color' field differently */ $color_id = db_fetch_cell("select id from colors where hex='" . $item_array[$field_name] . "'"); if (empty($color_id)) { db_execute("insert into colors (hex) values ('" . $item_array[$field_name] . "')"); $color_id = db_fetch_insert_id(); } $save[$field_name] = $color_id; } else { $save[$field_name] = addslashes(xml_character_decode($item_array[$field_name])); } } } $graph_template_item_id = sql_save($save, "graph_templates_item"); $hash_cache["graph_template_item"][$parsed_hash["hash"]] = $graph_template_item_id; } } /* import into: graph_template_input */ if (is_array($xml_array["inputs"])) { while (list($item_hash, $item_array) = each($xml_array["inputs"])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_graph_template_input_id = db_fetch_cell("select id from graph_template_input where hash='" . $parsed_hash["hash"] . "' and graph_template_id={$graph_template_id}"); $save["id"] = empty($_graph_template_input_id) ? "0" : $_graph_template_input_id; $save["hash"] = $parsed_hash["hash"]; $save["graph_template_id"] = $graph_template_id; reset($fields_graph_template_input_edit); while (list($field_name, $field_array) = each($fields_graph_template_input_edit)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { $save[$field_name] = addslashes(xml_character_decode($item_array[$field_name])); } } $graph_template_input_id = sql_save($save, "graph_template_input"); $hash_cache["graph_template_input"][$parsed_hash["hash"]] = $graph_template_input_id; /* import into: graph_template_input_defs */ $hash_items = explode("|", $item_array["items"]); if (!empty($hash_items[0])) { for ($i = 0; $i < count($hash_items); $i++) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($hash_items[$i]); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } if (isset($hash_cache["graph_template_item"][$parsed_hash["hash"]])) { db_execute("replace into graph_template_input_defs (graph_template_input_id,graph_template_item_id) values ({$graph_template_input_id}," . $hash_cache["graph_template_item"][$parsed_hash["hash"]] . ")"); } } } } } /* status information that will be presented to the user */ $_SESSION["import_debug_info"]["type"] = empty($_graph_template_id) ? "new" : "update"; $_SESSION["import_debug_info"]["title"] = $xml_array["name"]; $_SESSION["import_debug_info"]["result"] = empty($graph_template_id) ? "fail" : "success"; return $hash_cache; }
function syslog_db_fetch_insert_id($syslog_cnn) { return db_fetch_insert_id($syslog_cnn); }
/** data_source_to_data_template - converts a data source to a data template @param int $local_data_id - the id of the data source to be converted @param string $data_source_title - the data source title to use for the new data template. the variable <ds_title> will be substituted for the current data source title */ function data_source_to_data_template($local_data_id, $data_source_title) { /* create a new graph template entry */ db_execute("insert into data_template (id,name,hash) values (0,'" . str_replace("<ds_title>", db_fetch_cell("select name from data_template_data where local_data_id=$local_data_id"), $data_source_title) . "','" . get_hash_data_template(0) . "')"); $data_template_id = db_fetch_insert_id(); /* update graph to point to the new template */ db_execute("update data_template_data set local_data_id=0,local_data_template_data_id=0,data_template_id=$data_template_id where local_data_id=$local_data_id"); db_execute("update data_template_rrd set local_data_id=0,local_data_template_rrd_id=0,data_template_id=$data_template_id where local_data_id=$local_data_id"); /* create hashes for the data template items */ $items = db_fetch_assoc("select id from data_template_rrd where data_template_id='$data_template_id' and local_data_id=0"); for ($j=0; $j<count($items); $j++) { db_execute("update data_template_rrd set hash='" . get_hash_data_template($items[$j]["id"], "data_template_item") . "' where id=" . $items[$j]["id"]); } /* delete the old graph local entry */ db_execute("delete from data_local where id=$local_data_id"); db_execute("delete from poller_item where local_data_id=$local_data_id"); }
function data_source_to_data_template($local_data_id, $data_source_title) { /* create a new graph template entry */ db_execute("INSERT INTO data_template (id,name,hash) values (0,'" . str_replace("<ds_title>", db_fetch_cell("SELECT name FROM data_template_data WHERE local_data_id={$local_data_id}"), $data_source_title) . "','" . get_hash_data_template(0) . "')"); $data_template_id = db_fetch_insert_id(); /* update graph to point to the new template */ db_execute("UPDATE data_template_data SET local_data_id=0,local_data_template_data_id=0,data_template_id={$data_template_id} WHERE local_data_id={$local_data_id}"); db_execute("UPDATE data_template_rrd SET local_data_id=0,local_data_template_rrd_id=0,data_template_id={$data_template_id} WHERE local_data_id={$local_data_id}"); /* create hashes for the data template items */ $items = db_fetch_assoc("SELECT id FROM data_template_rrd WHERE data_template_id='{$data_template_id}' AND local_data_id=0"); for ($j = 0; $j < count($items); $j++) { db_execute("UPDATE data_template_rrd SET hash='" . get_hash_data_template($items[$j]["id"], "data_template_item") . "' WHERE id=" . $items[$j]["id"]); } /* delete the old graph local entry */ db_execute("DELETE FROM data_local WHERE id={$local_data_id}"); db_execute("DELETE FROM poller_item WHERE local_data_id={$local_data_id}"); }