Validate --max|critical-load (work in progress).

This commit is contained in:
Daniel Nichter
2012-05-25 12:15:11 -06:00
parent ed95534efa
commit bbe6125d8f
4 changed files with 79 additions and 17 deletions

View File

@@ -45,15 +45,17 @@ sub new {
}
PTDEBUG && _d('Parsing spec for max thresholds');
my $max_val_for = _parse_spec(
spec => $args{max_spec},
my $max_val_for = _parse_spec($args{max_spec});
_set_initial_vals(
vars => $max_val_for,
get_status => $args{get_status},
threshold_factor => 0.2, # +20%
);
PTDEBUG && _d('Parsing spec for critical thresholds');
my $critical_val_for = _parse_spec(
spec => $args{critical_spec} || [],
my $critical_val_for = _parse_spec($args{critical_spec} || []);
_set_initial_vals(
vars => $critical_val_for,
get_status => $args{get_status},
threshold_factor => 1.0, # double (x2; +100%)
);
@@ -79,27 +81,23 @@ sub new {
# Returns:
# Hashref with each variable's maximum permitted value.
sub _parse_spec {
my ( %args ) = @_;
my @required_args = qw(spec get_status);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless defined $args{$arg};
}
my ($spec, $get_status) = @args{@required_args};
my ($spec) = @_;
return unless $spec && scalar @$spec;
my $threshold_factor = $args{threshold_factor} || 0.20;
my %max_val_for;
foreach my $var_val ( @$spec ) {
my ($var, $val) = split /[:=]/, $var_val;
die "Invalid spec: $var_val" unless $var;
if ( !$val ) {
my $init_val = $get_status->($var);
PTDEBUG && _d('Initial', $var, 'value:', $init_val);
$val = int(($init_val * $threshold_factor) + $init_val);
PTDEBUG && _d('Will get intial value for', $var, 'later');
$max_val_for{$var} = undef;
}
else {
PTDEBUG && _d('Wait if', $var, '>=', $val);
$max_val_for{$var} = $val;
}
PTDEBUG && _d('Wait if', $var, '>=', $val);
$max_val_for{$var} = $val;
}
return \%max_val_for;
@@ -192,6 +190,31 @@ sub wait {
return;
}
sub _set_initial_vals {
my (%args) = @_;
my @required_args = qw(vars get_status threshold_factor);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless defined $args{$arg};
}
my ($vars, $get_status, $threshold_factor) = @args{@required_args};
PTDEBUG && _d('Setting initial values');
return unless $vars && scalar %$vars;
foreach my $var ( keys %$vars ) {
next if defined $vars->{$var};
my $init_val = $get_status->($var);
PTDEBUG && _d('Initial', $var, 'value:', $init_val);
die "Variable does not exist or has undefined value: $var"
unless defined $init_val;
my $val = int(($init_val * $threshold_factor) + $init_val);
$vars->{$var} = $val;
PTDEBUG && _d('Wait if', $var, '>=', $val);
}
}
sub _d {
my ($package, undef, $line) = caller 0;
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }