Exemple #1
0
function device_reload_query() {
	/* ================= input validation ================= */
	input_validate_input_number(get_request_var("id"));
	input_validate_input_number(get_request_var("device_id"));
	/* ==================================================== */

	run_data_query(get_request_var("device_id"), get_request_var("id"));
}
Exemple #2
0
	$i = 0;

	/* get all items on the form and write values for them  */
	while (list($name, $array) = each($input)) {
		if (isset($_POST[$name])) {
			db_execute("update settings set value='" . $_POST[$name] . "' where name='$name'");
		}
	}

	setcookie(session_name(),"",time() - 3600,"/");

	kill_session_var("sess_config_array");
	kill_session_var("sess_host_cache_array");

	/* just in case we have hard drive graphs to deal with */
	run_data_query(db_fetch_cell("select id from host where hostname='127.0.0.1'"), 6);

	/* it's always a good idea to re-populate the poller cache to make sure everything is refreshed and
	up-to-date */
	repopulate_poller_cache();

	db_execute("delete from version");
	db_execute("insert into version (cacti) values ('" . $config["cacti_version"] . "')");

	header ("Location: ../index.php");
	exit;
}elseif (($_REQUEST["step"] == "8") && ($_REQUEST["install_type"] == "3")) {
	/* if the version is not found, die */
	if (!is_int($old_version_index)) {
		print "	<p style='font-family: Verdana, Arial; font-size: 16px; font-weight: bold; color: red;'>Error</p>
			<p style='font-family: Verdana, Arial; font-size: 12px;'>Invalid Cacti version
Exemple #3
0
function api_device_save($id, $host_template_id, $description, $hostname, $snmp_community, $snmp_version,
	$snmp_username, $snmp_password, $snmp_port, $snmp_timeout, $disabled) {
	/* fetch some cache variables */
	if (empty($id)) {
		$_host_template_id = 0;
	}else{
		$_host_template_id = db_fetch_cell("select host_template_id from host where id=$id");
	}

	$save["id"] = $id;
	$save["host_template_id"] = form_input_validate($host_template_id, "host_template_id", "^[0-9]+$", false, 3);
	$save["description"] = form_input_validate($description, "description", "", false, 3);
	$save["hostname"] = form_input_validate($hostname, "hostname", "", false, 3);
	$save["snmp_community"] = form_input_validate($snmp_community, "snmp_community", "", true, 3);
	$save["snmp_version"] = form_input_validate($snmp_version, "snmp_version", "", true, 3);
	$save["snmp_username"] = form_input_validate($snmp_username, "snmp_username", "", true, 3);
	$save["snmp_password"] = form_input_validate($snmp_password, "snmp_password", "", true, 3);
	$save["snmp_port"] = form_input_validate($snmp_port, "snmp_port", "^[0-9]+$", false, 3);
	$save["snmp_timeout"] = form_input_validate($snmp_timeout, "snmp_timeout", "^[0-9]+$", false, 3);
	$save["disabled"] = form_input_validate($disabled, "disabled", "", true, 3);

	$host_id = 0;

	if (!is_error_message()) {
		$host_id = sql_save($save, "host");

		if ($host_id) {
			raise_message(1);

			/* push out relavant fields to data sources using this host */
			push_out_host($host_id, 0);

			/* the host substitution cache is now stale; purge it */
			kill_session_var("sess_host_cache_array");

			/* update title cache for graph and data source */
			update_data_source_title_cache_from_host($host_id);
			update_graph_title_cache_from_host($host_id);
		}else{
			raise_message(2);
		}

		/* if the user changes the host template, add each snmp query associated with it */
		if (($host_template_id != $_host_template_id) && (!empty($host_template_id))) {
			$snmp_queries = db_fetch_assoc("select snmp_query_id from host_template_snmp_query where host_template_id=$host_template_id");

			if (sizeof($snmp_queries) > 0) {
			foreach ($snmp_queries as $snmp_query) {
				db_execute("replace into host_snmp_query (host_id,snmp_query_id,reindex_method) values ($host_id," . $snmp_query["snmp_query_id"] . "," . DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME . ")");

				/* recache snmp data */
				run_data_query($host_id, $snmp_query["snmp_query_id"]);
			}
			}

			$graph_templates = db_fetch_assoc("select graph_template_id from host_template_graph where host_template_id=$host_template_id");

			if (sizeof($graph_templates) > 0) {
			foreach ($graph_templates as $graph_template) {
				db_execute("replace into host_graph (host_id,graph_template_id) values ($host_id," . $graph_template["graph_template_id"] . ")");
			}
			}
		}
	}

	return $host_id;
}
Exemple #4
0
function host_reload_query()
{
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var_request('id'));
    input_validate_input_number(get_request_var_request('host_id'));
    /* ==================================================== */
    run_data_query($_REQUEST['host_id'], $_REQUEST['id']);
}
Exemple #5
0
function host_reload_query()
{
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var("id"));
    input_validate_input_number(get_request_var("host_id"));
    /* ==================================================== */
    run_data_query($_GET["host_id"], $_GET["id"]);
}
function add_host_dq_graphs($host_id, $dq, $field = '', $regex = '', $include = true)
{
    global $config;
    /* add entry if it does not exist */
    $exists = db_fetch_cell("SELECT count(*) FROM host_snmp_query WHERE host_id={$host_id} AND snmp_query_id={$dq}");
    if (!$exists) {
        db_execute("REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) VALUES ({$host_id}, {$dq}, 1)");
    }
    /* recache snmp data */
    debug('Reindexing Host');
    run_data_query($host_id, $dq);
    $graph_templates = db_fetch_assoc('SELECT * 
		FROM snmp_query_graph 
		WHERE snmp_query_id=' . $dq);
    debug('Adding Graphs');
    if (sizeof($graph_templates)) {
        foreach ($graph_templates as $gt) {
            mikrotik_dq_graphs($host_id, $dq, $gt['graph_template_id'], $gt['id'], $field, $regex, $include);
        }
    }
}
Exemple #7
0
function createDQGraph($snmp_query_array, $graphTitle, $force) {

	/* is this data query already associated (independent of the reindex method) */
	$exists_already = db_fetch_cell("SELECT COUNT(device_id) FROM device_snmp_query WHERE device_id=" . $snmp_query_array["device_id"] . " AND snmp_query_id=" . $snmp_query_array["snmp_query_id"]);
	if ((isset($exists_already)) &&
	($exists_already > 0)) {
		/* yes: do nothing, everything's fine */
	}else{
		db_execute("REPLACE INTO device_snmp_query (device_id,snmp_query_id,reindex_method) " .
					   "VALUES (" .
		$snmp_query_array["device_id"] . "," .
		$snmp_query_array["snmp_query_id"] . "," .
		$snmp_query_array["reindex_method"] .
						")");
		/* recache snmp data, this is time consuming,
		 * but should happen only once even if multiple graphs
		 * are added for the same data query
		 * because we checked above, if dq was already associated */
		run_data_query($snmp_query_array["device_id"], $snmp_query_array["snmp_query_id"]);
	}

	$snmp_query_array["snmp_index_on"] = get_best_data_query_index_type($snmp_query_array["device_id"], $snmp_query_array["snmp_query_id"]);

	$snmp_indexes = db_fetch_assoc("SELECT snmp_index " .
										"FROM device_snmp_cache " .
										"WHERE device_id=" . $snmp_query_array["device_id"] . " " .
										"AND snmp_query_id=" . $snmp_query_array["snmp_query_id"] . " " .
										"AND field_name='" . $snmp_query_array["snmp_field"] . "' " .
										"AND field_value='" . $snmp_query_array["snmp_value"] . "'");

	if (sizeof($snmp_indexes)) {
		foreach ($snmp_indexes as $snmp_index) {
			$snmp_query_array["snmp_index"] = $snmp_index["snmp_index"];

			$existsAlready = db_fetch_cell("SELECT id " .
												"FROM graph_local " .
												"WHERE graph_template_id=" . $snmp_query_array["graph_template_id"] . " " .
												"AND device_id=" . $snmp_query_array["device_id"] . " " .
												"AND snmp_query_id=" . $snmp_query_array["snmp_query_id"] . " " .
												"AND snmp_index='" . $snmp_query_array["snmp_index"] . "'");

			if (isset($existsAlready) && $existsAlready > 0) {
				$dataSourceId = db_fetch_cell("SELECT
						data_template_rrd.local_data_id
						FROM graph_templates_item, data_template_rrd
						WHERE graph_templates_item.local_graph_id = " . $existsAlready . "
						AND graph_templates_item.task_item_id = data_template_rrd.id
						LIMIT 1");
				echo __("NOTE: Not Adding Graph - this graph already exists - graph-id: (%d) - data-source-id: (%d)", $existsAlready, $dataSourceId) . "\n";
				continue;
			}

			$empty = array(); /* Suggested Values are not been implemented */
			$returnArray = create_complete_graph_from_template($snmp_query_array["graph_template_id"], $snmp_query_array["device_id"], $snmp_query_array, $empty);

			if ($graphTitle != "") {
				db_execute("UPDATE graph_templates_graph " .
								"SET title='" . $graphTitle ."' " .
								"WHERE local_graph_id=" . $returnArray["local_graph_id"]);
				update_graph_title_cache($returnArray["local_graph_id"]);
			}

			$dataSourceId = db_fetch_cell("SELECT " .
					"data_template_rrd.local_data_id " .
					"FROM graph_templates_item, data_template_rrd " .
					"WHERE graph_templates_item.local_graph_id = " . $returnArray["local_graph_id"] . " " .
					"AND graph_templates_item.task_item_id = data_template_rrd.id " .
					"LIMIT 1");

			foreach($returnArray["local_data_id"] as $item) {
				push_out_device($snmp_query_array["device_id"], $item);
				$dataSourceId .= (strlen($dataSourceId) ? ", " : "") . $item;
			}

			echo __("Graph Added - graph-id: (%d) - data-source-ids: (%d)", $returnArray["local_graph_id"], $dataSourceId) . "\n";
		}
	}else{
		echo __("ERROR: Could not find snmp-field %s (%d) for device-id %d (%s)", $snmp_query_array["snmp_field"], $snmp_query_array["snmp_value"], $snmp_query_array["device_id"], $devices[$snmp_query_array["device_id"]]["hostname"]) . "\n";
		echo __("Try php -q graph_list.php --device-id=%s --list-snmp-fields", $snmp_query_array["device_id"]) . "\n";
		exit(1);
	}
}
Exemple #8
0
	while (list($name, $array) = each($input)) {
		if (isset($_POST[$name])) {
			db_execute("replace into settings (name,value) values ('$name','" . $_POST[$name] . "')");
		}
	}

	setcookie(session_name(),"",time() - 3600,"/");

	kill_session_var("sess_config_array");
	kill_session_var("sess_host_cache_array");

	/* just in case we have hard drive graphs to deal with */
	$host_id = db_fetch_cell("select id from host where hostname='127.0.0.1'");

	if (!empty($host_id)) {
		run_data_query($host_id, 6);
	}

	/* it's always a good idea to re-populate the poller cache to make sure everything is refreshed and
	up-to-date */
	repopulate_poller_cache();

	db_execute("delete from version");
	db_execute("insert into version (cacti) values ('" . $config["cacti_version"] . "')");

	header ("Location: ../index.php");
	exit;
}elseif (($_REQUEST["step"] == "8") && ($_REQUEST["install_type"] == "3")) {
	/* if the version is not found, die */
	if (!is_int($old_version_index)) {
		print "	<p style='font-family: Verdana, Arial; font-size: 16px; font-weight: bold; color: red;'>Error</p>
Exemple #9
0
function api_device_save($id, $host_template_id, $description, $hostname, $snmp_community, $snmp_version, $snmp_username, $snmp_password, $snmp_port, $snmp_timeout, $disabled, $availability_method, $ping_method, $ping_port, $ping_timeout, $ping_retries, $notes, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $max_oids, $device_threads)
{
    global $config;
    include_once $config['base_path'] . '/lib/utility.php';
    include_once $config['base_path'] . '/lib/variables.php';
    include_once $config['base_path'] . '/lib/data_query.php';
    /* fetch some cache variables */
    if (empty($id)) {
        $_host_template_id = 0;
    } else {
        $_host_template_id = db_fetch_cell_prepared('SELECT host_template_id FROM host WHERE id=?', array($id));
    }
    $save['id'] = form_input_validate($id, 'id', '^[0-9]+$', false, 3);
    $save['host_template_id'] = form_input_validate($host_template_id, 'host_template_id', '^[0-9]+$', false, 3);
    $save['description'] = form_input_validate($description, 'description', '', false, 3);
    $save['hostname'] = form_input_validate(trim($hostname), 'hostname', '', false, 3);
    $save['notes'] = form_input_validate($notes, 'notes', '', true, 3);
    $save['snmp_version'] = form_input_validate($snmp_version, 'snmp_version', '', true, 3);
    $save['snmp_community'] = form_input_validate($snmp_community, 'snmp_community', '', true, 3);
    if ($save['snmp_version'] == 3) {
        $save['snmp_username'] = form_input_validate($snmp_username, 'snmp_username', '', true, 3);
        $save['snmp_password'] = form_input_validate($snmp_password, 'snmp_password', '', true, 3);
        $save['snmp_auth_protocol'] = form_input_validate($snmp_auth_protocol, 'snmp_auth_protocol', "^\\[None\\]|MD5|SHA\$", true, 3);
        $save['snmp_priv_passphrase'] = form_input_validate($snmp_priv_passphrase, 'snmp_priv_passphrase', '', true, 3);
        $save['snmp_priv_protocol'] = form_input_validate($snmp_priv_protocol, 'snmp_priv_protocol', "^\\[None\\]|DES|AES128\$", true, 3);
        $save['snmp_context'] = form_input_validate($snmp_context, 'snmp_context', '', true, 3);
    } else {
        $save['snmp_username'] = '';
        $save['snmp_password'] = '';
        $save['snmp_auth_protocol'] = '';
        $save['snmp_priv_passphrase'] = '';
        $save['snmp_priv_protocol'] = '';
        $save['snmp_context'] = '';
    }
    $save['snmp_port'] = form_input_validate($snmp_port, 'snmp_port', '^[0-9]+$', false, 3);
    $save['snmp_timeout'] = form_input_validate($snmp_timeout, 'snmp_timeout', '^[0-9]+$', false, 3);
    /* disabled = 'on'   => regexp '^on$'
     * not disabled = '' => no regexp, but allow nulls */
    $save['disabled'] = form_input_validate($disabled, 'disabled', '^on$', true, 3);
    $save['availability_method'] = form_input_validate($availability_method, 'availability_method', '^[0-9]+$', false, 3);
    $save['ping_method'] = form_input_validate($ping_method, 'ping_method', '^[0-9]+$', false, 3);
    $save['ping_port'] = form_input_validate($ping_port, 'ping_port', '^[0-9]+$', true, 3);
    $save['ping_timeout'] = form_input_validate($ping_timeout, 'ping_timeout', '^[0-9]+$', true, 3);
    $save['ping_retries'] = form_input_validate($ping_retries, 'ping_retries', '^[0-9]+$', true, 3);
    $save['max_oids'] = form_input_validate($max_oids, 'max_oids', '^[0-9]+$', true, 3);
    $save['device_threads'] = form_input_validate($device_threads, 'device_threads', '^[0-9]+$', true, 3);
    $save = api_plugin_hook_function('api_device_save', $save);
    $host_id = 0;
    if (!is_error_message()) {
        $host_id = sql_save($save, 'host');
        if ($host_id) {
            raise_message(1);
            /* push out relavant fields to data sources using this host */
            push_out_host($host_id, 0);
            /* the host substitution cache is now stale; purge it */
            kill_session_var('sess_host_cache_array');
            /* update title cache for graph and data source */
            update_data_source_title_cache_from_host($host_id);
            update_graph_title_cache_from_host($host_id);
        } else {
            raise_message(2);
        }
        /* if the user changes the host template, add each snmp query associated with it */
        if ($host_template_id != $_host_template_id && !empty($host_template_id)) {
            $snmp_queries = db_fetch_assoc_prepared('SELECT snmp_query_id FROM host_template_snmp_query WHERE host_template_id = ?', array($host_template_id));
            if (sizeof($snmp_queries) > 0) {
                foreach ($snmp_queries as $snmp_query) {
                    db_execute_prepared('REPLACE INTO host_snmp_query (host_id, snmp_query_id, reindex_method) VALUES (?, ?, ?)', array($host_id, $snmp_query['snmp_query_id'], read_config_option('reindex_method')));
                    /* recache snmp data */
                    run_data_query($host_id, $snmp_query['snmp_query_id']);
                }
            }
            $graph_templates = db_fetch_assoc_prepared('SELECT graph_template_id FROM host_template_graph WHERE host_template_id = ?', array($host_template_id));
            if (sizeof($graph_templates) > 0) {
                foreach ($graph_templates as $graph_template) {
                    db_execute_prepared('REPLACE INTO host_graph (host_id, graph_template_id) VALUES (?, ?)', array($host_id, $graph_template['graph_template_id']));
                    api_plugin_hook_function('add_graph_template_to_host', array('host_id' => $host_id, 'graph_template_id' => $graph_template['graph_template_id']));
                }
            }
        }
    }
    # now that we have the id of the new host, we may plugin postprocessing code
    $save['id'] = $host_id;
    snmpagent_api_device_new($save);
    api_plugin_hook_function('api_device_new', $save);
    return $host_id;
}
Exemple #10
0
function api_graphs_new_reload_query() {
	run_data_query(get_request_var("device_id"), get_request_var("id"));
}
Exemple #11
0
		if ($debug) {
			print $sql_upd1 . $sql_upd2 . $sql_upd3 . "\n";
		} else {
			$ok = db_execute($sql_upd1 . $sql_upd2 . $sql_upd3);
			# add the snmp query name for printout
			$old["snmp_query_name"] = db_fetch_cell("SELECT name FROM snmp_query WHERE id=" . $old["snmp_query_id"]);

			if ($ok) {

				if (!$quietMode) {
					echo __("Data Query (%s: %s) reindex method (%s: %s) updated for %s Device(s)", $old["snmp_query_id"], $old["snmp_query_name"], $new["reindex_method"], $reindex_types{$new["reindex_method"]}, sizeof($verified_devices)) . "\n";
				}

				foreach ($verified_devices as $verified_device) {
					/* recache snmp data */
					run_data_query($verified_device["id"], $old["snmp_query_id"]);
					if (!$quietMode) {
						if (is_error_message()) {
							echo __("ERROR: Rerun of this data query failed for device (%s: %s) data query (%s: %s) reindex method (%s: %s)", $verified_device["id"], $verified_device["hostname"], $old["snmp_query_id"], $old["snmp_query_name"], $new["reindex_method"], $reindex_types[$new["reindex_method"]]) . "\n";
						} else {
							echo __("Data Query (%s: %s) reindex method (%s: %s) rerun for Device (%s: %s)", $old["snmp_query_id"], $old["snmp_query_name"], $new["reindex_method"], $reindex_types{$new["reindex_method"]}, $verified_device["id"], $verified_device["hostname"]) . "\n";
						}
					}
				}
			} else {
				echo __("ERROR: Failed to update Data Query (%s: %s) reindex method (%s: %s) for %s Device(s)", $old["snmp_query_id"], $old["snmp_query_name"], $new["reindex_method"], $reindex_types{$new["reindex_method"]}, sizeof($verified_devices)) . "\n";
			}
		}
	}
}
Exemple #12
0
	while (list($name, $array) = each($input)) {
		if (isset($_POST[$name])) {
			db_execute("replace into settings (name,value) values ('$name','" . get_request_var_post($name) . "')");
		}
	}

	setcookie(session_name(),"",time() - 3600,"/");

	kill_session_var("sess_config_array");
	kill_session_var("sess_device_cache_array");

	/* just in case we have hard drive graphs to deal with */
	$device_id = db_fetch_cell("select id from device where hostname='127.0.0.1'");

	if (!empty($device_id)) {
		run_data_query($device_id, 6);
	}

	/* it's not always a good idea to re-populate the poller cache to make sure everything is
	refreshed and up-to-date */
	if ($repopulate) {
		repopulate_poller_cache();
	}

	db_execute("delete from version");
	db_execute("insert into version (cacti) values ('" . CACTI_VERSION . "')");

	header ("Location: ../index.php");
	exit;
}elseif ((get_request_var_request("step") == "8") && (get_request_var_request("install_type") == "3")) {
	/* if the version is not found, die */
Exemple #13
0
function create_ds_graphs($args)
{
    $hostId = $args["hostId"];
    $hostTemplateId = $args["hostTemplateId"];
    $description = $args["description"];
    $queryTypeIds = $args["queryTypeIds"];
    $snmpQueryId = $args["snmpQueryId"];
    $ignoreIntsLike = $args["ignoreIntsLike"];
    if (!isset($hostId) || !isset($description) || !isset($queryTypeIds) || !isset($snmpQueryId)) {
        echo "ERROR: create_ds_graph: Missing required arguments\n";
        exit(1);
    }
    /* Check if host has associated data query */
    $host_data_query = "SELECT snmp_query_id\n                       FROM   host_snmp_query\n                       WHERE  host_id='{$hostId}'\n                          AND snmp_query_id='{$snmpQueryId}'";
    $snmpQuery = db_fetch_cell($host_data_query);
    if (!$snmpQuery) {
        // The query is not yet int the database.  Insert it
        debug("Inserting missing host_snmp_query");
        $insertQuery = "REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) \n                            VALUES ({$hostId},{$snmpQueryId},2)";
        $r = db_execute($insertQuery);
        if (!$r) {
            echo "ERROR: DB operation failed for {$insertQuery}\n";
            return 0;
        }
        // recache snmp data
        debug("Running Data query for new query id: {$snmpQueryId}");
        run_data_query($hostId, $snmpQueryId);
    }
    $snmpQueryArray = array();
    $snmpQueryArray["snmp_query_id"] = $snmpQueryId;
    $snmpQueryArray["snmp_index_on"] = get_best_data_query_index_type($hostId, $snmpQueryId);
    $indexes_query = "SELECT snmp_index\n                     FROM   host_snmp_cache\n                     WHERE  host_id='{$hostId}'\n                        AND snmp_query_id='{$snmpQueryId}'";
    if (isset($args["snmpCriteria"]) && $args["snmpCriteria"] != "") {
        $indexes_query .= " AND " . $args["snmpCriteria"];
    }
    $snmpIndexes = db_fetch_assoc($indexes_query);
    // Interfaces to ignore
    if ($snmpQueryId == 1 && count($snmpIndexes) && isset($ignoreIntsLike) && count($ignoreIntsLike)) {
        $ignQuery = "SELECT snmp_index\n                  FROM   host_snmp_cache\n                  WHERE  host_id='{$hostId}'\n                    AND  snmp_query_id=1\n                    AND  field_name='ifDescr'";
        $patts = array();
        foreach ($ignoreIntsLike as $patt) {
            array_push($patts, "field_value LIKE '{$patt}'");
        }
        $crit = implode(' OR ', $patts);
        $ignQuery .= " AND ({$crit})";
        $ignIndexes = db_fetch_assoc($ignQuery);
        // Make into an associative array for faster lookups
        $ignHash = array();
        foreach ($ignIndexes as $row) {
            $ignHash[$row["snmp_index"]] = TRUE;
        }
        if (count($ignHash)) {
            // Now exclude the indexes that matched the ignore patterns
            $temparr = array();
            foreach ($snmpIndexes as $row) {
                if (!isset($ignHash[$row["snmp_index"]])) {
                    array_push($temparr, $row);
                }
            }
            $snmpIndexes = $temparr;
        }
    }
    if (count($snmpIndexes)) {
        $graphsCreated = 0;
        $graphs = db_fetch_assoc("SELECT id, snmp_index, graph_template_id\n                              FROM   graph_local\n                              WHERE  host_id={$hostId}\n                                AND  snmp_query_id={$snmpQueryId}");
        foreach ($graphs as $row) {
            $graphsBySnmpIndex[$row["snmp_index"]][$row["graph_template_id"]] = $row["id"];
        }
        foreach ($queryTypeIds as $queryTypeId => $templateId) {
            $snmpQueryArray["snmp_query_graph_id"] = $queryTypeId;
            foreach ($snmpIndexes as $row) {
                $snmpIndex = $row["snmp_index"];
                if (isset($graphsBySnmpIndex[$snmpIndex][$templateId])) {
                    $graphId = $graphsBySnmpIndex[$snmpIndex][$templateId];
                    debug("{$description}: Graph already exists: ({$graphId})");
                    continue;
                }
                $snmpQueryArray["snmp_index"] = $snmpIndex;
                $empty = array();
                $returnArray = create_complete_graph_from_template($templateId, $hostId, $snmpQueryArray, $empty);
                echo "{$description}: Added Graph id: " . $returnArray["local_graph_id"] . "\n";
                $graphsCreated++;
            }
        }
        if ($graphsCreated > 0) {
            push_out_host($hostId, 0);
            return $graphsCreated;
        }
    } else {
        debug("{$description}: No rows in query: {$indexes_query}");
    }
}
function create_ds_graphs($args)
{
    $hostId = $args["hostId"];
    $hostTemplateId = $args["hostTemplateId"];
    $description = $args["description"];
    $queryTypeIds = $args["queryTypeIds"];
    $snmpQueryId = $args["snmpQueryId"];
    if (!isset($hostId) || !isset($description) || !isset($queryTypeIds) || !isset($snmpQueryId)) {
        echo "ERROR: create_ds_graph: Missing required arguments\n";
        exit(1);
    }
    /* Check if host has associated data query */
    $host_data_query = "SELECT snmp_query_id\n                       FROM   host_snmp_query\n                       WHERE  host_id='{$hostId}'\n                          AND snmp_query_id='{$snmpQueryId}'";
    $snmpQuery = db_fetch_cell($host_data_query);
    if (!$snmpQuery) {
        // The query is not yet int the database.  Insert it
        debug("Inserting missing host_snmp_query");
        $insertQuery = "REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) \n                            VALUES ({$hostId},{$snmpQueryId},2)";
        $r = db_execute($insertQuery);
        if (!$r) {
            echo "ERROR: DB operation failed for {$insertQuery}\n";
            return 0;
        }
        // recache snmp data
        debug("Running Data query for new query id: {$snmpQueryId}");
        run_data_query($hostId, $snmpQueryId);
    }
    $snmpQueryArray = array();
    $snmpQueryArray["snmp_query_id"] = $snmpQueryId;
    $snmpQueryArray["snmp_index_on"] = get_best_data_query_index_type($hostId, $snmpQueryId);
    $indexes_query = "SELECT snmp_index\n                     FROM   host_snmp_cache\n                     WHERE  host_id='{$hostId}'\n                        AND snmp_query_id='{$snmpQueryId}'";
    if (isset($args["snmpCriteria"]) && $args["snmpCriteria"] != "") {
        $indexes_query .= " AND " . $args["snmpCriteria"];
    }
    $snmpIndexes = db_fetch_assoc($indexes_query);
    if (sizeof($snmpIndexes)) {
        $graphsCreated = 0;
        $graphs = db_fetch_assoc("SELECT id, snmp_index, graph_template_id\n                              FROM   graph_local\n                              WHERE  host_id={$hostId}\n                                AND  snmp_query_id={$snmpQueryId}");
        foreach ($graphs as $row) {
            $graphsBySnmpIndex[$row["snmp_index"]][$row["graph_template_id"]] = $row["id"];
        }
        foreach ($queryTypeIds as $queryTypeId => $templateId) {
            $snmpQueryArray["snmp_query_graph_id"] = $queryTypeId;
            foreach ($snmpIndexes as $row) {
                $snmpIndex = $row["snmp_index"];
                if (isset($graphsBySnmpIndex[$snmpIndex][$templateId])) {
                    $graphId = $graphsBySnmpIndex[$snmpIndex][$templateId];
                    debug("{$description}: Graph already exists: ({$graphId})");
                    continue;
                }
                $snmpQueryArray["snmp_index"] = $snmpIndex;
                $empty = array();
                $returnArray = create_complete_graph_from_template($templateId, $hostId, $snmpQueryArray, $empty);
                echo "{$description}: Added Graph id: " . $returnArray["local_graph_id"] . "\n";
                $graphsCreated++;
            }
        }
        if ($graphsCreated > 0) {
            push_out_host($hostId, 0);
            return $graphsCreated;
        }
    } else {
        debug("{$description}: No rows in query: {$indexes_query}");
    }
}
Exemple #15
0
function host_reload_query()
{
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var("id"));
    input_validate_input_number(get_request_var("host_id"));
    /* ==================================================== */
    /* modify for multi user start */
    if (!check_host($_GET["host_id"])) {
        access_denied();
    }
    /* modify for multi user end */
    run_data_query($_GET["host_id"], $_GET["id"]);
}
Exemple #16
0
			$sql = "REPLACE INTO device_snmp_query " .
					"(device_id,snmp_query_id,reindex_method) " .
					"VALUES (".
						$device["id"] . "," .
						$dq["snmp_query_id"] . "," .
						$dq["reindex_method"] .
					")";
			if ($debug) {
				print $sql . "\n";
			} else {
				# update of sort_field and title are done later in update_data_query_sort_cache via run_data_query
				$ok = db_execute($sql);
				if (!$quietMode) {
					if ($ok) {
						/* recache snmp data */
						run_data_query($device["id"], $dq["snmp_query_id"]);
						if (is_error_message()) {
							echo __("ERROR: Failed to add this data query for device (%s: %s) data query (%s: %s) reindex method (%s: %s)", $device["id"], $device["hostname"], $dq["snmp_query_id"], $data_query_name, $dq["reindex_method"], $reindex_types[$dq["reindex_method"]]) . "\n";
						} else {
							echo __("Success - Device (%s: %s) data query (%s: %s) reindex method (%s: %s)", $device["id"], $device["hostname"], $dq["snmp_query_id"], $data_query_name, $dq["reindex_method"], $reindex_types{$dq["reindex_method"]}) . "\n";
						}
					} else {
						echo __("ERROR: Failed to add this data query for device (%s: %s) data query (%s: %s) reindex method (%s: %s)", $device["id"], $device["hostname"], $dq["snmp_query_id"], $data_query_name, $dq["reindex_method"], $reindex_types[$dq["reindex_method"]]) . "\n";
					}
				}
			}
		}
	}
}else{
	display_help($me);
	exit(0);
Exemple #17
0
function api_device_save($id, $host_template_id, $description, $hostname, $snmp_community, $snmp_version, $snmp_username, $snmp_password, $snmp_port, $snmp_timeout, $disabled, $availability_method, $ping_method, $ping_port, $ping_timeout, $ping_retries, $notes, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $max_oids, $device_threads)
{
    global $config;
    include_once $config["base_path"] . "/lib/utility.php";
    include_once $config["base_path"] . "/lib/variables.php";
    include_once $config["base_path"] . "/lib/data_query.php";
    /* fetch some cache variables */
    if (empty($id)) {
        $_host_template_id = 0;
    } else {
        $_host_template_id = db_fetch_cell("select host_template_id from host where id={$id}");
    }
    $save["id"] = $id;
    $save["host_template_id"] = form_input_validate($host_template_id, "host_template_id", "^[0-9]+\$", false, 3);
    $save["description"] = form_input_validate($description, "description", "", false, 3);
    $save["hostname"] = form_input_validate(trim($hostname), "hostname", "", false, 3);
    $save["notes"] = form_input_validate($notes, "notes", "", true, 3);
    $save["snmp_version"] = form_input_validate($snmp_version, "snmp_version", "", true, 3);
    $save["snmp_community"] = form_input_validate($snmp_community, "snmp_community", "", true, 3);
    if ($save["snmp_version"] == 3) {
        $save["snmp_username"] = form_input_validate($snmp_username, "snmp_username", "", true, 3);
        $save["snmp_password"] = form_input_validate($snmp_password, "snmp_password", "", true, 3);
        $save["snmp_auth_protocol"] = form_input_validate($snmp_auth_protocol, "snmp_auth_protocol", "", true, 3);
        $save["snmp_priv_passphrase"] = form_input_validate($snmp_priv_passphrase, "snmp_priv_passphrase", "", true, 3);
        $save["snmp_priv_protocol"] = form_input_validate($snmp_priv_protocol, "snmp_priv_protocol", "", true, 3);
        $save["snmp_context"] = form_input_validate($snmp_context, "snmp_context", "", true, 3);
    } else {
        $save["snmp_username"] = "";
        $save["snmp_password"] = "";
    }
    $save["snmp_port"] = form_input_validate($snmp_port, "snmp_port", "^[0-9]+\$", false, 3);
    $save["snmp_timeout"] = form_input_validate($snmp_timeout, "snmp_timeout", "^[0-9]+\$", false, 3);
    $save["disabled"] = form_input_validate($disabled, "disabled", "", true, 3);
    $save["availability_method"] = form_input_validate($availability_method, "availability_method", "^[0-9]+\$", false, 3);
    $save["ping_method"] = form_input_validate($ping_method, "ping_method", "^[0-9]+\$", false, 3);
    $save["ping_port"] = form_input_validate($ping_port, "ping_port", "^[0-9]+\$", true, 3);
    $save["ping_timeout"] = form_input_validate($ping_timeout, "ping_timeout", "^[0-9]+\$", true, 3);
    $save["ping_retries"] = form_input_validate($ping_retries, "ping_retries", "^[0-9]+\$", true, 3);
    $save["max_oids"] = form_input_validate($max_oids, "max_oids", "^[0-9]+\$", true, 3);
    $save["device_threads"] = form_input_validate($device_threads, "device_threads", "^[0-9]+\$", true, 3);
    $save = api_plugin_hook_function('api_device_save', $save);
    $host_id = 0;
    if (!is_error_message()) {
        $host_id = sql_save($save, "host");
        if ($host_id) {
            raise_message(1);
            /* push out relavant fields to data sources using this host */
            push_out_host($host_id, 0);
            /* the host substitution cache is now stale; purge it */
            kill_session_var("sess_host_cache_array");
            /* update title cache for graph and data source */
            update_data_source_title_cache_from_host($host_id);
            update_graph_title_cache_from_host($host_id);
        } else {
            raise_message(2);
        }
        /* if the user changes the host template, add each snmp query associated with it */
        if ($host_template_id != $_host_template_id && !empty($host_template_id)) {
            $snmp_queries = db_fetch_assoc("select snmp_query_id from host_template_snmp_query where host_template_id={$host_template_id}");
            if (sizeof($snmp_queries) > 0) {
                foreach ($snmp_queries as $snmp_query) {
                    db_execute("replace into host_snmp_query (host_id,snmp_query_id,reindex_method) values ({$host_id}," . $snmp_query["snmp_query_id"] . "," . read_config_option("reindex_method") . ")");
                    /* recache snmp data */
                    run_data_query($host_id, $snmp_query["snmp_query_id"]);
                }
            }
            $graph_templates = db_fetch_assoc("select graph_template_id from host_template_graph where host_template_id={$host_template_id}");
            if (sizeof($graph_templates) > 0) {
                foreach ($graph_templates as $graph_template) {
                    db_execute("replace into host_graph (host_id,graph_template_id) values ({$host_id}," . $graph_template["graph_template_id"] . ")");
                    api_plugin_hook_function('add_graph_template_to_host', array("host_id" => $host_id, "graph_template_id" => $graph_template["graph_template_id"]));
                }
            }
        }
    }
    # now that we have the id of the new host, we may plugin postprocessing code
    $save["id"] = $host_id;
    api_plugin_hook_function('api_device_new', $save);
    return $host_id;
}
Exemple #18
0
				$last_device_id = $device_id;
				$first_device = true;
				$recached_devices++;
			} else {
				$first_device = false;
			}

			if ($first_device) {
				cacti_log("Host[$device_id] WARNING: Recache Event Detected for Host", true, "PCOMMAND");
			}

			if (read_config_option("log_verbosity") == POLLER_VERBOSITY_DEBUG) {
				cacti_log("Host[$device_id] RECACHE: Re-cache for Host, data query #$data_query_id", true, "PCOMMAND");
			}

			run_data_query($device_id, $data_query_id);

			if (read_config_option("log_verbosity") == POLLER_VERBOSITY_DEBUG) {
				cacti_log("Host[$device_id] RECACHE: Re-cache successful.", true, "PCOMMAND");
			}
			break;
		default:
			cacti_log("ERROR: Unknown poller command issued", true, "PCOMMAND");
		}

		/* record current_time */
		list($micro,$seconds) = explode(" ", microtime());
		$current = $seconds + $micro;

		/* end if runtime has been exceeded */
		if (($current-$start) > MAX_RECACHE_RUNTIME) {
Exemple #19
0
/** api_device_save - save a device to the database
 *
 * @param int $id
 * @param int $site_id
 * @param int $poller_id
 * @param int $device_template_id
 * @param string $description
 * @param string $hostname
 * @param string $snmp_community
 * @param int $snmp_version
 * @param string $snmp_username
 * @param string $snmp_password
 * @param int $snmp_port
 * @param int $snmp_timeout
 * @param string $disabled
 * @param int $availability_method
 * @param int $ping_method
 * @param int $ping_port
 * @param int $ping_timeout
 * @param int $ping_retries
 * @param string $notes
 * @param string $snmp_auth_protocol
 * @param string $snmp_priv_passphrase
 * @param string $snmp_priv_protocol
 * @param string $snmp_context
 * @param int $max_oids
 * @param int $device_threads
 * @param string $template_enabled
 * @return unknown_type
 */
function api_device_save($id, $site_id, $poller_id, $device_template_id, $description, $hostname, $snmp_community, $snmp_version,
	$snmp_username, $snmp_password, $snmp_port, $snmp_timeout, $disabled,
	$availability_method, $ping_method, $ping_port, $ping_timeout, $ping_retries,
	$notes, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $max_oids, $device_threads, $template_enabled) {

	/* fetch some cache variables */
	if (empty($id)) {
		$_device_template_id = 0;
	}else{
		$_device_template_id = db_fetch_cell("select device_template_id from device where id=$id");
	}

	$save["id"] = $id;
	$save["site_id"]          = form_input_validate($site_id, "site_id", "^[0-9]+$", false, 3);
	$save["poller_id"]        = form_input_validate($poller_id, "poller_id", "^[0-9]+$", false, 3);
	$save["device_template_id"] = form_input_validate($device_template_id, "device_template_id", "^[0-9]+$", false, 3);
	$save["description"]      = form_input_validate($description, "description", "", false, 3);
	$save["hostname"]         = form_input_validate(trim($hostname), "hostname", "", false, 3);
	$save["notes"]            = form_input_validate($notes, "notes", "", true, 3);
	$save["disabled"]         = form_input_validate($disabled, "disabled", "", true, 3);
	$save["template_enabled"] = form_input_validate($template_enabled, "template_enabled", "", true, 3);

	$save["snmp_version"]     = form_input_validate($snmp_version, "snmp_version", "", true, 3);
	$save["snmp_community"]   = form_input_validate($snmp_community, "snmp_community", "", true, 3);

	if ($save["snmp_version"] == 3) {
		$save["snmp_username"]        = form_input_validate($snmp_username, "snmp_username", "", true, 3);
		$save["snmp_password"]        = form_input_validate($snmp_password, "snmp_password", "", true, 3);
		$save["snmp_auth_protocol"]   = form_input_validate($snmp_auth_protocol, "snmp_auth_protocol", "", true, 3);
		$save["snmp_priv_passphrase"] = form_input_validate($snmp_priv_passphrase, "snmp_priv_passphrase", "", true, 3);
		$save["snmp_priv_protocol"]   = form_input_validate($snmp_priv_protocol, "snmp_priv_protocol", "", true, 3);
		$save["snmp_context"]         = form_input_validate($snmp_context, "snmp_context", "", true, 3);
	} else {
		$save["snmp_username"]        = "";
		$save["snmp_password"]        = "";
		$save["snmp_auth_protocol"]   = "";
		$save["snmp_priv_passphrase"] = "";
		$save["snmp_priv_protocol"]   = "";
		$save["snmp_context"]         = "";
	}

	$save["snmp_port"]           = form_input_validate($snmp_port, "snmp_port", "^[0-9]+$", false, 3);
	$save["snmp_timeout"]        = form_input_validate($snmp_timeout, "snmp_timeout", "^[0-9]+$", false, 3);

	$save["availability_method"] = form_input_validate($availability_method, "availability_method", "^[0-9]+$", false, 3);
	$save["ping_method"]         = form_input_validate($ping_method, "ping_method", "^[0-9]+$", false, 3);
	$save["ping_port"]           = form_input_validate($ping_port, "ping_port", "^[0-9]+$", true, 3);
	$save["ping_timeout"]        = form_input_validate($ping_timeout, "ping_timeout", "^[0-9]+$", true, 3);
	$save["ping_retries"]        = form_input_validate($ping_retries, "ping_retries", "^[0-9]+$", true, 3);
	$save["max_oids"]            = form_input_validate($max_oids, "max_oids", "^[0-9]+$", true, 3);
	$save["device_threads"]      = form_input_validate($device_threads, "device_threads", "^[0-9]+$", true, 3);

	$save = api_plugin_hook_function('api_device_save', $save);

	$device_id = 0;

	if (!is_error_message()) {
		$device_id = sql_save($save, "device");

		if ($device_id) {
			raise_message(1);

			/* push out relavant fields to data sources using this device */
			push_out_device($device_id, 0);

			/* the device substitution cache is now stale; purge it */
			kill_session_var("sess_device_cache_array");

			/* update title cache for graph and data source */
			update_data_source_title_cache_from_device($device_id);
			update_graph_title_cache_from_device($device_id);
		}else{
			raise_message(2);
		}

		/* recache in case any snmp information was changed */
		if (!empty($id)) { /* a valid device was already existing */
			/* detect SNMP change, if current snmp parameters cannot be found in device table */
			$snmp_changed = ($id != db_fetch_cell("SELECT " .
					"id " .
					"FROM device " .
					"WHERE id=$id " .
					"AND snmp_version='$snmp_version' " .
					"AND snmp_community='$snmp_community' " .
					"AND snmp_username='******' " .
					"AND snmp_password='******' " .
					"AND snmp_auth_protocol='$snmp_auth_protocol' " .
					"AND snmp_priv_passphrase='$snmp_priv_passphrase' " .
					"AND snmp_priv_protocol='$snmp_priv_protocol' " .
					"AND snmp_context='$snmp_context' " .
					"AND snmp_port='$snmp_port' " .
					"AND snmp_timeout='$snmp_timeout' "));

			if ($snmp_changed) {
				/* fecth all existing snmp queries */
				$snmp_queries = db_fetch_assoc("SELECT " .
						"snmp_query_id, " .
						"reindex_method " .
						"FROM device_snmp_query " .
						"WHERE device_id=$id");

				if (sizeof($snmp_queries) > 0) {
					foreach ($snmp_queries as $snmp_query) {
						/* recache all existing snmp queries */
						run_data_query($id, $snmp_query["snmp_query_id"]);
					}
				}
			}
		}

		/* if the user changes the device template, add each snmp query associated with it */
		if (($device_template_id != $_device_template_id) && (!empty($device_template_id))) {
			$snmp_queries = db_fetch_assoc("select snmp_query_id, reindex_method from device_template_snmp_query where device_template_id=$device_template_id");

			if (sizeof($snmp_queries) > 0) {
			foreach ($snmp_queries as $snmp_query) {
				db_execute("replace into device_snmp_query (device_id,snmp_query_id,reindex_method) values ($device_id," . $snmp_query["snmp_query_id"] . "," . $snmp_query["reindex_method"] . ")");

				/* recache snmp data */
				run_data_query($device_id, $snmp_query["snmp_query_id"]);
			}
			}

			$graph_templates = db_fetch_assoc("select graph_template_id from device_template_graph where device_template_id=$device_template_id");

			if (sizeof($graph_templates) > 0) {
			foreach ($graph_templates as $graph_template) {
				db_execute("replace into device_graph (device_id,graph_template_id) values ($device_id," . $graph_template["graph_template_id"] . ")");
				api_plugin_hook_function('add_graph_template_to_device', array("device_id" => $device_id, "graph_template_id" => $graph_template["graph_template_id"]));
			}
			}
		}
	}

	return $device_id;
}
Exemple #20
0
function host_reload_query() {
	run_data_query($_GET["host_id"], $_GET["id"]);
}
	display_help();
	exit;
}

