ASNMTAP

 view release on metacpan or  search on metacpan

applications/collector.pl  view on Meta::CPAN


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

sub signalQUIT {
  printDebugAll ("kill -QUIT <$PROGNAME v$version -C $collectorlist> pid: <$pidfile>");
  printDebugAll ("           Wait until next timeslot");

  my ($prevSecs, $currSecs);
  $currSecs = int((localtime)[0]);

  do {
    sleep 1;
    $prevSecs = $currSecs;
    $currSecs = int((localtime)[0]);
  } until ($currSecs < $prevSecs);

  unlink $pidfile;
  printDebugAll ("           Done");
  $boolean_daemonQuit = 1;

  use Sys::Hostname;
  my $subject = "$prgtext\@". hostname() .": Config $APPLICATIONPATH/etc/$collectorlist successfully stopped at ". get_datetimeSignal();
  my $returnCode = sending_mail ( $SERVERLISTSMTP, $SENDEMAILTO, $SENDMAILFROM, $subject, $subject ."\n", 0 );
  print "Problem sending email to the '$APPLICATION' server administrators\n" unless ( $returnCode );

  exit 1;
}

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

sub signalHUP {
  printDebugAll ("kill -HUP <$PROGNAME v$version -C $collectorlist> pid: <$pidfile>");
  $boolean_signal_hup = 1;
}

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

sub write_pid {
  printDebugAll ("write PID <$PROGNAME v$version -C $collectorlist> pid: <$pidfile>");

  if (-e "$pidfile") {
    printDebugAll ("ERROR: couldn't create pid file <$pidfile> for <$PROGNAME v$version -C $collectorlist>");
    print "ERROR: couldn't create pid file <$pidfile> for <$PROGNAME v$version -C $collectorlist>\n";
    exit 0;
  } else {
    open(PID,">$pidfile") || die "Cannot open $pidfile!!\n";
    print PID $$;
    close(PID);
  }
}

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

sub create_dir {
  my ($directory) = @_;

  unless ( -e "$directory" ) { # create $directory
    my ($systemAction, $stdout, $stderr, $exit_value, $signal_num, $dumped_core);
    $systemAction = "mkdir $directory";
	
    if ($CAPTUREOUTPUT) {
      use IO::CaptureOutput qw(capture_exec);
     ($stdout, $stderr) = capture_exec("$systemAction");
    } else {
      system ("$systemAction"); $stdout = $stderr = '';
    }

    $exit_value  = $? >> 8;
    $signal_num  = $? & 127;
    $dumped_core = $? & 128;

    unless ( $exit_value == 0 && $signal_num == 0 && $dumped_core == 0 && $stderr eq '' ) {
      printDebugAll ("    create_dir ---- : mkdir $directory: <$exit_value><$signal_num><$dumped_core><$stderr>");
      printDebugNOK ("    create_dir ---- : mkdir $directory: <$exit_value><$signal_num><$dumped_core><$stderr>");
    }
  }
}

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

