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