Rewrite mailgraph with fastcgi

View: New views
1 Messages — Rating Filter:   Alert me  

Rewrite mailgraph with fastcgi

by hizel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, i rewrite mailgraph 1.14 with support fastcgi protocol

use FCGI и FCGI::ProcManager modules

it's configured for
1. /graph/ uri
2. 127.0.0.1:10000 socket for backend(i'm using nginx)
3. /var/run/mailgraph/mailgraph-fcgi.pid pid for rc script (i'm using
freebsd rc script)

nginx config:

        location /graph {
            fastcgi_pass 127.0.0.1:10000;
            include      fastcgi.conf;
        }

freebsd rc script:

#!/bin/sh
#
#
# PROVIDE: mailgraphfcgi
# REQUIRE: DAEMON
#
#
# mailgraphfcgi_enable="YES"
#

. /etc/rc.subr

name="mailgraphfcgi"
rcvar=`set_rcvar`

command=/usr/local/www/cgi-bin/mailgraph.fcgi
procname=perl-fcgi-pm
#command_interpreter=/usr/bin/perl
pidfile=/var/run/mailgraph/mailgraph-fcgi.pid
stop_postcmd=stop_postcmd

stop_postcmd()
{
    rm -f $pidfile
}

load_rc_config $name

: ${mailgraphfcgi_enable="NO"}

run_rc_command "$1"

examples look at http://mail.vyborg.ru/graph



Here is diff:
--- mailgraph.cgi       2008-09-16 14:24:57.000000000 +0400
+++ mailgraph.fcgi      2009-08-28 12:49:55.000000000 +0400
@@ -1,17 +1,23 @@
 #!/usr/bin/perl -w
-
 # mailgraph -- postfix mail traffic statistics
 # copyright (c) 2000-2007 ETH Zurich
 # copyright (c) 2000-2007 David Schweikert <david@...>
 # released under the GNU General Public License
 
+use strict;
+use warnings;
+
+use FCGI;
+use POSIX;
+use FCGI::ProcManager qw(pm_manage pm_pre_dispatch pm_post_dispatch
pm_write_pid_file); +
 use RRDs;
 use POSIX qw(uname);
 
 my $VERSION = "1.14";
 
 my $host = (POSIX::uname())[1];
-my $scriptname = 'mailgraph.cgi';
+my $scriptname = '/graph/';
 my $xpoints = 540;
 my $points_per_sample = 3;
 my $ypoints = 160;
@@ -19,6 +25,16 @@
 my $rrd = '/var/db/mailgraph/mailgraph.rrd'; # path to where the RRD
database is my $rrd_virus = '/var/db/mailgraph/mailgraph_virus.rrd'; #
path to where the Virus RRD database is my $tmp_dir = '/tmp/mailgraph';
# temporary directory where to store the images +my $pid_file =
'/var/run/mailgraph/mailgraph-fcgi.pid'; +my $bind_host =
'127.0.0.1:10000'; +
+
+sub reopen_std {    
+    open(STDIN,  "+>/dev/null") or die "Can't open STDIN: $!";
+    open(STDOUT, "+>&STDIN") or die "Can't open STDOUT: $!";
+    open(STDERR, "+>&STDIN") or die "Can't open STDERR: $!";
+};
+
 my @graphs = (
        { title => 'Last Day',   seconds => 3600*24,        },
@@ -163,7 +179,7 @@
 <title>Mail statistics for $host</title>
 <meta http-equiv="Refresh" content="300" />
 <meta http-equiv="Pragma" content="no-cache" />
-<link rel="stylesheet" href="/mailgraph/mailgraph.css"
type="text/css" /> +<link rel="stylesheet" href="mailgraph.css"
type="text/css" /> </head>
 <body>
 HEADER
@@ -241,4 +257,44 @@
        }
 }
 
-main;
+
+
+
+
+sub fork_proc {
+    my $pid;
+    
+    FORK: {
+        if (defined($pid = fork)) {
+            return $pid;
+        }
+        elsif ($! =~ /No more process/) {
+            sleep 5;
+            redo FORK;
+        }
+        else {
+            die "Can't fork: $!";
+        };
+    };
+};
+
+
+fork_proc() && exit 0;
+POSIX::setsid() or die "Can't set sid: $!";
+chdir '/' or die "Can't chdir: $!";
+POSIX::setuid(80) or die "Can't set uid: $!";
+my $socket = FCGI::OpenSocket($bind_host, 5);
+my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV,
$socket, FCGI::FAIL_ACCEPT_ON_INTR); +
+pm_write_pid_file($pid_file);
+pm_manage(n_processes => 2);
+reopen_std();
+
+my $count = 1;
+
+while($request->Accept() >= 0) {
+       pm_pre_dispatch();
+    main();    
+    pm_post_dispatch();
+};
+

--
Unsubscribe mailto:mailgraph-request@...?subject=unsubscribe
Help        mailto:mailgraph-request@...?subject=help
Archive     http://lists.ee.ethz.ch/mailgraph
WebAdmin    http://lists.ee.ethz.ch/lsg2.cgi