Beispiel #1
0
function api_data_query_snmp_execute($host_id, $data_query_id) {
	require_once(CACTI_BASE_PATH . "/include/data_query/data_query_constants.php");
	require_once(CACTI_BASE_PATH . "/lib/data_query/data_query_info.php");
	require_once(CACTI_BASE_PATH . "/lib/device/device_info.php");

	/* get information about the data query */
	$data_query = api_data_query_get($data_query_id);

	/* get a list of all input fields defined for this data query */
	$data_query_fields = api_data_query_field_list($data_query_id, DATA_QUERY_FIELD_TYPE_INPUT);

	/* fetch a list of indexes for this data query */
	$field_values{$data_query["index_field_id"]} = api_data_query_snmp_execute_field($host_id, $data_query["index_field_id"]);

	if (($field_values{$data_query["index_field_id"]} === false) || (sizeof($field_values{$data_query["index_field_id"]}) == 0)) {
		debug_log_insert("data_query", _("No indexes returned, cannot continue."));
		return false;
	}

	/* reindex the parsed index values as a hash (value->oid) for quicker access. DUPLICATE INDEX VALUES
	 * WILL CAUSE PROBLEMS HERE */
	foreach ($field_values{$data_query["index_field_id"]} as $result) {
		$index_field_values{$result["value_parsed"]} = true;
	}

	/* clear old data from the data query cache */
	db_execute("delete from host_data_query_cache where host_id = " . sql_sanitize($host_id) . " and data_query_id = " . sql_sanitize($data_query_id));

	if (is_array($data_query_fields)) {
		foreach ($data_query_fields as $field) {
			/* fetch a list of values for this field (assuming that we haven't already seen it */
			if (!isset($field_values{$field["id"]})) {
				$field_values{$field["id"]} = api_data_query_snmp_execute_field($host_id, $field["id"]);
			}else{
				debug_log_insert("data_query", "Walking OID '" . $field["source"] . "' (cached)");
			}

			/* see if we have some output to play with */
			if (($field_values{$field["id"]} !== false) && (sizeof($field_values{$field["id"]}) > 0)) {
				foreach ($field_values{$field["id"]} as $oid => $result) {
					/* stick with the 0.8.x behavior: use the value for the index when the actual value is
					 * derived from the oid */
					if (($field["method_type"] == DATA_QUERY_FIELD_METHOD_OID_OCTET) || ($field["method_type"] == DATA_QUERY_FIELD_METHOD_OID_PARSE)) {
						$expected_index = $result["value"];
					/* find the index at the end of the oid */
					}else{
						$expected_index = substr($oid, strlen($field["source"])+1);
					}

					/* a match for this index has been located */
					if (isset($index_field_values[$expected_index])) {
						debug_log_insert("data_query", sprintf(_("Found value [%s = '%s'] for index [%s]"), $field["name"], $result["value_parsed"], $expected_index));

						db_insert("host_data_query_cache",
							array(
								"host_id" => array("type" => DB_TYPE_INTEGER, "value" => $host_id),
								"data_query_id" => array("type" => DB_TYPE_INTEGER, "value" => $data_query_id),
								"field_name" => array("type" => DB_TYPE_STRING, "value" => $field["name"]),
								"field_value" => array("type" => DB_TYPE_STRING, "value" => $result["value_parsed"]),
								"index_value" => array("type" => DB_TYPE_STRING, "value" => $expected_index),
								"oid" => array("type" => DB_TYPE_STRING, "value" => $oid)
								),
							array("host_id", "data_query_id", "field_name", "index_value"));
					/* a match for this index has not been located */
					}else{
						debug_log_insert("data_query", _("Ignoring unknown index '$expected_index'."));
					}
				}
			}else{
				debug_log_insert("data_query", _("No values returned from the field '" . $field["name"] . "', ignoring."));
			}
		}
	}

	return true;
}
Beispiel #2
0
function data_query_edit()
{
    $_data_query_id = get_get_var_number("id");
    if (empty($_data_query_id)) {
        $header_label = "[new]";
    } else {
        $data_query = api_data_query_get($_data_query_id);
        $header_label = "[edit: " . $data_query["name"] . "]";
    }
    form_start("data_queries.php", "form_data_query");
    html_start_box("<strong>" . _("Data Queries") . "</strong> {$header_label}");
    _data_query_field__name("name", isset($data_query["name"]) ? $data_query["name"] : "", isset($data_query["id"]) ? $data_query["id"] : "0");
    _data_query_field__input_type("input_type", isset($data_query["input_type"]) ? $data_query["input_type"] : "", isset($data_query["id"]) ? $data_query["id"] : "0");
    _data_query_field__index_order_type("index_order_type", isset($data_query["index_order_type"]) ? $data_query["index_order_type"] : "", isset($data_query["id"]) ? $data_query["id"] : "0");
    _data_query_field__index_title_format("index_title_format", isset($data_query["index_title_format"]) ? $data_query["index_title_format"] : "|chosen_order_field|", isset($data_query["id"]) ? $data_query["id"] : "0");
    if (!empty($_data_query_id)) {
        _data_query_field__field_specific_hdr();
        _data_query_field__index_order("index_order", isset($data_query["index_order"]) ? $data_query["index_order"] : "", isset($data_query["id"]) ? $data_query["id"] : "0");
        _data_query_field__index_field_id("index_field_id", $_data_query_id, isset($data_query["index_field_id"]) ? $data_query["index_field_id"] : "", isset($data_query["id"]) ? $data_query["id"] : "0");
    }
    /* input type specific fields */
    _data_query_field__snmp_specific_hdr();
    _data_query_field__snmp_oid_num_rows("snmp_oid_num_rows", isset($data_query["snmp_oid_num_rows"]) ? $data_query["snmp_oid_num_rows"] : "", isset($data_query["id"]) ? $data_query["id"] : "0");
    _data_query_field__script_specific_hdr();
    _data_query_field__script_path("script_path", isset($data_query["script_path"]) ? $data_query["script_path"] : "", isset($data_query["id"]) ? $data_query["id"] : "0");
    _data_query_field__script_server_specific_hdr();
    _data_query_field__script_server_function("script_server_function", isset($data_query["script_server_function"]) ? $data_query["script_server_function"] : "", isset($data_query["id"]) ? $data_query["id"] : "0");
    /* be sure that we have the correct input type value show we display the correct form rows */
    if (isset_post_cache_field("input_type")) {
        $_input_type = get_post_cache_field("input_type");
    } else {
        $_input_type = isset($data_query["input_type"]) ? $data_query["input_type"] : "";
    }
    echo "<script language=\"JavaScript\">\n<!--\nupdate_data_query_type_fields('{$_input_type}');\n-->\n</script>\n";
    html_end_box();
    if (!empty($_data_query_id)) {
        html_start_box("<strong>" . _("Data Query Fields") . "</strong>");
        ?>
		<tr class="heading">
			<td colspan="2">
				Input Fields
			</td>
			<td align="right">
				<a href="data_queries.php?action=field_edit&field_type=<?php 
        echo DATA_QUERY_FIELD_TYPE_INPUT;
        ?>
&data_query_id=<?php 
        echo $_data_query_id;
        ?>
">Add</a>
			</td>
		</tr>
		<?php 
        $input_fields = api_data_query_field_list($_data_query_id, DATA_QUERY_FIELD_TYPE_INPUT);
        if (sizeof($input_fields) > 0) {
            foreach ($input_fields as $field) {
                ?>
				<tr class="item" id="row_<?php 
                echo $field["id"];
                ?>
" onClick="display_row_select('row_<?php 
                echo $field["id"];
                ?>
', 'chk_<?php 
                echo $field["id"];
                ?>
')" onMouseOver="display_row_hover('row_<?php 
                echo $field["id"];
                ?>
')" onMouseOut="display_row_clear('row_<?php 
                echo $field["id"];
                ?>
')">
					<td class="title">
						<a class="linkEditMain" onClick="display_row_block('row_<?php 
                echo $field["id"];
                ?>
')" href="data_queries.php?action=field_edit&id=<?php 
                echo $field["id"];
                ?>
&data_query_id=<?php 
                echo $field["data_query_id"];
                ?>
"><?php 
                echo $field["name"];
                ?>
</a>
					</td>
					<td>
						<?php 
                echo $field["name_desc"];
                ?>
					</td>
					<td align="right">
						<input type='checkbox' id='chk_<?php 
                echo $field["id"];
                ?>
' name='chk_<?php 
                echo $field["id"];
                ?>
' title="<?php 
                echo $field["name"];
                ?>
">
					</td>
				</tr>
				<?php 
            }
        } else {
            ?>
			<tr>
				<td class="empty" colspan="2">
					No input fields found. Remember that <strong>at least one index field</strong> must be defined!
				</td>
			</tr>
			<?php 
        }
        ?>
		<tr class="heading">
			<td colspan="2">
				Output Fields
			</td>
			<td align="right">
				<a href="data_queries.php?action=field_edit&field_type=<?php 
        echo DATA_QUERY_FIELD_TYPE_OUTPUT;
        ?>
&data_query_id=<?php 
        echo $_data_query_id;
        ?>
">Add</a>
			</td>
		</tr>
		<?php 
        $output_fields = api_data_query_field_list($_data_query_id, DATA_QUERY_FIELD_TYPE_OUTPUT);
        if (sizeof($output_fields) > 0) {
            foreach ($output_fields as $field) {
                ?>
				<tr class="item" id="row_<?php 
                echo $field["id"];
                ?>
" onClick="display_row_select('row_<?php 
                echo $field["id"];
                ?>
', 'chk_<?php 
                echo $field["id"];
                ?>
')" onMouseOver="display_row_hover('row_<?php 
                echo $field["id"];
                ?>
')" onMouseOut="display_row_clear('row_<?php 
                echo $field["id"];
                ?>
')">
					<td class="item">
						<a class="linkEditMain" onClick="display_row_block('row_<?php 
                echo $field["id"];
                ?>
')" href="data_queries.php?action=field_edit&id=<?php 
                echo $field["id"];
                ?>
&data_query_id=<?php 
                echo $field["data_query_id"];
                ?>
"><?php 
                echo $field["name"];
                ?>
</a>
					</td>
					<td>
						<?php 
                echo $field["name_desc"];
                ?>
					</td>
					<td align="right">
						<input type='checkbox' id='chk_<?php 
                echo $field["id"];
                ?>
' name='chk_<?php 
                echo $field["id"];
                ?>
' title="<?php 
                echo $field["name"];
                ?>
">
					</td>
				</tr>
				<?php 
            }
        } else {
            ?>
			<tr class="empty">
				<td colspan="2">
					No output fields found.
				</td>
			</tr>
			<?php 
        }
        html_end_box();
    }
    form_hidden_box("data_query_id", $_data_query_id);
    form_save_button("data_queries.php", "save_data_query");
}
Beispiel #3
0
function update_reindex_cache($host_id, $data_query_id) {
	require_once(CACTI_BASE_PATH . "/lib/sys/snmp.php");
	require_once(CACTI_BASE_PATH . "/include/data_query/data_query_constants.php");
	require_once(CACTI_BASE_PATH . "/lib/device/device_info.php");
	require_once(CACTI_BASE_PATH . "/lib/data_query/data_query_info.php");

	/* will be used to keep track of sql statements to execute later on */
	$recache_stack = array();

	/* get information about the host */
	$host = api_device_get($host_id);

	/* get information about the host->data query assignment */
	$host_data_query = api_device_data_query_get($host_id, $data_query_id);

	/* get information about the data query */
	$data_query = api_data_query_get($data_query_id);

	switch ($host_data_query["reindex_method"]) {
		case DATA_QUERY_AUTOINDEX_NONE:
			break;
		case DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME:
			/* the uptime backwards method requires snmp, so make sure snmp is actually enabled
			 * on this device first */
			if ($host["snmp_community"] != "") {
				$assert_value = cacti_snmp_get($host["hostname"],
					$host["snmp_community"],
					".1.3.6.1.2.1.1.3.0",
					$host["snmp_version"],
					$host["snmpv3_auth_username"],
					$host["snmpv3_auth_password"],
					$host["snmpv3_auth_protocol"],
					$host["snmpv3_priv_passphrase"],
					$host["snmpv3_priv_protocol"],
					$host["snmp_port"],
					$host["snmp_timeout"],
					SNMP_POLLER);

				array_push($recache_stack, "insert into poller_reindex (host_id,data_query_id,action,op,assert_value,arg1) values (" . sql_sanitize($host_id) . "," . sql_sanitize($data_query_id) . ",0,'<','" . sql_sanitize($assert_value) . "','.1.3.6.1.2.1.1.3.0')");
			}

			break;
		case DATA_QUERY_AUTOINDEX_INDEX_NUM_CHANGE:
			/* this method requires that some command/oid can be used to determine the
			 * current number of indexes in the data query */
			$assert_value = api_data_query_cache_num_rows_get($data_query_id, $host_id);

			if ($data_query_type == DATA_QUERY_INPUT_TYPE_SNMP_QUERY) {
				if ($data_query["snmp_oid_num_rows"] != "") {
					array_push($recache_stack, "insert into poller_reindex (host_id,data_query_id,action,op,assert_value,arg1) values (" . sql_sanitize($host_id) . "," . sql_sanitize($data_query_id) . ",0,'=','" . sql_sanitize($assert_value) . "','" . sql_sanitize($data_query["snmp_oid_num_rows"]) . "')");
				}
			}else if ($data_query_type == DATA_QUERY_INPUT_TYPE_SCRIPT_QUERY) {
				array_push($recache_stack, "insert into poller_reindex (host_id,data_query_id,action,op,assert_value,arg1) values (" . sql_sanitize($host_id) . "," . sql_sanitize($data_query_id) . ",1,'=','" . sql_sanitize($assert_value) . "','" . sql_sanitize(api_data_query_script_path_format($data_query_xml["script_path"]) . DATA_QUERY_SCRIPT_ARG_NUM_INDEXES) . "')");
			}

			break;
		case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION:
			$primary_indexes = api_data_query_cache_field_get($data_query_id, $host_id, $data_query["sort_field"]);

			if (sizeof($primary_indexes) > 0) {
				foreach ($primary_indexes as $index) {
					$assert_value = $index["field_value"];

					if ($data_query_type == DATA_QUERY_INPUT_TYPE_SNMP_QUERY) {
						array_push($recache_stack, "insert into poller_reindex (host_id,data_query_id,action,op,assert_value,arg1) values (" . sql_sanitize($host_id) . "," . sql_sanitize($data_query_id) . ",0,'=','" . sql_sanitize($assert_value) . "','" . sql_sanitize($index["oid"]) . "')");
					}else if ($data_query_type == DATA_QUERY_INPUT_TYPE_SCRIPT_QUERY) {
						array_push($recache_stack, "insert into poller_reindex (host_id,data_query_id,action,op,assert_value,arg1) values (" . sql_sanitize($host_id) . "," . sql_sanitize($data_query_id) . ",1,'=','" . sql_sanitize($assert_value) . "','" . sql_sanitize(api_data_query_script_path_format($data_query_xml["script_path"]) . DATA_QUERY_SCRIPT_ARG_GET . " " . $data_query_xml["fields"]{$data_query["sort_field"]}["query_name"] . " " . $index["snmp_index"]) . "')");
					}
				}
			}

			break;
	}

	/* save the delete for last since we need to reference this table in the code above */
	db_execute("delete from poller_reindex where host_id=$host_id and data_query_id=$data_query_id");

	for ($i=0; $i<count($recache_stack); $i++) {
		db_execute($recache_stack[$i]);
	}
}
Beispiel #4
0
function get_ordered_index_type_list($data_query_id, $host_id) {
	require_once(CACTI_BASE_PATH . "/include/data_query/data_query_constants.php");

	/* sanity checks */
	validate_id_die($data_query_id, "data_query_id");
	validate_id_die($host_id, "host_id");

	/* retrieve information about this data query */
	$data_query = api_data_query_get($data_query_id);

	/* get a list of all input fields for this data query */
	$data_query_fields = array_rekey(api_data_query_field_list($data_query_id, DATA_QUERY_FIELD_TYPE_INPUT), "name", "name_desc");

	$valid_index_fields = array();
	if (sizeof($data_query_fields) > 0) {
		foreach ($data_query_fields as $data_query_field_name => $data_query_field_description) {
			/* create a list of all values for this index */
			$field_values = db_fetch_assoc("select field_value from host_data_query_cache where host_id = " . sql_sanitize($host_id) . " and data_query_id = " . sql_sanitize($data_query_id) . " and field_name = '" . sql_sanitize($data_query_field_name) . "'");

			/* aggregate the above list so there are no duplicates */
			$field_values_nodups = array_rekey($field_values, "field_value", "field_value");

			/* fields that contain duplicate or empty values are not suitable to index off of */
			if (!((sizeof($field_values_nodups) < sizeof($field_values)) || (in_array("", $field_values_nodups) == true) || (sizeof($field_values_nodups) == 0))) {
				array_push($valid_index_fields, $data_query_field_name);
			}
		}
	}

	$return_array = array();

	/* the xml file contains an ordered list of "indexable" fields */
	if (ereg("^([a-zA-Z0-9_-]:?)+$", $data_query["index_order"])) {
		$index_order_array = explode(":", $data_query["index_order"]);

		for ($i=0; $i<count($index_order_array); $i++) {
			if (in_array($index_order_array[$i], $valid_index_fields)) {
				$return_array{$index_order_array[$i]} = $index_order_array[$i] . " (" . $data_query_fields{$index_order_array[$i]} . ")";
			}
		}
	/* the xml file does not contain a field list, ignore the order */
	}else{
		for ($i=0; $i<count($valid_index_fields); $i++) {
			$return_array{$valid_index_fields[$i]} = $valid_index_fields[$i] . " (" . $data_query_fields{$index_order_array[$i]} . ")";
		}
	}

	return $return_array;
}
Beispiel #5
0
function &package_data_query_export($data_query_id, $indent = 3) {
	require_once(CACTI_BASE_PATH . "/lib/data_query/data_query_info.php");

	$xml = "";

	/*
	 * XML Tag: <data_query>
	 */

	/* obtain a list of all data query specific fields */
	$data_query_fields = api_data_query_form_list();
	/* obtain a copy of this specfic data query */
	$data_query = api_data_query_get($data_query_id);

	$_xml = "";
	foreach (array_keys($data_query_fields) as $field_name) {
		/* create an XML key for each data query field */
		$_xml .= package_xml_tag_get($field_name, xml_character_encode($data_query[$field_name]), $indent + 2);
	}

	/* append the result onto the final XML string */
	$xml .= package_xml_tag_get("data_query", $_xml, $indent + 1, true);

	/*
	 * XML Tag: <fields>
	 */

	/* obtain a list of all data query field specific fields */
	$data_query_field_fields = api_data_query_field_form_list();
	/* obtain a list of all data query fields associated with this data query */
	$data_query_fields = api_data_query_field_list($data_query_id);

	$_xml = "";
	if (sizeof($data_query_fields) > 0) {
		$i = 0;
		foreach ($data_query_fields as $data_query_field) {
			$__xml = "";
			foreach (array_keys($data_query_field_fields) as $field_name) {
				/* create an XML key for each data query item field */
				$__xml .= package_xml_tag_get($field_name, xml_character_encode($data_query_field[$field_name]), $indent + 3);
			}

			/* append the result onto a temporary XML string */
			$_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("fields", $_xml, $indent + 1, true);

	/* wrap the whole XML string into a 'data_query' tag and return it */
	$xml = package_xml_tag_get(package_hash_get($data_query_id, "data_query"), $xml, $indent, true);

	return $xml;
}
Beispiel #6
0
function update_data_query_sort_cache($host_id, $data_query_id) {
	require_once(CACTI_BASE_PATH . "/lib/data_query/data_query_info.php");

	/* sanity check for $host_id */
	if ((!is_numeric($host_id)) || (empty($host_id))) {
		log_save("Invalid input '$host_id' for 'host_id' in " . __FUNCTION__ . "()", SEV_ERROR);
		return false;
	}

	/* sanity check for $data_query_id */
	if ((!is_numeric($data_query_id)) || (empty($data_query_id))) {
		log_save("Invalid input '$data_query_id' for 'data_query_id' in " . __FUNCTION__ . "()", SEV_ERROR);
		return false;
	}

	/* retrieve information about this data query */
	$data_query = api_data_query_get($data_query_id);

	/* get a list of valid data query types */
	$valid_index_types = get_ordered_index_type_list($data_query_id, $host_id);

	/* something is probably wrong with the data query */
	if (sizeof($valid_index_types) == 0) {
		$sort_field = "";
	}else{
		/* grab the first field off the list */
		list($sort_field, $sort_field_formatted) = each($valid_index_types);
	}

	/* substitute variables */
	if (isset($raw_xml["index_title_format"])) {
		$title_format = str_replace("|chosen_order_field|", "|query_$sort_field|", $data_query["index_title_format"]);
	}else{
		$title_format = "|query_$sort_field|";
	}

	/* update the cache */
	db_update("host_data_query",
		array(
			"sort_field" => array("type" => DB_TYPE_STRING, "value" => $sort_field),
			"title_format" => array("type" => DB_TYPE_STRING, "value" => $title_format),
			"host_id" => array("type" => DB_TYPE_INTEGER, "value" => $host_id),
			"data_query_id" => array("type" => DB_TYPE_INTEGER, "value" => $data_query_id)
			),
		array("host_id", "data_query_id"));
}