#!/usr/bin/perl
#
# This script is a component of Warewulf,
# http://www.runlevelzero.net/greg/warewulf
#
#########################################################################
#
# Copyright (c) 2003, The Regents of the University of California, through
# Lawrence Berkeley National Laboratory (subject to receipt of any
# required approvals from the U.S. Dept. of Energy).  All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# The GNU GPL Document can be found at:
# http://www.gnu.org/copyleft/gpl.html
#
#########################################################################
#
# Written and maintained by:
#       Greg Kurtzer, <gmkurtzer@lbl.gov>

use lib "/usr/lib/warewulf/", "/usr/lib64/warewulf/";
use Warewulf::Config;
use Warewulf::Util;
use Getopt::Long;

$boot_ip = "10.128.0.1";

my $usage = "USAGE: $0 [options]
  About:
   wwdebug helps identify problems with Warewulf configurations. If you are
   having a problem with Warewulf, you should include the output of this
   program with an email to the warewulf email list
   (http://lists.caosity.org/mailman/listinfo/warewulf).

  Options:
    --help     Show this banner

  This tool is part of the Warewulf cluster distribution
     http://warewulf-cluster.org/
";

GetOptions(
   'help'      => \$help,
);

if ( $help ) {
   print $usage;
   exit;
}

if ( $> != 0 ) {
   die "You must be root!\n";
}

sub node_count {
   my ($ipaddr, $netmask, @NULL) = @_;
   my ( $range1, $range2, $tmp);
   $start = &node_addr_start("0", "$ipaddr", "$netmask");
   $range1 = &node_addr($node_offset, "$ipaddr", "$netmask");
   for($i=1;;$i++) {
      $tmp = &node_addr($i+$node_offset, "$ipaddr", "$netmask");
      if ( $tmp eq $start) {
         # Woops, gone too far... back up 2 iterations...
         $i -= 2;
         $range2 = &node_addr($i+$node_offset, "$ipaddr", "$netmask");
         last;
      }
   }
   return($i, $range1, $range2);
}


%master = &master_config();
%nodes = &node_config();
%groups = &group_config();
%clients = &client_config();

chomp($hostname = `hostname`);
print "Warewulf master node debug information\n\n";
print "Master hostname:            $hostname\n";
print "Master nodename:            $master{'network'}{'nodename'}\n";

print "\n";

if ( ! $master{'network'}{'boot device'} or $master{'network'}{'boot device'} eq "auto" ) {
   $boot_netmask = $master{'network'}{'admin netmask'};
   $boot_dev = "$master{'network'}{'admin device'}:1";
   ($boot_count, $boot_1, $boot_2) = 
      &node_count($boot_ip, $master{'network'}{'admin netmask'});
} elsif ( $master{'network'}{'boot device'} ) {
   $boot_netmask = $master{'network'}{'boot netmask'};
   $boot_dev = $master{'network'}{'boot device'};
   $boot_ip = $master{'network'}{'boot ipaddr'};
   ($boot_count, $boot_1, $boot_2) = 
      &node_count($boot_ip, $master{'network'}{'boot netmask'});
}

($admin_count, $admin_1, $admin_2) = 
   &node_count($master{'network'}{'admin ipaddr'}, $master{'network'}{'admin netmask'});
($cluster_count, $cluster_1, $cluster_2) = 
   &node_count($master{'network'}{'cluster ipaddr'}, $master{'network'}{'cluster netmask'});
($sharedfs_count, $sharedfs_1, $sharedfs_2) = 
   &node_count($master{'network'}{'sharedfs ipaddr'}, $master{'network'}{'sharedfs netmask'});

print "Node addressing:\n";

printf ("   %-10s %-10s %-6s %-15s %-15s\n",
   "Network",
   "Device",
   "Nodes",
   "Starting IP",
   "Ending IP" );

printf ("   %-10s %-10s %-6d %-15s %-15s\n",
   "BOOT",
   $boot_dev,
   $boot_count,
   $boot_1,
   $boot_2 );

printf ("   %-10s %-10s %-6d %-15s %-15s\n",
   "ADMIN",
   $master{'network'}{'admin device'},
   $admin_count,
   $admin_1,
   $admin_2 );

printf ("   %-10s %-10s %-6d %-15s %-15s\n",
   "SHAREDFS",
   $master{'network'}{'sharedfs device'},
   $sharedfs_count,
   $sharedfs_1,
   $sharedfs_2 );
   
printf ("   %-10s %-10s %-6d %-15s %-15s\n",
   "CLUSTER",
   $master{'network'}{'cluster device'},
   $cluster_count,
   $cluster_1,
   $cluster_2 );


print "\nServices:\n";
print "   syslog:                  $master{'servers'}{'syslog'}\n";
print "   home nfs:                $master{'servers'}{'home nfs'}\n";
print "   vnfs nfs:                $master{'servers'}{'vnfs nfs'}\n";

print "\nWarewulfd:\n";
print "   master:                  $clients{'warewulf master'}\n";
print "   warewulfd udp allow:     $master{'warewulfd'}{'warewulfd udp allow'}\n";
print "   warewulfd udp deny:      $master{'warewulfd'}{'warewulfd udp deny'}\n";


print "\nNode Group info:\n";
foreach $group ( keys %groups ) {
   print "   group name:              $group\n";
   print "      vnfs:                 $groups{$group}{'vnfs'}\n";
   print "      admin dev:            $groups{$group}{'admin device'}\n";
   print "      cluster dev:          $groups{$group}{'cluster device'}\n";
   print "      sharedfs dev:         $groups{$group}{'sharedfs device'}\n";
   print "      kernel image:         $groups{$group}{'kernel image'}\n";
   print "      wwinitrd image:       $groups{$group}{'wwinitrd image'}\n";
   print "      user names:           $groups{$group}{'user names'}\n";
   print "      user groups:          $groups{$group}{'user groups'}\n";
   push(@kernels, "$groups{$group}{'kernel image'}");
   push(@wwinitrds, "$groups{$group}{'wwinitrd image'}");
}

print "\nChecking files:\n";
foreach $kernel ( @kernels ) {
   print "   kernel:                  /tftpboot/$kernel\n";
   if ( ! -f "/tftpboot/$kernel" ) {
      print "      WARNING: /tftpboot/$kernel does not exsist!\n";
   }
}
foreach $wwinitrd ( @wwinitrds ) {
   print "   wwinitrd:                /tftpboot/$wwinitrd\n";
   if ( ! -f "/tftpboot/$wwinitrd" ) {
      print "      WARNING: /tftpboot/$wwinitrd does not exsist!\n";
   }
}
      
open(IPTABLES, "/sbin/iptables -L |");
while(<IPTABLES>) {
   chomp;
   if ( $_ =~ /\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?/ ) {
      $rules++;
   }
}

if ( $rules ) {
   print "WARNING: IP Tables is running! You should disable when troubleshooting with\n";
   print "         the command \"/sbin/service iptables stop\".";
}







