root/resmon

Revision 7b9e8bc8986e3f6e1e20051d23be0f664abb46dd, 3.9 kB (checked in by Mark Harrison <mark@omniti.com>, 6 years ago)

Several fixes:

  • Broken modules on startup will not cause resmon to quit, but will be
    added to the failed modules list and have a status shown as BAD.
  • Broken modules on startup will no longer break the XML (because there is
    no config_as_hash to generate the configuration from)
  • Reloading modules that were initially broken (either from startup or
    from adding them to the config file and reloading) now works correctly.

git-svn-id: https://labs.omniti.com/resmon/trunk@108 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2
3 BEGIN {
4   (my $dir = $0) =~ s/\/?[^\/]+$//;
5   eval "use lib '$dir/lib';";
6   die $@ if($@);
7 };
8
9 use strict;
10 use Time::HiRes qw( gettimeofday tv_interval sleep );
11 use POSIX qw( setsid );
12 use Getopt::Long;
13 use Data::Dumper;
14 use vars qw($config_file $debug $status_file $interface $port $config);
15
16 use Resmon::Config;
17 use Resmon::ExtComm;
18 use Resmon::Status;
19 use Resmon::Module;
20
21 GetOptions(
22   "i=s" => \$interface,
23   "p=i" => \$port,
24   "c=s" => \$config_file,
25   "d"   => \$debug,
26   "f=s" => \$status_file,
27 );
28
29 $config_file ||= "$0.conf";
30 die "Cannot open configuration file: $config_file" unless (-r $config_file);
31
32 sub configure {
33   $config = Resmon::Config->new($config_file);
34   $config->{statusfile} = $status_file if($status_file);
35   $config->{port} = $port if($port);
36   $config->{interface} = $interface if($interface);
37 }
38
39 sub reconfigure {
40     my $modstatus = "";
41     print STDERR "Reloading modules...\n";
42     my $modules = $config->{Module};
43     while ( my ($key, $value) = each(%$modules) ) {
44         my $mod = $value->[0]; # Only need the first of each module
45         # Called this way rather than $mod->reload_module() in order to deal
46         # with modules that failed to load on startup and won't have a
47         # reload_module method.
48         my $errs = Resmon::Module::reload_module($mod);
49         if ($errs) {
50             my $modname = ref($mod) || $mod;
51             $modname =~ s/Resmon::Module:://;
52             $modstatus .= "$modname ";
53             print STDERR " Failed to reload module $modname\n";
54             print STDERR $errs;
55             print STDERR " This module is no longer available.\n";
56         }
57     }
58     print STDERR "Reloading configuration...\n";
59     eval { configure(); };
60     if ($@) {
61         # The config object is recreated every time we reload, so we shouldn't
62         # need to reset this BAD value to empty on a successful load.
63         $config->{'configstatus'} = "BAD";
64         print STDERR " Failed to reload: ";
65         print STDERR $@;
66         print STDERR " Continuing with old configuration\n";
67     }
68
69     $config->{'modstatus'} = $modstatus;
70 }
71
72 $SIG{'HUP'} = \&reconfigure;
73 configure();
74
75 my $sigint = 0;
76 sub sigint_handler { $sigint = 1; }
77 $SIG{'INT'} = \&sigint_handler;
78
79 my $rmlast = undef;
80 sub wait_interval {
81   $rmlast = [gettimeofday] unless defined($rmlast);
82   my $elapsed = $config->{interval} - tv_interval($rmlast);
83   if($elapsed > 0) {
84     sleep($elapsed);
85   }
86   $rmlast = [gettimeofday];
87 }
88
89 unless($debug) {
90   fork && exit;
91   setsid;
92   close(STDIN);
93   close(STDOUT);
94   close(STDERR);
95   fork && exit;
96 }
97
98 my $list = [];
99 my $status = Resmon::Status->new($config->{statusfile});
100 $status->open();
101 $status->serve_http_on($config->{interface}, $config->{port})
102   if($config->{port});
103
104 while(1) {
105   while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
106     my $coderef = undef;
107     eval { $coderef = Resmon::Module::fetch_monitor($module_name); };
108     foreach my $monobj (@$mod_configs) {
109       my $check_rv = 'BAD',
110       my $check_mess = 'no data';
111       my $starttime = [gettimeofday];
112       if($coderef) {
113         eval { ($check_rv, $check_mess) = $coderef->($monobj); };
114       } else {
115         eval { ($check_rv, $check_mess) = $monobj->handler(); };
116       }
117       my $checkstat = $@;
118       my $confighash = {};
119       eval { $confighash = $monobj->config_as_hash(); };
120       my $results = {
121         configuration => $confighash,
122         last_runtime_seconds => sprintf("%.6f", tv_interval($starttime)),
123       };
124       if($checkstat) {
125         $results->{state} = 'BAD';
126         $results->{message} = "Bad module or problem running handler code.";
127       } else {
128         $results->{state} = $check_rv;
129         $results->{message} = $check_mess;
130       }
131       $status->store($module_name,$monobj->{'object'}, $results);
132     }
133   }
134   $status->close();
135   die "Exiting.\n" if($sigint);
136   wait_interval();
137   die "Exiting.\n" if($sigint);
138   print "\n---- ".localtime(time)."----------\n"
139    unless $status->open();
140 }
141
Note: See TracBrowser for help on using the browser.