#!/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;

$ENV{PATH} = "/bin:/usr/bin:/sbin:/usr/sbin:/opt/zimbra/bin";

# Exit if software-only node.
exit(0) unless (-f '/opt/zimbra/conf/localconfig.xml');

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

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

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

my $hostname = `/opt/zimbra/bin/zmhostname`;
chomp $hostname;

#logStatus();

logDiskUsage();

exit 0;

sub logStatus {
	my @status = ();
	open STATUS, "/opt/zimbra/bin/zmcontrol status |" or die "Can't get status: $!";
	@status = <STATUS>;
	close STATUS;
	foreach my $s (@status) {
		chomp $s;
		if ($s =~ /^Host (.*)/) {
			$hostname = $1;
			next;
		}
		my ($service, $stat) = split(/\s+/, $s);
		Zimbra::Mon::Logger::Log( "info", "$dt, STATUS: ${hostname}: $service: $stat" );
	}
}

sub logDiskUsage {
	my @mounts = ();
	open MOUNTS, "$MNTCMD |" or die "Can't open $MNTCMD: $!";
	@mounts = <MOUNTS>;
	close MOUNTS;
	foreach my $m (@mounts) {
		chomp $m;
		my ($dev,undef,$mp) = split(/\s+/,$m);
		open DF, "$DFCMD $dev | tail -1 |" or die "Can't open $DFCMD: $!";
		my @df = <DF>;
		close DF;
		my (undef, $total, undef, $avail) = split(/\s+/, $df[0]);
		Zimbra::Mon::Logger::Log( "info", "$dt, DISK: ${hostname}: dev: $dev, mp: $mp, tot: $total, avail: $avail" );
	}
}
