From d9b99049633ad37646bfd23c4a8e073ef78e0580 Mon Sep 17 00:00:00 2001
From: Sergey Ivanov <seriv@cs.umd.edu>
Date: Mon, 27 Apr 2020 09:00:23 -0400
Subject: [PATCH] check_resmon_metric to match changes to check system time

---
 resources/check_resmon_metric | 36 ++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

diff --git a/resources/check_resmon_metric b/resources/check_resmon_metric
index 1171f1d..88ee30f 100755
--- a/resources/check_resmon_metric
+++ b/resources/check_resmon_metric
@@ -182,6 +182,7 @@ use Time::HiRes qw( gettimeofday tv_interval );
 use XML::Simple;
 use Getopt::Long;
 
+use lib "/usr/lib64/nagios/plugins";
 use utils qw($TIMEOUT %ERRORS &print_revision &support);
 
 delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
@@ -326,8 +327,8 @@ eval {
     die "error parsing XML\n" if($@);
 
     # Debugging
-    #use Data::Dumper;
-    #print Dumper($ref->{ResmonResult});
+    # use Data::Dumper;
+    # print Dumper($ref->{ResmonResult});
 
     my $idx;
     for ($idx=0; $idx < $#{$ref->{ResmonResult}}; $idx++) {
@@ -336,9 +337,21 @@ eval {
     }
 
     # If we have stale information, then go critical
-    my $last_update = time() - $ref->{ResmonResult}->[$idx]->{last_update}->[0];
-    die "Stale metrics. Last updated $last_update seconds ago"
-        if($age && $age < $last_update);
+    if($age) { 
+      my $last_update;
+      if ($module eq 'System::Time' && $check eq 'now') {
+        $last_update = Time::HiRes::time() -  $ref->{ResmonResult}->[$idx]->{metric}->{now}->{content};
+      } else {
+        $last_update = Time::HiRes::time() - $ref->{ResmonResult}->[$idx]->{last_update}->[0];
+      }
+      if ($age < abs($last_update)){
+        $state = "CRITICAL";
+      } else {
+        $state = "OK";
+      }
+      print "$state: updated in $last_update secs from now | diff=$last_update";
+      exit $ERRORS{$state};
+    }
 
     # Get the metrics
     my $metricval = $ref->{ResmonResult}->[$idx]->{metric}->{$metric};
@@ -350,7 +363,7 @@ eval {
         } else {
             $state = "CRITICAL";
         }
-        print "$state: Metric $metric is absent\n";
+        print "$state: Metric $metric is absent for module $module in check $check fetched by url $url\n";
         exit $ERRORS{$state};
     }
 
@@ -363,6 +376,15 @@ eval {
     die "Numeric threshold specified for a non-numeric metric"
         if ((defined $warning || defined $critical) && $type !~ /[0IlLni]/);
 
+    my $metrics=$ref->{ResmonResult}->[$idx]->{metric};
+    my $performance='';
+    foreach my $key ( sort keys %$metrics ) {
+      my $mtype= $metrics->{$key}{type} || "0";
+      if ($mtype ~~ /[0IlLni]/) {
+        $performance .= $key."=".$metrics->{$key}{content}.";";
+      }
+    }
+
 
     if (defined $equals) {
         if ($value eq $equals) {
@@ -388,7 +410,7 @@ eval {
     if (defined $warning || defined $critical) {
         my $message;
         ($state, $message) = check_threshold($value, $warning, $critical);
-        print "$state: $metric - $message\n";
+        print "$state: $metric - $message | ${performance} \[$value\]\n";
     }
 };
 
-- 
GitLab