#!/usr/bin/perl -w
# Copyright 2008 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

use strict;

sub PrintMemory
{
   return 1 if (!open(DATA, '/proc/meminfo'));

   while (<DATA>)
   {
      if (/(\S+):\s+(\d+)/)
      {
         print "$1 = $2\n";
      }
   }
   close(DATA);
   return 0;
}

sub PrintVMUsage
{
   my @vmstat_data;

   return 1 if (!open(DATA, '/usr/bin/vmstat|'));

   while (<DATA>)
   {
      next if (/[a-zA-Z]/);
      @vmstat_data = split;
      print "SwapInKBSec = $vmstat_data[6]\n";
      print "SwapOutKBSec = $vmstat_data[7]\n";
      print "UserTime = $vmstat_data[12]\n";
      print "KernelItme = $vmstat_data[13]\n";
      print "IdleTime = $vmstat_data[14]\n";
      print "IOWaitTime = $vmstat_data[15]\n";
      if (defined($vmstat_data[16]))
      {
         print "VMStolenTime = $vmstat_data[16]\n";
      }
   }
   close(DATA);
   return 0;
}

sub PrintLoad
{
   my @loadavg_data;

   return 1 if (!open(DATA, '/proc/loadavg'));

   while (<DATA>)
   {
      @loadavg_data = split;
      print "LoadAvg1Min = $loadavg_data[0]\n";
      print "LoadAvg5Min = $loadavg_data[1]\n";
      print "LoadAvg15Min = $loadavg_data[2]\n";
   }
   close(DATA);
   return 0;
}

sub PrintDisk
{
   my @disk_data;
   my $fs;

   return 1 if (!open(DATA, "df -P| grep -v Filesystem | grep -v '/dev/shm'|"));

   while (<DATA>)
   {
      @disk_data = split;
      $fs = $disk_data[5];
      if ($disk_data[5] eq "/")
      {
         $fs = "_slash";
      }
      else
      {
         $fs =~ s/\//_/g;
      }
      print "Filesystem${fs}_Free = $disk_data[3]\n";
   }
   close (DATA);
   return 0;
}

sub PrintLogData
{
   my $log_dir = `condor_config_val LOG`;
   chomp $log_dir;
   my %grep_data = ("ERROR" => "CondorLogCapitalError",
                    "error" => "CondorLogLowerError",
                    "DENIED" => "CondorLogCapitalDenied",
                    "denied" => "CondorLogLowerDenied",
                    "WARNING" => "CondorLogCapitalWarning",
                    "warning" => "CondorLogLowerWarning",
                    "Stack dump" => "CondorLogStackDump"
                   );
   my @dprintf_list = `find $log_dir -name dprintf_failure.*`;
   my $dprintf_cnt = @dprintf_list;
   my $list;
   my $log;
   my @grep_output;
   my $count;

   foreach my $keyword (keys(%grep_data))
   {
      $list = "";
      @grep_output = `grep "$keyword" $log_dir/*Log*`;
      $count = @grep_output;
      foreach my $line (@grep_output)
      {
         $line =~ /([^:]+):/;
         $log = `basename $1`;
         chomp $log;
         if (! ($list =~ /$log/))
         {
            $list = "$list$log,";
         }
      }
      chop $list;
      if ($list ne "")
      {
         print "$grep_data{$keyword} = \"$list\"\n";
         print "$grep_data{$keyword}Count = $count\n";
      }
   }

   $list = "";
   foreach my $line (@dprintf_list)
   {
      $log = `basename $line`;
      chomp $log;
      if (! ($list =~ /$log/))
      {
         $list = "$list$log,";
      }
   }
   chop $list;
   if ($list ne "")
   {
      print "CondorLogDPrintfs = \"$list\"\n";
      print "CondorLogDPrintfsCount = $dprintf_cnt\n";
   }
}

sub PrintCoreFiles()
{
   my $core_dir = `condor_config_val LOG`;

   chomp $core_dir;
   my @core_files = `find $core_dir -name core*`;
   my $core_cnt = @core_files;
   my $list = "";

   foreach my $core (@core_files)
   {
      chomp $core;
      $list = $list . "$core,";
   }
   chop $list;
   if ($list ne "")
   {
      print "CondorCoreFiles = \"$list\"\n";
      print "CondorCoreFilesCount = $core_cnt\n";
   }
}

sub PrintActivityTime()
{
   my $hostname = `hostname`;
   my @ad = `condor_status -l -direct $hostname`;
   my @lhf;
   my @eca;
   my $at = 0;

   foreach my $line (@ad)
   {
      if ($line =~ /LastHeardFrom /)
      {
         @lhf = split(/=/, $line);
      }
      elsif ($line =~ /EnteredCurrentActivity /)
      {
         @eca = split(/=/, $line);
      }
   }
   chomp $lhf[1];
   chomp $eca[1];
   $at = $lhf[1] - $eca[1];
   print "ActivityTime = $at\n";
}

PrintMemory();
PrintVMUsage();
PrintLoad();
PrintDisk();
PrintLogData();
PrintCoreFiles();
PrintActivityTime();

my $time = `date +%s`;
print "CurrTime = $time";
print "--\n";
