【问题标题】:Why doesn't rrdtool generate any PNG output in my Perl CGI program?为什么 rrdtool 不在我的 Perl CGI 程序中生成任何 PNG 输出?
【发布时间】:2011-04-23 02:47:58
【问题描述】:

我正在尝试使用 Perl 从 RRD 工具输出图像。我在下面发布了 CGI 脚本的相关部分:

sub graph
{
my $rrd_path = $co->param('rrd_path');
my $RRD_DIR = "../data/";

#generate a PNG from the RRD
my $png_filename = "-"; # a '-' as the filename send the PNG to stdout
my $rrd = "$RRD_DIR/$rrd_path";

my $png = `rrdtool graph $png_filename -a PNG -r -l 0 --base 1024 --start -151200 -- vertical-label 'bits per second' --width 500 --height 200 DEF:bytesInPerSec=$rrd:bytesInPerSec:AVERAGE DEF:bytesOutPerSec=$rrd:bytesOutPerSec:AVERAGE CDEF:sbytesInPerSec=bytesInPerSec,8,* CDEF:sbytesOutPerSec=bytesOutPerSec,8,* AREA:sbytesInPerSec#00cf00:AvgIn LINE1:sbytesOutPerSec#002a97:AvgOut VRULE:1246428000#ff0000:`;

#print the image header 
use bytes;
print $co->header(-type=>"image/png",-Content_length=>length($png));

binmode STDOUT;
print $png;
}#end graph

这在命令行上运行良好 (perl graph.cgi > test.png) - 当然,在我的 Ubuntu 10.04 开发机器上也可以注释掉标题。但是,当我移动到 Centos 5 生产服务器时,它没有,并且浏览器接收到的内容长度为 0:

Ubuntu 10.04/Apache:

Request URL:http://noc-student.nmsu.edu/grasshopper/web/graph.cgi
Request Method:GET
Status Code:200 OK
Request Headers
Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Cache-Control:max-age=0
User-Agent:Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko)     Chrome/7.0.517.36 Safari/534.7
Response Headers
Connection:Keep-Alive
Content-Type:image/png
Content-length:12319
Date:Fri, 08 Oct 2010 21:40:05 GMT
Keep-Alive:timeout=15, max=97
Server:Apache/2.2.14 (Ubuntu)

并且来自 Centos 5/Apache 服务器:

Request URL:http://grasshopper-new.nmsu.edu/grasshopper/branches/michael_dev/web/graph.cgi
Request Method:GET
Status Code:200 OK
Request Headers
Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Cache-Control:max-age=0
User-Agent:Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.36 Safari/534.7
Response Headers
Connection:close
Content-Type:image/png
Content-length:0
Date:Fri, 08 Oct 2010 21:40:32 GMT
Server:Apache/2.2.3 (CentOS)

use bytes 和内容长度的手动设置在那里尝试解决问题,但没有它们也是一样的。与在 STDOUT 上设置 binmode 相同。该脚本在两台机器上的命令行都可以正常工作。

【问题讨论】:

  • 在 Centos 机器上运行 webserver 的用户是否有权限运行 rrdtool?
  • 是的。以用户 apache 从命令行运行脚本会创建一个有效的图像文件。

标签: perl image apache cgi


【解决方案1】:

见我的How can I troubleshoot my Perl CGI program。通常,在命令行和从 Web 服务器运行程序之间的区别在于不同的环境。在这种情况下,我希望 rddtool 不在路径中,或者网络服务器用户无法运行它。

反引号仅捕获标准输出。 Web 服务器错误日志中可能有一些标准错误输出。

【讨论】:

  • rrdtool 不在 Apache 作为服务器运行时使用的路径中,但如果我 sudo su - apache 则它在路径中
【解决方案2】:

您确定您的网络用户可以访问您的数据吗?尝试让 CGI 将 png 写入文件系统,以便确保它正确生成。如果是,则问题出在传输中(标头、编码等)。如果不是,则与 Web 服务器无关,可能与权限有关。

【讨论】:

  • 原来是路径问题,但写入文件系统有助于解决问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2018-05-13
  • 2012-08-04
  • 2011-02-14
  • 2011-01-31
  • 1970-01-01
  • 2017-01-03
相关资源
最近更新 更多