add boot status for systemd-sc-status

This commit is contained in:
garnier-quentin 2019-10-02 15:43:03 +02:00
parent 2e02c68899
commit 482bf864b8
1 changed files with 66 additions and 43 deletions

View File

@ -29,8 +29,14 @@ use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold)
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = 'status : ' . $self->{result_values}->{load} . '/' . $self->{result_values}->{active} . '/' . $self->{result_values}->{sub};
my $msg = sprintf(
'status : %s/%s/%s [boot: %s]',
$self->{result_values}->{load},
$self->{result_values}->{active},
$self->{result_values}->{sub},
$self->{result_values}->{boot}
);
return $msg; return $msg;
} }
@ -40,6 +46,7 @@ sub custom_status_calc {
$self->{result_values}->{load} = $options{new_datas}->{$self->{instance} . '_load'}; $self->{result_values}->{load} = $options{new_datas}->{$self->{instance} . '_load'};
$self->{result_values}->{active} = $options{new_datas}->{$self->{instance} . '_active'}; $self->{result_values}->{active} = $options{new_datas}->{$self->{instance} . '_active'};
$self->{result_values}->{sub} = $options{new_datas}->{$self->{instance} . '_sub'}; $self->{result_values}->{sub} = $options{new_datas}->{$self->{instance} . '_sub'};
$self->{result_values}->{boot} = $options{new_datas}->{$self->{instance} . '_boot'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0; return 0;
} }
@ -53,7 +60,7 @@ sub set_counters {
]; ];
$self->{maps_counters}->{global} = [ $self->{maps_counters}->{global} = [
{ label => 'total-running', set => { { label => 'total-running', nlabel => 'systemd.services.running.count', set => {
key_values => [ { name => 'running' }, { name => 'total' } ], key_values => [ { name => 'running' }, { name => 'total' } ],
output_template => 'Total Running: %s', output_template => 'Total Running: %s',
perfdatas => [ perfdatas => [
@ -62,7 +69,7 @@ sub set_counters {
], ],
} }
}, },
{ label => 'total-failed', set => { { label => 'total-failed', nlabel => 'systemd.services.failed.count', set => {
key_values => [ { name => 'failed' }, { name => 'total' } ], key_values => [ { name => 'failed' }, { name => 'total' } ],
output_template => 'Total Failed: %s', output_template => 'Total Failed: %s',
perfdatas => [ perfdatas => [
@ -71,7 +78,7 @@ sub set_counters {
], ],
} }
}, },
{ label => 'total-dead', set => { { label => 'total-dead', nlabel => 'systemd.services.dead.count', set => {
key_values => [ { name => 'dead' }, { name => 'total' } ], key_values => [ { name => 'dead' }, { name => 'total' } ],
output_template => 'Total Dead: %s', output_template => 'Total Dead: %s',
perfdatas => [ perfdatas => [
@ -80,7 +87,7 @@ sub set_counters {
], ],
} }
}, },
{ label => 'total-exited', set => { { label => 'total-exited', nlabel => 'systemd.services.exited.count', set => {
key_values => [ { name => 'exited' }, { name => 'total' } ], key_values => [ { name => 'exited' }, { name => 'total' } ],
output_template => 'Total Exited: %s', output_template => 'Total Exited: %s',
perfdatas => [ perfdatas => [
@ -92,7 +99,7 @@ sub set_counters {
]; ];
$self->{maps_counters}->{sc} = [ $self->{maps_counters}->{sc} = [
{ label => 'status', threshold => 0, set => { { label => 'status', threshold => 0, set => {
key_values => [ { name => 'load' }, { name => 'active' }, { name => 'sub' }, { name => 'display' } ], key_values => [ { name => 'load' }, { name => 'active' }, { name => 'sub' }, { name => 'boot' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'), closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
@ -107,22 +114,22 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'hostname:s' => { name => 'hostname' },
"hostname:s" => { name => 'hostname' }, 'remote' => { name => 'remote' },
"remote" => { name => 'remote' }, 'ssh-option:s@' => { name => 'ssh_option' },
"ssh-option:s@" => { name => 'ssh_option' }, 'ssh-path:s' => { name => 'ssh_path' },
"ssh-path:s" => { name => 'ssh_path' }, 'ssh-command:s' => { name => 'ssh_command', default => 'ssh' },
"ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, 'timeout:s' => { name => 'timeout', default => 30 },
"timeout:s" => { name => 'timeout', default => 30 }, 'sudo' => { name => 'sudo' },
"sudo" => { name => 'sudo' }, 'command:s' => { name => 'command', default => 'systemctl' },
"command:s" => { name => 'command', default => 'systemctl' }, 'command-path:s' => { name => 'command_path' },
"command-path:s" => { name => 'command_path' }, 'command-options:s' => { name => 'command_options', default => '-a --no-pager --no-legend' },
"command-options:s" => { name => 'command_options', default => '-a --no-pager --no-legend' }, 'command-options2:s' => { name => 'command_options2', default => 'list-unit-files --no-pager --no-legend' },
"filter-name:s" => { name => 'filter_name' }, 'filter-name:s' => { name => 'filter_name' },
"warning-status:s" => { name => 'warning_status', default => '' }, 'warning-status:s' => { name => 'warning_status', default => '' },
"critical-status:s" => { name => 'critical_status', default => '%{active} =~ /failed/i' }, 'critical-status:s' => { name => 'critical_status', default => '%{active} =~ /failed/i' },
}); });
return $self; return $self;
} }
@ -143,12 +150,14 @@ sub prefix_sc_output {
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my ($stdout) = centreon::plugins::misc::execute(output => $self->{output}, my ($stdout) = centreon::plugins::misc::execute(
options => $self->{option_results}, output => $self->{output},
sudo => $self->{option_results}->{sudo}, options => $self->{option_results},
command => $self->{option_results}->{command}, sudo => $self->{option_results}->{sudo},
command_path => $self->{option_results}->{command_path}, command => $self->{option_results}->{command},
command_options => $self->{option_results}->{command_options}); command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options}
);
$self->{global} = { running => 0, exited => 0, failed => 0, dead => 0, total => 0 }; $self->{global} = { running => 0, exited => 0, failed => 0, dead => 0, total => 0 };
$self->{sc} = {}; $self->{sc} = {};
@ -164,7 +173,7 @@ sub manage_selection {
next; next;
} }
$self->{sc}->{$name} = { display => $name, load => $load, active => $active, sub => $sub }; $self->{sc}->{$name} = { display => $name, load => $load, active => $active, sub => $sub, boot => '-' };
$self->{global}->{$sub} += 1 if (defined($self->{global}->{$sub})); $self->{global}->{$sub} += 1 if (defined($self->{global}->{$sub}));
$self->{global}->{total} += 1; $self->{global}->{total} += 1;
} }
@ -173,6 +182,23 @@ sub manage_selection {
$self->{output}->add_option_msg(short_msg => "No service found."); $self->{output}->add_option_msg(short_msg => "No service found.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
($stdout) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options2}
);
#runlevel4.target enabled
#runlevel5.target static
#runlevel6.target disabled
while ($stdout =~ /^(.*?)\s+(\S+)\s*$/msig) {
my ($name, $boot) = ($1, $2);
next if (!defined($self->{sc}->{$name}));
$self->{sc}->{$name}->{boot} = $boot;
}
} }
1; 1;
@ -226,32 +252,29 @@ Command path (Default: none).
Command options (Default: '-a --no-pager --no-legend'). Command options (Default: '-a --no-pager --no-legend').
=item B<--command-options2>
Command options (Default: 'list-unit-files --no-pager --no-legend').
=item B<--filter-name> =item B<--filter-name>
Filter service name (can be a regexp). Filter service name (can be a regexp).
=item B<--warning-*> =item B<--warning-*> B<--critical-*>
Threshold warning. Thresholds.
Can be: 'total-running', 'total-dead', 'total-exited',
'total-failed'.
=item B<--critical-*>
Threshold critical.
Can be: 'total-running', 'total-dead', 'total-exited', Can be: 'total-running', 'total-dead', 'total-exited',
'total-failed'. 'total-failed'.
=item B<--warning-status> =item B<--warning-status>
Set warning threshold for status. Set warning threshold for status.
Can used special variables like: %{display}, %{active}, %{sub}, %{load} Can used special variables like: %{display}, %{active}, %{sub}, %{load}, %{boot}
=item B<--critical-status> =item B<--critical-status>
Set critical threshold for status (Default: '%{active} =~ /failed/i'). Set critical threshold for status (Default: '%{active} =~ /failed/i').
Can used special variables like: %{display}, %{active}, %{sub}, %{load} Can used special variables like: %{display}, %{active}, %{sub}, %{load}, %{boot}
=back =back