Beispiel #1
0
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);
    }
}
Beispiel #2
0
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["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', '0', '<', '$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 */
			$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"])) {
					$recache_stack[] = "($host_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[] = "($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) . "', '1')";
				}
			}

			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, 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[] = "('$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) . "', '1')";
					}
				}
			}

			break;
	}

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