root/resmon

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

Moving the update script to be an integral part of resmon. Run ./resmon -u to
update. Also takes the -d flag for debug info.

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