0

I am new in perl struggling to build my 1st script but, the partition module doesn't work. More details are commented in the code. Maybe the code should be rewritten using hash and reference but I have no idea how to do it. Can someone please, help me?

#!/usr/bin/perl -w

#This script compares the today's result and yesterday's results between partitions for each host.
#The "df" output is stored in a CSV file. If any partitions have been changed (mounted/unmounted)
#the script must to warn and don't compare the results for that host.

use warnings;

my $file = "/tmp/df.csv";
my $host = `hostname | awk -F'.' '{print \$1}'`;
my $yesterdays_date = `date -d "-1 day" '+%d.%m.%Y'`;
my $todays_date = `date '+%d.%m.%Y'`;
chomp ($host, $yesterdays_date, $todays_date);

open(HANDLE, "$file") or die "Error opening file $file: $!";
my @array = <HANDLE>;

foreach (@array){
     @columns = split /,/;

        if(/$host/ and /$todays_date/){
            my $todays_result = $columns[5];chomp;
            #my $todays_partition = $columns[6];chomp;

            print "Today\'s disk usage on $host: $columns[5]\n";
            #print "$todays_result <<T.Result      T.Partition>> $todays_partition"
        }

        elsif(/$host/ and /$yesterdays_date/){
            my $yesterdays_result = $columns[5];chomp;
            #my $yesterdays_partition = $columns[6];chomp;
 print "Yesterday\'s disk usage on $host: $columns[5]\n";
            #print "$yesterdays_result <<Y.Result     Y.Partition>>  $yesterdays_partition";
        }

        #Debug: Print differences in mount point (condition must be "ne" instead eq)
        #if ($todays_partition eq $yesterdays_partition){
        #print "$todays_partition <<Partition equal>> $yesterdays_partition";
        #}

        #else{
            #print "Debug: Host or Date DIFFERENT or NOT FOUND for today and yesterday\n";
        #}

        #TO DO: print "The diference: $todays_result-$yesterdays_result", "\n";
};

close HANDLE;

The CSV file contains the following lines:

testhost,25.08.2018,100M,0,100M,0,/run/user/0
localhost,01.09.2018,6.7G,1.5G,5.2G,23,/
localhost,01.09.2018,485M,0,485M,0,/dev
localhost,01.09.2018,496M,4.0K,496M,1,/dev/shm
localhost,01.09.2018,496M,6.7M,490M,2,/run
localhost,01.09.2018,496M,0,496M,0,/sys/fs/cgroup
localhost,01.09.2018,497M,110M,387M,23,/boot
localhost,01.09.2018,100M,0,100M,0,/run/user/0
localhost,02.09.2018,6.7G,1.5G,5.2G,23,/
localhost,02.09.2018,485M,0,485M,0,/dev
localhost,02.09.2018,496M,4.0K,496M,1,/dev/shm
localhost,02.09.2018,496M,6.7M,490M,2,/run
localhost,02.09.2018,496M,0,496M,0,/sys/fs/cgroup
localhost,02.09.2018,497M,110M,387M,23,/boot
localhost,02.09.2018,100M,0,100M,0,/run/user/0

Bonus: Help with English grammar :D

jtech
  • 9
  • 3
  • 1
    What "_partition module_" do you have in mind? – zdim Sep 03 '18 at 00:56
  • The partition module is supposed to verify each mounted partition line from yesterday to today and in the future use this result to calculate how much the partition has grown up in a day – jtech Sep 03 '18 at 08:34
  • OK, so it is what you are writing. I thought that it may have referred to some CPAN module or such. – zdim Sep 03 '18 at 18:31

1 Answers1

2

You need information from multiple lines, so you will need some variables outside of the loop.

If the records in the CSV are ordered chronologically, you can use the following:

use strict;
use warnings;

use DateTime      qw( );
use Sys::Hostname qw( hostname );
use Text::CSV_XS  qw( );

my $qfn = "/tmp/df.csv";
open(my $fh, '<', $qfn)
   or die("Can't open \"$qfn\": $!\n");

my $target_host = hostname =~ s/\..*//rs;   # /

my $today_dt = DateTime->now( time_zone => "local" )->set_time_zone("floating")->truncate( to => "day" );
my $yday_dt  = $today_dt->clone->subtract( days => 1 );

my $today = $today_dt->strftime("%d.%m.%Y");
my $yday  = $yday_dt ->strftime("%d.%m.%Y");

my $csv = Text::CSV_XS->new({ auto_diag => 2, binary => 1 });

my %yday_usage_by_partition;   
while ( my $row = $csv->getline($fh) ) {
   my ($host, $date, $partition, $usage) = @$row[0,1,6,5];
   next if $host ne $target_host;

   if ($date eq $yday) {
      $yday_usage_by_partition{$partition} = $usage;
   }
   elsif ($date eq $today) {
      if (!exists($yday_usage_by_partition{$partition})) {
         warn("No data for $yday for partition $partition\n");
         next;
      }

      print("$partition: $$yday_usage_by_partition{$partition} -> $usage\n");
   }
}
ikegami
  • 367,544
  • 15
  • 269
  • 518