Solved

How to code multiple thresholds --warning-* and --critical-* in a plugin?

  • 21 June 2023
  • 6 replies
  • 499 views

Userlevel 5
Badge +14

Hello

This question is probably for someone who know how the whole centreon plugin works, probably someone from centreon 

I’m working on a plugin for a SAN appliance with Rest, I did all the groundwork, followed the guidelines etc.. and I’m now tying up all the command line parameters and trying to have “correct” plugin

 

in practice I managed to get my data selection working, and for example I use this in the loop for “manage_selection”

        $self->{filesystem}->{ $filesystem->{'NAME'} } = {
name => $filesystem->{'NAME'},
storage_pool => $filesystem->{'PARENTNAME'},
space => {
total_space => $total,
used_space => $used,
free_space => $free,
prot_space => $prot,
prct_used_space => $prct
}

I have a custom “space usage output” and some standard long output/prefix output


sub custom_space_usage_output {
my ($self, %options) = @_;

my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space});
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space});
my ($total_prot_value, $total_prot_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{prot_space});
return sprintf(
"size: %s used: %s (%.2f%%) protection: %s",
$total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit,
$self->{result_values}->{prct_used_space},
$total_prot_value . " " . $total_prot_unit
);
}

sub filesystem_long_output {
my ($self, %options) = @_;

return sprintf(
"checking FileSystem '%s' [sp: %s]",
$options{instance_value}->{name},
$options{instance_value}->{storage_pool}
);
}

sub prefix_filesystem_output {
my ($self, %options) = @_;

return sprintf(
"FileSystem '%s' [sp: %s] ",
$options{instance_value}->{name},
$options{instance_value}->{storage_pool}
);
}

 

nothing fancy, I took idea from existing plugin.

then I have the “set_counter” headache 

 

sub set_counters {
my ($self, %options) = @_;

$self->{maps_counters_type} = [
{ name => 'filesystem', type => 3, cb_prefix_output => 'prefix_filesystem_output', cb_long_output => 'filesystem_long_output',
indent_long_output => ' ', message_multiple => 'All FileSystems are ok',
group => [
{ name => 'space', type => 0, skipped_code => { -10 => 1 } }
]
}
];

$self->{maps_counters}->{space} = [
{ label => 'space-usage', nlabel => 'filesystem.space.usage.bytes', set => {
key_values => [ { name => 'used_space' }, { name => 'total_space' },{ name => 'prct_used_space' }, { name => 'prot_space' }],
closure_custom_output => $self->can('custom_space_usage_output'),
perfdatas => [
{ template => '%d', min => 0, max => 'total_space', unit => 'B', cast_int => 1, label_extra_instance => 1 }
]
}
},
{ label => 'space-usage-prot', nlabel => 'filesystem.space.prot.bytes', set => {
key_values => [ { name => 'prot_space' }, { name => 'total_space' }, { name => 'used_space' }, { name => 'prct_used_space' } ],
closure_custom_output => $self->can('custom_space_usage_output'),
perfdatas => [
{ template => '%d', min => 0, max => 'total_space', unit => 'B', cast_int => 1, label_extra_instance => 1 }
]
}
},
{ label => 'space-usage-prct', nlabel => 'filesystem.space.usage.percentage', set => {
key_values => [ { name => 'prct_used_space' }, { name => 'total_space' }, { name => 'used_space' }, { name => 'prot_space' } ],
closure_custom_output => $self->can('custom_space_usage_output'),
perfdatas => [
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
]
}
}
];
}

 

 

so, here is the plugin output, it’s almost what I want :

OK: FileSystem 'Repository-TCO' [sp: StoragePool001] size: 1650.00 TB used: 1232.29 TB (74.68%) protection: 393.57 TB, size: 1650.00 TB used: 1232.29 TB (74.68%) protection: 393.57 TB, size: 1650.00 TB used: 1232.29 TB (74.68%) protection: 393.57 TB | 'Repository-TCO#filesystem.space.usage.bytes'=1354921323116032B;;;0;1814194185830400 'Repository-TCO#filesystem.space.prot.bytes'=432735102616064B;;;0;1814194185830400 'Repository-TCO#filesystem.space.usage.percentage'=74.68%;;;0;100
checking FileSystem 'Repository-TCO' [sp: StoragePool001]
    size: 1650.00 TB used: 1232.29 TB (74.68%) protection: 393.57 TB, size: 1650.00 TB used: 1232.29 TB (74.68%) protection: 393.57 TB, size: 1650.00 TB used: 1232.29 TB (74.68%) protection: 393.57 TB
 

I want to be able to have multiple counter alarm

--warning-space-usage

--warning-space-usage-protection

--warning-space-usage-prct

 

but I don’t want to have 3 time the short long output

if I want a --warning-XXX I couldn’t fin a way to have it work I didn’t put a counter with the name XXX

if I put a counter with the name XXX , I couldn’t find a way to “hide” it

 

ideally I want to have threshold in %, but metric in bytes

