#!/usr/bin/perl -w

#A. Static Configurations

#1. Check Quarantine Status - Local / Remote
my $localquarantine = 0;
#2. Check SMTP Virus Filtering Status
my $smtpvirus = 0;
#3. Check SMTP Message Filtering Status
my $smtpmessage = 0;
#4. Check SMTP Bayesian Message Filtering Status
my $smtpbayesian = 0;
#5. Check URL Categorization Status
my $urlcategory = 0;
#6. Check SMTP Proxy Status
my $smtpproxy = 0;
#7. Check POP3 Proxy Status
my $pop3proxy = 0;

#8. Check System Logging Status - Local / Remote
my $syslog = 0;
#9. Check Hardware Type
my $hardwaretype = "";

#B. Dynamic Configurations

#1. Check URL Category Database Update
my $urlcatupdate = 0;
#2. Check KAV Anti-Virus Update
my $virusupdate = 0;

=Niranjan
#C. Current Status

#1. Check Current Disk Usage of Qmail QUEUE
my $queuediskusage = 0;
#2. Check Current Disk Usage of Quarantine Mails
my $quarantinediskusage = 0;
#3. Check Current Disk Usage of Qmail Bounce Mails
my $bouncediskusage = 0;
#4. Check Current Free Disk Space
my $freediskspace = 0;

#5. Check Current RAM Utilization
my $ramusage = 0;
#6. Check SWAP Utilization
my $swapusage  = 0;

my $status = 0;
open (REMOTE, "/var/qmail/control/remotequarantine");
while(<REMOTE>)
{
	if (/enable/)
        {
		$status++;
        }
}
close (REMOTE);
if ($status == 3)
{
	print "Local Quarantine is disabled altogether\n";
	$localquarantine = 0;
}
else
{
	print "Local Quarantine is enabled for one of the methods\n";
	$localquarantine = 1;
}
=cut

open (PROCSTAT, "/etc/multiconf/processstatus");
while (<PROCSTAT>)
{
	if (/POP3VSCAN/)
	{
		@values = split (/=/, $_);
                $status = $values[1];
                chomp ($status);
                if ($status eq "ON")
                {
                        $pop3proxy = 1;
                }
	}
	if (/QMAIL/)
        {
                @values = split (/=/, $_);
                $status = $values[1];
                chomp ($status);
                if ($status eq "ON")
                {
                        $smtpproxy = 1;
                }
        }
	if (/SMTPVSCAN/)
        {
        	@values = split (/=/, $_);
                $status = $values[1];
                chomp ($status);
                if ($status eq "ON")
                {
                	$smtpvirus = 1;
                }
        }
	if (/SYSLOG_NG/)
        {
                @values = split (/=/, $_);
                $status = $values[1];
                chomp ($status);
                if ($status eq "ON")
                {
                        $syslog = 1;
                }
        }
	if (/SURF/)
        {
                @values = split (/=/, $_);
                $status = $values[1];
                chomp ($status);
                if ($status eq "ON")
                {
                        $urlcategory = 1;
                }
        }
	if (/^SPAMD/)
	{
		@values = split (/=/, $_);
                $status = $values[1];
                chomp ($status);
                if ($status eq "ON")
                {
                        $smtpmessage = 1;
                }
	}
}
close (PROCSTAT);

open (BAYESIAN, "/etc/mail/spamassassin/local.cf");
while (<BAYESIAN>)
{
	if (/use_bayes/)
        {
        	@values = split (/ /,$_);
                $status = $values[1];
                chomp ($status);
		if ($status eq "1")
		{
			$smtpbayesian = 1;
		}
        }
}
close (BAYESIAN);

#print "Local Quarantine = $localquarantine\n";
#print "SMTP Virus 	= $smtpvirus\n";
#print "SMTP Message	= $smtpmessage\n";
#print "SMTP Bayesian 	= $smtpbayesian\n";
#print "URL Category	= $urlcategory\n";
#print "System Logging	= $syslog\n";

=Niranjan
$return = `du -sb /var/qmail/queue`;
@values = split (' ', $return);
chomp ($values[0]);
$queuediskusage = $values[0];
print "Current Disk Usage of Qmail QUEUE = $queuediskusage Bytes\n";

