"; print "

$message

"; print "
"; } break; case 'error': print ""; print ""; print "

Error: $message


"; break; } } } } session_unregister("sess_messages"); } function clear_messages() { session_unregister("sess_messages"); } function array_rekey($array, $key, $key_value) { $ret_array = array(); if (sizeof($array) > 0) { foreach ($array as $item) { $item_key = $item[$key]; $ret_array[$item_key] = $item[$key_value]; } } return $ret_array; } function LogData($string) { global $config,$colors,$paths; /* fill in the current date for printing in the log */ $date = date("m/d/Y g:i A"); /* echo the data to the log (append) */ exec("echo '$date - $string' >> $paths[log]"); } function get_full_script_path($local_data_id) { global $paths; $data_source = db_fetch_row("select data_template_data.id, data_template_data.data_input_id, data_input.type_id, data_input.input_string from data_template_data,data_input where data_template_data.data_input_id=data_input.id and data_template_data.local_data_id=$local_data_id"); if ($data_source["type_id"] > 1) { return 0; } $data = db_fetch_assoc("select data_input_fields.data_name, data_input_data.value from data_input_fields left join data_input_data on data_input_fields.id=data_input_data.data_input_field_id where data_input_fields.data_input_id=" . $data_source["data_input_id"] . " and data_input_data.data_template_data_id=" . $data_source["id"] . " and data_input_fields.input_output='in'"); $full_path = $data_source["input_string"]; if (sizeof($data) > 0) { foreach ($data as $item) { $full_path = str_replace("<" . $item["data_name"] . ">", $item["value"], $full_path); } } $full_path = str_replace("", $paths["cacti"], $full_path); $full_path = str_replace("", read_config_option("path_snmpget"), $full_path); $full_path = str_replace("", read_config_option("path_php_binary"), $full_path); /* sometimes a certain input value will not have anything entered... null out these fields in the input string so we don't mess up the script */ $full_path = preg_replace("/(<[A-Za-z0-9_]+>)+/", "", $full_path); return $full_path; } function get_data_source_name($data_template_rrd_id) { if (empty($data_template_rrd_id)) { return ""; } $data_source = db_fetch_row("select data_template_rrd.data_source_name, data_template_data.name from data_template_rrd,data_template_data where data_template_rrd.local_data_id=data_template_data.local_data_id and data_template_rrd.id=$data_template_rrd_id"); /* use the cacti ds name by default or the user defined one, if entered */ if (empty($data_source["data_source_name"])) { /* limit input to 19 characters */ $data_source_name = clean_up_name($data_source["name"]); $data_source_name = substr(strtolower($data_source_name),0,(19-strlen($data_template_rrd_id))) . $data_template_rrd_id; return $data_source_name; }else{ return $data_source["data_source_name"]; } } function get_data_source_path($local_data_id, $expand_paths) { global $paths; if (empty($local_data_id)) { return ""; } $data_source = db_fetch_row("select name,data_source_path from data_template_data where local_data_id=$local_data_id"); if (sizeof($data_source) > 0) { if (empty($data_source["data_source_path"])) { /* no custom path was specified */ $data_source_path = generate_data_source_path($local_data_id); }else{ if (!strstr($data_source["data_source_path"], "/")) { $data_source_path = "/" . $data_source["data_source_path"]; }else{ $data_source_path = $data_source["data_source_path"]; } } /* whether to show the "actual" path or the variable name (for edit boxes) */ if ($expand_paths == true) { $data_source_path = str_replace("", $paths["rra"], $data_source_path); } return $data_source_path; } } function stri_replace($find, $replace, $string) { $parts = explode(strtolower($find), strtolower($string)); $pos = 0; foreach ($parts as $key=>$part) { $parts[$key] = substr($string, $pos, strlen($part)); $pos += strlen($part) + strlen($find); } return (join($replace, $parts)); } function clean_up_name($string) { $string = preg_replace("/[\s\.]+/", "_", $string); $string = preg_replace("/[*\/\*&%\"\',:]/", "", $string); return $string; } function subsitute_host_data($string, $l_escape_string, $r_escape_string, $host_id) { $host = db_fetch_row("select description,hostname,management_ip,snmp_community,snmp_version,snmp_username,snmp_password from host where id=$host_id"); $string = str_replace($l_escape_string . "host_hostname" . $r_escape_string, $host["hostname"], $string); $string = str_replace($l_escape_string . "host_description" . $r_escape_string, $host["description"], $string); $string = str_replace($l_escape_string . "host_management_ip" . $r_escape_string, $host["management_ip"], $string); $string = str_replace($l_escape_string . "host_snmp_community" . $r_escape_string, $host["snmp_community"], $string); $string = str_replace($l_escape_string . "host_snmp_version" . $r_escape_string, $host["snmp_version"], $string); $string = str_replace($l_escape_string . "host_snmp_username" . $r_escape_string, $host["snmp_username"], $string); $string = str_replace($l_escape_string . "host_snmp_password" . $r_escape_string, $host["snmp_password"], $string); return $string; } function subsitute_snmp_query_data($string, $l_escape_string, $r_escape_string, $host_id, $snmp_query_id, $snmp_index) { $snmp_cache_data = db_fetch_assoc("select field_name,field_value from host_snmp_cache where host_id=$host_id and snmp_query_id=$snmp_query_id and snmp_index=$snmp_index"); if (sizeof($snmp_cache_data) > 0) { foreach ($snmp_cache_data as $data) { $string = stri_replace($l_escape_string . "squery_" . $data["field_name"] . $r_escape_string, $data["field_value"], $string); } } return $string; } function generate_data_source_path($local_data_id) { $data_source_name = db_fetch_cell("select name from data_template_data where local_data_id=$local_data_id"); $new_path = "/" . substr(strtolower(clean_up_name($data_source_name)), 0, 15) . "_" . $local_data_id . ".rrd"; db_execute("update data_template_data set data_source_path='$new_path' where local_data_id=$local_data_id"); return $new_path; } function generate_graph_def_name($graph_item_id) { $lookup_table = array("a","b","c","d","e","f","g","h","i","j"); $result = ""; for($i=0; $i 0) { foreach ($graph_items as $item) { /* check to see if we are at the "target" spot in the loop; if we are, update the sequences and move on */ if ($target_id == $item["id"]) { if ($direction == "next") { $group_array1 = $target_graph_group_array; $group_array2 = $graph_group_array; }elseif ($direction == "previous") { $group_array1 = $graph_group_array; $group_array2 = $target_graph_group_array; } while (list($sequence,$graph_template_item_id) = each($group_array1)) { db_execute("update graph_templates_item set sequence=$sequence_counter where id=$graph_template_item_id"); /* propagate to ALL graphs using this template */ if (empty($graph_item["local_graph_id"])) { db_execute("update graph_templates_item set sequence=$sequence_counter where local_graph_template_item_id=$graph_template_item_id"); } $sequence_counter++; } while (list($sequence,$graph_template_item_id) = each($group_array2)) { db_execute("update graph_templates_item set sequence=$sequence_counter where id=$graph_template_item_id"); /* propagate to ALL graphs using this template */ if (empty($graph_item["local_graph_id"])) { db_execute("update graph_templates_item set sequence=$sequence_counter where local_graph_template_item_id=$graph_template_item_id"); } $sequence_counter++; } } /* make sure to "ignore" the items that we handled above */ if ((!isset($graph_group_array{$item["id"]})) && (!isset($target_graph_group_array{$item["id"]}))) { db_execute("update graph_templates_item set sequence=$sequence_counter where id=" . $item["id"]); $sequence_counter++; } } } } function get_graph_group($graph_template_item_id) { global $graph_item_types; $graph_item = db_fetch_row("select graph_type_id,sequence,local_graph_id,graph_template_id from graph_templates_item where id=$graph_template_item_id"); if (empty($graph_item["local_graph_id"])) { $sql_where = "graph_template_id = " . $graph_item["graph_template_id"] . " and local_graph_id=0"; }else{ $sql_where = "local_graph_id = " . $graph_item["local_graph_id"]; } /* a parent must NOT be the following graph item types */ if (ereg("(GPRINT|VRULE|HRULE|COMMENT)", $graph_item_types{$graph_item["graph_type_id"]})) { return 0; } $graph_item_children_array = array(); /* put the parent item in the array as well */ $graph_item_children_array[$graph_template_item_id] = $graph_template_item_id; $graph_items = db_fetch_assoc("select id,graph_type_id from graph_templates_item where sequence > " . $graph_item["sequence"] . " and $sql_where order by sequence"); if (sizeof($graph_items) > 0) { foreach ($graph_items as $item) { if ($graph_item_types{$item["graph_type_id"]} == "GPRINT") { /* a child must be a GPRINT */ $graph_item_children_array{$item["id"]} = $item["id"]; }else{ /* if not a GPRINT then get out */ return $graph_item_children_array; } } } return $graph_item_children_array; } function get_graph_parent($graph_template_item_id, $direction) { $graph_item = db_fetch_row("select sequence,local_graph_id,graph_template_id from graph_templates_item where id=$graph_template_item_id"); if (empty($graph_item["local_graph_id"])) { $sql_where = "graph_template_id = " . $graph_item["graph_template_id"] . " and local_graph_id=0"; }else{ $sql_where = "local_graph_id = " . $graph_item["local_graph_id"]; } if ($direction == "next") { $sql_operator = ">"; $sql_order = "ASC"; }elseif ($direction == "previous") { $sql_operator = "<"; $sql_order = "DESC"; } $next_parent_id = db_fetch_cell("select id from graph_templates_item where sequence $sql_operator " . $graph_item["sequence"] . " and graph_type_id != 9 and $sql_where order by sequence $sql_order limit 1"); if (empty($next_parent_id)) { return 0; }else{ return $next_parent_id; } } function get_item($tblname, $field, $startid, $lmt_query, $direction) { if ($direction == "next") { $sql_operator = ">"; $sql_order = "ASC"; }elseif ($direction == "previous") { $sql_operator = "<"; $sql_order = "DESC"; } $current_sequence = db_fetch_cell("select $field from $tblname where id=$startid"); $new_item_id = db_fetch_cell("select id from $tblname where $field $sql_operator $current_sequence and $lmt_query order by $field $sql_order limit 1"); if (empty($new_item_id)) { return $startid; }else{ return $new_item_id; } } function get_sequence($id, $field, $table_name, $group_query) { if (empty($id)) { $data = db_fetch_row("select max($field)+1 as seq from $table_name where $group_query"); if ($data["seq"] == "") { return 1; }else{ return $data["seq"]; } }else{ $data = db_fetch_row("select $field from $table_name where id=$id"); return $data[$field]; } } function move_item_down($table_name, $current_id, $group_query) { $next_item = get_item($table_name, "sequence", $current_id, $group_query, "next"); $sequence = db_fetch_cell("select sequence from $table_name where id=$current_id"); $sequence_next = db_fetch_cell("select sequence from $table_name where id=$next_item"); db_execute("update $table_name set sequence=$sequence_next where id=$current_id"); db_execute("update $table_name set sequence=$sequence where id=$next_item"); } function move_item_up($table_name, $current_id, $group_query) { $last_item = get_item($table_name, "sequence", $current_id, $group_query, "previous"); $sequence = db_fetch_cell("select sequence from $table_name where id=$current_id"); $sequence_last = db_fetch_cell("select sequence from $table_name where id=$last_item"); db_execute("update $table_name set sequence=$sequence_last where id=$current_id"); db_execute("update $table_name set sequence=$sequence where id=$last_item"); } function exec_into_array($command_line) { exec($command_line,$out,$err); $command_array = array(); for($i=0; list($key, $value) = each($out); $i++) { $command_array[$i] = $value; } return $command_array; } function convert_mac_address($mac_address) { return strtolower(str_replace(" ", ":", $mac_address)); } function get_web_browser() { if (stristr($_SERVER["HTTP_USER_AGENT"], "Mozilla") && (!(stristr($_SERVER["HTTP_USER_AGENT"], "compatible")))) { return "moz"; }elseif (stristr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { return "ie"; }else{ return "other"; } } function hex2bin($data) { $len = strlen($data); for($i=0;$i<$len;$i+=2) { $newdata .= pack("C",hexdec(substr($data,$i,2))); } return $newdata; } ?>