#!/usr/bin/perl

# purpose:
# verify the link type (A, B,C) matches the DB

# customize:
#  - location of ETR
#  - location of DB
#  - specify monitored end as FTW_ISTP or STK_ISTP 


use English;
use Carp;
use Cwd;
use DBI;
use Net::Ping;
use File::Basename;
use File::Path;
use File::Glob;
use File::Copy;


# db
# LS9231 0 166 D ARGENTINA_STPN1

# ETR
#
# LS9231 
# SS7 STK_ISTP ARGENTINA_STPN1 D LS9231 1
# Q703:NxDS0 0 CCITT-BLUE STI-ARGTN_STP1/0 STI-LS9231/0-166 acceSS7 ENABLED \

#  ARGENTINA_STPN1 
# SSP ARGENTINA_STPN1 274 ARGENTINA_STPN1 "" 0 REM SSP IN:7-44-4;ITU383 "" "" ""
# SS7 STK_ISTP ARGENTINA_STPN1 D LS9231 1

my $ETR = "/tmp/wm/wm.etr";
my $DB = "/h/bmetzger/s/p/db2";

  open DB, "<$DB" or croak "Can't open DB for read: $!\n";
  while (my $dbline = <DB>) {
#  @dblines = <DB>; 
#  foreach $dbline (@dblines) { 
        chomp $dbline;
        next if $dbline =~ /^$/;
        ($dblinkest, $dbslc, $dblinknumb, $dblinktype, $dbname) = split(/\s+/,$dbline);

#print "$dblinkest\n";
          open (ETR, "<$ETR") or croak "Can't open etr for read: $!\n";
          @etrlines = <ETR>; 
          foreach $etrline (@etrlines){
              next if $etrline =~ /^$/;
              if ( ($etrline =~ /$dblinkest/) && ($etrline =~ /^SS7/) ) {
                 ($junk1, $etrsite, $etrname, $etrlinktype, $etrlinkset) = split(/\s+/,$etrline);
              
#                  if ($etrsite =~ /STK_ISTP/){
                  if ($etrsite =~ /FTW_ISTP/){
                       if ($dblinktype !~ /$etrlinktype/)  {
                           print "$etrsite  $dblinkest  db:$dblinktype  etr:$etrlinktype -mismatch\n";                
                       }else {
                           print "$etrsite $dblinkest  db:$dblinktype  etr:$etrlinktype -ok\n";                
                       }
                  }
              } #matches linkset
          }
          close (ETR);
  }
   close (DB);
