CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Epoc time vs today date

 



ab
Novice

Jun 19, 2013, 1:15 AM

Post #1 of 7 (923 views)
Epoc time vs today date Can't Post

Hi All

I am in need of some help

I have the fuction that have written that get some information from a JSON


Code
 
for my $w (@$decoded_json) {



if ($w->{status} == 2){
printf "%-10s %-25s %-25s %-25s", "CRITICAL:", $w->{client}, $w->{check}, $w->{issued} ;
print "\n";
}

if ($w->{status} == 1){
printf "%-10s %-25s %-25s %-25s", "WARNING:", $w->{client}, $w->{check}, $w->{issued} ;
print "\n";
}
}


which works like a charm and turns this


Code
WARNING:   hostname          disk_usage                1371629580

the last collum is the date in epoch time, i am stuck on how i can get the difference between that time and todays date and then display the difference in normal format instead

Thanks


BillKSmith
Veteran

Jun 19, 2013, 3:43 AM

Post #2 of 7 (918 views)
Re: [ab] Epoc time vs today date [In reply to] Can't Post

The answer is hidden in the FAQ. Use perldoc -q time. The answer for a filestamp will work for you because filestamps are in epoch time.
Good Luck,
Bill


ab
Novice

Jun 19, 2013, 4:09 AM

Post #3 of 7 (917 views)
Re: [BillKSmith] Epoc time vs today date [In reply to] Can't Post

Thanks bill will check it out


Laurent_R
Veteran / Moderator

Jun 19, 2013, 4:29 AM

Post #4 of 7 (915 views)
Re: [ab] Epoc time vs today date [In reply to] Can't Post

To get the current time stamp, you may try this one-liner:


Code
$ perl -e 'print scalar time' 
1371641171



FishMonger
Veteran / Moderator

Jun 19, 2013, 6:48 AM

Post #5 of 7 (910 views)
Re: [ab] Epoc time vs today date [In reply to] Can't Post

To display a formatted date string, my first choice would be to use the strftime function from the POSIX module.

Example:

Code
my $datestamp = strftime('%A, %B %d, %Y', localtime(1371648320) );

http://www.lehman.cuny.edu/cgi-bin/man-cgi?strftime+3

I'd also suggest/recommend that you refactor your if blocks to remove the duplication.


Code
for my $w (@$decoded_json) { 

my $level = $w->{status} == 1 ? "WARNING:" : "CRITICAL:";

printf "%-10s %-25s %-25s %-25s\n", $level, $w->{client}, $w->{check}, $w->{issued} ;
}


If you have additional levels such as "INFO" and "DEBUG", then I'd use an array or hash lookup table.

Code
my %level = ( 
0 => 'INFO',
1 => 'WARNING',
2 => 'CRITICAL',
3 => 'DEBUG',
);

for my $w (@$decoded_json) {
printf "%-10s %-25s %-25s %-25s\n", $level{$w->{status}}, $w->{client}, $w->{check}, $w->{issued} ;
}



ab
Novice

Jun 19, 2013, 9:04 AM

Post #6 of 7 (904 views)
Re: [FishMonger] Epoc time vs today date [In reply to] Can't Post

so i have changed by code some what


Code
 
sub output {
my $status_level = shift ;
my $event = shift;
printf "%-10s %-25s %-25s %-25s \n", "$status_level :", $event->{client}, $event->{check}, "Occurrences: " . $event->{occurrences} ;
}


for my $w ( @$decoded_json) {
sort ($hash{$w}->{check} cmp $hash{$w}->{check})
if ($w->{status} == 2){
output("CRITICAL", $w)
}
if ($w->{status} == 1){
output("WARNING", $w)
}
}

which returns some nice output


Code
 
WARNING : es1..com. disk_usage Occurrences: 1017
CRITICAL : laint..com. disk_usage Occurrences: 1241
CRITICAL : old..com disk_usage Occurrences: 267
CRITICAL : tin.com. disk_usage Occurrences: 92


but i want a way to sort it so that the critical one always appear before the warnings, it been driving my crazy


FishMonger
Veteran / Moderator

Jun 19, 2013, 9:33 AM

Post #7 of 7 (897 views)
Re: [ab] Epoc time vs today date [In reply to] Can't Post

Assuming you're not working with a large dataset that could use up all of your memory, the "output" sub could store the data in a HoA (hash of arrays) using the status as the keys. Then you'd sort/output the hash once the entire @$decoded_json array has been processed.

Or, you could simply sort @$decoded_json on the status field before passing the data to the output sub.

http://stackoverflow.com/questions/1640430/how-do-i-sort-an-array-of-hash-references-by-one-of-the-hash-values

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives