#!/usr/bin/perl -I/opt/SUNWrasag/lib
use strict;
use System;

use PDM::ConfigFile;
my $DIR = "/opt/SUNWrasag";
my $CONFIG = "$DIR/DATA/rasagent.conf";

my $T3TO = 30;  # timeout getting t3 tokens

open(O, "$DIR/System/config");
my $version = <O>;
$version =~ s/version=//;
close(O);

if (!open(O,"/etc/hosts")) {
  print "Error: Cannot read /etc/hosts, aborting \n";
  exit(1);
}
my $hostname = System->hostname();

my $config =<<EOF;
active=Y
admin_email=root\@$hostname
categories=san|stools4|switch|t3|topo
contract=
cust_no=
customer=
email=root\@$hostname
frequency=5
host_adapter=S
hostname=$hostname
local_event_log=Y
log_months=
logfile=/var/adm/messages
mailer=/usr/bin/mail
ping=
pro.http.ip=http://system:2000/nscc/InsertService
pro.http.timeout=90
pro.netconnect.active=N
pro.netconnect.maxsize=2000
pro.uucp.active=N
production=Y
showoob=Y
shuttle=
site_address=
site_city=
site_contact=
site_country=
site_email=
site_name=
site_state=
site_zip=
solution=Y
st4logfile=/var/opt/SUNWvtsst/logs
t300logfile=/var/adm/messages.t3
ticker=
timeout.http=30
timeout.luxadm=200
timeout.ping=10
timeout.rm6=200
timeout.snmp=20
version=$version
EOF

my $notifs =<<EOF;

[notification1]
device=*
email=root\@$hostname
event=*
priority=*
type=Email
EOF


my $devs = "";
my $short = $hostname;
my $ix = index($short, ".");
if ($ix > 0) {
  $short = substr($short,0,$ix);
}
my $err = 0;

my $devcnt = 1;
my($l);
my $IP = System->ifconfig("dmfe1");
my($a1, $a2, $a3, $a4) = split(/\./, $IP);
$IP = "$a1.$a2.$a3";


while ($l = <O>) {
  chop($l);
  next if (substr($l,0,1) eq "#");
  my ($ipno, $ip, $rest) = split(/\s+/, $l);

  next if (substr($ipno,0,length($IP) ) ne $IP);

  my $no = substr($ipno,-2);
  print "$ip / $ipno \n";

  if ($no >= 40 && $no <= 54) { # T3
     if (&ping($ipno)) {
        print  "  -> accessing t3...\n";
        my($key, $wwn1, $wwn2) = &t3WWN($ipno, $T3TO);
        if (!$key) {
           print "  -> Error: cannot find the serial# of this T3 \n";
        } 
        if (!$wwn1) {
           print "  -> Error: cannot find the wwn of the first port of this T3 \n";
        } 
        if (!$wwn2) {
           print "  -> Error: cannot find the wwn of the second port of this T3 \n";
        } 
        if (!$wwn1) {
           $err++; next;
        } else {
            $devs .= "\n[device$devcnt]\nactive=Y\ndatahost=*None\nhost=\nip=$ipno\n".
                     "ipno=$ipno\nname=$short-$ip\ntype=t3\nwwn=$wwn1\n";
            $devcnt++;
            print "  -> added T3 ($wwn1). \n\n";
        }

     } else {
        print "  -> Warning: cannot ping $ipno \n";
     }
         
  } elsif ($no >= 30 && $no <= 33) { # switch
     if (&ping($ipno)) {
        my $wwn = &switchWWN($ipno);
        if (!$wwn) {
           print "  -> Error: cannot find the wwn of $ipno \n";
           $err++;
        } else {
            $devs .= "\n[device$devcnt]\nactive=Y\ndatahost=*None\nhost=\nip=$ipno\n".
                     "ipno=$ipno\nname=$short-$ip\ntype=switch\nwwn=$wwn\n";
            $devcnt++;
            print "  -> added Switch ($wwn). \n\n";
        }

     } else {
        print "  -> Warning: cannot ping $ipno \n";
     }
  }
}

if ($err) {
   print "Errors found, rasagent configfile not created! \n";
   exit(1);
} else {
   if(!open(O, ">$CONFIG")) {
      print "Error: Cannot open $CONFIG, aborting $! \n";
      exit(1);
   }
   print O  $config;
   print O  $devs;
   print O  $notifs;
   close(O);
   open(O,  ">$DIR/DATA/start");
   close(O);

   print "\nNo Errors: $CONFIG created! \n";
   print "Done. \n\n";
}





sub switchWWN {
  my($ip) = @_;

  my @l = `/opt/SUNWrasag/bin/sanbox version $ip`;
  if ("@l" =~ /WWN:\s+([0-9a-f]+)/) {
      return $1;
  } else {
      return undef;
  }
}

sub ping {
  my($ip) = @_;

  open(PING, "/usr/sbin/ping $ip 2|");
  my $ans = <PING>; close(PING);
  if ($ans =~ /is alive/) {
     return 1;
  } else {
     return 0;
  }
}

sub t3WWN {
  my($ipno, $T3TO) = @_;
  my($key, $key1, $key2, $key3, $wwn1, $wwn2);
  my($u2key1, $u2key2, $u2key3);

  my $elemprop0 = Util::Http->get(
       "http://guest\@$ipno/elemprop.htm?unitIndex=0",$T3TO);
  my $elemprop1 = Util::Http->get(
       "http://guest\@$ipno/elemprop.htm?unitIndex=1",$T3TO);

  my @x = split(/\n/, $elemprop0 . $elemprop1);

  foreach my $l (@x) {
        if ($l =~ /;u2mpn,fruVendor,(.*);/) {
           $u2key1 = lc($1);
        } elsif  ($l =~ /;u2mpn,fruModel,(.*);/) {
           $u2key2 = lc($1);
        } elsif  ($l =~ /;u2mpn,fruSerialNo,(.*);/) {
           $u2key3 = lc($1);
        } elsif ($l =~ /;u1mpn,fruVendor,(.*);/) {
           $key1 = lc($1);
        } elsif  ($l =~ /;u1mpn,fruModel,(.*);/) {
           $key2 = lc($1);
        } elsif  ($l =~ /;u1mpn,fruSerialNo,(.*);/) {
           $key3 = lc($1);
        } elsif ($l =~ /;u1p1,portWWN,(.*);/) {
           $wwn1 = lc($1);
        } elsif ($l =~ /;u2p1,portWWN,(.*);/) {
           $wwn2 = lc($1);
        }
  }
  if (!$u2key1 || !$u2key2 || !$u2key3) {
    print "  ** WARNING: cannot identify the serial# of the second midplane u2mpn: '$u2key1.$u2key2.$u2key3' !\n";
  }
  if ($key1 && $key2 && $key3) {
    $key = "$key1.$key2.$key3";
  }
  return ($key, $wwn1, $wwn2);
}


  
