root/resmon

Revision d606430da3834dc6eb565442c535f6c0603b474f, 5.1 kB (checked in by Mark Harrison <mark@omniti.com>, 5 years ago)

Fix to sys_wait_h import to work with different perl versions

git-svn-id: https://labs.omniti.com/resmon/trunk@190 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( :sys_wait_h setsid );
12 use Getopt::Long;
13 use Data::Dumper;
14 use vars qw($config_file $debug $status_file $interface $port $config
15             $status $update);
16
17 use Resmon::Config;
18 use Resmon::ExtComm;
19 use Resmon::Status;
20 use Resmon::Module;
21
22 GetOptions(
23   "i=s" => \$interface,
24   "p=i" => \$port,
25   "c=s" => \$config_file,
26   "d"   => \$debug,
27   "f=s" => \$status_file,
28   "u"   => \$update,
29 );
30
31 if ($update) {
32     use Resmon::Updater;
33     (my $resmondir = $0) =~ s/\/?[^\/]+$//;
34     exit(Resmon::Updater::update($debug, $resmondir));
35 }
36
37 $config_file ||= "$0.conf";
38 die "Cannot open configuration file: $config_file" unless (-r $config_file);
39
40 sub configure {
41   $config = Resmon::Config->new($config_file);
42   $config->{statusfile} = $status_file if($status_file);
43   $config->{port} = $port if($port);
44   $config->{interface} = $interface if($interface);
45 }
46
47 sub reconfigure {
48     my $modstatus = "";
49     print STDERR "Reloading modules...\n";
50     my $modules = $config->{Module};
51     while ( my ($key, $value) = each(%$modules) ) {
52         my $mod = $value->[0]; # Only need the first of each module
53         # Called this way rather than $mod->reload_module() in order to deal
54         # with modules that failed to load on startup and won't have a
55         # reload_module method.
56         my $errs = Resmon::Module::reload_module($mod);
57         if ($errs) {
58             my $modname = ref($mod) || $mod;
59             $modname =~ s/Resmon::Module:://;
60             $modstatus .= "$modname ";
61             print STDERR " Failed to reload module $modname\n";
62             print STDERR $errs;
63             print STDERR " This module is no longer available.\n";
64         }
65     }
66     print STDERR "Reloading configuration...\n";
67     eval { configure(); };
68     if ($@) {
69         # The config object is recreated every time we reload, so we shouldn't
70         # need to reset this BAD value to empty on a successful load.
71         $config->{'configstatus'} = "BAD";
72         print STDERR " Failed to reload: ";
73         print STDERR $@;
74         print STDERR " Continuing with old configuration\n";
75     }
76
77     $config->{'modstatus'} = $modstatus;
78     $status->purge($config);
79 }
80
81 my $sighup = 0;
82 sub sighup_handler { $sighup = 1; }
83 $SIG{'HUP'} = \&sighup_handler;
84
85 configure();
86
87 my $sigint = 0;
88 sub sigint_handler { $sigint = 1; }
89 $SIG{'INT'} = \&sigint_handler;
90
91 my $rmlast = undef;
92 sub wait_interval {
93   $rmlast = [gettimeofday] unless defined($rmlast);
94   my $elapsed = $config->{interval} - tv_interval($rmlast);
95   if($elapsed > 0) {
96     sleep($elapsed);
97   }
98   $rmlast = [gettimeofday];
99 }
100
101 sub reap_zombies {
102     my $kid;
103     do {
104         $kid = waitpid(-1, WNOHANG);
105     } while $kid > 0;
106 }
107
108 unless($debug) {
109   fork && exit;
110   setsid;
111   close(STDIN);
112   close(STDOUT);
113   close(STDERR);
114   fork && exit;
115 }
116
117 my $list = [];
118 $status = Resmon::Status->new($config->{statusfile});
119 $status->open();
120 $status->serve_http_on($config->{interface}, $config->{port})
121   if($config->{port});
122
123 while(1) {
124   while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
125     my $coderef = undef;
126     eval { $coderef = Resmon::Module::fetch_monitor($module_name); };
127     foreach my $monobj (@$mod_configs) {
128       my $check_rv = 'BAD',
129       my $check_mess = 'no data';
130       my $starttime = [gettimeofday];
131       # Get old status if it hasn't expired
132       my ($check_rv, $check_mess) = Resmon::Module::fresh_status_msg($monobj);
133       # Otherwise, run the check
134       if (!$check_rv) {
135         eval {
136           local $SIG{ALRM} = sub { die "alarm\n" };
137           my $timeout = $monobj->{'check_timeout'} || $config->{'timeout'};
138           alarm($timeout);
139           if($coderef) {
140             ($check_rv, $check_mess) = $coderef->($monobj);
141           } else {
142             ($check_rv, $check_mess) = $monobj->handler();
143           }
144           alarm 0;
145         };
146         # Store the last status for use by fresh_status_msg later
147         # Also converts old style status messages
148         ($check_rv, $check_mess) =
149           Resmon::Module::set_status($monobj, $check_rv, $check_mess);
150       }
151       my $checkstat = $@;
152       my $confighash = {};
153       eval { $confighash = $monobj->config_as_hash(); };
154       my $results = {
155         configuration => $confighash,
156         last_runtime_seconds => sprintf("%.6f", tv_interval($starttime)),
157       };
158       if($checkstat) {
159         $results->{state} = 'BAD';
160         $results->{message} = "Bad module or problem running handler code.";
161         if ($checkstat eq "alarm\n") {
162           $results->{message} = "Check timeout";
163         }
164       } else {
165         $results->{state} = $check_rv;
166         $results->{message} = $check_mess;
167       }
168       $status->store($module_name,$monobj->{'object'}, $results);
169     }
170   }
171   $status->close();
172   die "Exiting.\n" if($sigint);
173   if ($sighup) {
174     $sighup = 0;
175     reconfigure();
176   } else {
177     reap_zombies();
178     wait_interval();
179     reap_zombies();
180   }
181   die "Exiting.\n" if($sigint);
182   print "\n---- ".localtime(time)."----------\n"
183    unless $status->open();
184 }
185
Note: See TracBrowser for help on using the browser.