示例#1
0
function update_poller_cache($local_data_id, $commit = false) {
	global $config;
	require_once(CACTI_BASE_PATH . "/include/data_input/data_input_constants.php");
	include_once(CACTI_BASE_PATH . "/lib/data_query.php");
	include_once(CACTI_BASE_PATH . "/lib/api_poller.php");

	$poller_items = array();

	$data_input = db_fetch_row("select
		data_input.id,
		data_input.type_id,
		data_template_data.id as data_template_data_id,
		data_template_data.data_template_id,
		data_template_data.active,
		data_template_data.rrd_step
		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");

	$data_source = db_fetch_row("select device_id,snmp_query_id,snmp_index from data_local where id=$local_data_id");

	/* we have to perform some additional sql queries if this is a "query" */
	if (($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) ||
		($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY) ||
		($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER)){
		$field = data_query_field_list($data_input["data_template_data_id"]);

		if (strlen($field["output_type"])) {
			$output_type_sql = "and snmp_query_graph_rrd.snmp_query_graph_id=" . $field["output_type"];
		}else{
			$output_type_sql = "";
		}

		$outputs = db_fetch_assoc("select
			snmp_query_graph_rrd.snmp_field_name,
			data_template_rrd.id as data_template_rrd_id
			from (snmp_query_graph_rrd,data_template_rrd)
			where snmp_query_graph_rrd.data_template_rrd_id=data_template_rrd.local_data_template_rrd_id
			$output_type_sql
			and snmp_query_graph_rrd.data_template_id=" . $data_input["data_template_id"] . "
			and data_template_rrd.local_data_id=$local_data_id");
	}

	if ($data_input["active"] == CHECKED) {
		if (($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT) || ($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER)) { /* script */
			/* fall back to non-script server actions if the user is running a version of php older than 4.3 */
			if (($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) && (function_exists("proc_open"))) {
				$action = POLLER_ACTION_SCRIPT_PHP;
				$script_path = get_full_script_path($local_data_id);
			}else if (($data_input["type_id"] == DATA_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($local_data_id);
			}else{
				$action = POLLER_ACTION_SCRIPT;
				$script_path = get_full_script_path($local_data_id);
			}

			$num_output_fields = sizeof(db_fetch_assoc("select id from data_input_fields where data_input_id=" . $data_input["id"] . " and input_output='out' and update_rra='on'"));

			if ($num_output_fields == 1) {
				$data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id=$local_data_id");
				$data_source_item_name = get_data_source_item_name($data_template_rrd_id);
			}else{
				$data_source_item_name = "";
			}

			$poller_items[] = api_poller_cache_item_add($data_source["device_id"], array(), $local_data_id, $data_input["rrd_step"], $action, $data_source_item_name, 1, addslashes($script_path));
		}else if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP) { /* snmp */
			/* get the device override fields */
			$data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id=$local_data_id");

			/* get device fields first */
			$device_fields = array_rekey(db_fetch_assoc("SELECT
				data_input_fields.type_code,
				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 and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")
				WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))
				AND data_input_data.value != ''"), "type_code", "value");

			$data_template_fields = array_rekey(db_fetch_assoc("SELECT
				data_input_fields.type_code,
				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 and data_input_data.data_template_data_id=$data_template_id)
				WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))
				AND data_template_data_id=$data_template_id
				AND data_input_data.value != ''"), "type_code", "value");

			if (sizeof($device_fields)) {
				if (sizeof($data_template_fields)) {
				foreach($data_template_fields as $key => $value) {
					if (!isset($device_fields[$key])) {
						$device_fields[$key] = $value;
					}
				}
				}
			} elseif (sizeof($data_template_fields)) {
				$device_fields = $data_template_fields;
			}

			$data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id=$local_data_id");

			$poller_items[] = api_poller_cache_item_add($data_source["device_id"], $device_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($data_template_rrd_id), 1, (isset($device_fields["snmp_oid"]) ? $device_fields["snmp_oid"] : ""));
		}else if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) { /* snmp query */
			$snmp_queries = get_data_query_array($data_source["snmp_query_id"]);

			/* get the device override fields */
			$data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id=$local_data_id");

			/* get device fields first */
			$device_fields = array_rekey(db_fetch_assoc("SELECT
				data_input_fields.type_code,
				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 and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")
				WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))
				AND data_input_data.value != ''"), "type_code", "value");

			$data_template_fields = array_rekey(db_fetch_assoc("SELECT
				data_input_fields.type_code,
				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 and data_input_data.data_template_data_id=$data_template_id)
				WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))
				AND data_template_data_id=$data_template_id
				AND data_input_data.value != ''"), "type_code", "value");

			if (sizeof($device_fields)) {
				if (sizeof($data_template_fields)) {
				foreach($data_template_fields as $key => $value) {
					if (!isset($device_fields[$key])) {
						$device_fields[$key] = $value;
					}
				}
				}
			} elseif (sizeof($data_template_fields)) {
				$device_fields = $data_template_fields;
			}

			if (sizeof($outputs) > 0) {
			foreach ($outputs as $output) {
				if (isset($snmp_queries["fields"]{$output["snmp_field_name"]}["oid"])) {
					$oid_suffix = $data_source["snmp_index"];
					if(isset($snmp_queries["fields"]{$output["snmp_field_name"]}["rewrite_index"])){
						$oid_suffix = data_query_rewrite_indexes($errmsg, $data_source["device_id"], $data_source["snmp_query_id"], $snmp_queries["fields"]{$output["snmp_field_name"]}["rewrite_index"], $oid_suffix);
						if($oid_suffix == NULL){ // rewriting index failed for some reason
							if(sizeof($errmsg)){
								foreach($errmsg as $message){
									cacti_log(__("Field '%s':", $output["snmp_field_name"]) . $message, false, "POLLER");
								}
							}
							continue;
						}
					}
					$oid = $snmp_queries["fields"]{$output["snmp_field_name"]}["oid"] . "." . $oid_suffix;

					if (isset($snmp_queries["fields"]{$output["snmp_field_name"]}["oid_suffix"])) {
						$oid .= "." . $snmp_queries["fields"]{$output["snmp_field_name"]}["oid_suffix"];
					}
				}

				if (!empty($oid)) {
					$poller_items[] = api_poller_cache_item_add($data_source["device_id"], $device_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), $oid);
				}
			}
			}
		}else if (($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY) || ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER)) { /* script query */
			$script_queries = get_data_query_array($data_source["snmp_query_id"]);

			/* get the device override fields */
			$data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id=$local_data_id");

			/* get device fields first */
			$device_fields = array_rekey(db_fetch_assoc("SELECT
				data_input_fields.type_code,
				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 and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")
				WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))
				AND data_input_data.value != ''"), "type_code", "value");

			$data_template_fields = array_rekey(db_fetch_assoc("SELECT
				data_input_fields.type_code,
				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 and data_input_data.data_template_data_id=$data_template_id)
				WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))
				AND data_template_data_id=$data_template_id
				AND data_input_data.value != ''"), "type_code", "value");

			if (sizeof($device_fields)) {
				if (sizeof($data_template_fields)) {
				foreach($data_template_fields as $key => $value) {
					if (!isset($device_fields[$key])) {
						$device_fields[$key] = $value;
					}
				}
				}
			} elseif (sizeof($data_template_fields)) {
				$device_fields = $data_template_fields;
			}

			if (sizeof($outputs) > 0) {
				foreach ($outputs as $output) {
					if (isset($script_queries["fields"]{$output["snmp_field_name"]}["query_name"])) {
						$identifier = $script_queries["fields"]{$output["snmp_field_name"]}["query_name"];

						/* fall back to non-script server actions if the user is running a version of php older than 4.3 */
						if (($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) && (function_exists("proc_open"))) {
							$action = POLLER_ACTION_SCRIPT_PHP;
							$script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"] . " " . $script_queries["script_function"], $data_source["device_id"]);
						}else if (($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) && (!function_exists("proc_open"))) {
							$action = POLLER_ACTION_SCRIPT;
							$script_path = read_config_option("path_php_binary") . " -q " . get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["device_id"]);
						}else{
							$action = POLLER_ACTION_SCRIPT;
							$script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["device_id"]);
						}
					}

					if (isset($script_path)) {
						$poller_items[] = api_poller_cache_item_add($data_source["device_id"], $device_fields, $local_data_id, $data_input["rrd_step"], $action, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), addslashes($script_path));
					}
				}
			}
		}
	}

	if ($commit) {
		poller_update_poller_cache_from_buffer((array)$local_data_id, $poller_items);
	} else {
		return $poller_items;
	}
}
示例#2
0
function query_script_host($host_id, $snmp_query_id) {
	$script_queries = get_data_query_array($snmp_query_id);

	/* invalid xml check */
	if ((!is_array($script_queries)) || (sizeof($script_queries) == 0)) {
		debug_log_insert("data_query", "Error parsing XML file into an array.");
		return false;
	}

	debug_log_insert("data_query", "XML file parsed ok.");

	if (isset($script_queries["script_server"])) {
		$script_queries["script_path"] = "|path_php_binary| -q " . $script_queries["script_path"];
	}

	$script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_index"], $script_queries["script_path"], $host_id);

	/* fetch specified index at specified OID */
	$script_index_array = exec_into_array($script_path);

	debug_log_insert("data_query", "Executing script for list of indexes '$script_path'");

	db_execute("delete from host_snmp_cache where host_id=$host_id and snmp_query_id=$snmp_query_id");

	while (list($field_name, $field_array) = each($script_queries["fields"])) {
		if ($field_array["direction"] == "input") {
			$script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_query"] . " " . $field_array["query_name"], $script_queries["script_path"], $host_id);

			$script_data_array = exec_into_array($script_path);

			debug_log_insert("data_query", "Executing script query '$script_path'");

			for ($i=0;($i<sizeof($script_data_array));$i++) {
				if (preg_match("/(.*)" . preg_quote($script_queries["output_delimeter"]) . "(.*)/", $script_data_array[$i], $matches)) {
					$script_index = $matches[1];
					$field_value = $matches[2];

					db_execute("replace into host_snmp_cache
						(host_id,snmp_query_id,field_name,field_value,snmp_index,oid)
						values ($host_id,$snmp_query_id,'$field_name','$field_value','$script_index','')");

					debug_log_insert("data_query", "Found item [$field_name='$field_value'] index: $script_index");
				}
			}
		}
	}

	return true;
}
示例#3
0
function query_script_host($host_id, $snmp_query_id)
{
    $script_queries = get_data_query_array($snmp_query_id);
    /* invalid xml check */
    if (!is_array($script_queries) || sizeof($script_queries) == 0) {
        debug_log_insert("data_query", "Error parsing XML file into an array.");
        return false;
    }
    debug_log_insert("data_query", "XML file parsed ok.");
    /* are we talking to script server? */
    if (isset($script_queries["script_server"])) {
        $script_queries["script_path"] = "\"|path_php_binary|\" -q " . $script_queries["script_path"];
    }
    if (!verify_index_order($script_queries)) {
        debug_log_insert("data_query", "Invalid field &lt;index_order&gt;" . $script_queries["index_order"] . "&lt;/index_order&gt;");
        debug_log_insert("data_query", "Must contain &lt;direction&gt;input&lt;/direction&gt; fields only");
        return false;
    }
    /* provide data for arg_num_indexes, if given */
    if (isset($script_queries["arg_num_indexes"])) {
        $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " " : "") . $script_queries["arg_num_indexes"], $script_queries["script_path"], $host_id);
        /* fetch specified index at specified OID */
        $script_num_index_array = exec_into_array($script_path);
        debug_log_insert("data_query", "Executing script for num of indexes" . " '{$script_path}'");
        for ($i = 0; $i < sizeof($script_num_index_array); $i++) {
            debug_log_insert("data_query", "Found number of indexes: " . $script_num_index_array[$i]);
        }
    } else {
        if (isset($script_queries["script_server"])) {
            debug_log_insert("data_query", "&lt;arg_num_indexes&gt; missing in XML file, 'Index Count Changed' not supported");
        } else {
            debug_log_insert("data_query", "&lt;arg_num_indexes&gt; missing in XML file, 'Index Count Changed' emulated by counting arg_index entries");
        }
    }
    /* provide data for index, mandatory */
    $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " " : "") . $script_queries["arg_index"], $script_queries["script_path"], $host_id);
    /* fetch specified index */
    $script_index_array = exec_into_array($script_path);
    debug_log_insert("data_query", "Executing script for list of indexes" . " '{$script_path}' " . "Index Count: " . sizeof($script_index_array));
    for ($i = 0; $i < sizeof($script_index_array); $i++) {
        debug_log_insert("data_query", "Found index: " . $script_index_array[$i]);
    }
    /* set an array to host all updates */
    $output_array = array();
    while (list($field_name, $field_array) = each($script_queries["fields"])) {
        if ($field_array["direction"] == "input") {
            $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " " : "") . $script_queries["arg_query"] . " " . $field_array["query_name"], $script_queries["script_path"], $host_id);
            $script_data_array = exec_into_array($script_path);
            debug_log_insert("data_query", "Executing script query '{$script_path}'");
            for ($i = 0; $i < sizeof($script_data_array); $i++) {
                if (preg_match("/(.*)" . preg_quote($script_queries["output_delimeter"]) . "(.*)/", $script_data_array[$i], $matches)) {
                    $script_index = $matches[1];
                    $field_value = $matches[2];
                    $output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $field_value, $script_index, '');
                    debug_log_insert("data_query", "Found item [{$field_name}='{$field_value}'] index: {$script_index}");
                }
            }
        }
    }
    data_query_update_host_cache_from_buffer($host_id, $snmp_query_id, $output_array);
    return true;
}
示例#4
0
文件: poller.php 项目: teddywen/cacti
function update_reindex_cache($host_id, $data_query_id)
{
    global $config;
    include_once $config["library_path"] . "/data_query.php";
    include_once $config["library_path"] . "/snmp.php";
    /* will be used to keep track of sql statements to execute later on */
    $recache_stack = array();
    $host = db_fetch_row("select hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout from host where id={$host_id}");
    $data_query = db_fetch_row("select reindex_method, sort_field from host_snmp_query where host_id={$host_id} and snmp_query_id={$data_query_id}");
    $data_query_type = db_fetch_cell("select data_input.type_id from (data_input,snmp_query) where data_input.id=snmp_query.data_input_id and snmp_query.id={$data_query_id}");
    $data_query_xml = get_data_query_array($data_query_id);
    switch ($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_version"] > 0) {
                if (isset($data_query_xml["oid_uptime"])) {
                    $oid_uptime = $data_query_xml["oid_uptime"];
                } elseif (isset($data_query_xml["uptime_oid"])) {
                    $oid_uptime = $data_query_xml["uptime_oid"];
                } else {
                    $oid_uptime = ".1.3.6.1.2.1.1.3.0";
                }
                $assert_value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid_uptime, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], SNMP_POLLER);
                $recache_stack[] = "('{$host_id}', '{$data_query_id}'," . POLLER_ACTION_SNMP . ", '<', '{$assert_value}', '{$oid_uptime}', '1')";
            }
            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
             * pay ATTENTION to quoting!
             * the script parameters are usually enclosed in single tics: '
             * so we have to enclose the whole list of parameters in double tics: "
             * */
            /* the assert_value counts the number of distinct indexes currently available in host_snmp_cache
             * we do NOT make use of <oid_num_indexes> or the like!
             * this works, even if no <oid_num_indexes> was given
             */
            $assert_value = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id={$host_id} and snmp_query_id={$data_query_id} group by snmp_index"));
            /* now, we have to build the (list of) commands that are later used on a recache event
             * the result of those commands will be compared to the assert_value we have just computed
             * on a comparison failure, a reindex event will be generated
             */
            switch ($data_query_type) {
                case DATA_INPUT_TYPE_SNMP_QUERY:
                    if (isset($data_query_xml["oid_num_indexes"])) {
                        /* we have a specific OID for counting indexes */
                        $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP . ", '=', '{$assert_value}', '" . $data_query_xml["oid_num_indexes"] . "', '1')";
                    } else {
                        /* count all indexes found */
                        $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP_COUNT . ", '=', '{$assert_value}', '" . $data_query_xml["oid_index"] . "', '1')";
                    }
                    break;
                case DATA_INPUT_TYPE_SCRIPT_QUERY:
                    if (isset($data_query_xml["arg_num_indexes"])) {
                        /* we have a specific request for counting indexes */
                        /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */
                        $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id)) . "', '1')";
                    } else {
                        /* count all indexes found */
                        /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */
                        $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT_COUNT . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_index"], $data_query_xml["script_path"], $host_id)) . "', '1')";
                    }
                    break;
                case DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER:
                    if (isset($data_query_xml["arg_num_indexes"])) {
                        /* we have a specific request for counting indexes */
                        /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */
                        $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT_PHP . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path($data_query_xml["script_function"] . " " . (isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id)) . "', '1')";
                    } else {
                        /* count all indexes found */
                        # TODO: push the correct assert value
                        /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */
                        #$recache_stack[] = "($host_id, $data_query_id," . POLLER_ACTION_SCRIPT_PHP_COUNT . ", '=', '$assert_value', '" . addslashes(get_script_query_path($data_query_xml["script_function"] . " " . (isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_index"], $data_query_xml["script_path"], $host_id)) . "', '1')";
                        # omit the assert value until we are able to run an 'index' command through script server
                    }
                    break;
            }
            break;
        case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION:
            $primary_indexes = db_fetch_assoc("select snmp_index,oid,field_value from host_snmp_cache where host_id={$host_id} and snmp_query_id={$data_query_id} and field_name='" . $data_query["sort_field"] . "'");
            if (sizeof($primary_indexes) > 0) {
                foreach ($primary_indexes as $index) {
                    $assert_value = $index["field_value"];
                    if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) {
                        $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP . ", '=', '{$assert_value}', '" . $data_query_xml["fields"][$data_query["sort_field"]]["oid"] . "." . $index["snmp_index"] . "', '1')";
                    } else {
                        if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) {
                            $recache_stack[] = "('{$host_id}', '{$data_query_id}'," . POLLER_ACTION_SCRIPT . ", '=', '{$assert_value}', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_get"] . " " . $data_query_xml["fields"][$data_query["sort_field"]]["query_name"] . " " . $index["snmp_index"], $data_query_xml["script_path"], $host_id) . "', '1')";
                        }
                    }
                }
            }
            break;
    }
    if (sizeof($recache_stack)) {
        poller_update_poller_reindex_from_buffer($host_id, $data_query_id, $recache_stack);
    }
}
示例#5
0
function update_poller_cache($local_data_id, $truncate_performed = false) {
	global $config;

	include_once($config["library_path"] . "/data_query.php");
	include_once($config["library_path"] . "/api_poller.php");

	$data_input = db_fetch_row("select
		data_input.id,
		data_input.type_id,
		data_template_data.id as data_template_data_id,
		data_template_data.data_template_id,
		data_template_data.active,
		data_template_data.rrd_step
		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");

	$data_source = db_fetch_row("select host_id,snmp_query_id,snmp_index from data_local where id=$local_data_id");

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

	/* we have to perform some additional sql queries if this is a "query" */
	if (($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) ||
		($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY) ||
		($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER)){
		$field = data_query_field_list($data_input["data_template_data_id"]);

		if (($data_input["type_id"] != DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) &&
			(empty($field["output_type"]))) { return; }

		if (strlen($field["output_type"])) {
			$output_type_sql = "and snmp_query_graph_rrd.snmp_query_graph_id=" . $field["output_type"];
		}else{
			$output_type_sql = "";
		}

		$outputs = db_fetch_assoc("select
			snmp_query_graph_rrd.snmp_field_name,
			data_template_rrd.id as data_template_rrd_id
			from (snmp_query_graph_rrd,data_template_rrd)
			where snmp_query_graph_rrd.data_template_rrd_id=data_template_rrd.local_data_template_rrd_id
			$output_type_sql
			and snmp_query_graph_rrd.data_template_id=" . $data_input["data_template_id"] . "
			and data_template_rrd.local_data_id=$local_data_id");
	}

	if ($data_input["active"] == "on") {
		if (($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT) || ($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER)) { /* script */
			/* fall back to non-script server actions if the user is running a version of php older than 4.3 */
			if (($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) && (function_exists("proc_open"))) {
				$action = POLLER_ACTION_SCRIPT_PHP;
				$script_path = get_full_script_path($local_data_id);
			}else if (($data_input["type_id"] == DATA_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($local_data_id);
			}else{
				$action = POLLER_ACTION_SCRIPT;
				$script_path = get_full_script_path($local_data_id);
			}

			$num_output_fields = sizeof(db_fetch_assoc("select id from data_input_fields where data_input_id=" . $data_input["id"] . " and input_output='out' and update_rra='on'"));

			if ($num_output_fields == 1) {
				$data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id=$local_data_id");
				$data_source_item_name = get_data_source_item_name($data_template_rrd_id);
			}else{
				$data_source_item_name = "";
			}

			api_poller_cache_item_add($data_source["host_id"], array(), $local_data_id, $data_input["rrd_step"], $action, $data_source_item_name, 1, addslashes($script_path));
		}else if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP) { /* snmp */
			$host_fields = array_rekey(db_fetch_assoc("select
				data_input_fields.type_code,
				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 and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")
				where (data_input_fields.type_code='snmp_oid'
					or data_input_fields.type_code='hostname'
					or data_input_fields.type_code='snmp_community'
					or data_input_fields.type_code='snmp_username'
					or data_input_fields.type_code='snmp_password'
					or data_input_fields.type_code='snmp_auth_protocol'
					or data_input_fields.type_code='snmp_priv_passphrase'
					or data_input_fields.type_code='snmp_priv_protocol'
					or data_input_fields.type_code='snmp_context'
					or data_input_fields.type_code='snmp_version'
					or data_input_fields.type_code='snmp_port'
					or data_input_fields.type_code='snmp_timeout')
				and data_input_data.value != ''"), "type_code", "value");

			$data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id=$local_data_id");

			api_poller_cache_item_add($data_source["host_id"], $host_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($data_template_rrd_id), 1, (isset($host_fields["snmp_oid"]) ? $host_fields["snmp_oid"] : ""));
		}else if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) { /* snmp query */
			$snmp_queries = get_data_query_array($data_source["snmp_query_id"]);

			if (sizeof($outputs) > 0) {
			foreach ($outputs as $output) {
				if (isset($snmp_queries["fields"]{$output["snmp_field_name"]}["oid"])) {
					$oid = $snmp_queries["fields"]{$output["snmp_field_name"]}["oid"] . "." . $data_source["snmp_index"];
				}

				if (!empty($oid)) {
					api_poller_cache_item_add($data_source["host_id"], array(), $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), $oid);
				}
			}
			}
		}else if (($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY) || ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER)) { /* script query */
			$script_queries = get_data_query_array($data_source["snmp_query_id"]);

			if (sizeof($outputs) > 0) {
				foreach ($outputs as $output) {
					if (isset($script_queries["fields"]{$output["snmp_field_name"]}["query_name"])) {
						$identifier = $script_queries["fields"]{$output["snmp_field_name"]}["query_name"];

						/* fall back to non-script server actions if the user is running a version of php older than 4.3 */
						if (($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) && (function_exists("proc_open"))) {
							$action = POLLER_ACTION_SCRIPT_PHP;
							$script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"] . " " . $script_queries["script_function"], $data_source["host_id"]);
						}else if (($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) && (!function_exists("proc_open"))) {
							$action = POLLER_ACTION_SCRIPT;
							$script_path = read_config_option("path_php_binary") . " -q " . get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["host_id"]);
						}else{
							$action = POLLER_ACTION_SCRIPT;
							$script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["host_id"]);
						}
					}

					if (isset($script_path)) {
						api_poller_cache_item_add($data_source["host_id"], array(), $local_data_id, $data_input["rrd_step"], $action, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), addslashes($script_path));
					}
				}
			}
		}
	}
}
示例#6
0
文件: poller.php 项目: songchin/Cacti
function update_reindex_cache($host_id, $data_query_id) {
	global $config;

	include_once($config["library_path"] . "/data_query.php");
	include_once($config["library_path"] . "/snmp.php");

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

	$host            = db_fetch_row("select hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout from host where id=$host_id");
	$data_query      = db_fetch_row("select reindex_method, sort_field from host_snmp_query where host_id=$host_id and snmp_query_id=$data_query_id");
	$data_query_type = db_fetch_cell("select data_input.type_id from (data_input,snmp_query) where data_input.id=snmp_query.data_input_id and snmp_query.id=$data_query_id");
	$data_query_xml  = get_data_query_array($data_query_id);

	switch ($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"] != "") {
				if (isset($data_query_xml["oid_uptime"])) {
					$oid_uptime = $data_query_xml["uptime_oid"];
				}else{
					$oid_uptime = ".1.3.6.1.2.1.1.3.0";
				}

				$assert_value = cacti_snmp_get($host["hostname"],
					$host["snmp_community"],
					$oid_uptime,
					$host["snmp_version"],
					$host["snmp_username"],
					$host["snmp_password"],
					$host["snmp_auth_protocol"],
					$host["snmp_priv_passphrase"],
					$host["snmp_priv_protocol"],
					$host["snmp_context"],
					$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 ($host_id,$data_query_id,0,'<','$assert_value','$oid_uptime')");
			}

			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 = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=$host_id and snmp_query_id=$data_query_id group by snmp_index"));

			if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) {
				if (isset($data_query_xml["oid_num_indexes"])) {
					array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["oid_num_indexes"] . "')");
				}
			}else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) {
				if (isset($data_query_xml["arg_num_indexes"])) {
					array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id) . "')");
				}
			}

			break;
		case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION:
			$primary_indexes = db_fetch_assoc("select snmp_index,oid,field_value from host_snmp_cache where host_id=$host_id and snmp_query_id=$data_query_id and field_name='" . $data_query["sort_field"] . "'");

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

					if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) {
						array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["fields"]{$data_query["sort_field"]}["oid"] . "." . $index["snmp_index"] . "')");
					}else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) {
						array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_get"] . " " . $data_query_xml["fields"]{$data_query["sort_field"]}["query_name"] . " " . $index["snmp_index"], $data_query_xml["script_path"], $host_id) . "')");
					}
				}
			}

			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]);
	}
}
示例#7
0
function update_poller_cache($local_data_id, $commit = false)
{
    global $config;
    include_once $config["library_path"] . "/data_query.php";
    include_once $config["library_path"] . "/api_poller.php";
    $poller_items = array();
    $data_input = db_fetch_row("select\r\n\t\tdata_input.id,\r\n\t\tdata_input.type_id,\r\n\t\tdata_template_data.id as data_template_data_id,\r\n\t\tdata_template_data.data_template_id,\r\n\t\tdata_template_data.active,\r\n\t\tdata_template_data.rrd_step\r\n\t\tfrom (data_template_data,data_input)\r\n\t\twhere data_template_data.data_input_id=data_input.id\r\n\t\tand data_template_data.local_data_id={$local_data_id}");
    $data_source = db_fetch_row("select host_id,snmp_query_id,snmp_index from data_local where id={$local_data_id}");
    /* we have to perform some additional sql queries if this is a "query" */
    if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY || $data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY || $data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) {
        $field = data_query_field_list($data_input["data_template_data_id"]);
        if (strlen($field["output_type"])) {
            $output_type_sql = "and snmp_query_graph_rrd.snmp_query_graph_id='" . $field["output_type"] . "'";
        } else {
            $output_type_sql = "";
        }
        $outputs = db_fetch_assoc("select\r\n\t\t\tsnmp_query_graph_rrd.snmp_field_name,\r\n\t\t\tdata_template_rrd.id as data_template_rrd_id\r\n\t\t\tfrom (snmp_query_graph_rrd,data_template_rrd)\r\n\t\t\twhere snmp_query_graph_rrd.data_template_rrd_id=data_template_rrd.local_data_template_rrd_id\r\n\t\t\t{$output_type_sql}\r\n\t\t\tand snmp_query_graph_rrd.data_template_id=" . $data_input["data_template_id"] . "\r\n\t\t\tand data_template_rrd.local_data_id={$local_data_id}\r\n\t\t\torder by data_template_rrd.id");
    }
    if ($data_input["active"] == "on") {
        if ($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT || $data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) {
            /* script */
            /* fall back to non-script server actions if the user is running a version of php older than 4.3 */
            if ($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER && function_exists("proc_open")) {
                $action = POLLER_ACTION_SCRIPT_PHP;
                $script_path = get_full_script_path($local_data_id);
            } else {
                if ($data_input["type_id"] == DATA_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($local_data_id);
                } else {
                    $action = POLLER_ACTION_SCRIPT;
                    $script_path = get_full_script_path($local_data_id);
                }
            }
            $num_output_fields = sizeof(db_fetch_assoc("select id from data_input_fields where data_input_id=" . $data_input["id"] . " and input_output='out' and update_rra='on'"));
            if ($num_output_fields == 1) {
                $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id={$local_data_id}");
                $data_source_item_name = get_data_source_item_name($data_template_rrd_id);
            } else {
                $data_source_item_name = "";
            }
            $poller_items[] = api_poller_cache_item_add($data_source["host_id"], array(), $local_data_id, $data_input["rrd_step"], $action, $data_source_item_name, 1, addslashes($script_path));
        } else {
            if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP) {
                /* snmp */
                /* get the host override fields */
                $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id={$local_data_id}");
                /* get host fields first */
                $host_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname') OR (type_code='host_id'))\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value");
                $data_template_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id={$data_template_id})\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_template_data_id={$data_template_id}\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value");
                if (sizeof($host_fields)) {
                    if (sizeof($data_template_fields)) {
                        foreach ($data_template_fields as $key => $value) {
                            if (!isset($host_fields[$key])) {
                                $host_fields[$key] = $value;
                            }
                        }
                    }
                } elseif (sizeof($data_template_fields)) {
                    $host_fields = $data_template_fields;
                }
                $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id={$local_data_id}");
                $poller_items[] = api_poller_cache_item_add($data_source["host_id"], $host_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($data_template_rrd_id), 1, isset($host_fields["snmp_oid"]) ? $host_fields["snmp_oid"] : "");
            } else {
                if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) {
                    /* snmp query */
                    $snmp_queries = get_data_query_array($data_source["snmp_query_id"]);
                    /* get the host override fields */
                    $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id={$local_data_id}");
                    /* get host fields first */
                    $host_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value");
                    $data_template_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id={$data_template_id})\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_template_data_id={$data_template_id}\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value");
                    if (sizeof($host_fields)) {
                        if (sizeof($data_template_fields)) {
                            foreach ($data_template_fields as $key => $value) {
                                if (!isset($host_fields[$key])) {
                                    $host_fields[$key] = $value;
                                }
                            }
                        }
                    } elseif (sizeof($data_template_fields)) {
                        $host_fields = $data_template_fields;
                    }
                    if (sizeof($outputs) > 0) {
                        foreach ($outputs as $output) {
                            if (isset($snmp_queries["fields"][$output["snmp_field_name"]]["oid"])) {
                                $oid = $snmp_queries["fields"][$output["snmp_field_name"]]["oid"] . "." . $data_source["snmp_index"];
                                if (isset($snmp_queries["fields"][$output["snmp_field_name"]]["oid_suffix"])) {
                                    $oid .= "." . $snmp_queries["fields"][$output["snmp_field_name"]]["oid_suffix"];
                                }
                            }
                            if (!empty($oid)) {
                                $poller_items[] = api_poller_cache_item_add($data_source["host_id"], $host_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), $oid);
                            }
                        }
                    }
                } else {
                    if ($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY || $data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) {
                        /* script query */
                        $script_queries = get_data_query_array($data_source["snmp_query_id"]);
                        /* get the host override fields */
                        $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id={$local_data_id}");
                        /* get host fields first */
                        $host_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value");
                        $data_template_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id={$data_template_id})\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_template_data_id={$data_template_id}\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value");
                        if (sizeof($host_fields)) {
                            if (sizeof($data_template_fields)) {
                                foreach ($data_template_fields as $key => $value) {
                                    if (!isset($host_fields[$key])) {
                                        $host_fields[$key] = $value;
                                    }
                                }
                            }
                        } elseif (sizeof($data_template_fields)) {
                            $host_fields = $data_template_fields;
                        }
                        if (sizeof($outputs) > 0) {
                            foreach ($outputs as $output) {
                                if (isset($script_queries["fields"][$output["snmp_field_name"]]["query_name"])) {
                                    $identifier = $script_queries["fields"][$output["snmp_field_name"]]["query_name"];
                                    /* fall back to non-script server actions if the user is running a version of php older than 4.3 */
                                    if ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER && function_exists("proc_open")) {
                                        $action = POLLER_ACTION_SCRIPT_PHP;
                                        $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"] . " " . $script_queries["script_function"], $data_source["host_id"]);
                                    } else {
                                        if ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER && !function_exists("proc_open")) {
                                            $action = POLLER_ACTION_SCRIPT;
                                            $script_path = read_config_option("path_php_binary") . " -q " . get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["host_id"]);
                                        } else {
                                            $action = POLLER_ACTION_SCRIPT;
                                            $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["host_id"]);
                                        }
                                    }
                                }
                                if (isset($script_path)) {
                                    $poller_items[] = api_poller_cache_item_add($data_source["host_id"], $host_fields, $local_data_id, $data_input["rrd_step"], $action, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), addslashes($script_path));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if ($commit) {
        poller_update_poller_cache_from_buffer((array) $local_data_id, $poller_items);
    } else {
        return $poller_items;
    }
}
示例#8
0
/**
 * execute a script query for a given device
 * @param int $device_id
 * @param int $snmp_query_id
 */
