#!/usr/bin/perl -Tw
#                  Copyright (C) 2005 Zmanda Incorporated.
#                            All Rights Reserved.
#
#  The software you have just accessed, its contents, output and underlying
#  programming code are the proprietary and confidential information of Zmanda
#  Incorporated.  Only specially authorized employees, agents or licensees of
#  Zmanda may access and use this software.  If you have not been given
#  specific written permission by Zmanda, any attempt to access, use, decode,
#  modify or otherwise tamper with this software will subject you to civil
#  liability and/or criminal prosecution to the fullest extent of the law.



use strict;
use Getopt::Long;

my $confdir="/etc/amanda";
my $tmpdir="/var/log/amanda";
my $prefix="/usr";
my $localstatedir="/var";
my $tar="tar";
my $taropt="czvf";
my $date=`date +'%Y%m%d%H%M%S'`;
chomp($date);
my $logfile="";		
my $tempfile="./system-info-$date";
my $tempfile1="./var-log-messages-$date";
my $sep="="x40;
my $algn=""x4;
$sep="\n".$sep."\n";
my ($config,$nosysinfo,$help,$notar,$novarlogmessages,$myfl)="";
my ($network_id,$network_sessionID,$registration_date)="";


GetOptions  ("no-system-info!"=>\$nosysinfo,
            "no-tar!"=>\$notar,
	    "no-var-log-messages!"=>\$novarlogmessages,
            "help!"=>\$help
);


if($help){
	print "\nUsage :\n $0 <options>\n";
	print "\t\t --no-tar\n";
	print "\t\t --no-var-log-messages\n";
	print "\t\t --help\n";
	exit;
}


&init();
sub init(){
	open (SYSTMP,">$tempfile") or die("Could not open log file!");	
	open (SYSTMP1,">$tempfile1") or die("Could not open log file!") if (! $novarlogmessages);
	&startlog;
	&close;
}

sub startlog(){
	&createlogfilename();
	&get_system_info() if(! $nosysinfo); 
	&get_var_log_messages() if(! $novarlogmessages); 
	&get_amanda_info(); 
	if(! $notar){
		if(! $config){
			&execute_tar(); 
		}
	}

}

sub createlogfilename(){
		$logfile="client-logs-$date.tgz";
}
sub getamandaconfig(){
      my @configlist;
      my @filelist = <$confdir/*/amanda.conf>;
      foreach (@filelist){
	  if (/\/etc\/amanda\/([-\@\w.]+)\/amanda.conf/) {
		push(@configlist,$1);
	  }
      }
      return @configlist;	
}



sub get_system_info(){
	# get system info
	my $getdistro="Distrubution and OS information:\n".`cat /etc/*release`;
	my $kernelversion="Kernel Information:\n".`uname -a`;
	my $tarversion="Tar Version :\n".`tar --version`;
	my $netstat_info=system("which netstat");
	my $iptables_info=system("which iptables");
	my $sestatus_info=system("which sestatus");

        if ($netstat_info ne "0"){
                $netstat_info="\nnetstat not found\n";
        } else {
                $netstat_info="Getting System netstat Info:\n". `netstat -a | grep amanda`;
        }

	if ($iptables_info ne "0"){
                $iptables_info="\niptables not found\n";
        } else {
                $iptables_info="Getting System iptables Info:\n". `iptables -L`;
        }

	if ($sestatus_info ne "0"){
                $sestatus_info="\nsestatus not found\n";
        } else {
                $sestatus_info="Getting System sestatus Info:\n". `sestatus`;
        }


	
	# getting space information
	my @spacearr=("$confdir/"," $localstatedir" ,"$tmpdir/" ,"/tmp");
	my $space="Checking Space on ....\n";
	$space.="$_ \n:".`df -h $_`."\n" foreach(@spacearr);
	

	my $str=join($sep,$getdistro,$kernelversion,$tarversion,$netstat_info,$iptables_info,$sestatus_info,$space);
	print SYSTMP  $algn.$str;
}

sub get_var_log_messages() {
	# get last 500 lines of /var/log/mesages
	my $messages="System Messages:\n". `tail -n 500 /var/log/messages`;
	print SYSTMP1 $messages;
}

sub get_amanda_info(){
	# get amanda info
	my $xinetd="$sep Attempting to fetch xinetd configurations for amanda...\n";
	$xinetd.="\nxinetd file contents:\n".`cat /etc/xinetd.d/amanda*`;
	my $amanda_release="\nFetching amanda version number....\n".`cat $localstatedir/amanda-release`;
	my $amanda_version="\nFetching amanda version info....\n".`amadmin X version`;
	my $amanda_info_str=join($sep,$xinetd,$amanda_release);
	print SYSTMP $algn.$amanda_info_str;
}


sub execute_tar(){
	# execute tar
	my @conftar_list=&getamandaconfig();
	my $conftar_str="";
 	foreach(@conftar_list){
		s/^\s*//;
		s/\s*$//;
		$conftar_str.="$confdir/$_ ";
	}
	# building tar list
	my $tarlist="$tempfile";
	$tarlist.=" $tempfile1" if(! $novarlogmessages);
	$tarlist.=" /tmp/amanda";
	$tarlist.=" $conftar_str ";

	my $tarcmd = "$tar $taropt $logfile $tarlist";
	my $ret=system($tarcmd);
	if($ret eq "2"){ 
		print "\nSupport script unable to collect logs, please send following files to support team:\n$sep\t1. /opt/zmc/logs/*\n\t2. $tempfile\n\t3. $confdir/*\n\t4. $tmpdir/*\n";
		exit;
	}
		if($ret eq "512"){
			print "\n tar done with errors, please send this -> $logfile to Zmanda support team\n\n";
		}
		elsif($ret eq "0") {
			print "\n Please send this log file -> $logfile to Zmanda support team\n\n";
		}
}
	

sub close(){
	close(SYSTMP);
        close(SYSTMP1) if(! $novarlogmessages);
	if(defined $notar){
	   my $notarlist= "\nPlease send following files to support team:\n$sep\t1. $tempfile\n\t2. $confdir/*\n";
	print $notarlist;
	
	my $i=0;
	}
	else {
		`rm -rf $tempfile $tempfile1`;
	}
}

