Esempio n. 1
0
function process_poller_output(&$rrdtool_pipe, $remainder = FALSE)
{
    global $config, $debug;
    include_once $config["library_path"] . "/rrd.php";
    /* let's count the number of rrd files we processed */
    $rrds_processed = 0;
    if ($remainder) {
        $limit = "";
    } else {
        $limit = "LIMIT 10000";
    }
    /* create/update the rrd files */
    $results = db_fetch_assoc("select\n\t\tpoller_output.output,\n\t\tpoller_output.time,\n\t\tUNIX_TIMESTAMP(poller_output.time) as unix_time,\n\t\tpoller_output.local_data_id,\n\t\tpoller_item.rrd_path,\n\t\tpoller_item.rrd_name,\n\t\tpoller_item.rrd_num\n\t\tfrom (poller_output,poller_item)\n\t\twhere (poller_output.local_data_id=poller_item.local_data_id and poller_output.rrd_name=poller_item.rrd_name)\n\t\torder by poller_output.local_data_id\n\t\t{$limit}");
    if (sizeof($results) > 0) {
        /* create an array keyed off of each .rrd file */
        foreach ($results as $item) {
            /* trim the default characters, but add single and double quotes */
            $value = trim($item["output"], " \r\n\t\v\"'");
            $unix_time = $item["unix_time"];
            $rrd_update_array[$item["rrd_path"]]["local_data_id"] = $item["local_data_id"];
            /* single one value output */
            if (is_numeric($value) || $value == "U") {
                $rrd_update_array[$item["rrd_path"]]["times"][$unix_time][$item["rrd_name"]] = $value;
                /* special case of one value output: hexadecimal to decimal conversion */
            } elseif (is_hexadecimal($value)) {
                /* attempt to accomodate 32bit and 64bit systems */
                $value = str_replace(' ', '', $value);
                if (strlen($value) <= 8 || 2147483647 + 1 == intval(2147483647 + 1)) {
                    $rrd_update_array[$item["rrd_path"]]["times"][$unix_time][$item["rrd_name"]] = hexdec($value);
                } elseif (function_exists("bcpow")) {
                    $dec = 0;
                    $vallen = strlen($value);
                    for ($i = 1; $i <= $vallen; $i++) {
                        $dec = bcadd($dec, bcmul(strval(hexdec($value[$i - 1])), bcpow('16', strval($vallen - $i))));
                    }
                    $rrd_update_array[$item["rrd_path"]]["times"][$unix_time][$item["rrd_name"]] = $dec;
                } else {
                    $rrd_update_array[$item["rrd_path"]]["times"][$unix_time][$item["rrd_name"]] = "U";
                }
                /* multiple value output */
            } else {
                $values = explode(" ", $value);
                $rrd_field_names = array_rekey(db_fetch_assoc("select\n\t\t\t\t\tdata_template_rrd.data_source_name,\n\t\t\t\t\tdata_input_fields.data_name\n\t\t\t\t\tfrom (data_template_rrd,data_input_fields)\n\t\t\t\t\twhere data_template_rrd.data_input_field_id=data_input_fields.id\n\t\t\t\t\tand data_template_rrd.local_data_id=" . $item["local_data_id"]), "data_name", "data_source_name");
                if (sizeof($values)) {
                    foreach ($values as $value) {
                        $matches = explode(":", $value);
                        if (sizeof($matches) == 2) {
                            if (isset($rrd_field_names[$matches[0]])) {
                                if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_DEBUG || $debug) {
                                    cacti_log("Parsed MULTI output field '" . $matches[0] . ":" . $matches[1] . "' [map " . $matches[0] . "->" . $rrd_field_names[$matches[0]] . "]", true, "POLLER");
                                }
                                $rrd_update_array[$item["rrd_path"]]["times"][$unix_time][$rrd_field_names[$matches[0]]] = $matches[1];
                            }
                        }
                    }
                }
            }
            /* fallback values */
            if (!isset($rrd_update_array[$item["rrd_path"]]["times"][$unix_time]) && $item["rrd_name"] != "") {
                $rrd_update_array[$item["rrd_path"]]["times"][$unix_time][$item["rrd_name"]] = "U";
            } else {
                if (!isset($rrd_update_array[$item["rrd_path"]]["times"][$unix_time]) && $item["rrd_name"] == "") {
                    unset($rrd_update_array[$item["rrd_path"]]);
                }
            }
        }
        /* make sure each .rrd file has complete data */
        reset($results);
        $k = 0;
        $data_ids = array();
        foreach ($results as $item) {
            $unix_time = $item["unix_time"];
            if (isset($rrd_update_array[$item["rrd_path"]]["times"][$unix_time])) {
                if ($item["rrd_num"] <= sizeof($rrd_update_array[$item["rrd_path"]]["times"][$unix_time])) {
                    $data_ids[] = $item["local_data_id"];
                    $k++;
                    if ($k % 10000 == 0) {
                        db_execute("DELETE FROM poller_output WHERE local_data_id IN (" . implode(",", $data_ids) . ")");
                        $k = 0;
                        $data_ids = array();
                    }
                } else {
                    unset($rrd_update_array[$item["rrd_path"]]["times"][$unix_time]);
                }
            }
        }
        if ($k > 0) {
            db_execute("DELETE FROM poller_output WHERE local_data_id IN (" . implode(",", $data_ids) . ")");
        }
        api_plugin_hook_function('poller_output', $rrd_update_array);
        if (api_plugin_hook_function('poller_on_demand', $results)) {
            $rrds_processed = rrdtool_function_update($rrd_update_array, $rrdtool_pipe);
        }
    }
    return $rrds_processed;
}
Esempio n. 2
0
function boost_process_poller_output($use_server = FALSE, $local_data_id = "")
{
    global $config, $boost_sock, $boost_timeout, $debug, $get_memory, $memory_used;
    global $rrdtool_pipe, $rrdtool_read_pipe;
    include_once $config["library_path"] . "/rrd.php";
    /* suppress warnings */
    if (defined("E_DEPRECATED")) {
        error_reporting(E_ALL ^ E_DEPRECATED);
    } else {
        error_reporting(E_ALL);
    }
    /* install the boost error handler */
    set_error_handler("boost_error_handler");
    /* load system variables needed */
    $log_verbosity = read_config_option("log_verbosity");
    $upd_string_len = read_config_option("boost_rrd_update_string_length");
    /* tiny SQL where addendum for boost */
    if (strlen($local_data_id)) {
        /* we can simplify the delete process if only one local_data_id */
        $single_local_data_id = TRUE;
        $orig_local_data_id = $local_data_id;
        /* aquire lock in order to prevent race conditions */
        while (!db_fetch_cell("SELECT GET_LOCK('boost.single_ds.{$local_data_id}', 1)")) {
            usleep(50000);
        }
    } else {
        $single_local_data_id = FALSE;
        $poller_interval = read_config_option("poller_interval");
        $rrd_update_interval = read_config_option("boost_rrd_update_interval");
        $data_ids_to_get = read_config_option("boost_rrd_update_max_records_per_select");
        $archive_table = boost_get_arch_table_name();
        if ($archive_table === FALSE) {
            cacti_log("Failed to determine archive table", FALSE, "BOOST");
            return 0;
        }
    }
    /* get the records */
    if ($single_local_data_id) {
        $query_string = "";
        $arch_tables = db_fetch_assoc("\tSELECT table_name AS name\r\n\t\t\t\t\t\tFROM information_schema.tables\r\n\t\t\t\t\t\t\tWHERE table_schema=SCHEMA()\r\n\t\t\t\t\t\t\tAND table_name LIKE 'poller_output_boost_arch_%'\r\n\t\t\t\t\t\t\tAND table_rows>0;\r\n\t\t\t\t\t\t");
        if (count($arch_tables)) {
            foreach ($arch_tables as $table) {
                if (strlen($query_string)) {
                    $query_string .= " UNION ";
                }
                $query_string .= " ( SELECT local_data_id, UNIX_TIMESTAMP(time) AS timestamp, rrd_name, output FROM " . $table["name"] . " WHERE local_data_id='{$local_data_id}' ) ";
            }
        }
        if (strlen($query_string)) {
            $query_string .= " UNION ";
        }
        $timestamp = time();
        $query_string .= " ( SELECT local_data_id, UNIX_TIMESTAMP(time) AS timestamp, rrd_name, output FROM poller_output_boost WHERE local_data_id='{$local_data_id}' AND time < FROM_UNIXTIME('{$timestamp}') ) ";
        $query_string .= "  ORDER BY local_data_id ASC, timestamp ASC, rrd_name ASC ";
    } else {
        $query_string = "SELECT local_data_id, UNIX_TIMESTAMP(time) AS timestamp, rrd_name, output FROM {$archive_table} FORCE INDEX (PRIMARY)\r\n\t\t\t\tORDER BY local_data_id ASC, time ASC, rrd_name ASC\r\n\t\t\t\tLIMIT {$data_ids_to_get} ";
    }
    boost_timer("get_records", BOOST_TIMER_START);
    $results = db_fetch_assoc($query_string);
    boost_timer("get_records", BOOST_TIMER_END);
    /* log memory */
    if ($get_memory) {
        $cur_memory = memory_get_usage();
        if ($cur_memory > $memory_used) {
            $memory_used = $cur_memory;
        }
    }
    if ($single_local_data_id && ($debug || $log_verbosity >= POLLER_VERBOSITY_MEDIUM)) {
        cacti_log("NOTE: Updating Local Data ID:'{$local_data_id}', Total of '" . sizeof($results) . "' Updates to Process", FALSE, "BOOST");
    }
    if (sizeof($results) > 0) {
        /* open the boost socket connection if applicable */
        if (read_config_option("boost_server_enable") == "on" && $local_data_id != "" && $use_server) {
            $boost_timeout = read_config_option("boost_server_timeout");
            $boost_sock = boost_server_connect();
            if ($boost_sock < 0) {
                /* restore original error handler */
                restore_error_handler();
                return 0;
            }
        }
        /* create an array keyed off of each .rrd file */
        $local_data_id = -1;
        $time = -1;
        $outbuf = "";
        $last_update = -1;
        $last_item = array("local_data_id" => -1, "timestamp" => -1, "rrd_name" => "");
        /* we are going to blow away all record if ok */
        $vals_in_buffer = 0;
        /* cut last DS (likely to be incomplete due to LIMIT)*/
        if (!$single_local_data_id) {
            reset($results);
            $first_ds = $results[key($results)];
            $first_ds = intval($first_ds["local_data_id"]);
            end($results);
            $last_ds = $results[key($results)];
            $last_ds = intval($last_ds["local_data_id"]);
            reset($results);
            if ($first_ds == $last_ds) {
                if (sizeof($results) == $data_ids_to_get) {
                    cacti_log("FALURE: Current LIMIT ({$data_ids_to_get}) is too low to run multiple DS RRD writes, consider raising it", FALSE, "BOOST");
                }
                restore_error_handler();
                return boost_process_poller_output($use_server, $first_ds);
            }
        }
        boost_timer("results_cycle", BOOST_TIMER_START);
        /* go through each poller_output_boost entries and process */
        foreach ($results as $item) {
            $item["timestamp"] = trim($item["timestamp"]);
            if ($single_local_data_id && $last_item["local_data_id"] == $item["local_data_id"] && $last_item["timestamp"] == $item["timestamp"] && strcmp($last_item["rrd_name"], $item["rrd_name"]) == 0) {
                continue;
            }
            if (!$single_local_data_id && $first_ds != $last_ds && $last_ds == $item["local_data_id"]) {
                /* we faced last and possibly incomplete DS, bail out */
                break;
            }
            /* if the local_data_id changes, we need to flush the buffer */
            if ($local_data_id != $item["local_data_id"]) {
                /* update the rrd for the previous local_data_id */
                if ($vals_in_buffer) {
                    if ($debug || $log_verbosity >= POLLER_VERBOSITY_MEDIUM) {
                        cacti_log("NOTE: Updating Local Data Id:'{$local_data_id}', Template:" . $rrd_tmpl . ", Output:" . $outbuf, FALSE, "BOOST");
                    }
                    boost_timer("rrdupdate", BOOST_TIMER_START);
                    $return_value = boost_rrdtool_function_update($local_data_id, $rrd_path, $rrd_tmpl, $initial_time, $outbuf, $rrdtool_pipe);
                    boost_timer("rrdupdate", BOOST_TIMER_END);
                    $outbuf = "";
                    $vals_in_buffer = 0;
                    /* check return status for delete operation */
                    if (trim($return_value) != "OK") {
                        cacti_log("WARNING: RRD Update Warning '" . $return_value . "' for Local Data ID '{$local_data_id}'", FALSE, "BOOST");
                    }
                }
                /* reset the rrd file path and templates, assume non multi output */
                boost_timer("rrd_filename_and_template", BOOST_TIMER_START);
                $rrd_data = boost_get_rrd_filename_and_template($item["local_data_id"]);
                $rrd_tmpl = $rrd_data["rrd_template"];
                $rrd_path = $rrd_data["rrd_path"];
                boost_timer("rrd_filename_and_template", BOOST_TIMER_END);
                $pipe = is_resource($rrdtool_read_pipe) || is_array($rrdtool_read_pipe) ? $rrdtool_read_pipe : $rrdtool_pipe;
                boost_timer("rrd_lastupdate", BOOST_TIMER_START);
                $last_update = boost_rrdtool_get_last_update_time($rrd_path, $pipe);
                boost_timer("rrd_lastupdate", BOOST_TIMER_END);
                $local_data_id = $item["local_data_id"];
                $time = $item["timestamp"];
                $initial_time = $time;
                $outbuf = " " . $time;
                $multi_vals_set = FALSE;
            }
            /* don't generate error messages if the RRD has already been updated */
            if ($time <= $last_update) {
                cacti_log("WARNING: Stale Poller Data Found! Item Time:'" . $time . "', RRD Time:'" . $last_update . "' Ignoring Value!", FALSE, "BOOST");
                $value = 'DNP';
            } else {
                $value = trim($item["output"]);
            }
            if ($time != $item["timestamp"]) {
                if (strlen($outbuf) > $upd_string_len) {
                    if ($log_verbosity >= POLLER_VERBOSITY_MEDIUM) {
                        cacti_log("NOTE: Updating Local Data Id:'{$local_data_id}', Template:" . $rrd_tmpl . ", Output:" . $outbuf, FALSE, "BOOST");
                    }
                    boost_timer("rrdupdate", BOOST_TIMER_START);
                    $return_value = boost_rrdtool_function_update($local_data_id, $rrd_path, $rrd_tmpl, $initial_time, $outbuf, $rrdtool_pipe);
                    boost_timer("rrdupdate", BOOST_TIMER_END);
                    $outbuf = "";
                    $vals_in_buffer = 0;
                    /* check return status for delete operation */
                    if (trim($return_value) != "OK") {
                        cacti_log("WARNING: RRD Update Warning '" . $return_value . "' for Local Data ID '{$local_data_id}'", FALSE, "BOOST");
                    }
                }
                $outbuf .= " " . $item["timestamp"];
                $time = $item["timestamp"];
            }
            /* single one value output */
            if (strcmp($value, 'DNP') == 0) {
                /* continue, bad time */
            } elseif (is_numeric($value) || strcmp($value, "U") == 0) {
                $outbuf .= ":" . $value;
                $vals_in_buffer++;
            } elseif (function_exists("is_hexadecimal") && is_hexadecimal($value)) {
                $outbuf .= ":" . hexdec($value);
                $vals_in_buffer++;
            } elseif (strlen($value)) {
                /* break out multiple value output to an array */
                $values = explode(" ", $value);
                if (!$multi_vals_set) {
                    $rrd_field_names = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\t\t\tdata_template_rrd.data_source_name,\r\n\t\t\t\t\t\tdata_input_fields.data_name\r\n\t\t\t\t\t\tFROM (data_template_rrd,data_input_fields)\r\n\t\t\t\t\t\tWHERE data_template_rrd.data_input_field_id=data_input_fields.id\r\n\t\t\t\t\t\tAND data_template_rrd.local_data_id=" . $item["local_data_id"]), "data_name", "data_source_name");
                    $rrd_tmpl = "";
                }
                $first_tmpl = 1;
                $multi_ok = FALSE;
                for ($i = 0; $i < count($values); $i++) {
                    if (preg_match("/^([a-zA-Z0-9_\\.-]+):([eE0-9\\+\\.-]+)\$/", $values[$i], $matches)) {
                        if (isset($rrd_field_names[$matches[1]])) {
                            $multi_ok = TRUE;
                            if ($log_verbosity == POLLER_VERBOSITY_DEBUG) {
                                cacti_log("Parsed MULTI output field '" . $matches[0] . "' [map " . $matches[1] . "->" . $rrd_field_names[$matches[1]] . "]", FALSE, "BOOST");
                            }
                            if (!$multi_vals_set) {
                                if (!$first_tmpl) {
                                    $rrd_tmpl .= ":";
                                }
                                $rrd_tmpl .= $rrd_field_names[$matches[1]];
                                $first_tmpl = 0;
                            }
                            if (is_numeric($matches[2]) || $matches[2] == "U") {
                                $outbuf .= ":" . $matches[2];
                            } elseif (function_exists("is_hexadecimal") && is_hexadecimal($matches[2])) {
                                $outbuf .= ":" . hexdec($matches[2]);
                            } else {
                                $outbuf .= ":U";
                            }
                        }
                    }
                }
                /* we only want to process the template and gather the fields once */
                $multi_vals_set = TRUE;
                if ($multi_ok) {
                    $vals_in_buffer++;
                }
            } else {
                cacti_log("WARNING: Local Data Id [" . $item["local_data_id"] . "] Contains an empty value", FALSE, "BOOST");
            }
        }
        /* process the last rrdupdate if applicable */
        if ($vals_in_buffer) {
            if ($log_verbosity >= POLLER_VERBOSITY_MEDIUM) {
                cacti_log("NOTE: Updating Local Data Id:'{$local_data_id}', Template:" . $rrd_tmpl . ", Output:" . $outbuf, FALSE, "BOOST");
            }
            boost_timer("rrdupdate", BOOST_TIMER_START);
            $return_value = boost_rrdtool_function_update($local_data_id, $rrd_path, $rrd_tmpl, $initial_time, $outbuf, $rrdtool_pipe);
            boost_timer("rrdupdate", BOOST_TIMER_END);
            /* check return status for delete operation */
            if (trim($return_value) != "OK") {
                cacti_log("WARNING: RRD Update Warning '" . $return_value . "' for Local Data ID '{$local_data_id}'", FALSE, "BOOST");
            }
        }
        boost_timer("results_cycle", BOOST_TIMER_END);
        /* remove the entries from the table */
        boost_timer("delete", BOOST_TIMER_START);
        if ($single_local_data_id) {
            $tables = db_fetch_assoc("SELECT table_name AS name\r\n\t\t\t\t\t\tFROM information_schema.tables\r\n\t\t\t\t\t\tWHERE table_schema=SCHEMA()\r\n\t\t\t\t\t\tAND ( table_name LIKE 'poller_output_boost_arch_%' OR table_name LIKE 'poller_output_boost' )\r\n\t\t\t\t\t\tAND table_rows>0;\r\n\t\t\t\t\t\t");
            if (count($tables)) {
                foreach ($tables as $table) {
                    db_execute("DELETE FROM " . $table["name"] . " WHERE local_data_id='{$local_data_id}' AND time < FROM_UNIXTIME('{$timestamp}')");
                }
            }
        } else {
            db_execute("DELETE FROM {$archive_table} WHERE local_data_id BETWEEN '{$first_ds}' AND '" . ($last_ds - 1) . "'");
        }
        boost_timer("delete", BOOST_TIMER_END);
        /* close the boost server connection, if applicable */
        if (read_config_option("boost_server_enable") == "on" && $local_data_id != "" && $use_server) {
            boost_server_disconnect($boost_sock);
        }
    }
    if ($single_local_data_id) {
        db_execute("SELECT RELEASE_LOCK('boost.single_ds.{$orig_local_data_id}')");
    }
    /* restore original error handler */
    restore_error_handler();
    return sizeof($results);
}
Esempio n. 3
0
function process_poller_output($rrdtool_pipe, $remainder = FALSE) {
	global $config;

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

	/* let's count the number of rrd files we processed */
	$rrds_processed = 0;

	if ($remainder) {
		$limit = "";
	}else{
		$limit = "LIMIT 10000";
	}

	/* create/update the rrd files */
	$results = db_fetch_assoc("select
		poller_output.output,
		poller_output.time,
		poller_output.local_data_id,
		poller_item.rrd_path,
		poller_item.rrd_name,
		poller_item.rrd_num
		from (poller_output,poller_item)
		where (poller_output.local_data_id=poller_item.local_data_id and poller_output.rrd_name=poller_item.rrd_name)
		$limit");

	if (sizeof($results) > 0) {
		/* create an array keyed off of each .rrd file */
		foreach ($results as $item) {
			$value = trim($item["output"]);
			$unix_time = strtotime($item["time"]);

			$rrd_update_array{$item["rrd_path"]}["local_data_id"] = $item["local_data_id"];

			/* single one value output */
			if ((is_numeric($value)) || ($value == "U")) {
				$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = $value;
			/* special case of one value output: hexadecimal to decimal conversion */
			}elseif (is_hexadecimal($value)) {
				$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = hexdec($value);
			/* multiple value output */
			}else{
				$values = explode(" ", $value);

				$rrd_field_names = array_rekey(db_fetch_assoc("select
					data_template_rrd.data_source_name,
					data_input_fields.data_name
					from (data_template_rrd,data_input_fields)
					where data_template_rrd.data_input_field_id=data_input_fields.id
					and data_template_rrd.local_data_id=" . $item["local_data_id"]), "data_name", "data_source_name");

				for ($i=0; $i<count($values); $i++) {
					if (preg_match("/^([a-zA-Z0-9_\.-]+):([eE0-9\+\.-]+)$/", $values[$i], $matches)) {
						if (isset($rrd_field_names{$matches[1]})) {
							if (read_config_option("log_verbosity") == POLLER_VERBOSITY_DEBUG) {
								cacti_log("Parsed MULTI output field '" . $matches[0] . "' [map " . $matches[1] . "->" . $rrd_field_names{$matches[1]} . "]" , true, "POLLER");
							}

							$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$rrd_field_names{$matches[1]}} = $matches[2];
						}
					}
				}
			}

			/* fallback values */
			if ((!isset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) && ($item["rrd_name"] != "")) {
				$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = "U";
			}else if ((!isset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) && ($item["rrd_name"] == "")) {
				unset($rrd_update_array{$item["rrd_path"]});
			}
		}

		/* make sure each .rrd file has complete data */
		reset($results);
		foreach ($results as $item) {
			$unix_time = strtotime($item["time"]);

			if (isset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) {
				if ($item["rrd_num"] <= sizeof($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) {
					db_execute("delete from poller_output where local_data_id='" . $item["local_data_id"] . "' and rrd_name='" . $item["rrd_name"] . "' and time='" . $item["time"] . "'");
				}else{
					unset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time]);
				}
			}
		}

		$rrds_processed = rrdtool_function_update($rrd_update_array, $rrdtool_pipe);
	}

	return $rrds_processed;
}
Esempio n. 4
0
function strip_quotes($result)
{
    /* first strip all single and double quotes from the string */
    $result = trim(trim($result), "'\"");
    /* clean off ugly non-numeric data */
    if (!is_numeric($result) && !is_hexadecimal($result) && $result != 'U') {
        $len = strlen($result);
        for ($a = $len - 1; $a >= 0; $a--) {
            $p = ord($result[$a]);
            if ($p > 47 && $p < 58 || $p == 85) {
                $result = substr($result, 0, $a + 1);
                break;
            }
        }
    }
    return $result;
}
Esempio n. 5
0
function mikrotik_macParse($value)
{
    if (is_hexadecimal($value)) {
        return $value;
    } else {
        $newval = '';
        for ($i = 0; $i < strlen($value); $i++) {
            $newval .= (strlen($newval) ? ":" : "") . bin2hex($value[$i]);
        }
        return $newval;
    }
}
Esempio n. 6
0
function process_poller_output(&$rrdtool_pipe, $remainder = FALSE)
{
    global $config, $debug;
    include_once $config['library_path'] . '/rrd.php';
    /* let's count the number of rrd files we processed */
    $rrds_processed = 0;
    if ($remainder) {
        $limit = '';
    } else {
        $limit = 'LIMIT 10000';
    }
    /* create/update the rrd files */
    $results = db_fetch_assoc("select\n\t\tpoller_output.output,\n\t\tpoller_output.time,\n\t\tUNIX_TIMESTAMP(poller_output.time) as unix_time,\n\t\tpoller_output.local_data_id,\n\t\tpoller_item.rrd_path,\n\t\tpoller_item.rrd_name,\n\t\tpoller_item.rrd_num\n\t\tfrom (poller_output,poller_item)\n\t\twhere (poller_output.local_data_id=poller_item.local_data_id AND poller_output.rrd_name=poller_item.rrd_name)\n\t\torder by poller_output.local_data_id\n\t\t{$limit}");
    if (sizeof($results) > 0) {
        /* create an array keyed off of each .rrd file */
        foreach ($results as $item) {
            /* trim the default characters, but add single and double quotes */
            $value = trim($item['output'], " \r\n\t\v\"'");
            $unix_time = $item['unix_time'];
            $rrd_update_array[$item['rrd_path']]['local_data_id'] = $item['local_data_id'];
            /* single one value output */
            if (is_numeric($value) || $value == 'U') {
                $rrd_update_array[$item['rrd_path']]['times'][$unix_time][$item['rrd_name']] = $value;
                /* special case of one value output: hexadecimal to decimal conversion */
            } elseif (is_hexadecimal($value)) {
                /* attempt to accomodate 32bit and 64bit systems */
                $value = str_replace(' ', '', $value);
                if (strlen($value) <= 8 || 2147483647 + 1 == intval(2147483647 + 1)) {
                    $rrd_update_array[$item['rrd_path']]['times'][$unix_time][$item['rrd_name']] = hexdec($value);
                } elseif (function_exists('bcpow')) {
                    $dec = 0;
                    $vallen = strlen($value);
                    for ($i = 1; $i <= $vallen; $i++) {
                        $dec = bcadd($dec, bcmul(strval(hexdec($value[$i - 1])), bcpow('16', strval($vallen - $i))));
                    }
                    $rrd_update_array[$item['rrd_path']]['times'][$unix_time][$item['rrd_name']] = $dec;
                } else {
                    $rrd_update_array[$item['rrd_path']]['times'][$unix_time][$item['rrd_name']] = 'U';
                }
                /* multiple value output */
            } else {
                $values = explode(' ', $value);
                $rrd_field_names = array_rekey(db_fetch_assoc_prepared('SELECT
					data_template_rrd.data_source_name,
					data_input_fields.data_name
					FROM (data_template_rrd, data_input_fields)
					WHERE data_template_rrd.data_input_field_id = data_input_fields.id
					and data_template_rrd.local_data_id = ?', array($item['local_data_id'])), 'data_name', 'data_source_name');
                if (sizeof($values)) {
                    foreach ($values as $value) {
                        $matches = explode(':', $value);
                        if (sizeof($matches) == 2) {
                            if (isset($rrd_field_names[$matches[0]])) {
                                if (read_config_option('log_verbosity') >= POLLER_VERBOSITY_DEBUG || $debug) {
                                    cacti_log("Parsed MULTI output field '" . $matches[0] . ':' . $matches[1] . "' [map " . $matches[0] . '->' . $rrd_field_names[$matches[0]] . ']', true, 'POLLER');
                                }
                                $rrd_update_array[$item['rrd_path']]['times'][$unix_time][$rrd_field_names[$matches[0]]] = $matches[1];
                            }
                        }
                    }
                }
            }
            /* fallback values */
            if (!isset($rrd_update_array[$item['rrd_path']]['times'][$unix_time]) && $item['rrd_name'] != '') {
                $rrd_update_array[$item['rrd_path']]['times'][$unix_time][$item['rrd_name']] = 'U';
            } else {
                if (!isset($rrd_update_array[$item['rrd_path']]['times'][$unix_time]) && $item['rrd_name'] == '') {
                    unset($rrd_update_array[$item['rrd_path']]);
                }
            }
        }
        /* make sure each .rrd file has complete data */
        reset($results);
        $k = 0;
        $data_ids = array();
        foreach ($results as $item) {
            $unix_time = $item['unix_time'];
            if (isset($rrd_update_array[$item['rrd_path']]['times'][$unix_time])) {
                if ($item['rrd_num'] <= sizeof($rrd_update_array[$item['rrd_path']]['times'][$unix_time])) {
                    $data_ids[] = $item['local_data_id'];
                    $k++;
                    if ($k % 10000 == 0) {
                        db_execute('DELETE FROM poller_output WHERE local_data_id IN (' . implode(',', $data_ids) . ')');
                        $k = 0;
                        $data_ids = array();
                    }
                } else {
                    unset($rrd_update_array[$item['rrd_path']]['times'][$unix_time]);
                }
            }
        }
        if ($k > 0) {
            db_execute('DELETE FROM poller_output WHERE local_data_id IN (' . implode(',', $data_ids) . ')');
        }
        /* process dsstats information */
        dsstats_poller_output($rrd_update_array);
        api_plugin_hook_function('poller_output', $rrd_update_array);
        if (boost_poller_on_demand($results)) {
            $rrds_processed = rrdtool_function_update($rrd_update_array, $rrdtool_pipe);
        }
    }
    return $rrds_processed;
}
Esempio n. 7
0
function process_poller_output($rrdtool_pipe, $remainder = FALSE) {
	global $config;

	include_once(CACTI_BASE_PATH . "/lib/rrd.php");

	/* let's count the number of rrd files we processed */
	$rrds_processed = 0;

	if ($remainder) {
		$limit = "";
	}else{
		$limit = "LIMIT 10000";
	}

	/* create/update the rrd files */
	$results = db_fetch_assoc("select
		poller_output.output,
		poller_output.time,
		poller_output.local_data_id,
		poller_item.rrd_path,
		poller_item.rrd_name,
		poller_item.rrd_num
		from (poller_output,poller_item)
		where (poller_output.local_data_id=poller_item.local_data_id and poller_output.rrd_name=poller_item.rrd_name)
		$limit");

	if (sizeof($results) > 0) {
		/* create an array keyed off of each .rrd file */
		foreach ($results as $item) {
			/* trim the default characters, but add single and double quotes */
			$value = trim($item["output"], " \r\n\t\x0B\0\"'");
			$unix_time = strtotime($item["time"]);

			$rrd_update_array{$item["rrd_path"]}["local_data_id"] = $item["local_data_id"];

			/* single one value output */
			if ((is_numeric($value)) || ($value == "U")) {
				$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = $value;
			/* special case of one value output: hexadecimal to decimal conversion */
			}elseif (is_hexadecimal($value)) {
				/* attempt to accomodate 32bit and 64bit systems */
				$value = str_replace(' ', '', $value);
				if (strlen($value) <= 8 || ((2147483647+1) == intval(2147483647+1))) {
					$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = hexdec($value);
				}elseif (function_exists("bcpow")) {
					$dec = 0;
					for ($i = 1; $i <= $vallen; $i++) {
						$dec = bcadd($dec, bcmul(strval(hexdec($value[$i - 1])), bcpow('16', strval($vallen - $i))));
					}
					$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = $dec;
				}else{
					$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = "U";
				}
			/* multiple value output */
			}else{
				$values = explode(" ", $value);

				$rrd_field_names = array_rekey(db_fetch_assoc("select
					data_template_rrd.data_source_name,
					data_input_fields.data_name
					from (data_template_rrd,data_input_fields)
					where data_template_rrd.data_input_field_id=data_input_fields.id
					and data_template_rrd.local_data_id=" . $item["local_data_id"]), "data_name", "data_source_name");

				if (sizeof($values)) {
				foreach($values as $value) {
					$matches = explode(":", $value);

					if (sizeof($matches) == 2) {
						if (isset($rrd_field_names{$matches[0]})) {
							if (read_config_option("log_verbosity") == POLLER_VERBOSITY_DEBUG) {
								cacti_log("Parsed MULTI output field '" . $matches[0] . ":" . $matches[1] . "' [map " . $matches[0] . "->" . $rrd_field_names{$matches[0]} . "]" , true, "POLLER");
							}

							$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$rrd_field_names{$matches[0]}} = $matches[1];
						}
					}
				}
				}
			}

			/* fallback values */
			if ((!isset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) && ($item["rrd_name"] != "")) {
				$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = "U";
			}else if ((!isset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) && ($item["rrd_name"] == "")) {
				unset($rrd_update_array{$item["rrd_path"]});
			}
		}

		/* make sure each .rrd file has complete data */
		reset($results);
		$sql_wheres = array();
		foreach ($results as $item) {
			$unix_time = strtotime($item["time"]);

			if (isset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) {
				if ($item["rrd_num"] <= sizeof($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) {
					$sql_wheres[] = " ( local_data_id='{$item['local_data_id']}' AND rrd_name='{$item['rrd_name']}' AND time='{$item['time']}' ) ";
					if (count($sql_wheres) > 100) {
						db_execute("DELETE FROM poller_output WHERE " . implode(" OR ", $sql_wheres));
						$sql_wheres = array();
					}
				}else{
					unset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time]);
				}
			}
		}
		if (count($sql_wheres) > 100) {
			db_execute("DELETE FROM poller_output WHERE " . implode(" OR ", $sql_wheres));
		}
		api_plugin_hook_function('poller_output', $rrd_update_array);

		if (api_plugin_hook_function('poller_on_demand', $results)) {
			$rrds_processed = rrdtool_function_update($rrd_update_array, $rrdtool_pipe);
		}
	}

	return $rrds_processed;
}