WIP: paloalto cli in ssh

This commit is contained in:
garnier-quentin 2019-10-24 11:32:25 +02:00
parent 5a540d1712
commit 09a779fe74
8 changed files with 778 additions and 22 deletions

View File

@ -118,28 +118,27 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"hostname:s" => { name => 'hostname' },
"remote" => { name => 'remote' },
"ssh-option:s@" => { name => 'ssh_option' },
"ssh-path:s" => { name => 'ssh_path' },
"ssh-command:s" => { name => 'ssh_command', default => 'ssh' },
"timeout:s" => { name => 'timeout', default => 30 },
"sudo" => { name => 'sudo' },
"command:s" => { name => 'command' },
"command-path:s" => { name => 'command_path' },
"command-options:s" => { name => 'command_options' },
"warning-engine-status:s" => { name => 'warning_engine_status', default => '' },
"critical-engine-status:s" => { name => 'critical_engine_status', default => '%{last_engine_version} ne %{current_engine_version}' },
"warning-maindb-status:s" => { name => 'warning_maindb_status', default => '' },
"critical-maindb-status:s" => { name => 'critical_maindb_status', default => '%{last_maindb_version} ne %{current_maindb_version}' },
"warning-dailydb-status:s" => { name => 'warning_dailydb_status', default => '' },
"critical-dailydb-status:s" => { name => 'critical_dailydb_status', default => '%{last_dailydb_version} ne %{current_dailydb_version} || %{current_dailydb_timediff} > 432000' },
"nameservers:s@" => { name => 'nameservers' },
"maindb-file:s" => { name => 'maindb_file', default => '/var/lib/clamav/main.cvd' },
"dailydb-file:s" => { name => 'dailydb_file', default => '/var/lib/clamav/daily.cvd' },
});
$options{options}->add_options(arguments => {
'hostname:s' => { name => 'hostname' },
'remote' => { name => 'remote' },
'ssh-option:s@' => { name => 'ssh_option' },
'ssh-path:s' => { name => 'ssh_path' },
'ssh-command:s' => { name => 'ssh_command', default => 'ssh' },
'timeout:s' => { name => 'timeout', default => 30 },
'sudo' => { name => 'sudo' },
'command:s' => { name => 'command' },
'command-path:s' => { name => 'command_path' },
'command-options:s' => { name => 'command_options' },
'warning-engine-status:s' => { name => 'warning_engine_status', default => '' },
'critical-engine-status:s' => { name => 'critical_engine_status', default => '%{last_engine_version} ne %{current_engine_version}' },
'warning-maindb-status:s' => { name => 'warning_maindb_status', default => '' },
'critical-maindb-status:s' => { name => 'critical_maindb_status', default => '%{last_maindb_version} ne %{current_maindb_version}' },
'warning-dailydb-status:s' => { name => 'warning_dailydb_status', default => '' },
'critical-dailydb-status:s' => { name => 'critical_dailydb_status', default => '%{last_dailydb_version} ne %{current_dailydb_version} || %{current_dailydb_timediff} > 432000' },
'nameservers:s@' => { name => 'nameservers' },
'maindb-file:s' => { name => 'maindb_file', default => '/var/lib/clamav/main.cvd' },
'dailydb-file:s' => { name => 'dailydb_file', default => '/var/lib/clamav/daily.cvd' },
});
return $self;
}

View File

