db_execute("UPDATE graph_templates_graph\r\n\t\t\t\t\t\t\tSET title = \"{$graphTitle}\"\r\n\t\t\t\t\t\t\tWHERE local_graph_id = {$existsAlready}"); update_graph_title_cache($existsAlready); } $dataSourceId = db_fetch_cell("SELECT\r\n\t\t\t\t\t\tdata_template_rrd.local_data_id\r\n\t\t\t\t\t\tFROM graph_templates_item, data_template_rrd\r\n\t\t\t\t\t\tWHERE graph_templates_item.local_graph_id = " . $existsAlready . "\r\n\t\t\t\t\t\tAND graph_templates_item.task_item_id = data_template_rrd.id\r\n\t\t\t\t\t\tLIMIT 1"); /* modify for multi user start */ if ($dataTitle != "") { db_execute("UPDATE data_template_data\r\n SET name=\"{$dataTitle}\"\r\n WHERE local_data_id=" . $dataSourceId); update_data_source_title_cache($dataSourceId); } /* modify for multi user end */ echo "NOTE: Not Adding Graph - this graph already exists - graph-id: ({$existsAlready}) - data-source-id: ({$dataSourceId})\n"; continue; } $empty = array(); /* Suggested Values are not been implemented */ $returnArray = create_complete_graph_from_template($templateId, $hostId, $snmp_query_array, $empty); if ($graphTitle != "") { db_execute("UPDATE graph_templates_graph\r\n\t\t\t\t\t\tSET title=\"{$graphTitle}\"\r\n\t\t\t\t\t\tWHERE local_graph_id=" . $returnArray["local_graph_id"]); update_graph_title_cache($returnArray["local_graph_id"]); } $dataSourceId = db_fetch_cell("SELECT\r\n\t\t\t\t\tdata_template_rrd.local_data_id\r\n\t\t\t\t\tFROM graph_templates_item, data_template_rrd\r\n\t\t\t\t\tWHERE graph_templates_item.local_graph_id = " . $returnArray["local_graph_id"] . "\r\n\t\t\t\t\tAND graph_templates_item.task_item_id = data_template_rrd.id\r\n\t\t\t\t\tLIMIT 1"); foreach ($returnArray["local_data_id"] as $item) { push_out_host($hostId, $item); if (strlen($dataSourceId)) { $dataSourceId .= ", " . $item; } else { $dataSourceId = $item; } } echo "Graph Added - graph-id: (" . $returnArray["local_graph_id"] . ") - data-source-ids: ({$dataSourceId})\n"; }
function host_new_graphs_save() { $selected_graphs_array = unserialize(stripslashes($_POST['selected_graphs_array'])); /* form an array that contains all of the data on the previous form */ while (list($var, $val) = each($_POST)) { if (preg_match('/^g_(\\d+)_(\\d+)_(\\w+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['graph_template'][$matches[3]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['graph_template'][$matches[3]] = $val; } } elseif (preg_match('/^gi_(\\d+)_(\\d+)_(\\d+)_(\\w+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: graph_template_input_id, 4:field_name */ /* ================= input validation ================= */ input_validate_input_number($matches[3]); /* ==================================================== */ /* we need to find out which graph items will be affected by saving this particular item */ $item_list = db_fetch_assoc_prepared('SELECT graph_template_item_id FROM graph_template_input_defs WHERE graph_template_input_id = ?', array($matches[3])); /* loop through each item affected and update column data */ if (sizeof($item_list) > 0) { foreach ($item_list as $item) { if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['graph_template_item'][$item['graph_template_item_id']][$matches[4]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['graph_template_item'][$item['graph_template_item_id']][$matches[4]] = $val; } } } } elseif (preg_match('/^d_(\\d+)_(\\d+)_(\\d+)_(\\w+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['data_template'][$matches[3]][$matches[4]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['data_template'][$matches[3]][$matches[4]] = $val; } } elseif (preg_match('/^c_(\\d+)_(\\d+)_(\\d+)_(\\d+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:data_input_field_id */ if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['custom_data'][$matches[3]][$matches[4]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['custom_data'][$matches[3]][$matches[4]] = $val; } } elseif (preg_match('/^di_(\\d+)_(\\d+)_(\\d+)_(\\d+)_(\\w+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:local_data_template_rrd_id, 5:field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['data_template_item'][$matches[4]][$matches[5]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['data_template_item'][$matches[4]][$matches[5]] = $val; } } } debug_log_clear('new_graphs'); while (list($form_type, $form_array) = each($selected_graphs_array)) { $current_form_type = $form_type; while (list($form_id1, $form_array2) = each($form_array)) { /* enumerate information from the arrays stored in post variables */ if ($form_type == 'cg') { $graph_template_id = $form_id1; } elseif ($form_type == 'sg') { while (list($form_id2, $form_array3) = each($form_array2)) { $snmp_index_array = $form_array3; $snmp_query_array['snmp_query_id'] = $form_id1; $snmp_query_array['snmp_index_on'] = get_best_data_query_index_type($_POST['host_id'], $form_id1); $snmp_query_array['snmp_query_graph_id'] = $form_id2; } $graph_template_id = db_fetch_cell_prepared('SELECT graph_template_id FROM snmp_query_graph WHERE id = ?', array($snmp_query_array['snmp_query_graph_id'])); } if ($current_form_type == 'cg') { $return_array = create_complete_graph_from_template($graph_template_id, $_POST['host_id'], '', $values['cg']); debug_log_insert('new_graphs', 'Created graph: ' . get_graph_title($return_array['local_graph_id'])); /* lastly push host-specific information to our data sources */ if (sizeof($return_array['local_data_id'])) { # we expect at least one data source associated foreach ($return_array['local_data_id'] as $item) { push_out_host($_POST['host_id'], $item); } } else { debug_log_insert('new_graphs', 'ERROR: no Data Source associated. Check Template'); } } elseif ($current_form_type == 'sg') { while (list($snmp_index, $true) = each($snmp_index_array)) { $snmp_query_array['snmp_index'] = decode_data_query_index($snmp_index, $snmp_query_array['snmp_query_id'], $_POST['host_id']); $return_array = create_complete_graph_from_template($graph_template_id, $_POST['host_id'], $snmp_query_array, $values['sg'][$snmp_query_array['snmp_query_id']]); debug_log_insert('new_graphs', 'Created graph: ' . get_graph_title($return_array['local_graph_id'])); /* lastly push host-specific information to our data sources */ if (sizeof($return_array['local_data_id'])) { # we expect at least one data source associated foreach ($return_array['local_data_id'] as $item) { push_out_host($_POST['host_id'], $item); } } else { debug_log_insert('new_graphs', 'ERROR: no Data Source associated. Check Template'); } } } } } }
function host_new_graphs_save() { $selected_graphs_array = unserialize(stripslashes($_POST["selected_graphs_array"])); /* form an array that contains all of the data on the previous form */ while (list($var, $val) = each($_POST)) { if (preg_match("/^g_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["graph_template"][$matches[3]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["graph_template"][$matches[3]] = $val; } } elseif (preg_match("/^gi_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: graph_template_input_id, 4:field_name */ /* ================= input validation ================= */ input_validate_input_number($matches[3]); /* ==================================================== */ /* we need to find out which graph items will be affected by saving this particular item */ $item_list = db_fetch_assoc("select\n\t\t\t\tgraph_template_item_id\n\t\t\t\tfrom graph_template_input_defs\n\t\t\t\twhere graph_template_input_id=" . $matches[3]); /* loop through each item affected and update column data */ if (sizeof($item_list) > 0) { foreach ($item_list as $item) { if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["graph_template_item"][$item["graph_template_item_id"]][$matches[4]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["graph_template_item"][$item["graph_template_item_id"]][$matches[4]] = $val; } } } } elseif (preg_match("/^d_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["data_template"][$matches[3]][$matches[4]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["data_template"][$matches[3]][$matches[4]] = $val; } } elseif (preg_match("/^c_(\\d+)_(\\d+)_(\\d+)_(\\d+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:data_input_field_id */ if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["custom_data"][$matches[3]][$matches[4]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["custom_data"][$matches[3]][$matches[4]] = $val; } } elseif (preg_match("/^di_(\\d+)_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:local_data_template_rrd_id, 5:field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["data_template_item"][$matches[4]][$matches[5]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["data_template_item"][$matches[4]][$matches[5]] = $val; } } } debug_log_clear("new_graphs"); while (list($form_type, $form_array) = each($selected_graphs_array)) { $current_form_type = $form_type; while (list($form_id1, $form_array2) = each($form_array)) { /* enumerate information from the arrays stored in post variables */ if ($form_type == "cg") { $graph_template_id = $form_id1; } elseif ($form_type == "sg") { while (list($form_id2, $form_array3) = each($form_array2)) { $snmp_index_array = $form_array3; $snmp_query_array["snmp_query_id"] = $form_id1; $snmp_query_array["snmp_index_on"] = get_best_data_query_index_type($_POST["host_id"], $form_id1); $snmp_query_array["snmp_query_graph_id"] = $form_id2; } $graph_template_id = db_fetch_cell("select graph_template_id from snmp_query_graph where id=" . $snmp_query_array["snmp_query_graph_id"]); } if ($current_form_type == "cg") { $return_array = create_complete_graph_from_template($graph_template_id, $_POST["host_id"], "", $values["cg"]); debug_log_insert("new_graphs", "Created graph: " . get_graph_title($return_array["local_graph_id"])); /* lastly push host-specific information to our data sources */ if (sizeof($return_array["local_data_id"])) { # we expect at least one data source associated foreach ($return_array["local_data_id"] as $item) { push_out_host($_POST["host_id"], $item); } } else { debug_log_insert("new_graphs", "ERROR: no Data Source associated. Check Template"); } } elseif ($current_form_type == "sg") { while (list($snmp_index, $true) = each($snmp_index_array)) { $snmp_query_array["snmp_index"] = decode_data_query_index($snmp_index, $snmp_query_array["snmp_query_id"], $_POST["host_id"]); $return_array = create_complete_graph_from_template($graph_template_id, $_POST["host_id"], $snmp_query_array, $values["sg"][$snmp_query_array["snmp_query_id"]]); debug_log_insert("new_graphs", "Created graph: " . get_graph_title($return_array["local_graph_id"])); /* lastly push host-specific information to our data sources */ if (sizeof($return_array["local_data_id"])) { # we expect at least one data source associated foreach ($return_array["local_data_id"] as $item) { push_out_host($_POST["host_id"], $item); } } else { debug_log_insert("new_graphs", "ERROR: no Data Source associated. Check Template"); } } } } } }
function create_cg_graph($args) { $hostId = $args["hostId"]; $description = $args["description"]; $templateId = $args["GraphTemplateId"]; $values["cg"] = array(); // Not doing anything with this for now if (!isset($hostId) || !isset($description) || !isset($templateId)) { echo "ERROR: create_cg_graph: Missing required arguments\n"; exit(1); } $existsAlready = db_fetch_cell("SELECT id \n FROM graph_local \n WHERE graph_template_id={$templateId} \n AND host_id={$hostId}"); if (isset($existsAlready) && $existsAlready > 0) { debug("{$description}: Graph already exists: ({$existsAlready})"); return 0; } else { $returnArray = create_complete_graph_from_template($templateId, $hostId, "", $values["cg"]); echo "{$description}: Added Graph id: " . $returnArray["local_graph_id"] . "\n"; push_out_host($hostId, 0); return 1; } }
function createDQGraph($snmp_query_array, $graphTitle, $force) { /* is this data query already associated (independent of the reindex method) */ $exists_already = db_fetch_cell("SELECT COUNT(device_id) FROM device_snmp_query WHERE device_id=" . $snmp_query_array["device_id"] . " AND snmp_query_id=" . $snmp_query_array["snmp_query_id"]); if ((isset($exists_already)) && ($exists_already > 0)) { /* yes: do nothing, everything's fine */ }else{ db_execute("REPLACE INTO device_snmp_query (device_id,snmp_query_id,reindex_method) " . "VALUES (" . $snmp_query_array["device_id"] . "," . $snmp_query_array["snmp_query_id"] . "," . $snmp_query_array["reindex_method"] . ")"); /* recache snmp data, this is time consuming, * but should happen only once even if multiple graphs * are added for the same data query * because we checked above, if dq was already associated */ run_data_query($snmp_query_array["device_id"], $snmp_query_array["snmp_query_id"]); } $snmp_query_array["snmp_index_on"] = get_best_data_query_index_type($snmp_query_array["device_id"], $snmp_query_array["snmp_query_id"]); $snmp_indexes = db_fetch_assoc("SELECT snmp_index " . "FROM device_snmp_cache " . "WHERE device_id=" . $snmp_query_array["device_id"] . " " . "AND snmp_query_id=" . $snmp_query_array["snmp_query_id"] . " " . "AND field_name='" . $snmp_query_array["snmp_field"] . "' " . "AND field_value='" . $snmp_query_array["snmp_value"] . "'"); if (sizeof($snmp_indexes)) { foreach ($snmp_indexes as $snmp_index) { $snmp_query_array["snmp_index"] = $snmp_index["snmp_index"]; $existsAlready = db_fetch_cell("SELECT id " . "FROM graph_local " . "WHERE graph_template_id=" . $snmp_query_array["graph_template_id"] . " " . "AND device_id=" . $snmp_query_array["device_id"] . " " . "AND snmp_query_id=" . $snmp_query_array["snmp_query_id"] . " " . "AND snmp_index='" . $snmp_query_array["snmp_index"] . "'"); if (isset($existsAlready) && $existsAlready > 0) { $dataSourceId = db_fetch_cell("SELECT data_template_rrd.local_data_id FROM graph_templates_item, data_template_rrd WHERE graph_templates_item.local_graph_id = " . $existsAlready . " AND graph_templates_item.task_item_id = data_template_rrd.id LIMIT 1"); echo __("NOTE: Not Adding Graph - this graph already exists - graph-id: (%d) - data-source-id: (%d)", $existsAlready, $dataSourceId) . "\n"; continue; } $empty = array(); /* Suggested Values are not been implemented */ $returnArray = create_complete_graph_from_template($snmp_query_array["graph_template_id"], $snmp_query_array["device_id"], $snmp_query_array, $empty); if ($graphTitle != "") { db_execute("UPDATE graph_templates_graph " . "SET title='" . $graphTitle ."' " . "WHERE local_graph_id=" . $returnArray["local_graph_id"]); update_graph_title_cache($returnArray["local_graph_id"]); } $dataSourceId = db_fetch_cell("SELECT " . "data_template_rrd.local_data_id " . "FROM graph_templates_item, data_template_rrd " . "WHERE graph_templates_item.local_graph_id = " . $returnArray["local_graph_id"] . " " . "AND graph_templates_item.task_item_id = data_template_rrd.id " . "LIMIT 1"); foreach($returnArray["local_data_id"] as $item) { push_out_device($snmp_query_array["device_id"], $item); $dataSourceId .= (strlen($dataSourceId) ? ", " : "") . $item; } echo __("Graph Added - graph-id: (%d) - data-source-ids: (%d)", $returnArray["local_graph_id"], $dataSourceId) . "\n"; } }else{ echo __("ERROR: Could not find snmp-field %s (%d) for device-id %d (%s)", $snmp_query_array["snmp_field"], $snmp_query_array["snmp_value"], $snmp_query_array["device_id"], $devices[$snmp_query_array["device_id"]]["hostname"]) . "\n"; echo __("Try php -q graph_list.php --device-id=%s --list-snmp-fields", $snmp_query_array["device_id"]) . "\n"; exit(1); } }