(and the possibility to disply metric in % instead of byte, like the option you see sometime “--unit=’%’ ”, with “--unit=’B’ ” by default, but this is more a cosmetic need, if I put the plugin on the community, you will always have someone asking for the option, or asking to display free space instead of used space, etc… just thinking ahead here, I don’t need it for me.)

 

I couldn’t find a way to setup this as I couldn’t find more documentation than whats on the documentation page and I’m not sure how to approach the settings in the 

 $self->{maps_counters_type} call

and 

$self->{maps_counters}->{space} 

I think it’s probably somewhere in here, maybe I shouldn’t have used a “group”, no idea really

there are ‘type’ and lots of sub options to pass here but I couldn’t find more explanation

 

so : is there a documentation explaining both map_counters_type and map_counter somewhere?

or can someone guide me on how to create threshold without ouput?

 

thanks in advance

 

 

edit : here is the doc I was refering to : Develop with centreon-plugins | Centreon Documentation

icon

Best answer by ldubrunfaut 26 June 2023, 14:41

View original

6 replies

Userlevel 4
Badge +13

👋 @christophe.niel-ACT 

Maybe you can reference to other connectors?

 

https://github.com/centreon/centreon-plugins/blob/develop/src/cloud/aws/rds/mode/instancestatus.pm#L206

 

https://github.com/centreon/centreon-plugins/blob/develop/src/cloud/aws/rds/mode/instancestatus.pm#L54

 

@Dalfo 

Userlevel 5
Badge +14

hi

indeed that I was doing, found other things too, like adding “display_ok => 0” in the map counter, that will hide the counter if no error, but then the output is broken, 

but thanks to your example I will try to use “output_template => "blahblah : %s" in the metric I use for threshold… when i have time.

 

the example you gave me are interesting, still trying to understand the “type” of counter

 $self->{maps_counters_type} = [
        { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
        { name => 'aws_instances', type => 1, cb_prefix_output => 'prefix_awsinstance_output', message_multiple => 'All instances are ok' },
    ];

 

Type => 0 or 1, when the type I use is 3 (don’t ask me why, it was like that in another code I used)

 

and when you read the example doc specifically on this point : 

    $self->{maps_counters_type} = [
# health and queries are global metric, they don't refer to a specific instance.
# In other words, you cannot get several values for health or queries
# That's why the type is 0.
{ name => 'health', type => 0, cb_prefix_output => 'prefix_health_output' },
{ name => 'queries', type => 0, cb_prefix_output => 'prefix_queries_output' },
# app_metrics groups connections and errors and each will receive value for both instances (my-awesome-frontend and my-awesome-db)
# the type => 1 explicits that
# as above, you can define a callback (cb) function to manage the output prefix. This function is called
# each time a value is passed to the counter and can be shared across multiple counters.
{ name => 'app_metrics', type => 1, cb_prefix_output => 'prefix_app_output' }
];

 

but when you try to read the perl for /src/centreon/plugins/… where all this is defined, my head is hurting with this file https://github.com/centreon/centreon-plugins/blob/8c8f40632faeb03b014f1eacdfaf003f2289e572/src/centreon/plugins/templates/counter.pm#L42 

 

Userlevel 4
Badge +13

I will ask around, no promises though 😁😅

Userlevel 5
Badge +14

thanks i’m swamped with work now so i still need to play around with the various options I found

(i’ll also check the new disk-usage check in the linux snmp, from another post in the forum, I just saw it work exactly how I need, but I haven’t read the code yet)

Userlevel 2
Badge +2

Hi,

I’m not sure I can answer to all the questions you are asking here but I can explain the type variable.
The type can have 4 values :

  • type 0 (global) : counter with only one value return (example : space available on a disk)
  • type 1 (instance) : counter returning a table of values (example : space available on several disks)
  • type 2 : counter returning a string corresponding to a status (example : health statue of a service)
  • type 3 (complex instance) : counter returning multiple instances (type 1) => table of table (example : space available on several disks on several computers but also their CPU usage). It’s a complex structure used in some cases where counters need to keep together some informations that are more relevant in a same counter than in separate counters. 


Note that type in the maps_counters_type function can only be 0, 1 or 3 (a single value, a table of values or a table of tables).
The type 2 only occurs in maps_counters function to define a counter as a status. Also in this function if no type is specified, it’s type=0 by default.

Plugin development documentation should be soon updated with some examples about type variable.

Overall, type 3 is the most complex case to use, the easiest way is to take a plugin already available in our open source project and try to feet the most with the structure use. In your case I think you should define the counter “filesystem” as a type 1 and then with the function map_counters put counters one by one as type 0 (you don’t have to declare it because of default value type at 0). 
This plugin mode may help you to understand how to feet in your case for counters declaration:
https://github.com/centreon/centreon-plugins/blob/develop/src/network/cambium/cnpilot/snmp/mode/radios.pm

Userlevel 5
Badge +14

Thanks for this explanation,

I did find out the 4 type in the counters.pm code, but your explanation helps a lot

I think type 1 is the way to go, I don’t really understand  what the “closure” are, but I’m not sure I need that

 

I just need to play around with whatever perfdata I want to output or not, and text I want to display or not

Reply