From 1533dda22b2e7854c9d23a7dc3df5b57e838e66f Mon Sep 17 00:00:00 2001
From: Mark Harrison <mark@omniti.com>
Date: Wed, 14 May 2008 18:53:52 +0000
Subject: [PATCH] Resmon no longer dies on config file errors on reload. Also
 show a better error message when starting with a faulty module.

git-svn-id: https://labs.omniti.com/resmon/trunk@88 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55
---
 lib/Resmon/Config.pm | 13 +++++++++++++
 resmon               | 17 ++++++++++++++---
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/lib/Resmon/Config.pm b/lib/Resmon/Config.pm
index a15f323..9798da9 100755
--- a/lib/Resmon/Config.pm
+++ b/lib/Resmon/Config.pm
@@ -25,6 +25,19 @@ sub new {
 	grep { $kvs{$1} = $2 if /^\s*(\S+)\s*=>\s*(\S+)\s*$/ } @params;
         my $object = bless \%kvs, "Resmon::Module::$current";
         push(@{$self->{Module}->{$current}}, $object);
+
+        # Test to make sure the module actually works
+        my $coderef;
+        eval { $coderef = Resmon::Module::fetch_monitor($current); };
+        if (!$coderef) {
+                # Try to execute the config_as_hash method. If it fails, then
+                # the module didn't load properly (e.g. syntax error).
+                eval { $object->config_as_hash; };
+                die "Problem loading module $current" if $@;
+        } else {
+            print STDERR "moo";
+        }
+
       } elsif (/^\s*\}\s*$/) {
 	$current = undef;
       } else {
diff --git a/resmon b/resmon
index 4d9b50a..c8f9244 100755
--- a/resmon
+++ b/resmon
@@ -36,7 +36,17 @@ sub configure {
   $config->{interface} = $interface if($interface);
 }
 
-$SIG{'HUP'} = \&configure;
+sub reconfigure {
+    print STDERR "Reloading...\n";
+    eval { configure(); };
+    if ($@) {
+        print STDERR " Failed to reload: ";
+        print STDERR $@;
+        print STDERR " Continuing with old configuration\n";
+    }
+}
+
+$SIG{'HUP'} = \&reconfigure;
 configure();
 
 my $sigint = 0;
@@ -81,13 +91,14 @@ while(1) {
       } else {
         eval { ($check_rv, $check_mess) = $monobj->handler(); };
       }
+      my $checkstat = $@;
       my $results = {
         configuration => eval { $monobj->config_as_hash(); },
         last_runtime_seconds => sprintf("%.6f", tv_interval($starttime)),
       };
-      if($@) {
+      if($checkstat) {
         $results->{state} = 'BAD';
-        $results->{message} = $@;
+        $results->{message} = $checkstat;
       } else {
         $results->{state} = $check_rv;
         $results->{message} = $check_mess;
-- 
GitLab