/* determine data queries to rerun */
$data_queries = db_fetch_assoc("SELECT host_id, snmp_query_id FROM host_snmp_query" . $sql_where);

/* issue warnings and start message if applicable */
print "WARNING: Do not interrupt this script.  Reindexing can take quite some time\n";
debug("There are '" . sizeof($data_queries) . "' data queries to run");

$i = 1;
foreach ($data_queries as $data_query) {
	if (!$debug) print ".";
	debug("Data query number '" . $i . "' starting");
	run_data_query($data_query["host_id"], $data_query["snmp_query_id"]);
	debug("Data query number '" . $i . "' ending");
	$i++;
}

/*	display_help - displays the usage of the function */
function display_help () {
print "Cacti Reindex Host Script 1.0, Copyright 2007 - The Cacti Group\n\n";
	print "usage: poller_reindex_hosts.php -id=[host_id|All] [-d] [-h] [--help] [-v] [--version]\n\n";
	print "-id=host_id   - The host_id to have data queries reindexed or 'All' to reindex all hosts\n";
	print "-d            - Display verbose output during execution\n";
	print "-v --version  - Display this help message\n";
	print "-h --help     - Display this help message\n";
}

function debug($message) {
Exemple #22
0
function add_host_dq_graphs($host_id, $dq, $field = "", $regex = "", $include = TRUE)
{
    global $config;
    /* add entry if it does not exist */
    $exists = db_fetch_cell("SELECT count(*) FROM host_snmp_query WHERE host_id={$host_id} AND snmp_query_id={$dq}");
    if (!$exists) {
        db_execute("REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) VALUES ({$host_id}, {$dq}, 1)");
    }
    /* recache snmp data */
    debug("Reindexing Host");
    run_data_query($host_id, $dq);
    $graph_templates = db_fetch_assoc("SELECT * \n\t\tFROM snmp_query_graph \n\t\tWHERE snmp_query_id=" . $dq);
    debug("Adding Graphs");
    if (sizeof($graph_templates)) {
        foreach ($graph_templates as $gt) {
            hmib_dq_graphs($host_id, $dq, $gt["graph_template_id"], $gt["id"], $field, $regex, $include);
        }
    }
}
	$sql_where .= (strlen($sql_where) ? " AND device.description like '%%" . $device_descr . "%%' AND device.id=device_snmp_query.device_id" : " WHERE device.description like '%%" . $device_descr . "%%' AND device.id=device_snmp_query.device_id");
	$data_queries = db_fetch_assoc("SELECT device_id, snmp_query_id FROM device_snmp_query,device" . $sql_where);
} else {
	$data_queries = db_fetch_assoc("SELECT device_id, snmp_query_id FROM device_snmp_query" . $sql_where);
}

