function api_data_query_script_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/sys/exec.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); if ($data_query["input_type"] == DATA_QUERY_INPUT_TYPE_PHP_SCRIPT_SERVER_QUERY) { $script_path = api_data_query_script_path_format("|path_php_binary| -q " . $data_query["script_path"]); }else{ $script_path = api_data_query_script_path_format($data_query["script_path"]); } /* fetch a list of indexes for this data query */ $field_values{$data_query["index_field_id"]} = api_data_query_script_execute_field($host_id, $data_query["index_field_id"], $script_path); 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"]} = 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 $data_query_field) { /* fetch a list of values for this field (assuming that we haven't already seen it */ if (!isset($field_values{$data_query_field["id"]})) { $field_values{$data_query_field["id"]} = api_data_query_script_execute_field($host_id, $data_query_field["id"], $script_path); }else{ debug_log_insert("data_query", sprintf(_("Executing script for list of values '%s' (cached)"), $script_path . " " . DATA_QUERY_SCRIPT_ARG_QUERY . " " . $data_query_field["source"])); } /* see if we have some output to play with */ if (($field_values{$data_query_field["id"]} !== false) && (sizeof($field_values{$data_query_field["id"]}) > 0)) { foreach ($field_values{$data_query_field["id"]} as $found_index => $result) { /* a match for this index has been located */ if (isset($index_field_values[$found_index])) { debug_log_insert("data_query", sprintf(_("Found value [%s = '%s'] for index [%s]"), $data_query_field["name"], $result["value"], $found_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" => $data_query_field["name"]), "field_value" => array("type" => DB_TYPE_STRING, "value" => $result["value"]), "index_value" => array("type" => DB_TYPE_STRING, "value" => $found_index) ), 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 '$found_index'.")); } } }else{ debug_log_insert("data_query", _("No values returned from the field '" . $data_query_field["name"] . "', ignoring.")); } } } return true; }
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]); } }