@ -0,0 +1,195 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package network::paloalto::ssh::custom::cli;
use strict;
use warnings;
use centreon::plugins::misc;
use XML::Simple;
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
'hostname:s' => { name => 'hostname' },
'ssh-option:s@' => { name => 'ssh_option' },
'ssh-path:s' => { name => 'ssh_path' },
'ssh-command:s' => { name => 'ssh_command', default => 'ssh' },
'timeout:s' => { name => 'timeout', default => 45 },
'command:s' => { name => 'command' },
'command-path:s' => { name => 'command_path' },
'command-options:s' => { name => 'command_options' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'SSH OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {
my ($self, %options) = @_;
foreach (keys %{$options{default}}) {
if ($_ eq $self->{mode}) {
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
foreach my $opt (keys %{$options{default}->{$_}[$i]}) {
if (!defined($self->{option_results}->{$opt}[$i])) {
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
}
}
}
}
}
}
sub check_options {
my ($self, %options) = @_;
$self->{option_results}->{remote} = 1;
if (defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '') {
$self->{option_results}->{remote} = undef;
} elsif (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to set hostname option.');
$self->{output}->option_exit();
}
return 0;
}
##############
# Specific methods
##############
sub execute_command {
my ($self, %options) = @_;
$self->{ssh_commands} =
"set cli op-command-xml-output on\n" .
$options{command} . "\n";
my ($stdout, $exit) = centreon::plugins::misc::execute(
ssh_pipe => 1,
output => $self->{output},
options => $self->{option_results},
command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $self->{ssh_commands},
command_path => $self->{option_results}->{command_path},
command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : undef
);
$self->{output}->output_add(long_msg => "command response: $stdout", debug => 1);
if ($stdout !~ /(<response status="(.*?)">.*<\/response>)/ms) {
$self->{output}->add_option_msg(short_msg => "Cannot find xml response");
$self->{output}->option_exit();
}
my ($xml, $status) = ($1, $2);
if ($status ne 'success') {
$self->{output}->add_option_msg(short_msg => "command response status: $status");
$self->{output}->option_exit();
}
my $result;
eval {
$result = XMLin($xml, ForceArray => $options{ForceArray}, KeyAttr => []);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@");
$self->{output}->option_exit();
}
return $result->{result};
}
1;
__END__
=head1 NAME
ssh
=head1 SYNOPSIS
my ssh
=head1 SSH OPTIONS
=over 8
=item B<--hostname>
Hostname to query.
=item B<--ssh-option>
Specify multiple options like the user (example: --ssh-option='-l=centreon-engine' --ssh-option='-p=52').
=item B<--ssh-path>
Specify ssh command path (default: none)
=item B<--ssh-command>
Specify ssh command (default: 'ssh'). Useful to use 'plink'.
=item B<--timeout>
Timeout in seconds for the command (Default: 45).
=item B<--command>
Command to get information. Used it you have output in a file.
=item B<--command-path>
Command path.
=item B<--command-options>
Command options.
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,62 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package network::paloalto::ssh::mode::components::psu;
use strict;
use warnings;
sub check {
my ($self) = @_;
$self->{output}->output_add(long_msg => "checking power supplies");
$self->{components}->{psu} = { name => 'psu', total => 0, skip => 0 };
return if ($self->check_filter(section => 'psu'));
foreach (values %{$self->{results}->{'power-supply'}}) {
foreach my $entity (@{$_->{entry}}) {
my $instance = $entity->{description};
next if ($self->check_filter(section => 'psu', instance => $instance));
$self->{components}->{psu}->{total}++;
$self->{output}->output_add(
long_msg => sprintf(
"power supply '%s' alarm is '%s' [instance: %s]",
$instance,
$entity->{alarm},
$instance
)
);
my $exit = $self->get_severity(label => 'default', section => 'psu', value => $entity->{alarm});
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(
severity => $exit,
short_msg => sprintf(
"power supply '%s' alarm is '%s'",
$instance,
$entity->{alarm}
)
);
}
}
}
}
1;

View File

@ -0,0 +1,80 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package network::paloalto::ssh::mode::components::temperature;
use strict;
use warnings;
sub check {
my ($self) = @_;
$self->{output}->output_add(long_msg => "checking temperatures");
$self->{components}->{temperature} = { name => 'temperatures', total => 0, skip => 0 };
return if ($self->check_filter(section => 'temperature'));
foreach (values %{$self->{results}->{'thermal'}}) {
foreach my $entity (@{$_->{entry}}) {
my $instance = $entity->{description};
next if ($self->check_filter(section => 'temperature', instance => $instance));
$self->{components}->{temperature}->{total}++;
$self->{output}->output_add(
long_msg => sprintf(
"temperature '%s' alarm is '%s' [instance: %s, value: %s C]",
$instance,
$entity->{alarm},
$instance,
$entity->{DegreesC}
)
);
my $exit = $self->get_severity(label => 'default', section => 'temperature', value => $entity->{alarm});
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(
severity => $exit,
short_msg => sprintf(
"temperature '%s' alarm is '%s'",
$instance,
$entity->{alarm}
)
);
}
my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric(section => 'temperature', instance => $instance, value => $entity->{DegreesC});
if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(
severity => $exit2,
short_msg => sprintf("temperature '%s' is %s C", $instance, $entity->{DegreesC})
);
}
$self->{output}->perfdata_add(
unit => 'C',
nlabel => 'hardware.temperature.celsius',
instances => $instance,
value => $entity->{DegreesC},
warning => $warn,
critical => $crit,
min => $entity->{min}, max => $entity->{max}
);
}
}
}
1;