function query_script_device($device_id, $snmp_query_id) {
	$script_queries = get_data_query_array($snmp_query_id);

	/* invalid xml check */
	if ((!is_array($script_queries)) || (sizeof($script_queries) == 0)) {
		debug_log_insert("data_query", __("Error parsing XML file into an array."));
		return false;
	}

	debug_log_insert("data_query", __("XML file parsed ok."));

	if (isset($script_queries["script_server"])) {
		$script_queries["script_path"] = "|path_php_binary| -q " . $script_queries["script_path"];
	}

	$script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_index"], $script_queries["script_path"], $device_id);

	/* fetch specified index at specified OID */
	$script_index_array = exec_into_array($script_path);

	debug_log_insert("data_query", __("Executing script for list of indexes") . " '$script_path'");

	/* prepare an output array */
	$output_array = array();

	while (list($field_name, $field_array) = each($script_queries["fields"])) {
		if ($field_array["direction"] == "input") {
			$script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_query"] . " " . $field_array["query_name"], $script_queries["script_path"], $device_id);

			$script_data_array = exec_into_array($script_path);

			debug_log_insert("data_query", __("Executing script query") . " '$script_path'");

			for ($i=0;($i<sizeof($script_data_array));$i++) {
				if (preg_match("/(.*)" . preg_quote($script_queries["output_delimeter"]) . "(.*)/", $script_data_array[$i], $matches)) {
					$script_index = $matches[1];
					$field_value  = $matches[2];

					$output_array[] = data_query_format_record($device_id, $snmp_query_id, $field_name, $field_value, $script_index, '');

					debug_log_insert("data_query", __("Found item [%s='%s'] index: %s", $field_name, $field_value, $script_index));
				}
			}
		}
	}

	if (sizeof($output_array)) {
		data_query_update_device_cache_from_buffer($device_id, $snmp_query_id, $output_array);
	}

	return true;
}
示例#9
0
文件: poller.php 项目: songchin/Cacti
function update_reindex_cache($device_id, $data_query_id) {
	global $config;

	require_once(CACTI_BASE_PATH . "/include/data_query/data_query_constants.php");
	require_once(CACTI_BASE_PATH . "/include/data_input/data_input_constants.php");
	include_once(CACTI_BASE_PATH . "/lib/data_query.php");
	include_once(CACTI_BASE_PATH . "/lib/snmp.php");

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

	$device            = db_fetch_row("select hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout from device where id=$device_id");
	$data_query      = db_fetch_row("select reindex_method, sort_field from device_snmp_query where device_id=$device_id and snmp_query_id=$data_query_id");
	$data_query_type = db_fetch_cell("select data_input.type_id from (data_input,snmp_query) where data_input.id=snmp_query.data_input_id and snmp_query.id=$data_query_id");
	$data_query_xml  = get_data_query_array($data_query_id);

	switch ($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 ($device["snmp_community"] != "") {
				if (isset($data_query_xml["oid_uptime"])) {
					$oid_uptime = $data_query_xml["oid_uptime"];
				}elseif (isset($data_query_xml["uptime_oid"])) {
					$oid_uptime = $data_query_xml["uptime_oid"];
				}else{
					$oid_uptime = ".1.3.6.1.2.1.1.3.0";
				}

				$assert_value = cacti_snmp_get($device["hostname"],
					$device["snmp_community"],
					$oid_uptime,
					$device["snmp_version"],
					$device["snmp_username"],
					$device["snmp_password"],
					$device["snmp_auth_protocol"],
					$device["snmp_priv_passphrase"],
					$device["snmp_priv_protocol"],
					$device["snmp_context"],
					$device["snmp_port"],
					$device["snmp_timeout"],
					SNMP_POLLER);

				$recache_stack[] = "('$device_id', '$data_query_id', '0', '<', '$assert_value', '$oid_uptime', '1')";
			}

			break;
		case DATA_QUERY_AUTOINDEX_INDEX_COUNT_CHANGE:
			/* this method requires that some command/oid can be used to determine the
			 * current number of indexes in the data query */
			$assert_value = sizeof(db_fetch_assoc("select snmp_index from device_snmp_cache where device_id=$device_id and snmp_query_id=$data_query_id group by snmp_index"));

			if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) {
				if (isset($data_query_xml["oid_num_indexes"])) {
					$recache_stack[] = "($device_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["oid_num_indexes"] . "', '1')";
				}
			}else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) {
				if (isset($data_query_xml["arg_num_indexes"])) {
					$recache_stack[] = "($device_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $device_id) . "', '1')";
				}
			}

			break;
		case DATA_QUERY_AUTOINDEX_VALUE_CHANGE:
			/* this method uses the value of the index OID to determine if a re-index is required */
			$assert_value = db_fetch_cell("select assert_value from poller_reindex where device_id=$device_id and data_query_id=$data_query_id");

			if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) {
				if (isset($data_query_xml["oid_num_indexes"])) {
					$recache_stack[] = "($device_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["oid_num_indexes"] . "', '1')";
				}
			}else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) {
				if (isset($data_query_xml["arg_num_indexes"])) {
					$recache_stack[] = "($device_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $device_id) . "', '1')";
				}
			}

			break;
		case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION:
			$primary_indexes = db_fetch_assoc("select snmp_index,oid,field_value from device_snmp_cache where device_id=$device_id and snmp_query_id=$data_query_id and field_name='" . $data_query["sort_field"] . "'");

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

					if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) {
						$recache_stack[] = "($device_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["fields"]{$data_query["sort_field"]}["oid"] . "." . $index["snmp_index"] . "', '1')";
					}else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) {
						$recache_stack[] = "('$device_id', '$data_query_id', '1', '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_get"] . " " . $data_query_xml["fields"]{$data_query["sort_field"]}["query_name"] . " " . $index["snmp_index"], $data_query_xml["script_path"], $device_id) . "', '1')";
					}
				}
			}

			break;
	}

	if (sizeof($recache_stack)) {
		poller_update_poller_reindex_from_buffer($device_id, $data_query_id, $recache_stack);
	}
}