adapt nodeexporter to new counter template

This commit is contained in:
Colin Gagnaire 2019-02-04 14:30:38 +01:00
parent 1416e8991f
commit 0c3714e8a6
3 changed files with 117 additions and 95 deletions

View File

@ -29,47 +29,62 @@ sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes CPU usage are ok',
counters => [ { name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All CPU usage are ok' } ] },
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_node_output', cb_long_output => 'node_long_output',
message_multiple => 'All nodes usage are ok', indent_long_output => ' ',
group => [
{ name => 'global_cpu', cb_prefix_output => 'prefix_global_cpu_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'cpu', display_long => 1, cb_prefix_output => 'prefix_cpu_output',
message_multiple => 'All CPUs usage are ok', type => 1, skipped_code => { -10 => 1 } },
]
}
];
$self->{maps_counters}->{nodes} = [
$self->{maps_counters}->{global_cpu} = [
{ label => 'node-usage', set => {
key_values => [ { name => 'average' }, { name => 'display' } ],
output_template => 'usage %.2f %%',
key_values => [ { name => 'node_average' } ],
output_template => '%.2f %%',
perfdatas => [
{ label => 'node', value => 'average_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_extra_instance => 1, instance_use => 'display_absolute' },
{ label => 'node', value => 'node_average_absolute', template => '%.2f', unit => '%',
min => 0, max => 100, label_extra_instance => 1 },
],
}
},
];
$self->{maps_counters}->{cpu} = [
{ label => 'cpu-usage', set => {
key_values => [ { name => 'average' }, { name => 'multi' }, { name => 'display' } ],
output_template => 'usage %.2f %%',
key_values => [ { name => 'cpu_usage' }, { name => 'display' } ],
output_template => 'Usage: %.2f %%',
perfdatas => [
{ label => 'cpu', value => 'average_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
{ label => 'cpu', value => 'cpu_usage_absolute', template => '%.2f', unit => '%',
min => 0, max => 100, label_extra_instance => 1 },
],
}
},
];
}
sub prefix_nodes_output {
sub prefix_node_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{display} . "' ";
}
sub node_long_output {
my ($self, %options) = @_;
return "Checking node '" . $options{instance_value}->{display} . "'";
}
sub prefix_global_cpu_output {
my ($self, %options) = @_;
return "CPU Average Usage: ";
}
sub prefix_cpu_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{multi} . "' " . "Cpu '" . $options{instance_value}->{display} . "' ";
return "CPU '" . $options{instance_value}->{display} . "' ";
}
sub new {
@ -123,7 +138,6 @@ sub manage_selection {
my ($self, %options) = @_;
$self->{nodes} = {};
$self->{cpu} = {};
my $results = $options{custom}->query_range(queries => [ '(1 - irate({__name__=~"' . $self->{metrics}->{cpu} . '",' .
'mode="idle",' .
@ -134,15 +148,14 @@ sub manage_selection {
foreach my $result (@{$results}) {
my $average = $options{custom}->compute(aggregation => 'average', values => $result->{values});
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{display} = $result->{metric}->{$self->{labels}->{instance}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{average} += $average;
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{multi} = $result->{metric}->{$self->{labels}->{instance}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{display} = $result->{metric}->{$self->{labels}->{instance}},
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{global_cpu}->{node_average} += $average;
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{display} = $result->{metric}->{$self->{labels}->{cpu}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{average} = $average;
}
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{cpu_usage} = $average;
}
foreach my $node (keys %{$self->{nodes}}) {
$self->{nodes}->{$node}->{average} /= scalar(keys %{$self->{nodes}->{$node}->{cpu}});
$self->{nodes}->{$node}->{global_cpu}->{node_average} /= scalar(keys %{$self->{nodes}->{$node}->{cpu}});
}
if (scalar(keys %{$self->{nodes}}) <= 0) {

View File

@ -29,188 +29,188 @@ sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_nodes_output',
message_multiple => 'All nodes CPU usage are ok', skipped_code => { -10 => 1 },
counters => [ { name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output',
message_multiple => 'All CPU usage are ok', skipped_code => { -10 => 1 } } ] },
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_node_output', cb_long_output => 'node_long_output',
message_multiple => 'All nodes usage are ok', indent_long_output => ' ',
group => [
{ name => 'global_cpu', cb_prefix_output => 'prefix_global_cpu_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'cpu', display_long => 1, cb_prefix_output => 'prefix_cpu_output',
message_multiple => 'All CPUs usage are ok', type => 1, skipped_code => { -10 => 1 } },
]
}
];
$self->{maps_counters}->{nodes} = [
$self->{maps_counters}->{global_cpu} = [
{ label => 'node-wait', set => {
key_values => [ { name => 'iowait' }, { name => 'display' } ],
key_values => [ { name => 'iowait' } ],
output_template => 'Wait: %.2f %%',
perfdatas => [
{ label => 'wait', value => 'iowait_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'node-user', set => {
key_values => [ { name => 'user' }, { name => 'display' } ],
key_values => [ { name => 'user' } ],
output_template => 'User: %.2f %%',
perfdatas => [
{ label => 'user', value => 'user_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'node-softirq', set => {
key_values => [ { name => 'softirq' }, { name => 'display' } ],
key_values => [ { name => 'softirq' } ],
output_template => 'Soft Irq: %.2f %%',
perfdatas => [
{ label => 'softirq', value => 'softirq_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'node-interrupt', set => {
key_values => [ { name => 'irq' }, { name => 'display' } ],
key_values => [ { name => 'irq' } ],
output_template => 'Interrupt: %.2f %%',
perfdatas => [
{ label => 'interrupt', value => 'irq_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'node-idle', set => {
key_values => [ { name => 'idle' }, { name => 'display' } ],
key_values => [ { name => 'idle' } ],
output_template => 'Idle: %.2f %%',
perfdatas => [
{ label => 'idle', value => 'idle_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'node-steal', set => {
key_values => [ { name => 'steal' }, { name => 'display' } ],
key_values => [ { name => 'steal' } ],
output_template => 'Steal: %.2f %%',
perfdatas => [
{ label => 'steal', value => 'steal_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'node-system', set => {
key_values => [ { name => 'system' }, { name => 'display' } ],
key_values => [ { name => 'system' } ],
output_template => 'System: %.2f %%',
perfdatas => [
{ label => 'system', value => 'system_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'node-nice', set => {
key_values => [ { name => 'nice' }, { name => 'display' } ],
key_values => [ { name => 'nice' } ],
output_template => 'Nice: %.2f %%',
perfdatas => [
{ label => 'nice', value => 'nice_absolute', template => '%.2f',
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
];
$self->{maps_counters}->{cpu} = [
{ label => 'cpu-wait', set => {
key_values => [ { name => 'iowait' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'iowait' }, { name => 'display' } ],
output_template => 'Wait: %.2f %%',
perfdatas => [
{ label => 'wait', value => 'iowait_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'cpu-user', set => {
key_values => [ { name => 'user' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'user' }, { name => 'display' } ],
output_template => 'User: %.2f %%',
perfdatas => [
{ label => 'user', value => 'user_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'cpu-softirq', set => {
key_values => [ { name => 'softirq' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'softirq' }, { name => 'display' } ],
output_template => 'Soft Irq: %.2f %%',
perfdatas => [
{ label => 'softirq', value => 'softirq_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'cpu-interrupt', set => {
key_values => [ { name => 'irq' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'irq' }, { name => 'display' } ],
output_template => 'Interrupt: %.2f %%',
perfdatas => [
{ label => 'interrupt', value => 'irq_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'cpu-idle', set => {
key_values => [ { name => 'idle' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'idle' }, { name => 'display' } ],
output_template => 'Idle: %.2f %%',
perfdatas => [
{ label => 'idle', value => 'idle_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'cpu-steal', set => {
key_values => [ { name => 'steal' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'steal' }, { name => 'display' } ],
output_template => 'Steal: %.2f %%',
perfdatas => [
{ label => 'steal', value => 'steal_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'cpu-system', set => {
key_values => [ { name => 'system' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'system' }, { name => 'display' } ],
output_template => 'System: %.2f %%',
perfdatas => [
{ label => 'system', value => 'system_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
{ label => 'cpu-nice', set => {
key_values => [ { name => 'nice' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'nice' }, { name => 'display' } ],
output_template => 'Nice: %.2f %%',
perfdatas => [
{ label => 'nice', value => 'nice_absolute', template => '%.2f',
min => 0, max => 100, unit => '%',
label_multi_instances => 1, multi_use => 'multi_absolute',
label_extra_instance => 1, instance_use => 'display_absolute' },
min => 0, max => 100, unit => '%', label_extra_instance => 1 },
],
}
},
];
}
sub prefix_nodes_output {
sub prefix_node_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{display} . "' ";
}
sub node_long_output {
my ($self, %options) = @_;
return "Checking node '" . $options{instance_value}->{display} . "'";
}
sub prefix_global_cpu_output {
my ($self, %options) = @_;
return "CPU Average ";
}
sub prefix_cpu_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{multi} . "' " . "Cpu '" . $options{instance_value}->{display} . "' ";
return "CPU '" . $options{instance_value}->{display} . "' ";
}
sub new {
@ -276,16 +276,15 @@ sub manage_selection {
foreach my $result (@{$results}) {
my $average = $options{custom}->compute(aggregation => 'average', values => $result->{values});
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{display} = $result->{metric}->{$self->{labels}->{instance}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{$result->{metric}->{$self->{labels}->{type}}} += $average;
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{multi} = $result->{metric}->{$self->{labels}->{instance}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{global_cpu}->{$result->{metric}->{$self->{labels}->{type}}} += $average;
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{display} = $result->{metric}->{$self->{labels}->{cpu}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{$result->{metric}->{$self->{labels}->{type}}} = $average;
}
foreach my $node (keys %{$self->{nodes}}) {
foreach my $metric (keys %{$self->{nodes}->{$node}}) {
foreach my $metric (keys %{$self->{nodes}->{$node}->{global_cpu}}) {
next if ($metric =~ /cpu|display/);
$self->{nodes}->{$node}->{$metric} /= scalar(keys %{$self->{nodes}->{$node}->{cpu}});
$self->{nodes}->{$node}->{global_cpu}->{$metric} /= scalar(keys %{$self->{nodes}->{$node}->{cpu}});
}
}

View File

@ -30,15 +30,14 @@ my $instance_mode;
sub custom_usage_perfdata {
my ($self, %options) = @_;
my $extra_label = '';
$extra_label = '_' . $self->{result_values}->{label} if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
my $label = 'used';
my $value_perf = $self->{result_values}->{used};
if (defined($instance_mode->{option_results}->{free})) {
$label = 'free';
$value_perf = $self->{result_values}->{free};
}
my $extra_label = '';
$extra_label = '_' . $self->{result_values}->{multi} if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
$extra_label .= '_' . $self->{result_values}->{display} if (!defined($options{extra_instance_lvl2}) || $options{extra_instance_lvl2} != 0);
my %total_options = ();
if ($instance_mode->{option_results}->{units} eq '%') {
$total_options{total} = $self->{result_values}->{total};
@ -82,8 +81,8 @@ sub custom_usage_output {
sub custom_usage_calc {
my ($self, %options) = @_;
$self->{result_values}->{label} = $self->{instance};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{multi} = $options{new_datas}->{$self->{instance} . '_multi'};
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_size'};
$self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
$self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free};
@ -104,32 +103,44 @@ sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes storages usage are ok',
counters => [ { name => 'storage', type => 1, cb_prefix_output => 'prefix_storage_output', message_multiple => 'All storages usage are ok' } ] },
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_node_output', cb_long_output => 'node_long_output',
message_multiple => 'All nodes storages usage are ok', indent_long_output => ' ',
group => [
{ name => 'storage', display_long => 1, cb_prefix_output => 'prefix_storage_output',
message_multiple => 'All storages usage are ok', type => 1, skipped_code => { -10 => 1 } },
]
}
];
$self->{maps_counters}->{storage} = [
{ label => 'usage', set => {
key_values => [ { name => 'free' }, { name => 'size' }, { name => 'multi' }, { name => 'display' } ],
key_values => [ { name => 'free' }, { name => 'size' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_usage_calc'),
closure_custom_output => $self->can('custom_usage_output'),
closure_custom_perfdata => $self->can('custom_usage_perfdata'),
closure_custom_threshold_check => $self->can('custom_usage_threshold'),
label_extra_instance => 1, instance_use => 'display'
}
},
];
}
sub prefix_nodes_output {
sub prefix_node_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{display} . "'";
return "Node '" . $options{instance_value}->{display} . "' ";
}
sub node_long_output {
my ($self, %options) = @_;
return "Checking node '" . $options{instance_value}->{display} . "'";
}
sub prefix_storage_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{multi} . "' Storage '" . $options{instance_value}->{display} . "' ";
return "Storage '" . $options{instance_value}->{display} . "' ";
}
sub new {
@ -200,7 +211,6 @@ sub manage_selection {
foreach my $result (@{$results}) {
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{display} = $result->{metric}->{$self->{labels}->{instance}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{storage}->{$result->{metric}->{mountpoint}}->{multi} = $result->{metric}->{$self->{labels}->{instance}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{storage}->{$result->{metric}->{mountpoint}}->{display} = $result->{metric}->{$self->{labels}->{mountpoint}};
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{storage}->{$result->{metric}->{mountpoint}}->{$result->{metric}->{__name__}} = ${$result->{value}}[1];
}