#!/usr/bin/perl
#
# $Id: dvssnap.pl 1557 2015-12-01 Roger.Beck $
# $Loc: /root/dvssnap $
# 
# Gathering system information and backup file system configuration

use File::Copy;
use Getopt::Long;

GetOptions('days:i' => \$logFileDays);

# The number of days to grep logs for
if ( $logFileDays == 0 ) {
        $logFileDays = 2;
}

my @monthNames = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));

# Today's date
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;

# Create directory for holding things
$mon = $mon+1;
if ( $mon < 10 ) {
    $mon = "0$mon";
}

$hostname = qx( "hostname" );
chomp($hostname);
#$logdirectory = "$year-" . sprintf("%02d", $mon) . "-" . sprintf("%02d", $mday) . "-" . sprintf("%02d", $hour) . sprintf("%02d", $min) . sprintf("%02d", $sec);
$logdirectory = "$hostname.$year-" . sprintf("%02d", $mon) . "-" . sprintf("%02d", $mday) . "-" . sprintf("%02d", $min) ;
$logpath = "/tmp/$logdirectory";
$logarchive = "/tmp/$year-" . sprintf("%02d", $mon) . "-" . sprintf("%02d", $mday) . "." . sprintf("%s", $hostname) ;
print "SnapFile = [$logarchive] ";

mkdir( "$logpath", 0777 );
mkdir( "$logpath/showlong", 0777 );

#print "Placing files in $logpath\n";
print " .";

$cvfsCP = "/usr/cvfs/config";
$cvfsBP = "/usr/cvfs/bin";
$dvsCFG = "/etc/opt/DVS";

#print "Copying files\n";
print " .";

@filestocopy=( "/etc/fstab", "/etc/hosts", "/etc/grub.conf" , "/etc/samba/smb.conf", "/etc/exports");

foreach $filetocopy (@filestocopy) {
        if ( -e $filetocopy ) {
                $filename = substr( $filetocopy, rindex( $filetocopy, "/" ) + 1 );
                copy( $filetocopy, "$logpath/" . $filename );
        } else {
                print "$filetocopy not found!!!\n";
        }
}

# Copy /usr/cvfs/config/*.cfg
mkdir( "$logpath/config", 0777 );

@configs = glob( "/usr/cvfs/config/*.cfg?" );
foreach $config (@configs) {
        copy( "$config", "$logpath/config/" . substr( $confing, rindex( $config, "/" ) ) );
}

# Output of cvlabel -c
system( "cp -rp $cvfsCP $logpath/." );
system( "$cvfsBP/cvlabel -c | sort >> $logpath/config/cvlabel.txt" );
system( "$cvfsBP/cvfsid >> $logpath/cvfsid.txt" );
system( "$cvfsBP/cvversions >> $logpath/cvversions.txt" );

# Read fsmlist and do a show long on each one
#print "Doing a show long against file systems\n";
print " .";

$fsmlist="$cvfsCP/fsmlist";
open(DAT, $fsmlist) || print "Error could not read fsmlist\n";
@raw_data=<DAT>;
close(DAT);
foreach $fsmline (@raw_data) {
    @filesys = split ' ',$fsmline;
    print "\rshowing $filesys[0] ...";
    if ($filesys[0] !~ /^#/)  {
        sleep 1;
        system( "$cvfsBP/cvadmin -F $filesys[0] -e 'show long' >> $logpath/showlong/$filesys[0].out" );
    }
}

print " .";
# gather Spycer and license info
if ( -e $dvsCFG ) {
    system( "cp -rp $dvsCFG $logpath/." );
}

# Output of /proc/cpuinfo, and /proc/meminfo and more
system( "uname -a >> $logpath/uname.txt" );
system( "cat /etc/issue >> $logpath/issue.txt" );

system( "cat /proc/cpuinfo >> $logpath/cpuinfo.txt" );
system( "cat /proc/meminfo >> $logpath/meminfo.txt" );

system( "lspci | grep -i FIBR >> $logpath/fibre_hba.txt" );
system( "lspci -v >>  $logpath/lspci.txt" );
#system( "modinfo qla2xxx | grep 'filename\\\|version' >> $logpath/qla2xxx" );
#system( "modinfo qla2xxx | grep 'filename\\\|version' >> $logpath/qla2xxx" );


# Ifconfig
system( "ifconfig >> $logpath/ifcfg.txt" );

# Adaptec conf
if ( -e "/usr/StorMan/arcconf" ) {
    system( "/usr/StorMan/arcconf SAVECONFIG 2&>/dev/null" );
    if ( -e "/var/log/saveconfig.xml" ) {
       system( "cp /var/log/saveconfig.xml $logpath/adaptec_config.xml" );
    } else {
        system( "echo 'No Adaptec config file could be generated' > $logpath/adaptec_config.xml" );
    }
}

# Compress
#print "Compressing Snap Directory\n";
print " .";

system( "cd /tmp && tar czvfzP $logarchive.tgz $logdirectory/ 2>&1 >> /dev/null" );

#print "File \"$logarchive.tgz\" is ready.  Output size is " . get_filesize_str( "$logarchive.tgz" ) . "\n";

my $filesize = -s "$logarchive.tgz";
print "\rSnapFile = [$logarchive.tgz] \nSize: $filesize kb ... done\n\n";

# Cleaning up
system( "rm -rf /tmp/$logdirectory" );;

##########
# function get_local_time
sub get_local_time {
        my ($ysec,$ymin,$yhour,$ymday,$ymon,$yyear,$ywday,$yyday,$yisdst) = localtime(time - ( 24 * 60 * 60 * $day ) );
        $yyear += 1900;
        $ymon = $ymon+1;
        if ( $ymon < 10 ) {
            $ymon = "0$ymon";
        }
        return;
}