$return = `du -sb /var/spool/qmailscan`;
@values = split (' ', $return);
chomp ($values[0]);
$quarantinediskusage = $values[0];
print "Current Disk Usage of Quarantine Mails = $quarantinediskusage Bytes\n";

$return = `du -sb /var/qmail/queue/bounce`;
@values = split (' ', $return);
chomp ($values[0]);
$bouncediskusage = $values[0];
print "Current Disk Usage of Qmail Bounce Mails = $bouncediskusage Bytes\n";

#$count = `ls /usr/local/apache/htdocs/mrtg/*.png | wc -l`;
#chomp ($count);
#print "Number of PNG files in /usr/local/apache/htdocs/mrtg/ directory = $count\n";
#if ($count != 0)
#{
#        system ("rm -rf /usr/local/apache/htdocs/mrtg/*.png");
#}

=cut

$OURCPU = `cat /proc/cpuinfo | grep "cpu family" | cut -d: -f2`;
chomp ($OURCPU);
$OURVENDOR = `cat /proc/cpuinfo | grep "vendor_id" | cut -d: -f2`;
chomp ($OURVENDOR);
$OURMODEL = `cat /proc/cpuinfo | grep "model name" | cut -d: -f2`;
chomp ($OURMODEL);
if ($OURVENDOR eq " GenuineIntel")
{
	#if ($OURMODEL == "Celeron (Coppermine)") 
	#{
	#	print "Processor Type = Celeron (RF660 VPN)\n";
	#}
	
	# Now any Intel 686 processor we take it as P3
        if ($OURCPU == 6)
	{
        	#print "Processor Type = Pentium III\n";
		$hardwaretype = "P3";	
        }        

	#Check if it is a P4 processor (RF760 VPN)
        elsif ($OURCPU == 15)
	{
		#print "Processor Type = Pentium IV\n";
		$hardwaretype = "P4";
	}
}
# If it is a Cyrix processor then take the cyrix image
elsif ($OURVENDOR eq " CentaurHauls") 
{
	#print "Processor Type = Cyrix III\n ";
	$hardwaretype = "CYRIX";
}        
else
{
	#print "Processor Type = 486 - Default - (Most probably Geode)\n";
	$hardwaretype = "GEODE";
}
#print "Hardware Type = $hardwaretype\n";
=Niranjan
$TOTALRAM  = `cat /proc/meminfo | grep -m 1 "Mem" | tr -s " " | cut -d: -f2 | cut -d " " -f2`;
$TOTALSWAP = `cat /proc/meminfo | grep -m 1 "Swap" | tr -s " " | cut -d: -f2 | cut -d " " -f2`;
chomp ($TOTALRAM);
chomp ($TOTALSWAP);
print "Total RAM  = $TOTALRAM\n";
print "Total SWAP = $TOTALSWAP\n";

$USEDRAM  = `cat /proc/meminfo | grep -m 1 "Mem" | tr -s " " | cut -d: -f2 | cut -d " " -f3`;
$USEDSWAP = `cat /proc/meminfo | grep -m 1 "Swap" | tr -s " " | cut -d: -f2 | cut -d " " -f3`;
chomp ($USEDRAM);
chomp ($USEDSWAP);
print "Used RAM   = $USEDRAM\n";
print "Used SWAP  = $USEDSWAP\n";

$FREERAM  = `cat /proc/meminfo | grep -m 1 "Mem" | tr -s " " | cut -d: -f2 | cut -d " " -f4`;
$FREESWAP = `cat /proc/meminfo | grep -m 1 "Swap" | tr -s " " | cut -d: -f2 | cut -d " " -f4`;
chomp ($FREERAM);
chomp ($FREESWAP);
print "Free RAM   = $FREERAM\n";
print "Free SWAP  = $FREESWAP\n";

$TOTALRAMKB  = `cat /proc/meminfo | grep -m 1 "MemTotal" | tr -s " " | cut -d: -f2 | cut -d " " -f2`;
$TOTALSWAPKB = `cat /proc/meminfo | grep -m 1 "SwapTotal" | tr -s " " | cut -d: -f2 | cut -d " " -f2`;
chomp ($TOTALRAMKB);
chomp ($TOTALSWAPKB);
print "Total RAM in KB  = $TOTALRAMKB\n";
print "Total SWAP in KB = $TOTALSWAPKB\n";

