Home  >  Article  >  Backend Development  >  Using PHP's Popen to realize dynamic output of RRDTOOL drawing_PHP tutorial

Using PHP's Popen to realize dynamic output of RRDTOOL drawing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:44:551127browse

I took a closer look at how RRDTOOL draws pictures. The syntax is indeed cumbersome, but it is not too difficult. When executing from the command line, the output needs to be a file every time. We hope to make an application that can dynamically generate charts. After looking at the implementation method of Cacti, the core part is to use the popen function to put the output of the command into the pipeline, and then read all the data in a loop. After completion, Output to the client in the form of pictures. In this way, users can see dynamic pictures without the need for physical storage. Below is the code snippet I intercepted for your reference.

error_reporting(E_ALL);

//Graphing command, pay attention to the connector
added after graph $command = '/opt/rrdtool/bin/rrdtool graph - --start=-86400 --end=-300 --title=Test --height=400 --width=800 DEF:value1="/home/echo /workspace/misc/tianjin_dpool_web_21_traffic_in_22.rrd":traffic_in:AVERAGE AREA:value1#ff0000 2>&1';

//Test pipe character
session_write_close();

//Execute commands through pipelines and receive error output
$handle = popen("$command 2>&1", 'r');
$read = '';
while (!feof($handle)) {
           $read .= fgets($handle, 4096);
}
pclose($handle);
echo $read;

// Set the proper headers to allow caching
$this->request->headers['Content-Type'] = File::mime_by_ext('png');
$this->request->headers['Content-Length'] = strlen($read);
$this->request->headers['Last-Modified'] = date('r', time());

from:lonely blog

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478707.htmlTechArticleI took a closer look at how RRDTOOL draws pictures. The syntax is indeed complicated, but it is not too difficult. When executing from the command line, the output needs to be a file every time. We hope to make an application...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn