#!/usr/bin/perl -w
#
# Copyright (c) 2003-2011, The Ohio State University. All rights
# reserved.
#
# This file is part of the MVAPICH2 software package developed by the
# team members of The Ohio State University's Network-Based Computing
# Laboratory (NBCL), headed by Professor Dhabaleswar K. (DK) Panda.
#
# For detailed copyright and licensing information, please refer to the
# copyright file COPYRIGHT in the top level MVAPICH2 directory.
#
# Host analysis tool
#

use Sys::Hostname;
use strict;
$|=1;

my $missingIbstat = 1;

# ----------------------------
# XML encoding routines
# ----------------------------
sub printTag {
  my ($space, $name, $val, @attrs );
  ($space, $name, $val, @attrs) = @_;
  chomp( $val );
  print "$space<$name";
  foreach (@attrs) {
    print " $_";
  }
  print ">$val</$name>\n";
}


sub tagLines {
  my ($space, $tagName, $tagAtt, @lines);
  ($space, $tagName, $tagAtt, @lines) = @_;
  foreach (@lines) {
    my $v = $_;
    chomp( $v );
    print "$space<$tagName $tagAtt>$v</$tagName>\n";
  }
}

sub printColumnTag {
  my ($space, $tagname, $attname, @lines);
  ($space, $tagname, $attname, @lines) = @_;
  
  foreach( @lines ) {
    my $l = $_;
    chomp $l;
    if ($l=~/(.*): (.*)/) {
      print "$space<$tagname $attname='$1'>$2</$tagname>\n";
    };
  };
}

# ----------------------------
# Reporting routines
# ----------------------------

#
# Information about installed tools.
#
sub toolsReport {
  my @ver; 
  print "\n  <!-- Tools information -->\n";
  $missingIbstat  = system(`ibstat &> /dev/zero`);
  if ($missingIbstat != 0) {
    print "  <tool name='ibstat' error='true' >ERROR: ibstat not found.<tool>\n";
  } else {
    @ver = `ibstat -V`;
    printTag "  ", "tool", `which ibstat`, ("name='ibstat'", "version='$ver[0]'");
  }

  my $check = system('automake --version &> /dev/zero');
  if ($check!=0) {
    print "  <tool name='automake' error='true' >ERROR: automake not found.<tool>\n";
  } else {
    @ver = `automake --version`;
    $ver[0] =~ /.*\) (.*)/;
    printTag "  ", "tool", `which automake`, ( "name='automake'", "version='$1'");
  }



  $check = system('autoconf --version &> /dev/zero');
  if ($check!=0) {
    print "  <tool name='autoconf' error='true' >ERROR: autoconf not found.<tool>\n";
  } else {
    @ver = `autoconf --version`;
    $ver[0] =~ /.*\) (.*)/;
    printTag "  ", "tool", `which autoconf`, ("name='autoconf'", "version='$1'");
  }


  $check = system('libtool --version &> /dev/zero');
  if ($check!=0) {
    print "  <tool name='libtool' error='true' >ERROR: libtool not found.<tool>\n";
  } else {
    @ver = `libtool --version`;
    $ver[0] =~ /.*\) (.*)/;
    printTag "  ", "tool", `which libtool`, ("name='libtool'", "version='$1'");
  }
	
}

#
# Report information about the HCAs.
#
sub  devicesReport {
  print "\n  <!-- Devices information -->\n";
  print "  <devices>\n";
  if ($missingIbstat == 0) {
    my @lines = `ibstat`;
    printColumnTag "    ", "hca", "type", @lines;
  } else {
    print "    <error>Missing ibstat tool.</error>\n";  	
  }
  print "  <devices>\n";
}


#
# Report information about an host
#
sub hostreport {
  print " <host hostname='" . hostname()  ."'>\n";
  print "  <!-- MVAPICH2 Information -->\n";
  printTag "  ", "mpiname", `mpiname -n`, ();
  printTag "  ", "mpiversion", `mpiname -v`, ();
  printTag "  ", "mpireldate", `mpiname -r`, ();
  printTag "  ", "mpidevice", `mpiname -d`, ();
  print "  <mpicoconf>\n";
  my @lines = `mpiname -o`;
  my @words = split(/ /,$lines[1]);
  foreach( @words ) {
    printTag "    ", "flag", $_, ();
  }
  print "  </mpicoconf>\n";
  print "  <mpicompilers>\n";
  printColumnTag "    ", "compiler", "type", `mpiname -c`;
  print "  </mpicompilers>\n";

  print "\n  <!-- Operating system information -->\n";
  printTag "  ", "os", `uname -s`, ("subsys='Kernel'");
  printTag "  ", "os", `uname -r`, ("subsys='Kernel release'");
  printTag "  ", "os", `uname -v`, ("subsys='Kernel version'");
  printTag "  ", "os", `uname -m`, ("subsys='Machine'");
  printTag "  ", "os", `uname -p`, ("subsys='Processor'");
  printTag "  ", "os", `uname -i`, ("subsys='Platform'");
  printTag "  ", "os", `uname -o`, ("subsys='Operating system'");

  toolsReport;

  devicesReport;

  print "\n  <packages>\n";
  @lines = `rpm -qa | egrep 'ibverbs|ibmad|ibumad' | sort`;
  tagLines "    ", "package", "type='rpm'", @lines ;
  print "  </packages>\n";

  print " </host>\n";
}

# ------------------------------------------------------------
# 
# ------------------------------------------------------------
print "<?xml version='1.0' encoding='UTF-8'>\n";
print "<!-- MVAPICH2 host analysis tool -->\n";
my $now = localtime time;
print "<mvapich2 version='1.6' date='".$now."'>\n";
hostreport();
print "</mvapich2>\n";

