#!_PERL_PATH_
#
## @summary Retrieve various commands outputs from filer
##
## @description
##   Gather information from various filer commands on a remote filer
##   and place it in a specified directory. From that directory, also
##   create a tarball, either compressed (Solaris) or gzipped (Windows,
##   HP-UX)
##
## @arg -d dir Optional: specify alternate output directory
##
## @arg -l login[:password] Optional: specify user/password for filer
##
## @arg -n name Required: name of filer
##
#
# Copyright (C) 2002-2003 Network Appliance, Inc.
#
use strict;
use Socket;
use vars qw($opt_d $opt_n $opt_l $opt_h $opt_u $opt_f);
use Getopt::Std;
use Cwd;
use File::Basename;

my ($name, $path, $suffix) = fileparse($0);
push @INC, $path;
require SHsupport;

my ($filer,$partner, $user);

my $usage="filer_info [-d <directory>] [-l <login>[:<password>]] [-f] -n <filer_name> [-u <command>]\n";

#
# Map filer commands against output file names
#
my %cmd_map = (
	       "ifconfig" => "ifconfig -a",
	       "license" => "priv set -q advanced; license",
	       "sysconfig_av" => "sysconfig -av ",
	       "sysconfig_r" => "sysconfig -r ",
	       "df_r" => "df -r ",
	       "snap_list" => "snap list",
	       "snap_delta" => "snap delta",
	       "iswt_session_iswta" => "iswt session show -v iswta",
	       "iswt_session_iswtb" => "iswt session show -v iswtb",
	       "iswt_interface" => "iswt interface show",
	       "iscsi_security" => "iscsi security show",
	       "iscsi_show_adapter" => "iscsi show adapter",
	       "iscsi_show_initiator" => "iscsi show initiator",
	       "iscsi_status" => "iscsi status",
	       "iscsi_stats" => "iscsi stats",
	       "iscsi_config" => "iscsi config",
	       "iscsi_nodename" => "iscsi nodename",
	       "fcp_show_cfmode" => "fcp show cfmode",
	       "fcp_show_adapter" => "fcp show adapter -v",
	       "fcp_show_initiator" => "fcp show initiator -v",
	       "fcp_status" => "fcp status",
	       "fcp_stats" => "fcp stats",
	       "fcp_config" => "fcp config",
	       "fcp_nodename" => "fcp nodename",
	       "igroup_show" => "igroup show",
	       "igroup_show_verbose" => "igroup show -v",
	       "options_lun" => "options lun",
	       "lun_show" => "lun show -v all",
	       "lun_config_check" => 
			"priv set -q advanced; lun config_check -v",
	       "vol_status" => "vol status -v",
	       "version" => "version",
	       "cf_status" => "cf status",
	       "cf_monitor" => "cf monitor",
	       "cf_partner" => "cf partner",
	       "messages_log" => "rdfile /etc/log/messages",
	       "audit_log" => "rdfile /etc/log/auditlog",
	       );


my $usage1="\nCollects configuration information about filer and packages\n" .
    "it ready to be sent to Network Appliance Customer Support.\n" .
    "\t-d <directory>\tdirectory to place the files in " . 
    "(default is /tmp/netapp)\n" .
    "\t-l <login>[:<password>]\tgives the login/password " . 
    "for the filer\n" .
    "\t-f forces overwrite of output\n\t-hprint this message\n";


#
# Parse the possible options: "-d", "-n", and "-l"
getopts('fd:l:n:u:');

if ($opt_h) {
    print $usage;
    print $usage1;
    exit 0;
}

#
# Change output directory if desired. Do directory setup
#
&SHsupport::get_os_specific($opt_d);

#
# Check that we have root/admin privilege.
#
&SHsupport::check_root_status();

#
# The "-n" flag is mandatory, to figure out which filer to contact.
#
if (defined($opt_n)) {
    $filer = $opt_n;
} else {
    print $usage;
    exit 1;
}

#
# The "-l" flag has a default to root.
$user = defined($opt_l) ? $opt_l : "root";

#
# Ping the filer. If we don't reach it, the subroutine will complain
# and exit.
#
&SHsupport::ping_remote("filer", $filer);

#
# Test for the output files already in the way, and complain if so.
#
&SHsupport::test_existing_files("ntap_filer_" . "$filer", 0, $opt_f);

if (defined($opt_u)) {
    print "opt_u $opt_u\n";
    my $cmd = $cmd_map{$opt_u};
    my $out;
    if ($cmd ne "") {
	$out = &SHsupport::rshget($filer, $user, $cmd);
	if ($? != 0) {
	    print "Unable to rsh to filer $filer.  Please ";
	    print "configure $filer for rsh\n";
	    print "access from this host (or provide login:password)";
	    print " and try again.\n";
	    exit 1;
	} else {
	    print $out;
	    exit;
	}
    } else {
	print "Unknown command $opt_u\n";
	exit;
    }
}

#
# Cycle through all the commands we run against the filer and
# squirrel away the outputs.
#
&SHsupport::update_user();
foreach my $el (sort keys %cmd_map) {
    
    &SHsupport::update_user();
    if (&SHsupport::rshgetother($filer, $user, $cmd_map{$el}, $el) == 0) {
	print "Unable to rsh to filer $filer.  Please ";
	print "configure $filer for rsh\n";
	print "access from this host (or provide login:password)";
	print " and try again.\n";
	exit 1;
    }
}

# If this is a CFO config and the partner has been taken over, save the
# partner info too. partner info filers will be suffixed with ".<partnername>".
my $cfstatus = &SHsupport::rshget($filer, $user, "cf status");
if ($cfstatus =~ /^\w+ has taken over (\w+)/) {
    # We have taken over the partner. Run the 'partner cmd' variants
    foreach my $el (sort keys %cmd_map) {
	# This is a hack for priv set -q advanced
	my $cmd = $cmd_map{$el};
	$cmd =~ s/\;/\; partner/;
	
	&SHsupport::update_user();
	&SHsupport::rshgetother($filer, $user, "partner $cmd",
			       $el.".".$1);
    }
} elsif ($cfstatus =~ /Cluster enabled, (\w+) is up/) {
    # The partner is up. Go to it to get all the files.
    $partner = $1; 
    foreach my $el (sort keys %cmd_map) {
	&SHsupport::update_user();
	if (&SHsupport::rshgetother($partner, $user, $cmd_map{$el},
				    $el . "." . $1) == 0) {
	    print "Unable to rsh to filer $partner for partner info.";
	    print " Please configure $partner for rsh\n";
	    print "access from this host (or provide login:password)";
	    print " and try again.\n";
	    print "If the passwords on $filer and $partner differ, please\n";
	    print "run this script with \"-n $partner\" to gather complete\n";
	    print "information\n";
	    last;
	}
    }
} else {
    # It's probably not a cluster, so don't do anything
}

# Tar it up.
&SHsupport::make_tarball();

# Say bye-bye.
&SHsupport::print_summary("Filer $filer");

exit 0;


