Skip to content
Snippets Groups Projects
Commit f51cc7c8 authored by Mark Harrison's avatar Mark Harrison
Browse files

Indentation/formatting fixes (4 spaces per OmniTI coding style)

git-svn-id: https://labs.omniti.com/resmon/trunk@261 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55
parent 62a01cb2
No related branches found
No related tags found
No related merge requests found
......@@ -3,100 +3,102 @@ package Resmon::Config;
use strict;
sub new {
my $class = shift;
my $filename = shift;
my $self = bless {
configfile => $filename,
modstatus => '',
# Defaults
timeout => 10
}, $class;
open(CONF, "<$filename") || return undef;
my $class = shift;
my $filename = shift;
my $self = bless {
configfile => $filename,
modstatus => '',
# Defaults
timeout => 10
}, $class;
open(CONF, "<$filename") || return undef;
my $current;
my $line = 0;
while(<CONF>) {
$line++;
next if /^\s*#/;
next if /^\s*$/;
if($current) {
if(/^\s*([^:\s](?:[^:]*[^:\s])?)\s*:\s*(.+)\s*$/) {
my %kvs;
$kvs{'type'} = $current;
$kvs{'object'} = $1;
my @params = split(/,/, $2);
grep { $kvs{$1} = $2 if /^\s*(\S+)\s*=>\s*(\S(?:.*\S)?)\s*$/ } @params;
my $object = bless \%kvs, "Resmon::Module::$current";
push(@{$self->{Module}->{$current}}, $object);
my $current;
my $line = 0;
while(<CONF>) {
$line++;
next if /^\s*#/;
next if /^\s*$/;
if($current) {
if(/^\s*([^:\s](?:[^:]*[^:\s])?)\s*:\s*(.+)\s*$/) {
my %kvs;
$kvs{'type'} = $current;
$kvs{'object'} = $1;
my @params = split(/,/, $2);
grep { $kvs{$1} = $2 if /^\s*(\S+)\s*=>\s*(\S(?:.*\S)?)\s*$/ }
@params;
my $object = bless \%kvs, "Resmon::Module::$current";
push(@{$self->{Module}->{$current}}, $object);
# Test to make sure the module actually works
my $coderef;
eval { $coderef = Resmon::Module::fetch_monitor($current); };
if (!$coderef) {
# Try to execute the config_as_hash method. If it fails,
# then the module didn't load properly (e.g. syntax
# error).
eval { $object->config_as_hash; };
if ($@) {
# Module failed to load, print error and add to failed
# modules list.
print STDERR "Problem loading module $current\n";
print STDERR "This module will not be available\n";
$self->{'modstatus'} .= "$current ";
}
}
# Test to make sure the module actually works
my $coderef;
eval { $coderef = Resmon::Module::fetch_monitor($current); };
if (!$coderef) {
# Try to execute the config_as_hash method. If it fails, then
# the module didn't load properly (e.g. syntax error).
eval { $object->config_as_hash; };
if ($@) {
# Module failed to load, print error and add to failed
# modules list.
print STDERR "Problem loading module $current\n";
print STDERR "This module will not be available\n";
$self->{'modstatus'} .= "$current ";
} elsif (/^\s*\}\s*$/) {
$current = undef;
} else {
die "Syntax Error on line $line\n";
}
} else {
if(/\s*(\S+)\s*\{/) {
$current = $1;
$self->{Module}->{$current} = [];
next;
}
elsif(/\S*LIB\s+(\S+)\s*;\s*/) {
eval "use lib '$1';";
next;
}
elsif(/\S*PORT\s+(\d+)\s*;\s*/) {
$self->{port} = $1;
next;
}
elsif(/\S*INTERFACE\s+(\S+)\s*;\s*/) {
$self->{interface} = $1;
next;
}
elsif(/\s*INTERVAL\s+(\d+)\s*;\s*/) {
$self->{interval} = $1;
next;
}
elsif(/\s*STATUSFILE\s+(\S+)\s*;\s*/) {
$self->{statusfile} = $1;
next;
}
elsif(/\s*TIMEOUT\s+(\d+)\s*;\s*/) {
$self->{timeout} = $1;
next;
}
elsif(/\S*AUTHUSER\s+(\S+)\s*;\s*/) {
$self->{authuser} = $1;
next;
}
elsif(/\S*AUTHPASS\s+(\S+)\s*;\s*/) {
$self->{authpass} = $1;
next;
}
else {
die "Syntax Error on line $line\n";
}
}
} elsif (/^\s*\}\s*$/) {
$current = undef;
} else {
die "Syntax Error on line $line\n";
}
} else {
if(/\s*(\S+)\s*\{/) {
$current = $1;
$self->{Module}->{$current} = [];
next;
}
elsif(/\S*LIB\s+(\S+)\s*;\s*/) {
eval "use lib '$1';";
next;
}
elsif(/\S*PORT\s+(\d+)\s*;\s*/) {
$self->{port} = $1;
next;
}
elsif(/\S*INTERFACE\s+(\S+)\s*;\s*/) {
$self->{interface} = $1;
next;
}
elsif(/\s*INTERVAL\s+(\d+)\s*;\s*/) {
$self->{interval} = $1;
next;
}
elsif(/\s*STATUSFILE\s+(\S+)\s*;\s*/) {
$self->{statusfile} = $1;
next;
}
elsif(/\s*TIMEOUT\s+(\d+)\s*;\s*/) {
$self->{timeout} = $1;
next;
}
elsif(/\S*AUTHUSER\s+(\S+)\s*;\s*/) {
$self->{authuser} = $1;
next;
}
elsif(/\S*AUTHPASS\s+(\S+)\s*;\s*/) {
$self->{authpass} = $1;
next;
}
else {
die "Syntax Error on line $line\n";
}
}
}
if($current) {
die "unclosed stanza\n";
}
return $self;
if($current) {
die "unclosed stanza\n";
}
return $self;
}
1;
......@@ -12,17 +12,17 @@ my %commcache;
my %children;
sub cache_command($$;$) {
my ($command, $expiry, $timeout) = @_;
$timeout ||= $expiry;
my ($command, $expiry, $timeout) = @_;
$timeout ||= $expiry;
my $now = time;
if($commhist{$command}>$now) {
my $now = time;
if($commhist{$command}>$now) {
return $commcache{$command};
}
# TODO: timeouts
$commcache{$command} = run_cmd($command);
$commhist{$command} = $now + $expiry;
return $commcache{$command};
}
# TODO: timeouts
$commcache{$command} = run_cmd($command);
$commhist{$command} = $now + $expiry;
return $commcache{$command};
}
sub clean_up {
......
......@@ -9,80 +9,81 @@ my %coderefs;
my $rmloading = "Registering";
sub fetch_monitor {
my $type = shift;
my $coderef = $coderefs{$type};
return $coderef if ($coderef);
my $type = shift;
my $coderef = $coderefs{$type};
return $coderef if ($coderef);
# First if the monitor name is raw and looks right:
# is a subclass of Resmon::Module and can 'handler'
# then we will promote it into the Resmon::Module namespace
# and use this one.
eval "use $type;";
if($type->isa(__PACKAGE__) && $type->can('handler')) {
eval "
package Resmon::Module::$type;
use vars qw/\@ISA/;
\@ISA = qw($type);
1;
";
if($@) {
die "Could not repackage $type as Resmon::Module::$type\n";
# First if the monitor name is raw and looks right:
# is a subclass of Resmon::Module and can 'handler'
# then we will promote it into the Resmon::Module namespace
# and use this one.
eval "use $type;";
if($type->isa(__PACKAGE__) && $type->can('handler')) {
eval "
package Resmon::Module::$type;
use vars qw/\@ISA/;
\@ISA = qw($type);
1;
";
if($@) {
die "Could not repackage $type as Resmon::Module::$type\n";
}
return undef;
}
eval "use Resmon::Module::$type;";
return undef;
}
eval "use Resmon::Module::$type;";
return undef;
}
sub register_monitor {
my ($type, $ref) = @_;
if(ref $ref eq 'CODE') {
$coderefs{$type} = $ref;
}
print STDERR "$rmloading $type monitor\n";
my ($type, $ref) = @_;
if(ref $ref eq 'CODE') {
$coderefs{$type} = $ref;
}
print STDERR "$rmloading $type monitor\n";
}
sub fresh_status {
my $arg = shift;
print STDERR $arg->{type} . ": Warning: fresh_status() is deprecated, and no longer required.\n";
return undef;
my $arg = shift;
print STDERR $arg->{type} .
": Warning: fresh_status() is deprecated, and no longer required.\n";
return undef;
}
sub fresh_status_msg {
# Deal with result caching if an 'interval' entry is placed in the config
# for that module
my $arg = shift;
return undef unless $arg->{interval};
my $now = time;
if(($arg->{lastupdate} + $arg->{interval}) >= $now) {
return $arg->{laststatus}, $arg->{lastmessage};
}
return undef;
# Deal with result caching if an 'interval' entry is placed in the config
# for that module
my $arg = shift;
return undef unless $arg->{interval};
my $now = time;
if(($arg->{lastupdate} + $arg->{interval}) >= $now) {
return $arg->{laststatus}, $arg->{lastmessage};
}
return undef;
}
sub set_status {
my $arg = shift;
$arg->{laststatus} = shift;
$arg->{lastmessage} = shift;
$arg->{lastupdate} = time;
if($arg->{laststatus} =~ /^([A-Z]+)\((.*)\)$/s) {
# This handles old-style modules that return just set status as
# STATE(message)
$arg->{laststatus} = $1;
$arg->{lastmessage} = $2;
}
return ($arg->{laststatus}, $arg->{lastmessage});
my $arg = shift;
$arg->{laststatus} = shift;
$arg->{lastmessage} = shift;
$arg->{lastupdate} = time;
if($arg->{laststatus} =~ /^([A-Z]+)\((.*)\)$/s) {
# This handles old-style modules that return just set status as
# STATE(message)
$arg->{laststatus} = $1;
$arg->{lastmessage} = $2;
}
return ($arg->{laststatus}, $arg->{lastmessage});
}
sub config_as_hash {
my $self = shift;
my $conf = {};
while(my ($key, $value) = each %$self) {
if(! ref $value) {
# only stash scalars here.
$conf->{$key} = $value;
my $self = shift;
my $conf = {};
while(my ($key, $value) = each %$self) {
if(! ref $value) {
# only stash scalars here.
$conf->{$key} = $value;
}
}
}
return $conf;
return $conf;
}
sub reload_module {
......
This diff is collapsed.
......@@ -101,8 +101,8 @@ sub get_resmon_status {
my $host = "127.0.0.1";
my $handle = IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => $port);
PeerAddr => $host,
PeerPort => $port);
if (!$handle) {
print STDERR "can't connect to port $port on $host: $!";
return 0;
......@@ -160,15 +160,15 @@ sub reload_resmon {
}
sub track_mod_times {
my $mtime = (stat $_)[9];
return unless -f $_;
return if /\/\.svn$/ || /\/\.svn\//;
if($assess == 0) {
$times{$_} = $mtime;
} else {
$newfiles++ unless(exists($times{$_}));
$changedfiles++ if(exists($times{$_}) and ($times{$_} != $mtime));
}
my $mtime = (stat $_)[9];
return unless -f $_;
return if /\/\.svn$/ || /\/\.svn\//;
if($assess == 0) {
$times{$_} = $mtime;
} else {
$newfiles++ unless(exists($times{$_}));
$changedfiles++ if(exists($times{$_}) and ($times{$_} != $mtime));
}
}
1;
#!/usr/bin/perl
BEGIN {
(my $dir = $0) =~ s/\/?[^\/]+$//;
eval "use lib '$dir/lib';";
die $@ if($@);
(my $dir = $0) =~ s/\/?[^\/]+$//;
eval "use lib '$dir/lib';";
die $@ if($@);
};
use strict;
......@@ -12,7 +12,7 @@ use POSIX qw( :sys_wait_h setsid );
use Getopt::Long;
use Data::Dumper;
use vars qw($config_file $debug $status_file $interface $port $config
$status $update);
$status $update);
use Resmon::Config;
use Resmon::ExtComm;
......@@ -20,12 +20,12 @@ use Resmon::Status;
use Resmon::Module;
GetOptions(
"i=s" => \$interface,
"p=i" => \$port,
"c=s" => \$config_file,
"d" => \$debug,
"f=s" => \$status_file,
"u" => \$update,
"i=s" => \$interface,
"p=i" => \$port,
"c=s" => \$config_file,
"d" => \$debug,
"f=s" => \$status_file,
"u" => \$update,
);
if ($update) {
......@@ -38,10 +38,10 @@ $config_file ||= "$0.conf";
die "Cannot open configuration file: $config_file" unless (-r $config_file);
sub configure {
$config = Resmon::Config->new($config_file);
$config->{statusfile} = $status_file if($status_file);
$config->{port} = $port if($port);
$config->{interface} = $interface if($interface);
$config = Resmon::Config->new($config_file);
$config->{statusfile} = $status_file if($status_file);
$config->{port} = $port if($port);
$config->{interface} = $interface if($interface);
}
sub reconfigure {
......@@ -90,12 +90,12 @@ $SIG{'INT'} = \&sigint_handler;
my $rmlast = undef;
sub wait_interval {
$rmlast = [gettimeofday] unless defined($rmlast);
my $elapsed = $config->{interval} - tv_interval($rmlast);
if($elapsed > 0) {
sleep($elapsed);
}
$rmlast = [gettimeofday];
$rmlast = [gettimeofday] unless defined($rmlast);
my $elapsed = $config->{interval} - tv_interval($rmlast);
if($elapsed > 0) {
sleep($elapsed);
}
$rmlast = [gettimeofday];
}
sub reap_zombies {
......@@ -106,93 +106,95 @@ sub reap_zombies {
}
unless($debug) {
fork && exit;
setsid;
open(STDIN, "</dev/null");
open(STDOUT, ">/dev/null");
open(STDERR, ">/dev/null");
fork && exit;
fork && exit;
setsid;
open(STDIN, "</dev/null");
open(STDOUT, ">/dev/null");
open(STDERR, ">/dev/null");
fork && exit;
}
my $list = [];
$status = Resmon::Status->new($config->{statusfile});
$status->open();
$status->serve_http_on($config->{interface}, $config->{port},
$config->{authuser}, $config->{authpass})
if($config->{port});
$config->{authuser}, $config->{authpass})
if($config->{port});
while(1) {
while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
my $coderef = undef;
eval { $coderef = Resmon::Module::fetch_monitor($module_name); };
foreach my $monobj (@$mod_configs) {
my $check_rv = 'BAD',
my $check_metric = 'no data';
my $starttime = [gettimeofday];
# Get old status if it hasn't expired
my ($check_rv, $check_metric) = Resmon::Module::fresh_status_msg($monobj);
# Otherwise, run the check
if (!$check_rv) {
my $timeout = $monobj->{'check_timeout'} || $config->{'timeout'};
alarm($timeout);
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
if($coderef) {
($check_rv, $check_metric) = $coderef->($monobj);
} else {
($check_rv, $check_metric) = $monobj->handler();
}
};
alarm 0;
# Store the last status for use by fresh_status_msg later
# Also converts old style status messages
($check_rv, $check_metric) =
Resmon::Module::set_status($monobj, $check_rv, $check_metric);
}
my $checkstat = $@;
my $confighash = {};
eval { $confighash = $monobj->config_as_hash(); };
my $results = {
#configuration => $confighash,
last_runtime_seconds => sprintf("%.6f", tv_interval($starttime)),
};
if($checkstat) {
$results->{state} = 'BAD';
$results->{metric} = { "message" =>
"Bad module or problem running handler code."};
if ($checkstat eq "alarm\n") {
$results->{metric} = { "message" =>
"Check timeout"};
Resmon::ExtComm::clean_up;
}
} else {
$results->{state} = $check_rv;
if (ref($check_metric) eq "HASH") {
my $metric = {};
while(my ($k, $v) = each %$check_metric) {
$metric->{$k} = $v;
while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
my $coderef = undef;
eval { $coderef = Resmon::Module::fetch_monitor($module_name); };
foreach my $monobj (@$mod_configs) {
my $check_rv = 'BAD',
my $check_metric = 'no data';
my $starttime = [gettimeofday];
# Get old status if it hasn't expired
my ($check_rv, $check_metric) = Resmon::Module::fresh_status_msg(
$monobj);
# Otherwise, run the check
if (!$check_rv) {
my $timeout = $monobj->{'check_timeout'} ||
$config->{'timeout'};
alarm($timeout);
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
if($coderef) {
($check_rv, $check_metric) = $coderef->($monobj);
} else {
($check_rv, $check_metric) = $monobj->handler();
}
};
alarm 0;
# Store the last status for use by fresh_status_msg later
# Also converts old style status messages
($check_rv, $check_metric) =
Resmon::Module::set_status($monobj, $check_rv, $check_metric);
}
my $checkstat = $@;
my $confighash = {};
eval { $confighash = $monobj->config_as_hash(); };
my $results = {
#configuration => $confighash,
last_runtime_seconds => sprintf("%.6f", tv_interval($starttime))
};
if($checkstat) {
$results->{state} = 'BAD';
$results->{metric} = { "message" =>
"Bad module or problem running handler code."};
if ($checkstat eq "alarm\n") {
$results->{metric} = { "message" =>
"Check timeout"};
Resmon::ExtComm::clean_up;
}
} else {
$results->{state} = $check_rv;
if (ref($check_metric) eq "HASH") {
my $metric = {};
while(my ($k, $v) = each %$check_metric) {
$metric->{$k} = $v;
}
$results->{metric} = $metric;
} else {
$results->{metric} = { "message" => $check_metric };
}
}
$results->{metric} = $metric;
} else {
$results->{metric} = { "message" => $check_metric };
$status->store($module_name,$monobj->{'object'}, $results);
printf("%s: %s\n%s\n", $module_name, $monobj->{'object'},
Dumper($results)) if $debug;
}
}
$status->store($module_name,$monobj->{'object'}, $results);
printf("%s: %s\n%s\n", $module_name, $monobj->{'object'}, Dumper($results)) if $debug;
}
}
$status->close();
die "Exiting.\n" if($sigint);
if ($sighup) {
$sighup = 0;
reconfigure();
} else {
reap_zombies();
wait_interval();
reap_zombies();
}
die "Exiting.\n" if($sigint);
print "\n---- ".localtime(time)."----------\n"
unless $status->open();
$status->close();
die "Exiting.\n" if($sigint);
if ($sighup) {
$sighup = 0;
reconfigure();
} else {
reap_zombies();
wait_interval();
reap_zombies();
}
die "Exiting.\n" if($sigint);
print "\n---- ".localtime(time)."----------\n"
unless $status->open();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment