summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow (lxpbuetow) <paul.buetow@1und1.de>2015-01-22 18:00:56 +0100
committerPaul Buetow (lxpbuetow) <paul.buetow@1und1.de>2015-01-22 18:00:56 +0100
commitc13eac632e8bca4175bd71e9c7646cee6ece006c (patch)
tree45ef8754db6413b20aaa0828c6a56385361bf9bc
parent16e255ba87acc54c41d522b7c45d6a25c021d55e (diff)
add oldage and nocolor switches as well as ANSI color support
-rw-r--r--docs/guprecords.pod8
-rwxr-xr-xsrc/guprecords26
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,
};
}