enh(collection): add builtin variables (#3113)

This commit is contained in:
qgarnier 2021-09-16 14:36:20 +02:00 committed by GitHub
parent e6b1a5df89
commit f9f974c546
2 changed files with 48 additions and 6 deletions

View File

@ -97,7 +97,7 @@ sub custom_select_output {
# without formatting: [name: xxxxxx][test: xxxx][test2: xxx][mytable.plcRead: xxx][mytable.plcWrite: xxx] # without formatting: [name: xxxxxx][test: xxxx][test2: xxx][mytable.plcRead: xxx][mytable.plcWrite: xxx]
my $output = ''; my $output = '';
foreach (sort keys %{$self->{result_values}->{expand}}) { foreach (sort keys %{$self->{result_values}->{expand}}) {
next if (/^constants\./); next if (/^(?:constants|builtin)\./);
$output .= '[' . $_ . ': ' . $self->{result_values}->{expand}->{$_} . ']'; $output .= '[' . $_ . ': ' . $self->{result_values}->{expand}->{$_} . ']';
} }
@ -139,6 +139,8 @@ sub new {
$self->{safe_func} = Safe->new(); $self->{safe_func} = Safe->new();
$self->{safe_func}->share('$assign_var'); $self->{safe_func}->share('$assign_var');
$self->{builtin} = {};
$self->{snmp_cache} = centreon::plugins::statefile->new(%options); $self->{snmp_cache} = centreon::plugins::statefile->new(%options);
return $self; return $self;
} }
@ -401,6 +403,7 @@ sub collect_snmp {
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->add_builtin(name => 'currentTime', value => time());
if ($self->use_snmp_cache(snmp => $options{snmp}) == 0) { if ($self->use_snmp_cache(snmp => $options{snmp}) == 0) {
$self->{snmp_collected_sampling} = { tables => {}, leefs => {}, epoch => time() }; $self->{snmp_collected_sampling} = { tables => {}, leefs => {}, epoch => time() };
$self->{snmp_collected} = { tables => {}, leefs => {}, epoch => time(), sampling => 0 }; $self->{snmp_collected} = { tables => {}, leefs => {}, epoch => time(), sampling => 0 };
@ -753,6 +756,20 @@ sub substitute_string {
return $str; return $str;
} }
sub add_builtin {
my ($self, %options) = @_;
$self->{builtin}->{ $options{name} } = $options{value};
}
sub set_builtin {
my ($self, %options) = @_;
foreach (keys %{$self->{builtin}}) {
$self->{expand}->{ 'builtin.' . $_ } = $self->{builtin}->{$_};
}
}
sub set_constants { sub set_constants {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -1298,6 +1315,7 @@ sub add_selection {
$i++; $i++;
my $config = {}; my $config = {};
$self->{expand} = $self->set_constants(); $self->{expand} = $self->set_constants();
$self->set_builtin();
$self->{expand}->{name} = $_->{name} if (defined($_->{name})); $self->{expand}->{name} = $_->{name} if (defined($_->{name}));
$self->set_functions(section => "selection > $i > functions", functions => $_->{functions}, position => 'before_expand'); $self->set_functions(section => "selection > $i > functions", functions => $_->{functions}, position => 'before_expand');
$self->set_expand_table(section => "selection > $i > expand_table", expand => $_->{expand_table}); $self->set_expand_table(section => "selection > $i > expand_table", expand => $_->{expand_table});
@ -1336,6 +1354,7 @@ sub add_selection_loop {
foreach my $instance (keys %{$self->{snmp_collected}->{tables}->{ $result->{table} }}) { foreach my $instance (keys %{$self->{snmp_collected}->{tables}->{ $result->{table} }}) {
$self->{expand} = $self->set_constants(); $self->{expand} = $self->set_constants();
$self->set_builtin();
$self->{expand}->{ $result->{table} . '.instance' } = $instance; $self->{expand}->{ $result->{table} . '.instance' } = $instance;
foreach my $label (keys %{$self->{snmp_collected}->{tables}->{ $result->{table} }->{$instance}}) { foreach my $label (keys %{$self->{snmp_collected}->{tables}->{ $result->{table} }->{$instance}}) {
$self->{expand}->{ $result->{table} . '.' . $label } = $self->{expand}->{ $result->{table} . '.' . $label } =
@ -1393,10 +1412,10 @@ sub disco_show {
foreach (values %{$self->{selections}}) { foreach (values %{$self->{selections}}) {
my $entry = {}; my $entry = {};
foreach my $label (keys %{$_->{expand}}) { foreach my $label (keys %{$_->{expand}}) {
next if ($label =~ /^constants\./); next if ($label =~ /^(?:constants|builtin)\./);
my $name = $label; my $name = $label;
$name =~ s/\./_/g; $name =~ s/\./_/g;
$entry->{$name} = $_->{expand}->{$label}; $entry->{$name} = defined($_->{expand}->{$label}) ? $_->{expand}->{$label} : '';
} }
$self->{output}->add_disco_entry(%$entry); $self->{output}->add_disco_entry(%$entry);
} }

View File

@ -28,6 +28,7 @@ use JSON::XS;
use Safe; use Safe;
use centreon::plugins::statefile; use centreon::plugins::statefile;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use Time::HiRes qw(gettimeofday tv_interval);
sub custom_select_threshold { sub custom_select_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -97,7 +98,7 @@ sub custom_select_output {
# without formatting: [name: xxxxxx][test: xxxx][test2: xxx][mytable.plcRead: xxx][mytable.plcWrite: xxx] # without formatting: [name: xxxxxx][test: xxxx][test2: xxx][mytable.plcRead: xxx][mytable.plcWrite: xxx]
my $output = ''; my $output = '';
foreach (sort keys %{$self->{result_values}->{expand}}) { foreach (sort keys %{$self->{result_values}->{expand}}) {
next if (/^constants\./); next if (/^(?:constants|builtin)\./);
$output .= '[' . $_ . ': ' . (defined($self->{result_values}->{expand}->{$_}) ? $self->{result_values}->{expand}->{$_} : '') . ']'; $output .= '[' . $_ . ': ' . (defined($self->{result_values}->{expand}->{$_}) ? $self->{result_values}->{expand}->{$_} : '') . ']';
} }
@ -139,6 +140,8 @@ sub new {
$self->{safe_func} = Safe->new(); $self->{safe_func} = Safe->new();
$self->{safe_func}->share('$assign_var'); $self->{safe_func}->share('$assign_var');
$self->{builtin} = {};
$self->{sql_cache} = centreon::plugins::statefile->new(%options); $self->{sql_cache} = centreon::plugins::statefile->new(%options);
return $self; return $self;
} }
@ -220,7 +223,10 @@ sub collect_sql_tables {
$self->{expand} = $self->set_constants(); $self->{expand} = $self->set_constants();
$table->{query} = $self->substitute_string(value => $table->{query}); $table->{query} = $self->substitute_string(value => $table->{query});
my $timing0 = [gettimeofday];
$options{sql}->query(query => $table->{query}); $options{sql}->query(query => $table->{query});
$self->add_builtin(name => 'sqlExecutionTime.' . $table->{name}, value => tv_interval($timing0, [gettimeofday]));
my $i = 0; my $i = 0;
while (my $entry = $options{sql}->fetchrow_hashref()) { while (my $entry = $options{sql}->fetchrow_hashref()) {
my $instance = $i; my $instance = $i;
@ -365,6 +371,7 @@ sub collect_sql {
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->add_builtin(name => 'currentTime', value => time());
if ($self->use_sql_cache(sql => $options{sql}) == 0) { if ($self->use_sql_cache(sql => $options{sql}) == 0) {
$self->{sql_collected_sampling} = { tables => {}, epoch => time() }; $self->{sql_collected_sampling} = { tables => {}, epoch => time() };
$self->{sql_collected} = { tables => {}, epoch => time(), sampling => 0 }; $self->{sql_collected} = { tables => {}, epoch => time(), sampling => 0 };
@ -685,6 +692,20 @@ sub substitute_string {
return $str; return $str;
} }
sub add_builtin {
my ($self, %options) = @_;
$self->{builtin}->{ $options{name} } = $options{value};
}
sub set_builtin {
my ($self, %options) = @_;
foreach (keys %{$self->{builtin}}) {
$self->{expand}->{ 'builtin.' . $_ } = $self->{builtin}->{$_};
}
}
sub set_constants { sub set_constants {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -1209,6 +1230,7 @@ sub add_selection {
$i++; $i++;
my $config = {}; my $config = {};
$self->{expand} = $self->set_constants(); $self->{expand} = $self->set_constants();
$self->set_builtin();
$self->{expand}->{name} = $_->{name} if (defined($_->{name})); $self->{expand}->{name} = $_->{name} if (defined($_->{name}));
$self->set_functions(section => "selection > $i > functions", functions => $_->{functions}, position => 'before_expand'); $self->set_functions(section => "selection > $i > functions", functions => $_->{functions}, position => 'before_expand');
$self->set_expand_table(section => "selection > $i > expand_table", expand => $_->{expand_table}); $self->set_expand_table(section => "selection > $i > expand_table", expand => $_->{expand_table});
@ -1247,6 +1269,7 @@ sub add_selection_loop {
foreach my $instance (keys %{$self->{sql_collected}->{tables}->{ $result->{table} }}) { foreach my $instance (keys %{$self->{sql_collected}->{tables}->{ $result->{table} }}) {
$self->{expand} = $self->set_constants(); $self->{expand} = $self->set_constants();
$self->set_builtin();
$self->{expand}->{ $result->{table} . '.instance' } = $instance; $self->{expand}->{ $result->{table} . '.instance' } = $instance;
foreach my $label (keys %{$self->{sql_collected}->{tables}->{ $result->{table} }->{$instance}}) { foreach my $label (keys %{$self->{sql_collected}->{tables}->{ $result->{table} }->{$instance}}) {
$self->{expand}->{ $result->{table} . '.' . $label } = $self->{expand}->{ $result->{table} . '.' . $label } =
@ -1304,10 +1327,10 @@ sub disco_show {
foreach (values %{$self->{selections}}) { foreach (values %{$self->{selections}}) {
my $entry = {}; my $entry = {};
foreach my $label (keys %{$_->{expand}}) { foreach my $label (keys %{$_->{expand}}) {
next if ($label =~ /^constants\./); next if ($label =~ /^(?:constants|builtin)\./);
my $name = $label; my $name = $label;
$name =~ s/\./_/g; $name =~ s/\./_/g;
$entry->{$name} = $_->{expand}->{$label}; $entry->{$name} = defined($_->{expand}->{$label}) ? $_->{expand}->{$label} : '';
} }
$self->{output}->add_disco_entry(%$entry); $self->{output}->add_disco_entry(%$entry);
} }