#!/usr/bin/perl
# 
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.zimbra.com/license
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is: Zimbra Collaboration Suite Server.
# 
# The Initial Developer of the Original Code is Zimbra, Inc.
# Portions created by Zimbra are Copyright (C) 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

use strict;

use lib "/opt/zimbra/zimbramon/lib";
use Zimbra::Util::Common;
use Zimbra::Mon::Logger;

my $platform=`/opt/zimbra/libexec/get_plat_tag.sh`;
chomp $platform;

my $MNTCMD;
my $DFCMD;
if ($platform eq "MACOSX") {
	$MNTCMD = "mount -t hfs";
	$DFCMD = "df -ml ";
} else {
	$MNTCMD = "mount -t ext3";
	$DFCMD = "df -ml ";
}

my $dt = `date "+%Y-%m-%d %H:%M:%S"`;
chomp $dt;

checkPid();
logQueue();
clearPid();

exit 0;

sub checkPid {
	if (-f "/opt/zimbra/log/zmqueuelog.pid") {
		my $P = `cat /opt/zimbra/log/zmqueuelog.pid`;
		chomp $P;
		if ($P ne "") {
			my $c = kill, 0, $P;
			if ($c) {exit 0;}
		}
	}
	`echo $$ > "/opt/zimbra/log/zmqueuelog.pid"`;
}

sub clearPid {
	unlink ("/opt/zimbra/log/zmqueuelog.pid");
}

sub logQueue {
	my @status = ();
	open STATUS, "/opt/zimbra/postfix/sbin/postqueue -p |" or die "Can't get status: $!";
	@status = <STATUS>;
	close STATUS;
	my $kb = 0;
	my $msgs = 0;
	my $s = $status[$#status];
	if ($s =~ /^--/) {
		my @foo = split (' ', $s);
		$kb = $foo[1];
		$msgs = $foo[4];
	}
	Zimbra::Mon::Logger::Log( "info", "$dt, QUEUE: $kb $msgs" );
}
