root/resmon

Revision 6ace5cf97aee2e334a8f34943d429296fba30cd0, 5.5 kB (checked in by Mark Harrison <mark@omniti.com>, 8 years ago)

Change method of daemonizing to fix an issue with run_cmd

git-svn-id: https://labs.omniti.com/resmon/trunk@243 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   open(STDIN, "</dev/null");
112   open(STDOUT, ">/dev/null");
113   open(STDERR, ">/dev/null");
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                        $config->{authuser}, $config->{authpass})
122   if($config->{port});
123
124 while(1) {
125   while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
126     my $coderef = undef;
127     eval { $coderef = Resmon::Module::fetch_monitor($module_name); };
128     foreach my $monobj (@$mod_configs) {
129       my $check_rv = 'BAD',
130       my $check_metric = 'no data';
131       my $starttime = [gettimeofday];
132       # Get old status if it hasn't expired
133       my ($check_rv, $check_metric) = Resmon::Module::fresh_status_msg($monobj);
134       # Otherwise, run the check
135       if (!$check_rv) {
136         my $timeout = $monobj->{'check_timeout'} || $config->{'timeout'};
137         alarm($timeout);
138         eval {
139           local $SIG{ALRM} = sub { die "alarm\n" };
140           if($coderef) {
141             ($check_rv, $check_metric) = $coderef->($monobj);
142           } else {
143             ($check_rv, $check_metric) = $monobj->handler();
144           }
145         };
146         alarm 0;
147         # Store the last status for use by fresh_status_msg later
148         # Also converts old style status messages
149         ($check_rv, $check_metric) =
150           Resmon::Module::set_status($monobj, $check_rv, $check_metric);
151       }
152       my $checkstat = $@;
153       my $confighash = {};
154       eval { $confighash = $monobj->config_as_hash(); };
155       my $results = {
156         #configuration => $confighash,
157         last_runtime_seconds => sprintf("%.6f", tv_interval($starttime)),
158       };
159       if($checkstat) {
160         $results->{state} = 'BAD';
161         $results->{metric} = { "message" =>
162             "Bad module or problem running handler code."};
163         if ($checkstat eq "alarm\n") {
164           $results->{metric} = { "message" =>
165               "Check timeout"};
166           Resmon::ExtComm::clean_up;
167         }
168       } else {
169         $results->{state} = $check_rv;
170         if (ref($check_metric) eq "HASH") {
171             my $metric = {};
172             while(my ($k, $v) = each %$check_metric) {
173                 $metric->{$k} = $v;
174             }
175             $results->{metric} = $metric;
176         } else {
177             $results->{metric} = { "message" => $check_metric };
178         }
179       }
180       $status->store($module_name,$monobj->{'object'}, $results);
181     }
182   }
183   $status->close();
184   die "Exiting.\n" if($sigint);
185   if ($sighup) {
186     $sighup = 0;
187     reconfigure();
188   } else {
189     reap_zombies();
190     wait_interval();
191     reap_zombies();
192   }
193   die "Exiting.\n" if($sigint);
194   print "\n---- ".localtime(time)."----------\n"
195    unless $status->open();
196 }
197
Note: See TracBrowser for help on using the browser.