From c13eac632e8bca4175bd71e9c7646cee6ece006c Mon Sep 17 00:00:00 2001 From: "Paul Buetow (lxpbuetow)" Date: Thu, 22 Jan 2015 18:00:56 +0100 Subject: add oldage and nocolor switches as well as ANSI color support --- docs/guprecords.pod | 8 ++++++++ src/guprecords | 26 +++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/docs/guprecords.pod b/docs/guprecords.pod index d060e11..07e94fa 100644 --- a/docs/guprecords.pod +++ b/docs/guprecords.pod @@ -20,6 +20,10 @@ Shows every individual uptime of all hosts. Show i num of entries. Default is 23. +=item --nocolor + +Don't display with ANSI colors + =item --nofqdn Don't display the FQDNs @@ -32,6 +36,10 @@ Shows the help Read all the *.records files from dir s. Default is ./ +=item --oldage=i + +The number of days until a host is defined as old. + =item --total Aggregates a total uptime for every single host. diff --git a/src/guprecords b/src/guprecords index b7d298f..882d6f0 100755 --- a/src/guprecords +++ b/src/guprecords @@ -1,5 +1,5 @@ #!/usr/bin/env perl -# guprecords (c) 2014, Paul Buetow +# guprecords (c) 2014, 2015 Paul Buetow # E-Mail: guprecords@mx.buetow.org WWW: http://guprecords.buetow.org use strict; @@ -8,9 +8,11 @@ use v5.14; use Getopt::Long; use POSIX qw(strftime); +use Term::ANSIColor; our $VERSION = 'VERSION_DEVEL'; our $FLAG = 0; +our $NOW = time(); our %ARGS = ( all => 0, @@ -18,6 +20,8 @@ our %ARGS = ( nofqdn => 0, reverse => 0, total => 0, + nocolor => 0, + 'oldage=i' => 180, 'count=i' => 23, 'indir=s' => '.', ); @@ -70,12 +74,16 @@ sub out(\@;$) { my $name = $ARGS{nofqdn} ? $_->{hostname} : $_->{fqdn}; + print color 'bold' if !$ARGS{nocolor} and $_->{is_old}; + printf "%3d | %17s | %20s | %13s | %24s\n", $count, trimlen($name,17), trimlen($_->{kernel},20), uptime($_->{uptime}), (defined $show_bt ? ''.localtime($_->{bootime}) : ''); + + print color 'reset' if !$ARGS{nocolor} and $_->{is_old}; } do { unless ($ARGS{reverse}) { @@ -86,6 +94,14 @@ sub out(\@;$) { }; } +sub is_old($) { + my $time = shift; + # Count per day + my $oldage = 86400 * $ARGS{'oldage=i'}; + + return $NOW - $time < $oldage ? 1 : 0; +} + sub all() { my @records; @@ -106,6 +122,7 @@ sub all() { hostname => $hostname, kernel => $kernel, uptime => $uptime, + is_old => is_old $uptime + $boot, }; } @@ -125,7 +142,7 @@ sub total() { my $uptime_ = 0; my $kernel_; - my $highest = 0; + my ($highest, $newest) = (0, 0); open my $fh, $file or die "$file: $!\n"; while (<$fh>) { @@ -137,7 +154,9 @@ sub total() { if ($highest < $uptime) { $highest = $uptime; $kernel_ = $kernel; - } + } + + $newest = $uptime + $boot if $newest < $uptime + $boot; } close $file; @@ -145,6 +164,7 @@ sub total() { uptime => $uptime_, kernel => $kernel_, fqdn => $fqdn, + is_old => is_old $newest, }; } -- cgit v1.2.3