View File

@ -0,0 +1,80 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package network::paloalto::ssh::mode::components::voltage;
use strict;
use warnings;
sub check {
my ($self) = @_;
$self->{output}->output_add(long_msg => "checking voltages");
$self->{components}->{voltage} = { name => 'voltages', total => 0, skip => 0 };
return if ($self->check_filter(section => 'voltage'));
foreach (values %{$self->{results}->{power}}) {
foreach my $entity (@{$_->{entry}}) {
my $instance = $entity->{description};
next if ($self->check_filter(section => 'voltage', instance => $instance));
$self->{components}->{voltage}->{total}++;
$self->{output}->output_add(
long_msg => sprintf(
"voltage '%s' alarm is '%s' [instance: %s, value: %s V]",
$instance,
$entity->{alarm},
$instance,
$entity->{Volts}
)
);
my $exit = $self->get_severity(label => 'default', section => 'voltage', value => $entity->{alarm});
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(
severity => $exit,
short_msg => sprintf(
"voltage '%s' alarm is '%s'",
$instance,
$entity->{alarm}
)
);
}
my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric(section => 'voltage', instance => $instance, value => $entity->{Volts});
if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(
severity => $exit2,
short_msg => sprintf("voltage '%s' is %s V", $instance, $entity->{Volts})
);
}
$self->{output}->perfdata_add(
unit => 'V',
nlabel => 'hardware.voltage.volt',
instances => $instance,
value => $entity->{Volts},
warning => $warn,
critical => $crit,
min => $entity->{min}, max => $entity->{max}
);
}
}
}
1;

View File

@ -0,0 +1,112 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package network::paloalto::ssh::mode::environment;
use base qw(centreon::plugins::templates::hardware);
use strict;
use warnings;
sub set_system {
my ($self, %options) = @_;
$self->{regexp_threshold_overload_check_section_option} =
'^(?:psu|temperature|voltage)$';
$self->{regexp_threshold_numeric_check_section_option} = '^(?:temperature|voltage)$';
$self->{cb_hook2} = 'ssh_execute';
$self->{thresholds} = {
default => [
['false', 'OK'],
['.*', 'CRITICAL'],
]
};
$self->{components_exec_load} = 0;
$self->{components_path} = 'network::paloalto::ssh::mode::components';
$self->{components_module} = ['temperature', 'voltage', 'psu'];
}
sub ssh_execute {
my ($self, %options) = @_;
($self->{results}, $self->{exit_code}) = $options{custom}->execute_command(
command => 'show system environmentals',
);
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, no_absent => 1, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
});
return $self;
}
1;
__END__
=head1 MODE
Check components.
=over 8
=item B<--component>
Which component to check (Default: '.*').
Can be: 'psu', 'temperature', 'voltage'.
=item B<--filter>
Exclude some parts (comma seperated list) (Example: --filter=temperature)
Can also exclude specific instance: --filter='temperature,Temperature @ U48'
=item B<--no-component>
Return an error if no compenents are checked.
If total (with skipped) is 0. (Default: 'critical' returns).
=item B<--threshold-overload>
Set to overload default threshold values (syntax: section,[instance,]status,regexp)
It used before default thresholds (order stays).
Example: --threshold-overload='temperture,OK,true'
=item B<--warning>
Set warning threshold for 'temperature', 'voltage' (syntax: type,regexp,threshold)
Example: --warning='temperature,.*,30'
=item B<--critical>
Set critical threshold for 'temperature', 'voltage' (syntax: type,regexp,threshold)
Example: --critical='temperature,.*,50'
=back
=cut

