PDA

View Full Version : Mailgraph problems


JuStInEr
28th July 2006, 21:05
First of all, thanks for the great how-to's!:)
-----

I have a working postfix configuration on ubuntu. And now I'm trying to install mailgraph, but when I surf to he .cgi file i don't see graphs. What I see is the text of the .cgi file:

#!/usr/bin/perl -w

# mailgraph -- a postfix statistics rrdtool frontend
# copyright (c) 2000-2005 David Schweikert <dws@ee.ethz.ch>
# released under the GNU General Public License

use RRDs;
use POSIX qw(uname);

my $VERSION = "1.12";

my $host = (POSIX::uname())[1];
my $scriptname = 'mailgraph.cgi';
my $xpoints = 540;
my $points_per_sample = 3;
my $ypoints = 160;
my $ypoints_err = 96;
my $rrd = '/var/lib/mailgraph/mailgraph.rrd'; # path to where the RRD database is
my $rrd_virus = '/var/lib/mailgraph/mailgraph_virus.rrd'; # path to where the Virus RRD database is
my $tmp_dir = '/var/lib/mailgraph'; # temporary directory where to store the images

my @graphs = (
{ title => 'Day Graphs', seconds => 3600*24, },
{ title => 'Week Graphs', seconds => 3600*24*7, },
{ title => 'Month Graphs', seconds => 3600*24*31, },
{ title => 'Year Graphs', seconds => 3600*24*365, },
);

my %color = (
sent => '000099', # rrggbb in hex
received => '009900',
rejected => 'AA0000',
bounced => '000000',
virus => 'DDBB00',
spam => '999999',
);

sub rrd_graph(@)
{
my ($range, $file, $ypoints, @rrdargs) = @_;
my $step = $range*$points_per_sample/$xpoints;
# choose carefully the end otherwise rrd will maybe pick the wrong RRA:
my $end = time; $end -= $end % $step;
my $date = localtime(time);
$date =~ s|:|\\:|g unless $RRDs::VERSION < 1.199908;

my ($graphret,$xs,$ys) = RRDs::graph($file,
'--imgformat', 'PNG',
'--width', $xpoints,
'--height', $ypoints,
'--start', "-$range",
'--end', $end,
'--vertical-label', 'msgs/min',
'--lower-limit', 0,
'--units-exponent', 0, # don't show milli-messages/s
'--lazy',
'--color', 'SHADEA#ffffff',
'--color', 'SHADEB#ffffff',
'--color', 'BACK#ffffff',

$RRDs::VERSION < 1.2002 ? () : (
'--slope-mode'
),

@rrdargs,

'COMMENT:['.$date.']\r',
);

my $ERR=RRDs::error;
die "ERROR: $ERR\n" if $ERR;
}

sub graph($$)
{
my ($range, $file) = @_;
my $step = $range*$points_per_sample/$xpoints;
rrd_graph($range, $file, $ypoints,
"DEF:sent=$rrd:sent:AVERAGE",
"DEF:msent=$rrd:sent:MAX",
"CDEF:rsent=sent,60,*",
"CDEF:rmsent=msent,60,*",
"CDEF:dsent=sent,UN,0,sent,IF,$step,*",
"CDEF:ssent=PREV,UN,dsent,PREV,IF,dsent,+",
"AREA:rsent#$color{sent}:Sent ",
'GPRINT:ssent:MAX:total\: %8.0lf msgs',
'GPRINT:rsent:AVERAGE:avg\: %5.2lf msgs/min',
'GPRINT:rmsent:MAX:max\: %4.0lf msgs/min\l',

"DEF:recv=$rrd:recv:AVERAGE",
"DEF:mrecv=$rrd:recv:MAX",
"CDEF:rrecv=recv,60,*",
"CDEF:rmrecv=mrecv,60,*",
"CDEF:drecv=recv,UN,0,recv,IF,$step,*",
"CDEF:srecv=PREV,UN,drecv,PREV,IF,drecv,+",
"LINE2:rrecv#$color{received}:Received",
'GPRINT:srecv:MAX:total\: %8.0lf msgs',
'GPRINT:rrecv:AVERAGE:avg\: %5.2lf msgs/min',
'GPRINT:rmrecv:MAX:max\: %4.0lf msgs/min\l',
);
}

