root/resmon

Revision c203f3d61063c12a4ce36e097772325e4ca8f26f, 2.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

more sane signal handling

git-svn-id: https://labs.omniti.com/resmon/trunk@24 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2
3 use lib '/opt/resmon/lib';
4
5 use strict;
6 use Time::HiRes qw( gettimeofday tv_interval sleep );
7 use POSIX qw( setsid );
8 use Getopt::Long;
9 use Data::Dumper;
10 use vars qw($config_file $debug $status_file $config);
11
12 use Resmon::Config;
13 use Resmon::ExtComm;
14 use Resmon::Status;
15 use Resmon::Module;
16
17 GetOptions(
18   "c=s" => \$config_file,
19   "d"   => \$debug,
20   "f=s" => \$status_file,
21 );
22
23 $config_file ||= 'resmon.conf';
24 die "Cannot open configuration file: $config_file" unless (-r $config_file);
25
26 sub configure {
27   $config = Resmon::Config->new($config_file);
28   $config->{statusfile} = $status_file if($status_file);
29   print Dumper($config) if($debug);
30 }
31
32 $SIG{'HUP'} = \&configure;
33 configure();
34
35 unless($debug) {
36   fork && exit;
37   setsid;
38   close(STDIN);
39   close(STDOUT);
40   close(STDERR);
41   fork && exit;
42 }
43
44 my $sigint = 0;
45 sub sigint_handler { $sigint = 1; }
46 $SIG{'INT'} = \&sigint_handler;
47
48 my $rmlast = undef;
49 sub wait_interval {
50   $rmlast = [gettimeofday] unless defined($rmlast);
51   my $elapsed = $config->{interval} - tv_interval($rmlast);
52   if($elapsed > 0) {
53     sleep($elapsed);
54   }
55   $rmlast = [gettimeofday];
56 }
57
58 my $list = [];
59 my $status = Resmon::Status->new($config->{statusfile});
60 while(1) {
61   while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
62     my $coderef = Resmon::Module::fetch_monitor($module_name);
63     foreach my $monobj (@$mod_configs) {
64       my $check_rv = 'BAD',
65       my $check_mess = 'no data';
66       if($coderef) {
67         eval { ($check_rv, $check_mess) = $coderef->($monobj); };
68       } else {
69         eval { ($check_rv, $check_mess) = $monobj->handler(); };
70       }
71       if($@) {
72         $status->store($module_name,$monobj->{'object'},'BAD',$@);
73       } else {
74         $status->store($module_name,$monobj->{'object'},$check_rv,$check_mess);
75       }
76     }
77   }
78   $status->close();
79   die "Exiting.\n" if($sigint);
80   wait_interval();
81   die "Exiting.\n" if($sigint);
82   print "\n---- ".localtime(time)."----------\n"
83    unless $status->open();
84 }
85
Note: See TracBrowser for help on using the browser.