sub call_system {
  my ($asnmtapEnv, $currentDate, $catalogID_uniqueKey, $catalogID, $uniqueKey, $resultsdir, $title, $system_action, $status, $interval, $instability, $persistent, $downtime, $debug, $dbiFilename, $logging, $httpdump, $dumphttp, $queryMySQL) = @_;

  my $rvOpen;
  my $action = '';
  my $httpdumpFilename    = '';
  my $httpdumpFilenameTmp = '';
  my $debugFilename       = '<NIHIL>';
  my $dumphttpRename      = '<NIHIL>';
  my ($stdout, $stderr, $exit_value, $signal_num, $dumped_core);

  my ($loggedStatus, $returnStatus, $startDate, $startTime, $endDate, $endTime, $msgCommand);
  $startDate = get_csvfiledate();
  $startTime = get_csvfiletime();
  ($msgCommand, undef) = split(/\.pl/, $system_action);

  if ($dumphttp ne 'N') {
    $httpdumpFilename = $httpdump . get_datetime() .'-'. $msgCommand .'-'. $catalogID_uniqueKey;
    $httpdumpFilenameTmp = $httpdumpFilename .'.tmp';
  }

  $boolean_signal_kill = 0;

  if (-e "$PLUGINPATH/$msgCommand.pl") {
    my $systemAction = "cd $PLUGINPATH; ./$system_action --status=$status --debug=$debug --logging=$logging --asnmtapEnv='$asnmtapEnv'";
    $systemAction .= " --dumphttp=$httpdumpFilenameTmp" if ($dumphttp ne 'N');

    my $_handler = $SIG{ALRM};
    my $alarm = $interval * 60;
    $SIG{ALRM} = sub { $CHILD_OFF = $alarm; warn "ASNMTAP::Asnmtap::Applications::Collector::CHILD_OFF = $alarm\n" };
    alarm ( $alarm );

    if ($CAPTUREOUTPUT) {
      use IO::CaptureOutput qw(capture_exec);
      ($stdout, $stderr) = capture_exec("$systemAction");
      my (@returnStatus) = split (/\n/, $stdout);
      $returnStatus = $returnStatus[-1];
    } else {
      system ("$systemAction"); $stdout = $stderr = '';
    }

    $exit_value  = $? >> 8;
    $signal_num  = $? & 127;
    $dumped_core = $? & 128;
  # print "$CAPTUREOUTPUT -> <$returnStatus> < $stdout >< $stderr >< $exit_value >< $signal_num >< $dumped_core >\n";

    alarm (0);
    $CHILD_OFF = 0;
    $SIG{ALRM} = $_handler ? $_handler : 'DEFAULT';
  } else {
    $exit_value  = -1;
    $signal_num  = 1;
    $dumped_core = 0;
    $stdout = $stderr = '';
  }

  if ($exit_value >= 0 && $exit_value <= 4 && $signal_num == 0 && $dumped_core == 0) {
    $action = "Success";

    if ($exit_value == 0) {
      $dumphttpRename = "OK";
      printDebugAll ("    OK ------------ : <$PROGNAME v$version -C $collectorlist>");
    } elsif ($exit_value == 1) {
      $dumphttpRename = "WARNING";
      printDebugAll ("    WARNING ------- : <$PROGNAME v$version -C $collectorlist>");
      printDebugNOK ("    WARNING ------- : $system_action");
    } elsif ($exit_value == 2) {
      $dumphttpRename = "CRITICAL";
      printDebugAll ("    CRITICAL ------ : <$PROGNAME v$version -C $collectorlist>");
      printDebugNOK ("    CRITICAL ------ : $system_action");
    } elsif ($exit_value == 3) {
      $dumphttpRename = "UNKNOWN";
      printDebugAll ("    UNKNOWN ------- : <$PROGNAME v$version -C $collectorlist>");
      printDebugNOK ("    UNKNOWN ------- : $system_action");
    } elsif ($exit_value == 4) {
      $dumphttpRename = "DEPENDENT";
      printDebugAll ("    DEPENDENT ----- : <$PROGNAME v$version -C $collectorlist>");
      printDebugNOK ("    DEPENDENT ----- : $system_action");
    }

    if ($dumphttp ne 'N') {
      my $httpdumpFilenameTmpKnownError = $httpdumpFilenameTmp .'-KnownError';
      unlink ($httpdumpFilenameTmpKnownError) if (-e "$httpdumpFilenameTmpKnownError");

      if ($dumphttp eq 'A' || (($dumphttp eq 'W' or $dumphttp eq 'C' or $dumphttp eq 'U') && $exit_value > 0)) {
        if (-e "$httpdumpFilenameTmp") {
          $debugFilename = $httpdumpFilename .'-'. $dumphttpRename .'.htm';
          rename("$httpdumpFilenameTmp", "$debugFilename");
        }
      } else {
        unlink ($httpdumpFilenameTmp) if (-e "$httpdumpFilenameTmp");
      }
    }
  } else {
    $action = 'Failed';
    $dumphttpRename = 'UNKNOWN';
    printDebugAll ("    call_system --- : $system_action: <$exit_value><$signal_num><$dumped_core><$stderr>");
    printDebugNOK ("    call_system --- : $system_action: <$exit_value><$signal_num><$dumped_core><$stderr>");

    if ( $exit_value == -1 ) {
      $returnStatus = "$dumphttpRename - $title: PLUGIN '$msgCommand.pl' doesn't exist - contact administrators";
    } elsif ( $boolean_signal_kill ) {
      $returnStatus = "$dumphttpRename - $title: TIMING OUT SLOW PLUGIN";

      my $httpdumpFilenameTmpKnownError = $httpdumpFilenameTmp .'-KnownError';
      unlink ($httpdumpFilenameTmpKnownError) if (-e "$httpdumpFilenameTmpKnownError");
      unlink ($httpdumpFilenameTmp) if (-e "$httpdumpFilenameTmp");
    } else {
      $returnStatus = "$dumphttpRename - $title: ERROR NOT DEFINED - contact server administrators";
    }
  }

  $endDate = get_csvfiledate();
  $endTime = get_csvfiletime();

  unless ( $CAPTUREOUTPUT ) {
    $loggedStatus = ( $dumphttp ne 'N' ) ? $httpdumpFilenameTmp : $logging;
    $loggedStatus .= '-status.txt';
    $returnStatus = "<NIHIL> - $title: $loggedStatus";

    if (-e "$loggedStatus") {
      unless ( $boolean_signal_kill ) {
        $rvOpen = open(DEBUG, "$loggedStatus");

        if ($rvOpen) {
          while (<DEBUG>) {
	        chomp;
            $returnStatus = $_;
          }
	
          close(DEBUG);
        } else {
          $dumphttpRename = 'UNKNOWN';
          $returnStatus = "$dumphttpRename - $title: Cannot open $loggedStatus to retrieve debug information - contact server administrators";
        }
      }

      unlink ($loggedStatus);
    } else {
      $dumphttpRename = 'UNKNOWN';
      $returnStatus = "$dumphttpRename - $title: $loggedStatus doesn't exist - contact server administrators";
    }
  }

  my ($duration) = $returnStatus =~ m/Trendline=([0-9.]+)s;[0-9.]+;;;$/i;

  if (defined $duration) {
    my ($thour, $tmin, $tsec);
    $thour = int ($duration / 3600);
    $tmin  = int (int ($duration % 3600) / 60);
    $tsec  = int ($duration % 60);
    $duration = sprintf("%02d:%02d:%02d", $thour, $tmin, $tsec);
  } else {
    my ($tyear, $tmonth, $tday, $thour, $tmin, $tsec, @startDateTime, @endDateTime, @diffDateTime);

    ($tyear, $tmonth, $tday) = split(/\//, $startDate);
    ($thour, $tmin, $tsec)   = split(/\:/, $startTime);
    @startDateTime = ($tyear, $tmonth, $tday, $thour, $tmin, $tsec);

    ($tyear, $tmonth, $tday) = split(/\//, $endDate);
    ($thour, $tmin, $tsec)   = split(/\:/, $endTime);
    @endDateTime = ($tyear, $tmonth, $tday, $thour, $tmin, $tsec);

    @diffDateTime = Delta_DHMS(@startDateTime, @endDateTime);
    $duration = sprintf("%02d:%02d:%02d", $diffDateTime[1], $diffDateTime[2], $diffDateTime[3]);
  }

# my ($outputData, $performanceData) = split(/\|/, $returnStatus, 2);
  my $_returnStatus = reverse $returnStatus;
  my ($_outputData, $_performanceData) = reverse split(/\|/, $_returnStatus, 2);
  my $outputData = reverse $_outputData;
  my $performanceData = reverse $_performanceData;

  $rvOpen = open(CSV,">>$logging-$msgCommand-$catalogID_uniqueKey-csv.txt");

  if ($rvOpen) {
    print CSV '"', $catalogID, '","","', $uniqueKey, '","I","', $system_action, '","', $title, '","', $dumphttpRename, '","', $startDate, '","', $startTime, '","', $endDate, '","', $endTime, '","', $duration, '","', $outputData, '","', $performanceDa...
    close(CSV);
  } else {
    print "Cannot open $logging-$msgCommand-$catalogID_uniqueKey-csv.txt to print debug information\n";
  }

  if ( $boolean_perfParseInstalled ) {
    if (defined $performanceData) {
      my $perfParseTimeslot = get_timeslot ($currentDate);

      my $perfParseCommand;
      my $environment = (($system_action =~ /\-\-environment=([PASTDL])/) ? $1 : 'P');
      my $eTitle = $title .' ('. $ENVIRONMENT{$environment} .')' if (defined $environment);
	    $eTitle .= ' from '. $catalogID;

      if ( $perfParseMethode eq 'PULP' ) {
        $perfParseCommand = "$APPLICATIONPATH/sbin/perfparse_asnmtap_pulp_command.pl $PREFIXPATH/log/perfdata-asnmtap.log \"$perfParseTimeslot\t$eTitle\t$catalogID_uniqueKey\t$outputData\t$dumphttpRename\t$performanceData\"";
      } else {
        $perfParseCommand = "printf \"%b\" \"$perfParseTimeslot\t$eTitle\t$catalogID_uniqueKey\t$outputData\t$dumphttpRename\t$performanceData\n\" | $PERFPARSEBIN/perfparse-log2mysql -c $PERFPARSEETC/$PERFPARSECONFIG";
      }

      if ($CAPTUREOUTPUT) {
        use IO::CaptureOutput qw(capture_exec);
        ($stdout, $stderr) = capture_exec("$perfParseCommand");
      } else {
        system ("$perfParseCommand"); $stdout = $stderr = '';
      }

      $exit_value  = $? >> 8;
      $signal_num  = $? & 127;
      $dumped_core = $? & 128;
      printDebugNOK ("    perfParse ----- : $perfParseCommand: <$exit_value><$signal_num><$dumped_core><$stderr>") unless ( $exit_value == 0 && $signal_num == 0 && $dumped_core == 0 && $stderr eq '' );
    }
  }

  insertEntryDBI ($currentDate, $catalogID_uniqueKey, $catalogID, $uniqueKey, $title, $dbiFilename.$msgCommand.'-'.$catalogID_uniqueKey.'-sql', $system_action, $interval, $dumphttpRename, $logging, $debug, $startDate, $startTime, $endDate, $endTime, ...
  return $action;
}

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

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

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

    my $date = scalar(localtime());
    my $tlogging = $logging . 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 . 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";
    }



( run in 3.733 seconds using v1.01-cache-2.11-cpan-d8267643d1d )