<?php
$fd=popen("/usr/local/rrdtool/bin/rrdtool - ","w");
if(!$fd){
print("cannot open");
return;
}
$start=1245000000;
$end=1245500000;
$t="--start $start --end $end";
$def="DEF:t1=\"sample.rrd\":item1:AVERAGE DEF:t2=sample.rrd:item2:AVERAGE";
$line="LINE1:t1#00ff00 LINE2:t2#330088";
$command="$t $def $line\r\n";$i=0;
while(1){
   if(fwrite($fd," graph - ".$command)==false){
fclose($fd);
$fd=popen("/usr/local/rrdtool/bin/rrdtool - ","w");
if($i>4){
print("cannot write");
return;
}else{
$i++;
}
continue;
   }else{
fflush($fd); break;
   }
}
if(isset($fd)){
print fpassthru($fd);
exit;
}
?>
图片出不来???
第二行改成$fd=popen("/usr/local/rrdtool/bin/rrdtool - >1.png","w");可以生成1.png图片

解决方案 »

  1.   

    弄错了,不是print fpassthru($fd);而是fpassthru($fd);
    而且print fpassthru($fd);输出0,
    不知道该怎么写,高手?????
      

  2.   


    while(1){
      if(fwrite($fd," graph - ".$command)==false){
    fclose($fd);
    $fd=popen("/usr/local/rrdtool/bin/rrdtool - ","w");
    if($i>4){
    print("cannot write");
    return;
    }else{
    $i++;

    没看明白这写的什么东西,支持一下,顶上去
      

  3.   

    调用RRDTOOL生成流量图片,一般都是rrdtool graph 1.png ...... 可以生成1.png,但我不想生成其他文件而是直接显示,参考了一下CACTI里的rrd.php,他的函数:
    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) && (!isset($rrd_struc["fd"]) || (sizeof($rrd_struc["fd"]) == 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 (!isset($rrd_struc["fd"]) || (sizeof($rrd_struc["fd"]) == 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 (!isset($rrd_struc["fd"]) || (sizeof($rrd_struc["fd"]) == 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;
    }
    }