/* issue warnings and start message if applicable */
echo __("WARNING: Do not interrupt this script.  Reindexing can take quite some time") . "\n";
debug("There are '" . sizeof($data_queries) . "' data queries to run");

$i = 1;
if (sizeof($data_queries)) {
	foreach ($data_queries as $data_query) {
		if (!$debug) print ".";
		debug("Data query number '" . $i . "' device: '".$data_query["device_id"]."' SNMP Query Id: '".$data_query["snmp_query_id"]."' starting");
		run_data_query($data_query["device_id"], $data_query["snmp_query_id"]);
		debug("Data query number '" . $i . "' device: '".$data_query["device_id"]."' SNMP Query Id: '".$data_query["snmp_query_id"]."' ending");
		$i++;
	}
}

/*	display_help - displays the usage of the function */
function display_help($me) {
	echo "Cacti Reindex Device Script 1.2" . ", " . __("Copyright 2004-2010 - The Cacti Group") . "\n";
	echo __("usage: ") . $me . " --id=[device-id|All] [--qid=[ID|All]] [--device-descr=[description]]\n";
	echo "              [-d] [-h] [--help] [-v] [--version]\n\n";
	echo "   --id          " . __("The device-id to have data queries reindexed or 'All' to reindex all devices") . "\n";
	echo "   --qid         " . __("Only index on a specific data query id; defaults to 'All'") . "\n";
	echo "   --device-descr  " . __("The device description to filter by (SQL filters acknowledged)") . "\n";
	echo "   --debug       " . __("Display verbose output during execution") . "\n";
	echo "   -v --version  " . __("Display this help message") . "\n";
Exemple #24
0
				$last_host_id = $host_id;
				$first_host = true;
				$recached_hosts++;
			} else {
				$first_host = false;
			}

			if ($first_host) {
				cacti_log("Host[$host_id] WARNING: Recache Event Detected for Host", true, "PCOMMAND");
			}

			if (read_config_option("log_verbosity") == POLLER_VERBOSITY_DEBUG) {
				cacti_log("Host[$host_id] RECACHE: Re-cache for Host, data query #$data_query_id", true, "PCOMMAND");
			}

			run_data_query($host_id, $data_query_id);

			if (read_config_option("log_verbosity") == POLLER_VERBOSITY_DEBUG) {
				cacti_log("Host[$host_id] RECACHE: Re-cache successful.", true, "PCOMMAND");
			}
			break;
		default:
			cacti_log("ERROR: Unknown poller command issued", true, "PCOMMAND");
		}

		/* record current_time */
		list($micro,$seconds) = split(" ", microtime());
		$current = $seconds + $micro;

		/* end if runtime has been exceeded */
		if (($current-$start) > MAX_RECACHE_RUNTIME) {
	if (sizeof($hosts)) {
	foreach($hosts as $host) {
		echo "NOTE: Updating Host '" . $host["description"] . "'\n";
		$snmp_queries = db_fetch_assoc("SELECT snmp_query_id 
			FROM host_template_snmp_query 
			WHERE host_template_id=" . $host["host_template_id"]);

		if (sizeof($snmp_queries) > 0) {
			echo "NOTE: Updating Data Queries. There were '" . sizeof($snmp_queries) . "' Found\n";
			foreach ($snmp_queries as $snmp_query) {
				echo "NOTE: Updating Data Query ID '" . $snmp_query["snmp_query_id"] . "'\n";
				db_execute("REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) 
					VALUES (" . $host["id"] . ", " . $snmp_query["snmp_query_id"] . "," . DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME . ")");

				/* recache snmp data */
				run_data_query($host["id"], $snmp_query["snmp_query_id"]);
			}
		}

		$graph_templates = db_fetch_assoc("SELECT graph_template_id FROM host_template_graph WHERE host_template_id=" . $host["host_template_id"]);

		if (sizeof($graph_templates) > 0) {
			echo "NOTE: Updating Graph Templates. There were '" . sizeof($graph_templates) . "' Found\n";

			foreach ($graph_templates as $graph_template) {
				db_execute("REPLACE INTO host_graph (host_id, graph_template_id) VALUES (" . $host["id"] . ", " . $graph_template["graph_template_id"] . ")");
			}
		}
	}
	}
}else{
Exemple #26
0
         }
     }
     if (!($listHosts || $listSNMPFields || $listSNMPValues || $listQueryTypes || $listSNMPQueries || $listInputFields)) {
         /* if data query is not yet associated,
          * add it and run it once to get the cache filled */
         /* is this data query already associated (independent of the reindex method)? */
         $exists_already = db_fetch_cell("SELECT COUNT(host_id) FROM host_snmp_query WHERE host_id={$hostId} AND snmp_query_id=" . $dsGraph["snmpQueryId"]);
         if (isset($exists_already) && $exists_already > 0) {
             /* yes: do nothing, everything's fine */
         } else {
             db_execute("REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) " . "VALUES (" . $hostId . "," . $dsGraph["snmpQueryId"] . "," . $dsGraph["reindex_method"] . ")");
             /* recache snmp data, this is time consuming,
              * but should happen only once even if multiple graphs
              * are added for the same data query
              * because we checked above, if dq was already associated */
             run_data_query($hostId, $dsGraph["snmpQueryId"]);
         }
     }
 }
 /* Verify the host's existance */
 if (!isset($hosts[$hostId]) || $hostId == 0) {
     echo "ERROR: Unknown Host ID ({$hostId})\n";
     echo "Try --list-hosts\n";
     exit(1);
 }
 /* process the snmp fields */
 $snmpFields = getSNMPFields($hostId, $dsGraph["snmpQueryId"]);
 if ($listSNMPFields) {
     displaySNMPFields($snmpFields, $hostId, $quietMode);
     exit(0);
 }