sub graph_err($$)
{
my ($range, $file) = @_;
my $step = $range*$points_per_sample/$xpoints;
rrd_graph($range, $file, $ypoints_err,
"DEF:rejected=$rrd:rejected:AVERAGE",
"DEF:mrejected=$rrd:rejected:MAX",
"CDEF:rrejected=rejected,60,*",
"CDEF:drejected=rejected,UN,0,rejected,IF,$step,*",
"CDEF:srejected=PREV,UN,drejected,PREV,IF,drejected ,+",
"CDEF:rmrejected=mrejected,60,*",
"LINE2:rrejected#$color{rejected}:Rejected",
'GPRINT:srejected:MAX:total\: %8.0lf msgs',
'GPRINT:rrejected:AVERAGE:avg\: %5.2lf msgs/min',
'GPRINT:rmrejected:MAX:max\: %4.0lf msgs/min\l',

"DEF:bounced=$rrd:bounced:AVERAGE",
"DEF:mbounced=$rrd:bounced:MAX",
"CDEF:rbounced=bounced,60,*",
"CDEF:dbounced=bounced,UN,0,bounced,IF,$step,*",
"CDEF:sbounced=PREV,UN,dbounced,PREV,IF,dbounced,+",
"CDEF:rmbounced=mbounced,60,*",
"AREA:rbounced#$color{bounced}:Bounced ",
'GPRINT:sbounced:MAX:total\: %8.0lf msgs',
'GPRINT:rbounced:AVERAGE:avg\: %5.2lf msgs/min',
'GPRINT:rmbounced:MAX:max\: %4.0lf msgs/min\l',

"DEF:virus=$rrd_virus:virus:AVERAGE",
"DEF:mvirus=$rrd_virus:virus:MAX",
"CDEF:rvirus=virus,60,*",
"CDEF:dvirus=virus,UN,0,virus,IF,$step,*",
"CDEF:svirus=PREV,UN,dvirus,PREV,IF,dvirus,+",
"CDEF:rmvirus=mvirus,60,*",
"STACK:rvirus#$color{virus}:Viruses ",
'GPRINT:svirus:MAX:total\: %8.0lf msgs',
'GPRINT:rvirus:AVERAGE:avg\: %5.2lf msgs/min',
'GPRINT:rmvirus:MAX:max\: %4.0lf msgs/min\l',

"DEF:spam=$rrd_virus:spam:AVERAGE",
"DEF:mspam=$rrd_virus:spam:MAX",
"CDEF:rspam=spam,60,*",
"CDEF:dspam=spam,UN,0,spam,IF,$step,*",
"CDEF:sspam=PREV,UN,dspam,PREV,IF,dspam,+",
"CDEF:rmspam=mspam,60,*",
"STACK:rspam#$color{spam}:Spam ",
'GPRINT:sspam:MAX:total\: %8.0lf msgs',
'GPRINT:rspam:AVERAGE:avg\: %5.2lf msgs/min',
'GPRINT:rmspam:MAX:max\: %4.0lf msgs/min\l',
);
}

sub print_html()
{
print "Content-Type: text/html\n\n";

print <<HEADER;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>Mail Statistics for $host</TITLE>
<META HTTP-EQUIV="Refresh" CONTENT="300">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
HEADER

print "<H1>Mail Statistics for $host</H1>\n";
for my $n (0..$#graphs) {
print '<div style="background: #dddddd; width: 632px">';
print "<H2>$graphs[$n]{title}</H2>\n";
print "</div>\n";
print "<P><IMG BORDER=\"0\" SRC=\"$scriptname?${n}-n\" ALT=\"mailgraph\">\n";
print "<P><IMG BORDER=\"0\" SRC=\"$scriptname?${n}-e\" ALT=\"mailgraph\">\n";
}

print <<FOOTER;
<hr width="630" align="left" size="1" noshade>
<table border="0" width="630" cellpadding="0" cellspacing="0" background="#dddddd"><tr><td align="left">
<A href="http://people.ee.ethz.ch/~dws/software/mailgraph">Mailgraph</A> $VERSION
by <A href="http://people.ee.ethz.ch/~dws/">David Schweikert</A></td>
<td ALIGN="right">
<a HREF="http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/"><img border="0" src="http://people.ethz.ch/~oetiker/webtools/rrdtool/.pics/rrdtool.gif" alt="" width="120" height="34"></a>
</td></tr></table>
</BODY></HTML>
FOOTER
}

