I have created a simple-ish plugin for tracking Postfix queues across multiple servers and displaying as a graph in Panic’s new Status Board app.
There is a lot of room for improvement, but it’s a functional start! If you see improvements that can be made, please let me know. You can see the basic result in the upper-right corner.
Step 1:
On each server that you want to push postqueue data from, add the following script to a cron job:
echo “msgcount=`postqueue -p | grep Requests` &servername=SERVERNAME” | curl -s -d @- http://YOURDOMAIN/PATH/pqload.php
At the location on the ‘home’ server that is pointed to in the http path above, create the following script:
pqload.php:
<?php
if(isset( $_POST[‘msgcount’], $_POST[‘servername’] )){
$serverdat=”./” . $_POST[‘servername’] . “.dat”;
unlink($serverdat);
$outfile=fopen($serverdat, “w”);
fwrite($outfile,$_POST[‘msgcount’]);
fclose($outfile);
} else {
$serverdat=”BAD.dat”;
unlink($serverdat);
$outfile=fopen($serverdat, “w”);
fwrite($outfile,”FAILED”);
fclose($outfile);
}
?>
This script will create the files needed by the LAST script, which generates the files for the following script:
pqout.php:
<?
$MYDIR=”/srv/www/main/scripts”;
$OUTPATH=”/srv/www/main/scripts/postq.csv”;
if(!($dir=opendir($MYDIR))) {
die(“Cannot open $MYDIR”);
}
$dataCount=0;
while ($file=readdir($dir)) {
if ($file !=”.” && $file !=”..” && strpos($file,”.dat”)) {
$inFile=fopen($file,”r”);
$inData=fread($inFile, 200);
fclose($inFile);
$breakData=explode(” “, $inData);
$serverData[$dataCount]=substr($file,0,strlen($file)-4);
if ($breakData[4]) {
$data[$dataCount]=$breakData[4];
} else {
$data[$dataCount]=”0″;
}
$dataCount++;
}
}
unlink($OUTPATH);
$outfile=fopen($OUTPATH,”w”);
fwrite($outfile,”POSTFIX,QUEUES\n”);
for ($i=0;$i<$dataCount;$i++) {
fwrite($outfile,”$serverData[$i],$data[$i]\n”);
}
fclose($outfile);
?>
Now we’ve got to get it into cron to work.
I created two cron jobs, samples below. The first job must run on ALL Postqueue servers
*/3 * * * * root /usr/local/bin/sendpqdata
The below job only needs to run on the ‘server’ server, where the graph file will be generated:
*/5 * * * * root /srv/www/main/scripts/runpq
(runpq simply changes directory and then executes the script)
cd /srv/www/main/scripts
php /srv/www/main/scripts/pqout.php
Once you have all that working, you will need to point a new graph object in Status Board to the appropriate path, for example http://www.SOMETIME.com/scripts/postq.csv