The line always begins with number 100 or number 200, follow the year, the day in the year, the hour, and other stuff
Here, the important fields are, 2, 3 and 4.
-Filed 2 --> year
-Field 3 --> the day of the year (date +%j)
-Filed 4 --> time, can be from 3 or 4 characters. From 0 to 9 AM it has 3 characters, from 10 to 23 it has 4 characters. Example: 910 - 9:10AM, 21:30 - 9:30PM
What I need?
To get the date and time and compare with the real time of the server, and if it differ from 5 min, it sends a mail to root.
I've been 3 days trying to do it without success. Can help me?
See, perl has inbuilt functions for date and time comparison. So, it would be better to use.
I have written a code for date and time comparison for you. This may help you.
#!/bin/perl
# Packages to import functions
use Date::DateCalc qw(:all);
use Date::Calc;
use FileHandle;
# Name of your i/p file
my $inpFile = "yourInp.txt";
# Open file for reading
open FF, "<$inpFile";
while (<FF>) {
chomp $_;
my $timeDiff;
# Get the server current date and time
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
$year += 1900;
$mon += 1;
# Get the current line's date and time
my ( $cYear, $cYday, $cTime ) = split(/\,/,$_);
my $hr_s;
my $min_s;
if ( $cTime < 1000 ) {
$hr_s=substr($cTime,0,1);
$min_s=substr($cTime,1,2);
} else {
$hr_s=substr($cTime,0,2);
$min_s=substr($cTime,2,2);
}
my $startTimeMin=$hr_s*60 + $min_s;
my $endTimeMin=$hour*60 + $min;
# Calculate date diff (server date - current line date)
# and calculate timediff only if it is greater then 0
my $ydayDiff = $yday - $cYday;
$timeDiff=$endTimeMin - $startTimeMin if ($ydayDiff > 0);
# Mail if timediff is greater then 5
my $cmd = "mailx -s \"subject\" mailid@server.com";
system("$cmd") if ($timeDiff > 5);
}
close FF;