Exemple #27
0
function device_template_form_save_dq() {
	/* required for "run_data_query" */
	include_once(CACTI_BASE_PATH . "/lib/data_query.php");

	/* We are on the data queries tab.
	 *
	 * User may request EITHER a reindex method update
	 *                  OR     adding a new data query
	 */

	if (isset($_POST["reindex"])) {
		/*
		 * loop for all possible changes of reindex_method
		 * post variable is build like this
		 * 		reindex_method_device_template_<device_id>_query_<snmp_query_id>_method_<old_reindex_method>
		 * if values of this variable differs from <old_reindex_method>, we will have to update
		 */
		while (list($var,$val) = each($_POST)) {
			if (preg_match("/^reindex_method_device_template_([0-9]+)_query_([0-9]+)_method_([0-9]+)$/", $var, $matches)) {
				/* ================= input validation ================= */
				input_validate_input_number(get_request_var_post("id"));
				input_validate_input_number($matches[1]); # device_template
				input_validate_input_number($matches[2]); # snmp_query_id
				input_validate_input_number($matches[3]); # old reindex method
				$reindex_method = $val;
				input_validate_input_number($reindex_method); # new reindex_method
				/* ==================================================== */

				# change reindex method of this very item
				if ( $reindex_method != $matches[3]) {
					db_execute("REPLACE INTO device_template_snmp_query (device_template_id,snmp_query_id,reindex_method) VALUES (" .
								$matches[1] . "," . $matches[2] . "," . $reindex_method .
								")");
					$reindex_performed = true;
				}
			}
		}


	}elseif (!isset($_POST["selected_items"])) {
		/*
		 * The user just requested to add a new data query to this device template.
		 *
		 * We will add the data query to the device template.
		 * Then, we will perform two runs:
		 * 1. We will prompt the user with a list of devices to be updated and ask for confirmation
		 *    If the device list is empty, there will be NO confirmation!
		 * 2. After confirmation. we will actually update the devices
		 */

		/* We will prompt the user with a list of devices to be updated and ask for confirmation */

		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("id"));
		input_validate_input_number(get_request_var_post("snmp_query_id"));
		input_validate_input_number(get_request_var_post("reindex_method"));
		/* ==================================================== */

		/*
		 * unconditionally add the new data query to this device template
		 * this DOES NOT require a confirmation!
		 */
		db_execute("REPLACE INTO device_template_snmp_query (device_template_id,snmp_query_id, reindex_method) VALUES(" .
					get_request_var_post("id") . "," . get_request_var_post("snmp_query_id") . ", " . get_request_var_post("reindex_method") . ")");

		/*
		 * list all related hosts for confirmation,
		 * but omit those devices that already have this graph template
		 * */
		$new_dq_device_entries = db_fetch_assoc("SELECT device.id AS device_id, " .
											"device.description AS description, " .
											"device.hostname AS hostname " .
											"FROM  	device " .
											"WHERE	device.device_template_id = " . get_request_var_post("id") . " " .
											"AND	device.id NOT IN (" .
												"SELECT device_snmp_query.device_id " .
												"FROM   device_snmp_query " .
												"WHERE  device_snmp_query.snmp_query_id = " . $_POST["snmp_query_id"] .
											")");


		if (sizeof($new_dq_device_entries) > 0) {
			$device_list = ""; $i = 0; $device_array = array();
			/* fetch the graph template's name */
			$template_name = db_fetch_cell("SELECT name FROM snmp_query WHERE id = " . get_request_var_post("snmp_query_id"));

			/* list all devices to be treated for confirmation */
			foreach($new_dq_device_entries as $entry) {
				$device_list .= "<li>" . $entry["hostname"] . " - " . $entry["description"] . "</li>\n";
				$device_array[$i++] = $entry["device_id"];
			}

			/* now draw the html page */
			include_once(CACTI_BASE_PATH . "/include/top_header.php");
			html_start_box("<strong>" . __("Confirm") . "</strong>", "60", $colors["header_panel"], "3", "center", "");


			print "<form action='device_templates.php' method='post' id='device_template_add_dq'>";
			# pass device template id, data query id and reindex method to the updating code below
			form_hidden_box("id", get_request_var_post("id"), "");
			form_hidden_box("snmp_query_id", get_request_var_post("snmp_query_id"), "");
			form_hidden_box("reindex_method", get_request_var_post("reindex_method"), "");
			print "
					<tr>
						<td class='textArea'>
							<p>" . __("Are you sure you want to add the following Data Query:") . " <strong>" . $template_name . "</strong><br>" .
								__("All devices currently attached to the current Device Template will be updated.") . "</p>
							<p><ul>$device_list</ul></p>
						</td>
					</tr>\n
					";

			form_yesno_button_alt2(serialize($device_array), "save_dq");
			html_end_box();
			include_once(CACTI_BASE_PATH . "/include/bottom_footer.php");
			exit;
		}


	} else {
		/*
		 * 2. After confirmation. we will actually update the devices
		 */
		/* get all confirmed devices that were passed as a serializes array */
		$selected_items = unserialize(stripslashes($_POST["selected_items"]));
		if (sizeof($selected_items)) {

			foreach($selected_items as $device_id) {
				/* ================= input validation ================= */
				input_validate_input_number($device_id);
				/* ==================================================== */
				/* add the Data Query */
				db_execute("REPLACE INTO device_snmp_query (device_id,snmp_query_id,reindex_method)
							VALUES (". $device_id . ","
							. get_request_var_post("snmp_query_id") . ","
							. get_request_var_post("reindex_method") . "
							)");

				/* recache snmp data */
				run_data_query($device_id, get_request_var_post("snmp_query_id"));
			}
		}
	}


	header("Location: device_templates.php?action=edit&tab=data_queries&id=" . (empty($device_template_id) ? $_POST["id"] : $device_template_id));
	exit;
}