| 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 |
$SIG{'HUP'} = \&configure; |
|---|
| 40 |
configure(); |
|---|
| 41 |
|
|---|
| 42 |
my $sigint = 0; |
|---|
| 43 |
sub sigint_handler { $sigint = 1; } |
|---|
| 44 |
$SIG{'INT'} = \&sigint_handler; |
|---|
| 45 |
|
|---|
| 46 |
my $rmlast = undef; |
|---|
| 47 |
sub wait_interval { |
|---|
| 48 |
$rmlast = [gettimeofday] unless defined($rmlast); |
|---|
| 49 |
my $elapsed = $config->{interval} - tv_interval($rmlast); |
|---|
| 50 |
if($elapsed > 0) { |
|---|
| 51 |
sleep($elapsed); |
|---|
| 52 |
} |
|---|
| 53 |
$rmlast = [gettimeofday]; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
unless($debug) { |
|---|
| 57 |
fork && exit; |
|---|
| 58 |
setsid; |
|---|
| 59 |
close(STDIN); |
|---|
| 60 |
close(STDOUT); |
|---|
| 61 |
close(STDERR); |
|---|
| 62 |
fork && exit; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
my $list = []; |
|---|
| 66 |
my $status = Resmon::Status->new($config->{statusfile}); |
|---|
| 67 |
$status->open(); |
|---|
| 68 |
$status->serve_http_on($config->{interface}, $config->{port}) |
|---|
| 69 |
if($config->{port}); |
|---|
| 70 |
|
|---|
| 71 |
while(1) { |
|---|
| 72 |
while(my($module_name, $mod_configs) = each %{$config->{Module}}) { |
|---|
| 73 |
my $coderef = undef; |
|---|
| 74 |
eval { $coderef = Resmon::Module::fetch_monitor($module_name); }; |
|---|
| 75 |
foreach my $monobj (@$mod_configs) { |
|---|
| 76 |
my $check_rv = 'BAD', |
|---|
| 77 |
my $check_mess = 'no data'; |
|---|
| 78 |
my $starttime = [gettimeofday]; |
|---|
| 79 |
if($coderef) { |
|---|
| 80 |
eval { ($check_rv, $check_mess) = $coderef->($monobj); }; |
|---|
| 81 |
} else { |
|---|
| 82 |
eval { ($check_rv, $check_mess) = $monobj->handler(); }; |
|---|
| 83 |
} |
|---|
| 84 |
my $results = { |
|---|
| 85 |
configuration => eval { $monobj->config_as_hash(); }, |
|---|
| 86 |
last_runtime_seconds => tv_interval($starttime), |
|---|
| 87 |
}; |
|---|
| 88 |
if($@) { |
|---|
| 89 |
$results->{state} = 'BAD'; |
|---|
| 90 |
$results->{message} = $@; |
|---|
| 91 |
} else { |
|---|
| 92 |
$results->{state} = $check_rv; |
|---|
| 93 |
$results->{message} = $check_mess; |
|---|
| 94 |
} |
|---|
| 95 |
$status->store($module_name,$monobj->{'object'}, $results); |
|---|
| 96 |
} |
|---|
| 97 |
} |
|---|
| 98 |
$status->close(); |
|---|
| 99 |
die "Exiting.\n" if($sigint); |
|---|
| 100 |
wait_interval(); |
|---|
| 101 |
die "Exiting.\n" if($sigint); |
|---|
| 102 |
print "\n---- ".localtime(time)."----------\n" |
|---|
| 103 |
unless $status->open(); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|