Beispiel #1
0
function boost_rrdtool_execute_internal($command_line, $log_to_stdout, $output_flag, $logopt = "WEBLOG")
{
    global $config, $rrdtool_pipe;
    static $last_command;
    if (!is_numeric($output_flag)) {
        $output_flag = RRDTOOL_OUTPUT_STDOUT;
    }
    /* WIN32: before sending this command off to rrdtool, get rid
    	of all of the '\' characters. Unix does not care; win32 does.
    	Also make sure to replace all of the fancy \'s at the end of the line,
    	but make sure not to get rid of the "\n"'s that are supposed to be
    	in there (text format) */
    $command_line = str_replace("\\\n", " ", $command_line);
    /* output information to the log file if appropriate */
    if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_DEBUG) {
        cacti_log("CACTI2RRD: " . read_config_option("path_rrdtool") . " {$command_line}", $log_to_stdout, $logopt);
    }
    /* if we want to see the error output from rrdtool; make sure to specify this */
    if ($output_flag == RRDTOOL_OUTPUT_STDERR) {
        if (!(is_resource($rrdtool_pipe) || is_array($rrdtool_pipe))) {
            $command_line .= " 2>&1";
        }
    }
    /* an empty $rrdtool_pipe resource or no array means no fd is available */
    if (!(is_resource($rrdtool_pipe) || is_array($rrdtool_pipe))) {
        if ($config["cacti_server_os"] == "unix") {
            $popen_type = "r";
        } else {
            $popen_type = "rb";
        }
        session_write_close();
        $fp = popen(read_config_option("path_rrdtool") . escape_command(" {$command_line}"), $popen_type);
    } else {
        $i = 0;
        while (1) {
            if (function_exists("rrd_get_fd")) {
                $fd = rrd_get_fd($rrdtool_pipe, RRDTOOL_PIPE_CHILD_READ);
            } else {
                $fd = $rrdtool_pipe;
            }
            if (fwrite($fd, escape_command(" {$command_line}") . "\r\n") == false) {
                cacti_log("ERROR: Detected RRDtool Crash on '{$command_line}'.  Last command was '{$last_command}'");
                /* close the invalid pipe */
                rrd_close($rrdtool_pipe);
                /* open a new rrdtool process */
                $rrdtool_pipe = rrd_init();
                if ($i > 4) {
                    cacti_log("FATAL: RRDtool Restart Attempts Exceeded.  Giving up on command.");
                    break;
                } else {
                    $i++;
                }
                continue;
            } else {
                fflush($fd);
                break;
            }
        }
    }
    /* store the last command to provide rrdtool segfault diagnostics */
    $last_command = $command_line;
    switch ($output_flag) {
        case RRDTOOL_OUTPUT_NULL:
            return;
            break;
        case RRDTOOL_OUTPUT_STDOUT:
            if (isset($fp) && is_resource($fp)) {
                $line = "";
                while (!feof($fp)) {
                    $line .= fgets($fp, 4096);
                }
                pclose($fp);
                return $line;
            }
            break;
        case RRDTOOL_OUTPUT_STDERR:
            if (isset($fp) && is_resource($fp)) {
                $line = "";
                while (!feof($fp)) {
                    $line .= fgets($fp, 4096);
                }
                pclose($fp);
                if (substr($output, 1, 3) == "PNG") {
                    return "OK";
                }
                if (substr($output, 0, 5) == "GIF87") {
                    return "OK";
                }
                print $output;
            }
            break;
        case RRDTOOL_OUTPUT_GRAPH_DATA:
            if (isset($fp) && is_resource($fp)) {
                $line = "";
                while (!feof($fp)) {
                    $line .= fgets($fp, 4096);
                }
                pclose($fp);
                return $line;
            }
            break;
    }
}
Beispiel #2
0
function rrdtool_execute($command_line, $log_to_stdout, $output_flag, &$rrd_struc = array(), $logopt = "WEBLOG") {
	global $config;

	if (!is_numeric($output_flag)) {
		$output_flag = RRDTOOL_OUTPUT_STDOUT;
	}

	/* WIN32: before sending this command off to rrdtool, get rid
	of all of the '\' characters. Unix does not care; win32 does.
	Also make sure to replace all of the fancy \'s at the end of the line,
	but make sure not to get rid of the "\n"'s that are supposed to be
	in there (text format) */
	$command_line = str_replace("\\\n", " ", $command_line);

	/* output information to the log file if appropriate */
	if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_DEBUG) {
		cacti_log("CACTI2RRD: " . read_config_option("path_rrdtool") . " $command_line", $log_to_stdout, $logopt);
	}

	/* if we want to see the error output from rrdtool; make sure to specify this */
	if (($output_flag == RRDTOOL_OUTPUT_STDERR) && (sizeof($rrd_struc) == 0)) {
		$command_line .= " 2>&1";
	}

	/* use popen to eliminate the zombie issue */
	if ($config["cacti_server_os"] == "unix") {
		/* an empty $rrd_struc array means no fp is available */
		if (sizeof($rrd_struc) == 0) {
			session_write_close();
			$fp = popen(read_config_option("path_rrdtool") . escape_command(" $command_line"), "r");
		}else{
			$i = 0;

			while (1) {
				if (fwrite(rrd_get_fd($rrd_struc, RRDTOOL_PIPE_CHILD_READ), escape_command(" $command_line") . "\r\n") == false) {
					cacti_log("ERROR: Detected RRDtool Crash attempting to perform write");

					/* close the invalid pipe */
					rrd_close($rrd_struc);

					/* open a new rrdtool process */
					$rrd_struc = rrd_init();

					if ($i > 4) {
						cacti_log("FATAL: RRDtool Restart Attempts Exceeded.  Giving up on command.");

						break;
					}else{
						$i++;
					}

					continue;
				}else{
					fflush(rrd_get_fd($rrd_struc, RRDTOOL_PIPE_CHILD_READ));

					break;
				}
			}
		}
	}elseif ($config["cacti_server_os"] == "win32") {
		/* an empty $rrd_struc array means no fp is available */
		if (sizeof($rrd_struc) == 0) {
			session_write_close();
			$fp = popen(read_config_option("path_rrdtool") . escape_command(" $command_line"), "rb");
		}else{
			$i = 0;

			while (1) {
				if (fwrite(rrd_get_fd($rrd_struc, RRDTOOL_PIPE_CHILD_READ), escape_command(" $command_line") . "\r\n") == false) {
					cacti_log("ERROR: Detected RRDtool Crash attempting to perform write");

					/* close the invalid pipe */
					rrd_close($rrd_struc);

					/* open a new rrdtool process */
					$rrd_struc = rrd_init();

					if ($i > 4) {
						cacti_log("FATAL: RRDtool Restart Attempts Exceeded.  Giving up on command.");

						break;
					}else{
						$i++;
					}

					continue;
				}else{
					fflush(rrd_get_fd($rrd_struc, RRDTOOL_PIPE_CHILD_READ));

					break;
				}
			}
		}
	}

	switch ($output_flag) {
		case RRDTOOL_OUTPUT_NULL:
			return; break;
		case RRDTOOL_OUTPUT_STDOUT:
			if (isset($fp)) {
				$line = "";
				while (!feof($fp)) {
					$line .= fgets($fp, 4096);
				}

				return $line;
			}

			break;
		case RRDTOOL_OUTPUT_STDERR:
			if (isset($fp)) {
				$output = fgets($fp, 1000000);

				if (substr($output, 1, 3) == "PNG") {
					return "OK";
				}

				if (substr($output, 0, 5) == "GIF87") {
					return "OK";
				}

				print $output;
			}

			break;
		case RRDTOOL_OUTPUT_GRAPH_DATA:
			if (isset($fp)) {
				return fpassthru($fp);
			}

			break;
	}
}
Beispiel #3
0
function rrdtool_execute($command_line, $log_to_stdout, $output_flag, $rrd_struc = array(), $syslog_facility = FACIL_POLLER) {
	require_once(CACTI_BASE_PATH . "/lib/sys/exec.php");

	if (!is_numeric($output_flag)) {
		$output_flag = RRDTOOL_OUTPUT_STDOUT;
	}

	/* WIN32: before sending this command off to rrdtool, get rid
	of all of the '\' characters. Unix does not care; win32 does.
	Also make sure to replace all of the fancy \'s at the end of the line,
	but make sure not to get rid of the "\n"'s that are supposed to be
	in there (text format) */
	$command_line = str_replace("\\\n", " ", $command_line);

	/* output information to the log file if appropriate */
	if (($syslog_facility == FACIL_POLLER) || ($syslog_facility == FACIL_CMDPHP)) {
		log_save("RRD: " . addslashes(read_config_option("path_rrdtool")) . " $command_line", SEV_DEBUG, $syslog_facility, "", 0, 0, $log_to_stdout);
	} else {
		log_save("RRD: " . addslashes(read_config_option("path_rrdtool")) . " $command_line", SEV_DEBUG, $syslog_facility);
	}

	/* if we want to see the error output from rrdtool; make sure to specify this */
	if (($output_flag == RRDTOOL_OUTPUT_STDERR) && (sizeof($rrd_struc) == 0)) {
		$command_line .= " 2>&1";
	}

	/* use popen to eliminate the zombie issue */
	if (CACTI_SERVER_OS == "unix") {
		/* an empty $rrd_struc array means no fp is available */
		if (sizeof($rrd_struc) == 0) {
			$fp = popen(read_config_option("path_rrdtool") . escape_command(" $command_line"), "r");
		}else{
			fwrite(rrd_get_fd($rrd_struc, RRDTOOL_PIPE_CHILD_READ), escape_command(" $command_line") . "\r\n");
			fflush(rrd_get_fd($rrd_struc, RRDTOOL_PIPE_CHILD_READ));
		}
	}elseif (CACTI_SERVER_OS == "win32") {
		/* an empty $rrd_struc array means no fp is available */
		if (sizeof($rrd_struc) == 0) {
			$fp = popen(read_config_option("path_rrdtool") . escape_command(" $command_line"), "rb");
		}else{
			fwrite(rrd_get_fd($rrd_struc, RRDTOOL_PIPE_CHILD_READ), escape_command(" $command_line") . "\r\n");
			fflush(rrd_get_fd($rrd_struc, RRDTOOL_PIPE_CHILD_READ));
		}
	}

	switch ($output_flag) {
		case RRDTOOL_OUTPUT_NULL:
			return; break;
		case RRDTOOL_OUTPUT_STDOUT:
			if (isset($fp)) {
				$line = "";
				while (!feof($fp)) {
					$line .= fgets($fp, 4096);
				}

				return $line;
			}

			break;
		case RRDTOOL_OUTPUT_STDERR:
			if (isset($fp)) {
				$output = fgets($fp, 1000000);

				if (substr($output, 1, 3) == "PNG") {
					return "OK";
				}

				if (substr($output, 0, 5) == "GIF87") {
					return "OK";
				}

				print $output;
			}

			break;
		case RRDTOOL_OUTPUT_GRAPH_DATA:
			if (isset($fp)) {
				return fpassthru($fp);
			}

			break;
	}
}