root/resmon

Revision 1533dda22b2e7854c9d23a7dc3df5b57e838e66f, 2.8 kB (checked in by Mark Harrison <mark@omniti.com>, 7 years ago)

Resmon no longer dies on config file errors on reload. Also show a better
error message when starting with a faulty module.

git-svn-id: https://labs.omniti.com/resmon/trunk@88 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
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     print STDERR "Reloading...\n";
41     eval { configure(); };
42     if ($@) {
43         print STDERR " Failed to reload: ";
44         print STDERR $@;
45         print STDERR " Continuing with old configuration\n";
46     }
47 }
48
49 $SIG{'HUP'} = \&reconfigure;
50 configure();
51
52 my $sigint = 0;
53 sub sigint_handler { $sigint = 1; }
54 $SIG{'INT'} = \&sigint_handler;
55
56 my $rmlast = undef;
57 sub wait_interval {
58   $rmlast = [gettimeofday] unless defined($rmlast);
59   my $elapsed = $config->{interval} - tv_interval($rmlast);
60   if($elapsed > 0) {
61     sleep($elapsed);
62   }
63   $rmlast = [gettimeofday];
64 }
65
66 unless($debug) {
67   fork && exit;
68   setsid;
69   close(STDIN);
70   close(STDOUT);
71   close(STDERR);
72   fork && exit;
73 }
74
75 my $list = [];
76 my $status = Resmon::Status->new($config->{statusfile});
77 $status->open();
78 $status->serve_http_on($config->{interface}, $config->{port})
79   if($config->{port});
80
81 while(1) {
82   while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
83     my $coderef = undef;
84     eval { $coderef = Resmon::Module::fetch_monitor($module_name); };
85     foreach my $monobj (@$mod_configs) {
86       my $check_rv = 'BAD',
87       my $check_mess = 'no data';
88       my $starttime = [gettimeofday];
89       if($coderef) {
90         eval { ($check_rv, $check_mess) = $coderef->($monobj); };
91       } else {
92         eval { ($check_rv, $check_mess) = $monobj->handler(); };
93       }
94       my $checkstat = $@;
95       my $results = {
96         configuration => eval { $monobj->config_as_hash(); },
97         last_runtime_seconds => sprintf("%.6f", tv_interval($starttime)),
98       };
99       if($checkstat) {
100         $results->{state} = 'BAD';
101         $results->{message} = $checkstat;
102       } else {
103         $results->{state} = $check_rv;
104         $results->{message} = $check_mess;
105       }
106       $status->store($module_name,$monobj->{'object'}, $results);
107     }
108   }
109   $status->close();
110   die "Exiting.\n" if($sigint);
111   wait_interval();
112   die "Exiting.\n" if($sigint);
113   print "\n---- ".localtime(time)."----------\n"
114    unless $status->open();
115 }
116
Note: See TracBrowser for help on using the browser.