ASNMTAP

 view release on metacpan or  search on metacpan

applications/bin/importDataThroughCatalog.pl  view on Meta::CPAN

    open(PID,">$pidfile") || die "Cannot open $pidfile!!\n";
    print PID $$;
    close(PID);
  }
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub printDebugAll {
  my ($l_text) = @_;

  if ($boolean_screenDebug or $boolean_debug_all) {
    chomp ($l_text);

    my $date = scalar(localtime());
    my $tlogging = $logging .'importDataThroughCatalog'. get_logfiledate();
    print "$l_text $date\n" if ( $boolean_screenDebug );

    if ($boolean_debug_all and $logging ne '<NIHIL>') {
      my $rvOpen = open(ALLDEBUG,">>$tlogging-all.txt");

      if ($rvOpen) {
        print ALLDEBUG "$l_text $date\n";
        close(ALLDEBUG);
      } else {
        print "Cannot open $tlogging-all.txt to print debug information\n";
      }
    }
  }
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub printDebugNOK {
  my ($l_text) = @_;

  if ($boolean_debug_NOK and $logging ne '<NIHIL>') {
    chomp ($l_text);

    my $date = scalar(localtime());
    my $tlogging = $logging .'importDataThroughCatalog'. get_logfiledate();
    my $rvOpen = open(NOKDEBUG,">>$tlogging-nok.txt");

    if ($rvOpen) {
      print NOKDEBUG "$l_text $date\n";
      close(NOKDEBUG);
    } else {
      print "Cannot open $tlogging-nok.txt to print debug information\n";
    }
  }
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub do_importDataThroughCatalog {
  printDebugAll (" IN: do_importDataThroughCatalog <$PROGNAME v$version pid: <$pidfile>");
  ($dbh, $rv, $alarmMessage) = DBI_connect ( $DATABASE, $SERVERNAMEREADWRITE, $SERVERPORTREADWRITE, $SERVERUSERREADWRITE, $SERVERPASSREADWRITE, $alarm, \&DBI_error_trap, [*EMAILREPORT, "Cannot connect to the database"], \$logger, $debug, $boolean_deb...

  if ($dbh and $rv) {
    my ($catalogID, $catalogType, $databaseFQDN, $databasePort);
    $sql = "select catalogID, catalogType, databaseFQDN, databasePort from $SERVERTABLCATALOG where catalogID <> '$CATALOGID' and catalogType <> 'central' and activated = '1'";
    $sth = $dbh->prepare( $sql ) or $rv = DBI_error_trap(*EMAILREPORT, "Cannot dbh->prepare: $sql", \$logger, $debug);
    ($rv, undef) = DBI_execute ($rv, \$sth, $alarm, \&DBI_error_trap, [*EMAILREPORT, "Cannot sth->execute: $sql"], \$logger, $debug);
    $sth->bind_columns( \$catalogID, \$catalogType, \$databaseFQDN, \$databasePort ) or $rv = DBI_error_trap(*EMAILREPORT, "Cannot sth->bind_columns: $sql", \$logger, $debug) if $rv;

    if ( $rv ) {
      my %catalog;

      if ( $sth->rows ) {
        while( $sth->fetch() ) {
          print "- $catalogID, $catalogType, $databaseFQDN, $databasePort\n" if ($debug);
          $catalog{$catalogID}{catalogType}  = $catalogType;
          $catalog{$catalogID}{databaseFQDN} = $databaseFQDN;
          $catalog{$catalogID}{databasePort} = $databasePort;
        }
      }

      $sth->finish() or $rv = DBI_error_trap(*EMAILREPORT, "Cannot sth->finish: $sql", \$logger, $debug);

      foreach my $catalogID ( keys %catalog ) {
        if ($debug) {
          print "+ Catalog ID: $catalogID\n"; 
          print "  - catalogType  => ". $catalog{$catalogID}{catalogType} ."\n";
          print "  - databaseFQDN => ". $catalog{$catalogID}{databaseFQDN} ."\n";
          print "  - databasePort => ". $catalog{$catalogID}{databasePort} ."\n";
        }

        # Open connection to database and query data
        my $dbhSOURCE;
        ($dbhSOURCE, $rv, $alarmMessage) = DBI_connect ( $DATABASE, $catalog{$catalogID}{databaseFQDN}, $catalog{$catalogID}{databasePort}, $SERVERUSERREADWRITE, $SERVERPASSREADWRITE, $alarm, \&DBI_error_trap, [*EMAILREPORT, "Cannot connect to the da...

        if ($dbhSOURCE and $rv) {
          print EMAILREPORT "\nCatalog ID: $catalogID\n" unless ($debug);

          # config contrains: events, eventsChangesLogData & comments = = =
          if ( $type == 1 or $type == 3 ) {
            if ($debug) {
              print "- config contrains: events, eventsChangesLogData & comments\n";
            } else {
              print EMAILREPORT "\n- config contrains: events, eventsChangesLogData & comments\n";
            }

            # 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLHOLIDYS,         "catalogID = '$catalogID'", 'holidayID');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLUSERS,           "catalogID = '$catalogID'", 'remoteUser');
            # 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLHOLIDYSBNDL,     "catalogID = '$catalogID'", 'holidayBundleID');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLRESULTSDIR,      "catalogID = '$catalogID'", 'resultsdir');
            # 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLPLUGINS,         "catalogID = '$catalogID'", 'uKey');
          }

          # events, eventsChangesLogData & comments = = = = = = = = = = =
          if ( $type >= 2 ) {
            if ($debug) {
              print "- events, eventsChangesLogData & comments\n";
            } else {
              print EMAILREPORT "\n- events, eventsChangesLogData & comments\n";
            }

            # 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 1, $SERVERTABLEVENTS,          "catalogID = '$catalogID' and replicationStatus <> 'R'", 'uKey', 'timeslot');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 1, $SERVERTABLEVENTSCHNGSLGDT, "catalogID = '$catalogID' and replicationStatus <> 'R'", 'uKey');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 1, $SERVERTABLEVENTSDISPLAYDT, "catalogID = '$catalogID' and replicationStatus <> 'R'", 'uKey', 'posTimeslot');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 1, $SERVERTABLCOMMENTS,        "catalogID = '$catalogID' and replicationStatus <> 'R'", 'uKey', 'entryTimeslot');
          }

          # config contrains: ALL = = = = = = = = = = = = = = = = = = = =

          if ( $type == 1 or $type == 3 ) {
            if ($debug) {
              print "- config contrains: ALL\n";
            } else {
              print EMAILREPORT "\n- config contrains: ALL\n";
            }

            # 1'- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLTIMEPERIODS,     "catalogID = '$catalogID'", 'timeperiodID');
            # 2'- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLDISPLAYGRPS,     "catalogID = '$catalogID'", 'displayGroupID');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLPAGEDIRS,        "catalogID = '$catalogID'", 'pagedir');
            # 3'- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLSERVERS,         "catalogID = '$catalogID'", 'serverID');
            # 4'- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLCLLCTRDMNS,      "catalogID = '$catalogID'", 'collectorDaemon');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLDISPLAYDMNS,     "catalogID = '$catalogID'", 'displayDaemon');
            # 5'- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLCRONTABS,        "catalogID = '$catalogID'", 'lineNumber', 'uKey');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLVIEWS,           "catalogID = '$catalogID'", 'uKey', 'displayDaemon');
            # 6'- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLREPORTS,         "catalogID = '$catalogID'", 'id');
            importData (*EMAILREPORT, $dbhSOURCE, $dbh, 0, $SERVERTABLREPORTSPRFDT,    "catalogID = '$catalogID'", 'uKey', 'metric_id');
          }

          # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

          $dbhSOURCE->disconnect or $rv = DBI_error_trap(*EMAILREPORT, "Sorry, the database was unable to add your entry.", \$logger, $debug);
        }
      }
    }

    $dbh->disconnect or $rv = DBI_error_trap(*EMAILREPORT, "Sorry, the database was unable to add your entry.", \$logger, $debug);
  }

  printDebugAll ("OUT: do_importDataThroughCatalog <$PROGNAME v$version pid: <$pidfile>");
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub importData {
  my ($EMAILREPORT, $dbhSOURCE, $dbh, $replicationStatus, $table, $whereCLAUSE, @primaryKeys) = @_;

  printDebugAll ("     IN: importData <$PROGNAME v$version pid: <$pidfile>: $replicationStatus, $table, $whereCLAUSE, @primaryKeys");
  print $EMAILREPORT "  - importData: $replicationStatus, $table, $whereCLAUSE, @primaryKeys\n" unless ($debug);

  my $sqlSOURCE = "select * from `$table` where $whereCLAUSE limit $limit";
  print "+ $sqlSOURCE\n" if ($debug);

  my $sthSOURCE = $dbhSOURCE->prepare( $sqlSOURCE ) or $rv = DBI_error_trap(*EMAILREPORT, "Cannot dbhSOURCE->prepare: $sqlSOURCE", \$logger, $debug);
  ($rv, undef) = DBI_execute ($rv, \$sthSOURCE, $alarm, \&DBI_error_trap, [*EMAILREPORT, "Cannot sthSOURCE->execute: $sqlSOURCE"], \$logger, $debug);

  if ( $rv ) {
    while (my $ref = $sthSOURCE->fetchrow_hashref()) {
      my ($whereReplicationStatus, $where, $action) = ("where $whereCLAUSE", '');
      foreach my $primaryKey (@primaryKeys) { $whereReplicationStatus .= " and $primaryKey = '$ref->{$primaryKey}'" };

      my $sqlVERIFY = "select count(catalogID) from `$table` $whereReplicationStatus";
      print "... $sqlVERIFY\n" if ( $debug );
      my $sthVERIFY = $dbh->prepare( $sqlVERIFY ) or $rv = DBI_error_trap(*EMAILREPORT, "Cannot dbh->prepare: $sqlVERIFY", \$logger, $debug);
     ($rv, undef) = DBI_execute ($rv, \$sthVERIFY, $alarm, \&DBI_error_trap, [*EMAILREPORT, "Cannot $sthVERIFY->execute: $sqlVERIFY"], \$logger, $debug);

      if ( $rv ) {
        my $updateRecord = $sthVERIFY->fetchrow_array();
        $sthVERIFY->finish() or $rv = DBI_error_trap(*EMAILREPORT, "Cannot sthVERIFY->finish: $sqlVERIFY", \$logger, $debug);

        if ( $updateRecord ) {
          $action = "UPDATE `$table` SET";
          $where  = $whereReplicationStatus;
        } else {
          $action = "INSERT INTO `$table` SET";
        }

        foreach my $databaseField ( @{$sthSOURCE->{NAME}} ) { 
          my $value = ( ( defined $ref->{$databaseField} ) ? $ref->{$databaseField} : '' );
          $action .= " $table.$databaseField = \"$value\",";
        }

        chop $action if (defined $action);

        my $sql = "$action $where";
        print "  - $sql\n" if ($debug);
        ($rv, undef, undef) = DBI_do ($rv, \$dbh, $sql, $alarm, \&DBI_error_trap, [*EMAILREPORT, "Cannot dbh->do: $sql"], \$logger, $debug);
		
        if ( $rv and $replicationStatus ) {
          $sql = "UPDATE `$table` SET replicationStatus = 'R' $whereReplicationStatus";
          print "  + $sql\n" if ($debug);
          $dbhSOURCE->do ( $sql ) or $rv = DBI_error_trap(*EMAILREPORT, "Cannot $dbhSOURCE->do: $sql", \$logger, $debug);
        }
      }
    }

    $sthSOURCE->finish() or $rv = DBI_error_trap(*EMAILREPORT, "Cannot sthSOURCE->finish: $sqlSOURCE", \$logger, $debug);
  }

  print $EMAILREPORT "  ERROR: DBH/STH\n" unless ($debug or $rv);
  printDebugAll ("    OUT: importData <$PROGNAME v$version pid: <$pidfile>");
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub print_usage () {
  print "Usage: $PROGNAME [-T <CONFIG|DATA|ALL>] [-D <debug>] [-V version] [-h help]\n";
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub print_help () {
  print_revision($PROGNAME, $version);
  print "ASNMTAP Import Data Through Catalog for the '$APPLICATION'

-T, --type=<CONFIG|DATA|ALL> (default: current ALL)
-M, --mode=O|D
   O(nce)   : run the program once
   D(aemon) : run the program as a loop
-D, --debug=F|T|L
   F(alse)  : screendebugging off (default)
   T(true)  : normal screendebugging on



( run in 1.101 second using v1.01-cache-2.11-cpan-39bf76dae61 )