sub send_image($)
{
my ($file)= @_;

-r $file or do {
print "Content-type: text/plain\n\nERROR: can't find $file\n";
exit 1;
};

print "Content-type: image/png\n";
print "Content-length: ".((stat($file))[7])."\n";
print "\n";
open(IMG, $file) or die;
my $data;
print $data while read(IMG, $data, 16384)>0;
}

sub main()
{
my $uri = $ENV{REQUEST_URI} || '';
$uri =~ s/\/[^\/]+$//;
$uri =~ s/\//,/g;
$uri =~ s/(\~|\%7E)/tilde,/g;
mkdir $tmp_dir, 0777 unless -d $tmp_dir;
mkdir "$tmp_dir/$uri", 0777 unless -d "$tmp_dir/$uri";

my $img = $ENV{QUERY_STRING};
if(defined $img and $img =~ /\S/) {
if($img =~ /^(\d+)-n$/) {
my $file = "$tmp_dir/$uri/mailgraph_$1.png";
graph($graphs[$1]{seconds}, $file);
send_image($file);
}
elsif($img =~ /^(\d+)-e$/) {
my $file = "$tmp_dir/$uri/mailgraph_$1_err.png";
graph_err($graphs[$1]{seconds}, $file);
send_image($file);
}
else {
die "ERROR: invalid argument\n";
}
}
else {
print_html;
}
}

main;



Does someone know a solution?

falko
29th July 2006, 14:02
Did you configure Perl scripts for your Apache vhosts? Did you put the file into the cgi-bin directory?
Have a look here: http://httpd.apache.org/docs/2.0/howto/cgi.html

JuStInEr
29th July 2006, 15:17
Did you configure Perl scripts for your Apache vhosts? Did you put the file into the cgi-bin directory?
Have a look here: http://httpd.apache.org/docs/2.0/howto/cgi.html
I did put the files in the cgi-bin directory, so that should not be the problem.
And I don't run the scripts from a virtual host...

falko
30th July 2006, 17:26
I did put the files in the cgi-bin directory
You should then. And make the CGI scripts executable.

JuStInEr
1st August 2006, 02:38
I think that I know what may be the problem: Perl. But I don't know how to solve it.

I downloaded your test files (hello_world.pl), and placed it in my cgi-bin directory. When I go to the file, my browser asks if I want to download it.

This is my /etc/apache2/sites-available/default:
NameVirtualHost *
<virtualhost *>
ServerAdmin justin@vwees.net

DocumentRoot /var/www/
<directory />
Options FollowSymLinks
AllowOverride None
</directory>

<directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</directory>

ErrorLog /var/log/apache2/error.log

LogLevel warn

CustomLog /var/log/apache2/access.log combined
ServerSignature On

</virtualhost>

NameVirtualHost *:443
<virtualhost *:443>
ServerAdmin justin@vwees.net
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/apache.pem

DocumentRoot /var/www/
<directory />
Options FollowSymLinks
AllowOverride None
</directory>

<directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<directory "/usr/lib/cgi-bin">
AllowOverride all
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</directory>

ErrorLog /var/log/apache2/error.log

LogLevel warn

CustomLog /var/log/apache2/access.log combined
ServerSignature On

</virtualhost>

falko
2nd August 2006, 13:46
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</directory>You must put your Perl scripts into /usr/lib/cgi-bin then.