View File

@ -0,0 +1,178 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package network::paloalto::ssh::mode::system;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
use DateTime;
use centreon::plugins::misc;
sub custom_status_output {
my ($self, %options) = @_;
my $msg = 'system operational mode: ' . $self->{result_values}->{oper_mode};
return $msg;
}
sub custom_av_output {
my ($self, %options) = @_;
return sprintf(
"antivirus version '%s', last update %s",
$self->{result_values}->{av_version_absolute},
centreon::plugins::misc::change_seconds(value => $self->{result_values}->{av_lastupdate_time_absolute})
);
}
sub custom_threat_output {
my ($self, %options) = @_;
return sprintf(
"threat version '%s', last update %s",
$self->{result_values}->{threat_version_absolute},
centreon::plugins::misc::change_seconds(value => $self->{result_values}->{threat_lastupdate_time_absolute})
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'system', type => 0, message_separator => ' - ' }
];
$self->{maps_counters}->{system} = [
{ label => 'status', threshold => 0, set => {
key_values => [ { name => 'oper_mode' } ],
closure_custom_calc => \&catalog_status_calc,
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold,
}
},
{ label => 'av-update', nlabel => 'system.antivirus.lastupdate.time.seconds', set => {
key_values => [ { name => 'av_lastupdate_time' }, { name => 'av_version' } ],
closure_custom_output => $self->can('custom_av_output'),
perfdatas => [
{ value => 'av_lastupdate_time_absolute', template => '%d', min => 0, unit => 's' }
],
}
},
{ label => 'threat-update', nlabel => 'system.threat.lastupdate.time.seconds', set => {
key_values => [ { name => 'threat_lastupdate_time' }, { name => 'threat_version' } ],
closure_custom_output => $self->can('custom_threat_output'),
perfdatas => [
{ value => 'threat_lastupdate_time_absolute', template => '%d', min => 0, unit => 's' }
],
}
},
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => { name => 'critical_status', default => '%{oper_mode} !~ /normal/i' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status']);
}
sub get_diff_time {
my ($self, %options) = @_;
# '2019/10/15 12:03:58 BST'
return if ($options{time} !~ /^\s*(\d{4})\/(\d{2})\/(\d{2})\s+(\d+):(\d+):(\d+)\s+(\S+)/);
my $tz = $7 eq 'BST' ? 'GMT' : $7;
my $dt = DateTime->new(
year => $1,
month => $2,
day => $3,
hour => $4,
minute => $5,
second => $6,
time_zone => $tz
);
return (time() - $dt->epoch);
}
sub manage_selection {
my ($self, %options) = @_;
my $result = $options{custom}->execute_command(command => 'show system info');
$self->{system} = {
av_lastupdate_time => $self->get_diff_time(time => $result->{system}->{'av-release-date'}),
threat_lastupdate_time => $self->get_diff_time(time => $result->{system}->{'threat-release-date'}),
av_version => $result->{system}->{'av-version'},
threat_version => $result->{system}->{'threat-version'},
oper_mode => $result->{system}->{'operational-mode'},
};
}
1;
__END__
=head1 MODE
Check system.
=over 8
=item B<--filter-counters>
Only display some counters (regexp can be used).
Example: --filter-counters='^status$'
=item B<--warning-status>
Set warning threshold for status (Default: '').
Can used special variables like: %{oper_mode}
=item B<--critical-status>
Set critical threshold for status (Default: '%{oper_mode} !~ /normal/i').
Can used special variables like: %{oper_mode}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'av-update' (s), 'threat-update' (s).
=back
=cut

View File

@ -0,0 +1,50 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package network::paloalto::ssh::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
%{$self->{modes}} = (
'environment' => 'network::paloalto::ssh::mode::environment',
'system' => 'network::paloalto::ssh::mode::system',
);
$self->{custom_modes}{ssh} = 'network::paloalto::ssh::custom::cli';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check PaloAlto with cli in ssh.
=cut