$RAMFREEKB  = `cat /proc/meminfo | grep -m 1 "MemFree" | tr -s " " | cut -d: -f2 | cut -d " " -f2`;
$SWAPFREEKB = `cat /proc/meminfo | grep -m 1 "SwapFree" | tr -s " " | cut -d: -f2 | cut -d " " -f2`;
chomp ($RAMFREEKB);
chomp ($SWAPFREEKB);
print "Free RAM in KB  = $RAMFREEKB\n";
print "Free SWAP in KB = $SWAPFREEKB\n";
=cut
if ($smtpproxy == 1)
{
	$urlcatupdate = `pidof updater`;
	chomp ($urlcatupdate);

	$procstat = `ps ax --cols 250 | grep -v grep | grep "/usr/bin/perl -w /etc/multiconf/scripts/http updatefiles_up" | tr -s " " | cut -d" " -f1`;
	chomp ($procstat);

	$procstatus = `ps ax --cols 250 | grep -v grep | grep "/usr/bin/perl -w /etc/multiconf/scripts/http catupdate" | tr -s " " | cut -d" " -f1`;
	chomp ($procstatus);

	if (($urlcatupdate ne "") || ($procstat ne "") || ($procstatus ne "") || (-e "/etc/cron.fivemins/urlupdatefiles"))
        {
        	#print "URL Category Updation in is Progress\n";
        }
	else
	{
		
		$current = `cat /var/qmail/control/concurrencyincoming`;
		chomp ($current);
		my $multiplier = 1;

		if (($hardwaretype eq "P3") || ($hardwaretype eq "P4"))
		{
			$multiplier = 2;
		}

		if ($smtpmessage == 1)
		{
			$concurrencyincoming = $multiplier * 8;
		}
		else
		{
			$concurrencyincoming = $multiplier * 10;
		}

		if (($smtpvirus == 1) || ($smtpbayesian == 1))
		{
			$concurrencyincoming = $multiplier * 6;
		}
		if ($urlcategory == 1)
		{
			$concurrencyincoming = $multiplier * 5;
		}
		$currentremote = `cat /var/qmail/control/concurrencyremote`;
		chomp ($currentremote);
		$concurrencyremote = $multiplier * 20;
		if (($concurrencyincoming == $current) && ($concurrencyremote == $currentremote))
		{
			#print "SMTP: Same as current one\n";
		}
		else
		{
			#Lock Aide
	        	system ("/etc/multiconf/scripts/aide_dis create dont_run_aide");
	
			open (CONCRNCY, ">/var/qmail/control/concurrencyincoming");
			print CONCRNCY "$concurrencyincoming\n";
			close (CONCRNCY);
			open (RCONCRNCY, ">/var/qmail/control/concurrencyremote");
                        print RCONCRNCY "$concurrencyremote\n";
                        close (RCONCRNCY);

			system ("killall -v qmail-smtpd");
			system ("/etc/init.d/qmail restart");
			
			# Update CheckIntrusion
			system ("/etc/multiconf/scripts/CheckIntrusion update");

			#Unlock Aide
		        system ("/etc/multiconf/scripts/aide_dis delete dont_run_aide");
		}	
	}
}

if ($pop3proxy == 1)
{
	$current = `cat /var/qmail/control/maxchilds`;
        chomp ($current);
        my $multiplier = 1;
                                                                                                                             
        if (($hardwaretype eq "P3") || ($hardwaretype eq "P4"))
        {
        	$multiplier = 2;
        }
	$maxchilds = $multiplier * 10;
	
	if ($urlcategory == 1)
        {
        	$maxchilds = $multiplier * 5;
        }

	if ($maxchilds == $current)
        {
        	#print "POP3: Same as current one\n";
        }
        else
        {
        	open (MAXCHILDS, ">/var/qmail/control/maxchilds");
                print MAXCHILDS "$maxchilds\n";
                close (MAXCHILDS);
                system ("/etc/init.d/pop3vscan restart");
        }
}
