Example #1
0
function update_poller_cache($data_source_id, $truncate_performed = false) {
	global $c_xml_data;

	require_once(CACTI_BASE_PATH . "/include/data_query/data_query_constants.php");
	require_once(CACTI_BASE_PATH . "/include/data_source/data_source_constants.php");
	require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php");
	require_once(CACTI_BASE_PATH . "/lib/data_query/data_query_info.php");
	require_once(CACTI_BASE_PATH . "/lib/api_poller.php");

	if (empty($data_source_id)) {
		return;
	}

	/* clear cache for this local_data_id */
	if (!$truncate_performed) {
		db_execute("delete from poller_item where local_data_id = $data_source_id");
	}

	/* fetch information about this data source */
	$data_source = api_data_source_get($data_source_id);

	/* device is marked as disabled */
	if ((!empty($data_source["host_id"])) && (db_fetch_cell("select disabled from host where id = " . $data_source["host_id"]) == "on")) {
		return;
	}

	$field_list = array_rekey(db_fetch_assoc("select name,value from data_source_field where data_source_id = $data_source_id"), "name", "value");

	if (!empty($data_source["active"])) {
		if (($data_source["data_input_type"] == DATA_INPUT_TYPE_SCRIPT) && (isset($field_list["script_id"]))) {
			/* how does the script get its data? */
			$script_input_type = db_fetch_cell("select type_id from data_input where id = " . $field_list["script_id"]);

			/* fall back to non-script server actions if the user is running a version of php older than 4.3 */
			if (($script_input_type == SCRIPT_INPUT_TYPE_PHP_SCRIPT_SERVER) && (function_exists("proc_open"))) {
				$action = POLLER_ACTION_SCRIPT_PHP;
				$script_path = get_full_script_path($data_source_id);
			}else if (($script_input_type == SCRIPT_INPUT_TYPE_PHP_SCRIPT_SERVER) && (!function_exists("proc_open"))) {
				$action = POLLER_ACTION_SCRIPT;
				$script_path = read_config_option("path_php_binary") . " -q " . get_full_script_path($data_source_id);
			}else{
				$action = POLLER_ACTION_SCRIPT;
				$script_path = get_full_script_path($data_source_id);
			}

			$num_output_fields = db_fetch_cell("select count(*) from data_input_fields where data_input_id = " . $field_list["script_id"] . " and input_output = 'out' and update_rra = 1");

			api_poller_cache_item_add($data_source["host_id"], $data_source_id, $data_source["polling_interval"], $action, (($num_output_fields == 1) ? db_fetch_cell("select data_source_name from data_source_item where data_source_id = $data_source_id") : ""), 1, addslashes($script_path));
		}else if ($data_source["data_input_type"] == DATA_INPUT_TYPE_SNMP) {
			$data_source_items = db_fetch_assoc("select data_source_name,field_input_value from data_source_item where data_source_id = $data_source_id");

			if (sizeof($data_source_items) > 0) {
				foreach ($data_source_items as $item) {
					api_poller_cache_item_add($data_source["host_id"], $data_source_id, $data_source["polling_interval"], POLLER_ACTION_SNMP, $item["data_source_name"], 1, $item["field_input_value"]);
				}
			}
		}else if (($data_source["data_input_type"] == DATA_INPUT_TYPE_DATA_QUERY) && (isset($field_list["data_query_id"])) && (isset($field_list["data_query_index"]))) {
			/* how does this data query get its data? */
			$data_query = api_data_query_get($field_list["data_query_id"]);

			$data_query_input_type = db_fetch_cell("select input_type from snmp_query where id = " . $field_list["data_query_id"]);

			/* obtain a list of data source items for this data source */
			$data_source_items = db_fetch_assoc("select id,field_input_value,data_source_name from data_source_item where data_source_id = $data_source_id");

			if (sizeof($data_source_items) > 0) {
				foreach ($data_source_items as $item) {
					$data_query_field = api_data_query_field_get_by_name($field_list["data_query_id"], $item["field_input_value"]);

					if ($data_query["input_type"] == DATA_QUERY_INPUT_TYPE_SNMP_QUERY) {
						api_poller_cache_item_add($data_source["host_id"], $data_source_id, $data_source["polling_interval"], POLLER_ACTION_SNMP, $item["data_source_name"], sizeof($data_source_items), $data_query_field["source"] . "." . $field_list["data_query_index"]);
					}else if (($data_query["input_type"] == DATA_QUERY_INPUT_TYPE_SCRIPT_QUERY) || ($data_query["input_type"] == DATA_QUERY_INPUT_TYPE_PHP_SCRIPT_SERVER_QUERY)) {
						/* fall back to non-script server actions if the user is running a version of php older than 4.3 */
						if (($data_query["input_type"] == DATA_QUERY_INPUT_TYPE_PHP_SCRIPT_SERVER_QUERY) && (function_exists("proc_open"))) {
							$action = POLLER_ACTION_SCRIPT_PHP;
							$script_path = $data_query["script_path"] . " " . $data_query["script_server_function"] . " " . DATA_QUERY_SCRIPT_ARG_GET . " " . $data_query_field["source"] . " " . $field_list["data_query_index"];
						}else if (($data_query["input_type"] == DATA_QUERY_INPUT_TYPE_PHP_SCRIPT_SERVER_QUERY) && (!function_exists("proc_open"))) {
							$action = POLLER_ACTION_SCRIPT;
							$script_path = read_config_option("path_php_binary") . " -q " . $data_query["script_path"] . " " . DATA_QUERY_SCRIPT_ARG_GET . " " . $data_query_field["source"] . " " . $field_list["data_query_index"];
						}else{
							$action = POLLER_ACTION_SCRIPT;
							$script_path = $data_query["script_path"] . " " . DATA_QUERY_SCRIPT_ARG_GET . " " . $data_query_field["source"] . " " . $field_list["data_query_index"];
						}

						api_poller_cache_item_add($data_source["host_id"], $data_source_id, $data_source["polling_interval"], $action, $item["data_source_name"], sizeof($data_source_items), addslashes($script_path));
					}
				}
			}
		}
	}
}
Example #2
0
function rrdtool_function_create($data_source_id, $show_source, $rrd_struc, $syslog_facility = FACIL_POLLER) {
	require_once(CACTI_BASE_PATH . "/include/data_preset/data_preset_rra_constants.php");
	require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php");
	require_once(CACTI_BASE_PATH . "/lib/data_preset/data_preset_rra_info.php");

	$data_source_path = api_data_source_path_get($data_source_id, true);

	/* make sure the RRD file does not already exist */
	if (($show_source != true) && (file_exists($data_source_path) == true)) {
		return;
	}

	/* get the current data source */
	$data_source = api_data_source_get($data_source_id);

	/* create the "--step" line */
	$create_ds = RRD_NL . "--step ". $data_source["polling_interval"] . " " . RRD_NL;

	/* get a list of valid data source types (COUNTER, GAUGE, etc) */
	$data_source_types = api_data_source_type_list();

	/* query the data sources to be used in this .rrd file */
	$data_source_items = api_data_source_item_list($data_source_id);

	if (is_array($data_source_items) > 0) {
		foreach ($data_source_items as $data_source_item) {
			$create_ds .= "DS:" . $data_source_item["data_source_name"] . ":" . $data_source_types{$data_source_item["data_source_type"]} . ":" . $data_source_item["rrd_heartbeat"] . ":" . $data_source_item["rrd_minimum"] . ":" . (empty($data_source_item["rrd_maximum"]) ? "U" : $data_source_item["rrd_maximum"]) . RRD_NL;
		}
	}

	/* get a list of valid consolidation functions (AVERAGE, MAX, etc) */
	$rra_cf_types = api_data_preset_rra_cf_type_list();

	/* get a list of RRA's that have been associated with this data source */
	$rra_items = api_data_source_rra_item_list($data_source_id);

	/* if we find that this data source has no RRA associated; get out */
	if ((!is_array($rra_items)) || (sizeof($rra_items) == 0)) {
		log_save(_("There are no RRA's assigned to data_source_id: ") . $data_source_id . ".", SEV_ERROR, FACIL_POLLER);
		return false;
	}

	$create_rra = "";
	/* loop through each available RRA for this data source */
	foreach ($rra_items as $rra_item) {
		/* calculate the correct number of steps based on the RRA steps and the polling interval */
		$rra_steps = ceil($rra_item["steps"] / $data_source["polling_interval"]);

		/* calculate the correct number of rows based on the polling interval and the RRA
		 * retention interval */
		$rra_rows = ceil($rra_item["rows"] / ($data_source["polling_interval"] * $rra_steps));

		if (($rra_item["consolidation_function"] == RRA_CF_TYPE_AVERAGE) || ($rra_item["consolidation_function"] == RRA_CF_TYPE_MIN) || ($rra_item["consolidation_function"] == RRA_CF_TYPE_MAX) || ($rra_item["consolidation_function"] == RRA_CF_TYPE_LAST)) {
			$create_rra .= "RRA:" . $rra_cf_types{$rra_item["consolidation_function"]} . ":" . $rra_item["x_files_factor"] . ":" . $rra_steps . ":" . $rra_rows . RRD_NL;
		}else if ($rra_item["consolidation_function"] == RRA_CF_TYPE_HWPREDICT) {
			$create_rra .= "RRA:" . $rra_cf_types{$rra_item["consolidation_function"]} . ":" . $rra_item["rows"] . ":" . $rra_item["hw_alpha"] . ":" . $rra_item["hw_beta"] . ":" . $rra_item["hw_seasonal_period"] . (empty($rra_item["hw_rra_num"]) ? "" : ":" . $rra_item["hw_rra_num"]) . RRD_NL;
		}else if (($rra_item["consolidation_function"] == RRA_CF_TYPE_SEASONAL) || ($rra_item["consolidation_function"] == RRA_CF_TYPE_DEVSEASONAL)) {
			$create_rra .= "RRA:" . $rra_cf_types{$rra_item["consolidation_function"]} . ":" . $rra_item["hw_seasonal_period"] . ":" . $rra_item["hw_gamma"] . ":" . $rra_item["hw_rra_num"] . RRD_NL;
		}else if ($rra_item["consolidation_function"] == RRA_CF_TYPE_DEVPREDICT) {
			$create_rra .= "RRA:" . $rra_cf_types{$rra_item["consolidation_function"]} . ":" . $rra_item["rows"] . ":" . $rra_item["hw_rra_num"] . RRD_NL;
		}else if ($rra_item["consolidation_function"] == RRA_CF_TYPE_FAILURES) {
			$create_rra .= "RRA:" . $rra_cf_types{$rra_item["consolidation_function"]} . ":" . $rra_item["rows"] . ":" . $rra_item["hw_threshold"] . ":" . $rra_item["hw_window_length"] . ":" . $rra_item["hw_rra_num"] . RRD_NL;
		}
	}

	if ($show_source == true) {
		return read_config_option("path_rrdtool") . " create" . RRD_NL . "$data_source_path$create_ds$create_rra";
	}else{
		rrdtool_execute("create $data_source_path $create_ds$create_rra", true, RRDTOOL_OUTPUT_STDOUT, $rrd_struc, $syslog_facility);
	}
}