root/resmon

Revision b8d17f1fe46efac764a9f6b52b2e89a5695676fd, 4.5 kB (checked in by Mark Harrison <mark@omniti.com>, 9 years ago)

Resmon should run checks immediately after a reload and not wait for 60
seconds.

git-svn-id: https://labs.omniti.com/resmon/trunk@138 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 $status);
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     $status->purge($config);
71 }
72
73 my $sighup = 0;
74 sub sighup_handler { $sighup = 1; }
75 $SIG{'HUP'} = \&sighup_handler;
76
77 configure();
78
79 my $sigint = 0;
80 sub sigint_handler { $sigint = 1; }
81 $SIG{'INT'} = \&sigint_handler;
82
83 my $rmlast = undef;
84 sub wait_interval {
85   $rmlast = [gettimeofday] unless defined($rmlast);
86   my $elapsed = $config->{interval} - tv_interval($rmlast);
87   if($elapsed > 0) {
88     sleep($elapsed);
89   }
90   $rmlast = [gettimeofday];
91 }
92
93 unless($debug) {
94   fork && exit;
95   setsid;
96   close(STDIN);
97   close(STDOUT);
98   close(STDERR);
99   fork && exit;
100 }
101
102 my $list = [];
103 $status = Resmon::Status->new($config->{statusfile});
104 $status->open();
105 $status->serve_http_on($config->{interface}, $config->{port})
106   if($config->{port});
107
108 while(1) {
109   while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
110     my $coderef = undef;
111     eval { $coderef = Resmon::Module::fetch_monitor($module_name); };
112     foreach my $monobj (@$mod_configs) {
113       my $check_rv = 'BAD',
114       my $check_mess = 'no data';
115       my $starttime = [gettimeofday];
116       # Get old status if it hasn't expired
117       my ($check_rv, $check_mess) = Resmon::Module::fresh_status_msg($monobj);
118       # Otherwise, run the check
119       if (!$check_rv) {
120           if($coderef) {
121             eval { ($check_rv, $check_mess) = $coderef->($monobj); };
122           } else {
123             eval { ($check_rv, $check_mess) = $monobj->handler(); };
124           }
125           # Store the last status for use by fresh_status_msg later
126           # Also converts old style status messages
127           ($check_rv, $check_mess) =
128             Resmon::Module::set_status($monobj, $check_rv, $check_mess);
129       }
130       my $checkstat = $@;
131       my $confighash = {};
132       eval { $confighash = $monobj->config_as_hash(); };
133       my $results = {
134         configuration => $confighash,
135         last_runtime_seconds => sprintf("%.6f", tv_interval($starttime)),
136       };
137       if($checkstat) {
138         $results->{state} = 'BAD';
139         $results->{message} = "Bad module or problem running handler code.";
140       } else {
141         $results->{state} = $check_rv;
142         $results->{message} = $check_mess;
143       }
144       $status->store($module_name,$monobj->{'object'}, $results);
145     }
146   }
147   $status->close();
148   die "Exiting.\n" if($sigint);
149   if ($sighup) {
150     $sighup = 0;
151     reconfigure();
152   } else {
153     wait_interval();
154   }
155   die "Exiting.\n" if($sigint);
156   print "\n---- ".localtime(time)."----------\n"
157    unless $status->open();
158 }
159
Note: See TracBrowser for help on using the browser.