root/resmon

Revision 021df693bf0087dac900e2f698d4065c8c2856e7, 4.7 kB (checked in by Mark Harrison <mark@omniti.com>, 6 years ago)

Removing hardcoded reference to /opt/resmon

git-svn-id: https://labs.omniti.com/resmon/trunk@140 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             $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 unless($debug) {
102   fork && exit;
103   setsid;
104   close(STDIN);
105   close(STDOUT);
106   close(STDERR);
107   fork && exit;
108 }
109
110 my $list = [];
111 $status = Resmon::Status->new($config->{statusfile});
112 $status->open();
113 $status->serve_http_on($config->{interface}, $config->{port})
114   if($config->{port});
115
116 while(1) {
117   while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
118     my $coderef = undef;
119     eval { $coderef = Resmon::Module::fetch_monitor($module_name); };
120     foreach my $monobj (@$mod_configs) {
121       my $check_rv = 'BAD',
122       my $check_mess = 'no data';
123       my $starttime = [gettimeofday];
124       # Get old status if it hasn't expired
125       my ($check_rv, $check_mess) = Resmon::Module::fresh_status_msg($monobj);
126       # Otherwise, run the check
127       if (!$check_rv) {
128           if($coderef) {
129             eval { ($check_rv, $check_mess) = $coderef->($monobj); };
130           } else {
131             eval { ($check_rv, $check_mess) = $monobj->handler(); };
132           }
133           # Store the last status for use by fresh_status_msg later
134           # Also converts old style status messages
135           ($check_rv, $check_mess) =
136             Resmon::Module::set_status($monobj, $check_rv, $check_mess);
137       }
138       my $checkstat = $@;
139       my $confighash = {};
140       eval { $confighash = $monobj->config_as_hash(); };
141       my $results = {
142         configuration => $confighash,
143         last_runtime_seconds => sprintf("%.6f", tv_interval($starttime)),
144       };
145       if($checkstat) {
146         $results->{state} = 'BAD';
147         $results->{message} = "Bad module or problem running handler code.";
148       } else {
149         $results->{state} = $check_rv;
150         $results->{message} = $check_mess;
151       }
152       $status->store($module_name,$monobj->{'object'}, $results);
153     }
154   }
155   $status->close();
156   die "Exiting.\n" if($sigint);
157   if ($sighup) {
158     $sighup = 0;
159     reconfigure();
160   } else {
161     wait_interval();
162   }
163   die "Exiting.\n" if($sigint);
164   print "\n---- ".localtime(time)."----------\n"
165    unless $status->open();
166 }
167
Note: See TracBrowser for help on using the browser.