This commit is contained in:
garnier-quentin 2019-04-15 16:04:56 +02:00
parent cc073fdfc0
commit a3636fdd3e
8 changed files with 143 additions and 71 deletions

View File

@ -115,11 +115,17 @@ sub add_options {
# $options{arguments} = ref to hash table with string and name to store (example: { 'mode:s' => { name => 'mode', default => 'defaultvalue' )
foreach (keys %{$options{arguments}}) {
if (defined($options{arguments}->{$_}->{redirect})) {
$self->{options}->{$_} = \$self->{options_stored}->{$options{arguments}->{$_}->{redirect}};
next;
}
if (defined($options{arguments}->{$_}->{default})) {
$self->{options_stored}->{$options{arguments}->{$_}->{name}} = $options{arguments}->{$_}->{default};
} else {
$self->{options_stored}->{$options{arguments}->{$_}->{name}} = undef;
}
$self->{options}->{$_} = \$self->{options_stored}->{$options{arguments}->{$_}->{name}};
}
}

View File

@ -41,6 +41,7 @@ sub new {
"extend-perfdata:s@" => { name => 'extend_perfdata' },
"extend-perfdata-group:s@"=> { name => 'extend_perfdata_group' },
"change-short-output:s@" => { name => 'change_short_output' },
"use-new-perfdata" => { name => 'use_new_perfdata' },
"filter-uom:s" => { name => 'filter_uom' },
"verbose" => { name => 'verbose' },
"debug" => { name => 'debug' },
@ -153,12 +154,12 @@ sub set_status {
sub output_add {
my ($self, %params) = @_;
my %args = (
severity => 'OK',
separator => ' - ',
debug => 0,
short_msg => undef,
long_msg => undef,
);
severity => 'OK',
separator => ' - ',
debug => 0,
short_msg => undef,
long_msg => undef,
);
my $options = {%args, %params};
if (defined($options->{short_msg})) {
@ -181,11 +182,27 @@ sub output_add {
sub perfdata_add {
my ($self, %options) = @_;
my $perfdata = {label => '', value => '', unit => '', warning => '', critical => '', min => '', max => ''};
my $perfdata = {
label => '', value => '', unit => '', warning => '', critical => '', min => '', max => ''
};
foreach (keys %options) {
next if (!defined($options{$_}));
$perfdata->{$_} = $options{$_};
}
if (defined($self->{option_results}->{use_new_perfdata}) && defined($options{nlabel})) {
$perfdata->{label} = $options{nlabel};
}
if (defined($options{instances})) {
$options{instances} = [$options{instances}] if (!ref($options{instances}));
my ($external_instance_separator, $internal_instance_separator) = ('#', '~');
if (defined($self->{option_results}->{use_new_perfdata})) {
$perfdata->{label} = join('~', @{$options{instances}}) . '#' . $perfdata->{label};
} else {
$perfdata->{label} .= '_' . join('_', @{$options{instances}});
}
}
$perfdata->{label} =~ s/'/''/g;
push @{$self->{perfdatas}}, $perfdata;
}
@ -225,19 +242,19 @@ sub output_json {
my $lcode_litteral = ($code_litteral eq 'UNQUALIFIED_YET' ? uc($options{exit_litteral}) : $code_litteral);
push @{$json_content->{plugin}->{outputs}}, {
type => 1,
msg => ($options{nolabel} == 0 ? ($lcode_litteral . ': ') : '') . $_,
exit => $lcode_litteral
};
type => 1,
msg => ($options{nolabel} == 0 ? ($lcode_litteral . ': ') : '') . $_,
exit => $lcode_litteral
};
}
}
if (defined($self->{option_results}->{verbose}) || $force_long_output == 1) {
foreach (@{$self->{global_long_output}}) {
push @{$json_content->{plugin}->{outputs}}, {
type => 2,
msg => $_,
};
type => 2,
msg => $_,
};
}
}
@ -256,8 +273,8 @@ sub output_json {
}
push @{$json_content->{plugin}->{perfdatas}}, {
%values
};
%values
};
}
}
@ -763,6 +780,24 @@ sub is_debug {
return 0;
}
sub use_new_perfdata {
my ($self) = @_;
if (defined($self->{option_results}->{use_new_perfdata})) {
return 1;
}
return 0;
}
sub get_instance_perfdata_separator {
my ($self) = @_;
if (defined($self->{option_results}->{use_new_perfdata})) {
return '~';
}
return '_';
}
sub parse_pfdata_scale {
my ($self, %options) = @_;

View File

@ -101,15 +101,27 @@ sub new {
foreach my $key (keys %{$self->{maps_counters}}) {
foreach (@{$self->{maps_counters}->{$key}}) {
my $label = $_->{label};
$label = $_->{nlabel} if ($self->{output}->use_new_perfdata() && defined($_->{nlabel}));
my $thlabel = $label;
$thlabel =~ s/\./-/g;
if (!defined($_->{threshold}) || $_->{threshold} != 0) {
$options{options}->add_options(arguments => {
'warning-' . $_->{label} . ':s' => { name => 'warning-' . $_->{label} },
'critical-' . $_->{label} . ':s' => { name => 'critical-' . $_->{label} },
});
'warning-' . $thlabel . ':s' => { name => 'warning-' . $thlabel },
'critical-' . $thlabel . ':s' => { name => 'critical-' . $thlabel },
});
if (defined($_->{nlabel})) {
$options{options}->add_options(arguments => {
'warning-' . $_->{label} . ':s' => { name => 'warning-' . $_->{label}, redirect => 'warning-' . $thlabel },
'critical-' . $_->{label} . ':s' => { name => 'critical-' . $_->{label}, redirect => 'critical-' . $thlabel },
});
}
}
$_->{obj} = centreon::plugins::values->new(statefile => $self->{statefile_value},
output => $self->{output}, perfdata => $self->{perfdata},
label => $_->{label});
label => $label, thlabel => $thlabel);
$_->{obj}->set(%{$_->{set}});
}
}
@ -408,7 +420,7 @@ sub run_multiple_instances {
my $instance = $id;
if ($multiple_parent == 1 && $multiple == 1) {
$instance = $options{instance_parent} . "_" . $id;
$instance = $options{instance_parent} . ($self->{output}->get_instance_perfdata_separator()) . $id;
} elsif ($multiple_parent == 1 && $multiple == 0) {
$instance = $options{instance_parent};
}

View File

@ -35,6 +35,7 @@ sub new {
$self->{output} = $options{output};
$self->{perfdata} = $options{perfdata};
$self->{label} = $options{label};
$self->{thlabel} = defined($options{thlabel}) ? $options{thlabel} : $self->{label};
$self->{perfdatas} = [];
@ -61,8 +62,8 @@ sub new {
sub init {
my ($self, %options) = @_;
my $warn = defined($self->{threshold_warn}) ? $self->{threshold_warn} : 'warning-' . $self->{label};
my $crit = defined($self->{threshold_crit}) ? $self->{threshold_crit} : 'critical-' . $self->{label};
my $warn = defined($self->{threshold_warn}) ? $self->{threshold_warn} : 'warning-' . $self->{thlabel};
my $crit = defined($self->{threshold_crit}) ? $self->{threshold_crit} : 'critical-' . $self->{thlabel};
if (($self->{perfdata}->threshold_validate(label => $warn, value => $options{option_results}->{$warn})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong $warn threshold '" . $options{option_results}->{$warn} . "'.");
@ -108,8 +109,8 @@ sub threshold_check {
return &{$self->{closure_custom_threshold_check}}($self, %options);
}
my $warn = defined($self->{threshold_warn}) ? $self->{threshold_warn} : 'warning-' . $self->{label};
my $crit = defined($self->{threshold_crit}) ? $self->{threshold_crit} : 'critical-' . $self->{label};
my $warn = defined($self->{threshold_warn}) ? $self->{threshold_warn} : 'warning-' . $self->{thlabel};
my $crit = defined($self->{threshold_crit}) ? $self->{threshold_crit} : 'critical-' . $self->{thlabel};
my $first = ${${$self->{key_values}}[0]}{name};
my $value;
@ -167,8 +168,8 @@ sub perfdata {
return &{$self->{closure_custom_perfdata}}($self, %options);
}
my $warn = defined($self->{threshold_warn}) ? $self->{threshold_warn} : 'warning-' . $self->{label};
my $crit = defined($self->{threshold_crit}) ? $self->{threshold_crit} : 'critical-' . $self->{label};
my $warn = defined($self->{threshold_warn}) ? $self->{threshold_warn} : 'warning-' . $self->{thlabel};
my $crit = defined($self->{threshold_crit}) ? $self->{threshold_crit} : 'critical-' . $self->{thlabel};
foreach my $perf (@{$self->{perfdatas}}) {
my ($label, $extra_label, $min, $max, $th_total) = ($self->{label}, '');
@ -187,21 +188,35 @@ sub perfdata {
$th_total = ($perf->{threshold_total} =~ /[^0-9]/) ? $self->{result_values}->{$perf->{threshold_total}} : $perf->{threshold_total};
}
if (defined($perf->{label_extra_instance}) && $perf->{label_extra_instance} == 1 &&
(!defined($options{extra_instance}) || $options{extra_instance} != 0)) {
my $instances;
if (defined($perf->{label_extra_instance}) && $perf->{label_extra_instance} == 1) {
my $instance = '';
if (defined($perf->{instance_use})) {
$extra_label .= '_' . $self->{result_values}->{$perf->{instance_use}};
$instance = $self->{result_values}->{$perf->{instance_use}};
} else {
$extra_label .= '_' . $self->{instance};
$instance = $self->{instance};
}
if (!defined($options{extra_instance}) || $options{extra_instance} != 0 || $self->{output}->use_new_perfdata()) {
$instances = $instance;
}
}
my %nlabel_option = ();
if ($self->{output}->use_new_perfdata()) {
$nlabel_option{nlabel} = $self->{label};
}
$self->{output}->perfdata_add(label => $label . $extra_label, unit => $perf->{unit},
value => $cast_int == 1 ? int($self->{result_values}->{$perf->{value}}) : sprintf($template, $self->{result_values}->{$perf->{value}}),
warning => $self->{perfdata}->get_perfdata_for_output(label => $warn, total => $th_total, cast_int => $cast_int),
critical => $self->{perfdata}->get_perfdata_for_output(label => $crit, total => $th_total, cast_int => $cast_int),
min => $min, max => $max);
$self->{output}->perfdata_add(
label => $label,
instances => $instances,
%nlabel_option,
unit => $perf->{unit},
value => $cast_int == 1 ? int($self->{result_values}->{$perf->{value}}) : sprintf($template, $self->{result_values}->{$perf->{value}}),
warning => $self->{perfdata}->get_perfdata_for_output(label => $warn, total => $th_total, cast_int => $cast_int),
critical => $self->{perfdata}->get_perfdata_for_output(label => $crit, total => $th_total, cast_int => $cast_int),
min => $min, max => $max
);
}
}

View File

@ -34,7 +34,7 @@ sub set_counters {
];
$self->{maps_counters}->{global} = [
{ label => 'stdev-3phases', set => {
{ label => 'stdev-3phases', nlabel => 'output.3phases.stdev.gauge', set => {
key_values => [ { name => 'stdev' } ],
output_template => 'Load Standard Deviation : %.2f',
perfdatas => [
@ -45,38 +45,38 @@ sub set_counters {
];
$self->{maps_counters}->{oline} = [
{ label => 'load', set => {
{ label => 'load', nlabel => 'line.output.load.percentage', set => {
key_values => [ { name => 'mgoutputLoadPerPhase', no_value => 0 } ],
output_template => 'Load : %.2f %%',
perfdatas => [
{ label => 'load', value => 'mgoutputLoadPerPhase_absolute', template => '%.2f',
{ value => 'mgoutputLoadPerPhase_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'current', set => {
{ label => 'current', nlabel => 'line.output.current.ampere', set => {
key_values => [ { name => 'mgoutputCurrent', no_value => 0 } ],
output_template => 'Current : %.2f A',
perfdatas => [
{ label => 'current', value => 'mgoutputCurrent_absolute', template => '%.2f',
{ value => 'mgoutputCurrent_absolute', template => '%.2f',
min => 0, unit => 'A', label_extra_instance => 1 },
],
}
},
{ label => 'voltage', set => {
{ label => 'voltage', nlabel => 'line.output.voltage.volt', set => {
key_values => [ { name => 'mgoutputVoltage', no_value => 0 } ],
output_template => 'Voltage : %.2f V',
perfdatas => [
{ label => 'voltage', value => 'mgoutputVoltage_absolute', template => '%.2f',
{ value => 'mgoutputVoltage_absolute', template => '%.2f',
unit => 'V', label_extra_instance => 1 },
],
}
},
{ label => 'frequence', set => {
{ label => 'frequence', nlabel => 'line.output.frequence.hertz', set => {
key_values => [ { name => 'mgoutputFrequency', no_value => -1 } ],
output_template => 'Frequence : %.2f Hz',
perfdatas => [
{ label => 'frequence', value => 'mgoutputFrequency_absolute', template => '%.2f',
{ value => 'mgoutputFrequency_absolute', template => '%.2f',
unit => 'Hz', label_extra_instance => 1 },
],
}

View File

@ -34,7 +34,7 @@ sub set_counters {
];
$self->{maps_counters}->{global} = [
{ label => 'stdev-3phases', set => {
{ label => 'stdev-3phases', nlabel => 'output.3phases.stdev.gauge', set => {
key_values => [ { name => 'stdev' } ],
output_template => 'Load Standard Deviation : %.2f',
perfdatas => [
@ -45,38 +45,38 @@ sub set_counters {
];
$self->{maps_counters}->{oline} = [
{ label => 'load', set => {
{ label => 'load', nlabel => 'line.output.load.percentage', set => {
key_values => [ { name => 'upsOutputPercentLoad' } ],
output_template => 'Load : %.2f %%',
perfdatas => [
{ label => 'load', value => 'upsOutputPercentLoad_absolute', template => '%.2f',
{ value => 'upsOutputPercentLoad_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'current', set => {
{ label => 'current', nlabel => 'line.output.current.ampere', set => {
key_values => [ { name => 'upsOutputCurrent' } ],
output_template => 'Current : %.2f A',
perfdatas => [
{ label => 'current', value => 'upsOutputCurrent_absolute', template => '%.2f',
{ value => 'upsOutputCurrent_absolute', template => '%.2f',
min => 0, unit => 'A', label_extra_instance => 1 },
],
}
},
{ label => 'voltage', set => {
{ label => 'voltage', nlabel => 'line.output.voltage.volt', set => {
key_values => [ { name => 'upsOutputVoltage' } ],
output_template => 'Voltage : %.2f V',
perfdatas => [
{ label => 'voltage', value => 'upsOutputVoltage_absolute', template => '%.2f',
{ value => 'upsOutputVoltage_absolute', template => '%.2f',
unit => 'V', label_extra_instance => 1 },
],
}
},
{ label => 'power', set => {
{ label => 'power', nlabel => 'line.output.power.watt', set => {
key_values => [ { name => 'upsOutputPower' } ],
output_template => 'Power : %.2f W',
perfdatas => [
{ label => 'power', value => 'upsOutputPower_absolute', template => '%.2f',
{ value => 'upsOutputPower_absolute', template => '%.2f',
unit => 'W', label_extra_instance => 1 },
],
}

View File

@ -34,7 +34,7 @@ sub set_counters {
];
$self->{maps_counters}->{cpu_avg} = [
{ label => 'average', set => {
{ label => 'average', nlabel => 'cpu.utilization.percentage', set => {
key_values => [ { name => 'average' }, { name => 'count' } ],
output_template => '%.2f %%',
perfdatas => [
@ -46,7 +46,7 @@ sub set_counters {
];
$self->{maps_counters}->{cpu_core} = [
{ label => 'core', set => {
{ label => 'core', nlabel => 'core.cpu.utilization.percentage', set => {
key_values => [ { name => 'cpu' }, { name => 'display' } ],
output_template => 'usage : %.2f %%',
perfdatas => [
@ -77,9 +77,8 @@ sub new {
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
});
$options{options}->add_options(arguments => {
});
return $self;
}

View File

@ -72,25 +72,30 @@ my %storage_types_manage = (
sub custom_usage_perfdata {
my ($self, %options) = @_;
my $label = 'used';
my ($label, $nlabel) = ('used', $self->{label});
my $value_perf = $self->{result_values}->{used};
if (defined($self->{instance_mode}->{option_results}->{free})) {
$label = 'free';
($label, $nlabel) = ('free', 'storage.space.free.bytes');
$value_perf = $self->{result_values}->{free};
}
my $extra_label = '';
$extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
$extra_label = $self->{result_values}->{display}
if (!defined($options{extra_instance}) || $options{extra_instance} != 0 || $self->{output}->use_new_perfdata());
my %total_options = ();
if ($self->{instance_mode}->{option_results}->{units} eq '%') {
$total_options{total} = $self->{result_values}->{total};
$total_options{cast_int} = 1;
}
$self->{output}->perfdata_add(label => $label . $extra_label, unit => 'B',
value => $value_perf,
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options),
min => 0, max => $self->{result_values}->{total});
$self->{output}->perfdata_add(
label => $label, unit => 'B',
instances => $extra_label,
nlabel => $nlabel,
value => $value_perf,
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
min => 0, max => $self->{result_values}->{total}
);
}
sub custom_usage_threshold {
@ -103,7 +108,7 @@ sub custom_usage_threshold {
$threshold_value = $self->{result_values}->{prct_used};
$threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
}
$exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]);
$exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
return $exit;
}
@ -163,7 +168,7 @@ sub set_counters {
];
$self->{maps_counters}->{storage} = [
{ label => 'usage', set => {
{ label => 'usage', nlabel => 'storage.space.usage.bytes', set => {
key_values => [ { name => 'display' }, { name => 'used' }, { name => 'size' }, { name => 'allocation_units' } ],
closure_custom_calc => $self->can('custom_usage_calc'),
closure_custom_output => $self->can('custom_usage_output'),
@ -171,7 +176,7 @@ sub set_counters {
closure_custom_threshold_check => $self->can('custom_usage_threshold'),
}
},
{ label => 'access', set => {
{ label => 'access', nlabel => 'storage.access', set => {
key_values => [ { name => 'access' }, { name => 'display' } ],
closure_custom_output => $self->can('custom_access_output'),
perfdatas => [