示例#1
0
文件: errors.php 项目: rboyatt/mahara
 public function render_exception()
 {
     global $USER;
     if (defined('PUBLIC') && !$USER->is_logged_in()) {
         $loginurl = get_full_script_path();
         $loginurl .= false === strpos($loginurl, '?') ? '?' : '&';
         $loginurl .= 'login';
         redirect($loginurl);
     }
     header("HTTP/1.0 403 Forbidden", true);
     return parent::render_exception();
 }
示例#2
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;
	}
}
示例#3
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;
    }
}
示例#4
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));
					}
				}
			}
		}
	}
}
示例#5
0
文件: poller.php 项目: songchin/Cacti
function update_poller_cache($data_source_id, $truncate_performed = false) {
	global $c_xml_data;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

						api_poller_cache_item_add($data_source["host_id"], $data_source_id, $data_source["polling_interval"], $action, $item["data_source_name"], sizeof($data_source_items), addslashes($script_path));
					}
				}
			}
		}
	}
}