Debian Codegraph @

src/os/aix/local/mode/cmdreturn.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package os::aix::local::mode::cmdreturn;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
32 bless $self, $class;
33
34 $options{options}->add_options(arguments => {
35 'exec-command:s' => { name => 'exec_command' },
36 'exec-command-path:s' => { name => 'exec_command_path' },
37 'exec-command-options:s' => { name => 'exec_command_options' },
38 'manage-returns:s' => { name => 'manage_returns', default => '' },
39 'separator:s' => { name => 'separator', default => '#' }
40 });
41
42 return $self;
43 }
44
45 sub check_options {
46 my ($self, %options) = @_;
47 $self->SUPER::init(%options);
48
49 if (!defined($self->{option_results}->{exec_command})) {
50 $self->{output}->add_option_msg(short_msg => "Need to specify exec-command option.");
51 $self->{output}->option_exit();
52 }
53
54 $self->{expressions} = [];
55 foreach my $entry (split(/$self->{option_results}->{separator}/, $self->{option_results}->{manage_returns})) {
56 next if (!($entry =~ /(.*?),(.*?),(.*)/));
57 next if (!$self->{output}->is_litteral_status(status => $2));
58 my ($expr, $rv, $msg) = ($1, $2, $3);
59
60 if ($expr ne '') {
61 if ($expr =~ /^\s*([0-9]+)\s*$/) {
62 push @{$self->{expressions}}, { test => "%(code) == $1", rv => $rv, msg => $msg };
63 } else {
64 push @{$self->{expressions}}, { test => $expr, rv => $rv, msg => $msg };
65 }
66 } else {
67 $self->{expression_default} = { rv => $rv, msg => $msg };
68 }
69 }
70
71 if ($self->{option_results}->{manage_returns} eq '' ||
72 (scalar(@{$self->{expressions}}) == 0 && !defined($self->{expression_default}))) {
73 $self->{output}->add_option_msg(short_msg => "Need to specify manage-returns option correctly.");
74 $self->{output}->option_exit();
75 }
76
77 for (my $i = 0; $i < scalar(@{$self->{expressions}}); $i++) {
78 $self->{expressions}->[$i]->{test} =~ s/%\{(.*?)\}/\$values->{$1}/g;
79 $self->{expressions}->[$i]->{test} =~ s/%\((.*?)\)/\$values->{$1}/g;
80 }
81
82 centreon::plugins::misc::check_security_whitelist(
83 output => $self->{output},
84 command => $self->{option_results}->{exec_command},
85 command_path => $self->{option_results}->{exec_command_path},
86 command_options => $self->{option_results}->{exec_command_options}
87 );
88 }
89
90 sub run {
91 my ($self, %options) = @_;
92
93 my ($stdout, $exit_code) = $options{custom}->execute_command(
94 command => $self->{option_results}->{exec_command},
95 command_path => $self->{option_results}->{exec_command_path},
96 command_options => $self->{option_results}->{exec_command_options},
97 no_quit => 1
98 );
99
100 my $long_msg = $stdout;
101 $long_msg =~ s/\|/~/mg;
102 $self->{output}->output_add(long_msg => $long_msg);
103
104 my $matched = 0;
105 my $values = { code => $exit_code, output => $stdout };
106 foreach (@{$self->{expressions}}) {
107 if ($self->{output}->test_eval(test => $_->{test}, values => $values)) {
108 $self->{output}->output_add(
109 severity => $_->{rv},
110 short_msg => $_->{msg}
111 );
112 $matched = 1;
113 last;
114 }
115 }
116
117 if ($matched == 0 && defined($self->{expression_default})) {
118 $self->{output}->output_add(
119 severity => $self->{expression_default}->{rv},
120 short_msg => $self->{expression_default}->{msg}
121 );
122 } elsif ($matched == 0) {
123 $self->{output}->output_add(
124 severity => 'UNKNOWN',
125 short_msg => "Command exit code ($exit_code)"
126 );
127 }
128
129 if (defined($exit_code)) {
130 $self->{output}->perfdata_add(
131 nlabel => 'command.exit.code.count',
132 value => $exit_code
133 );
134 }
135
136 $self->{output}->display();
137 $self->{output}->exit();
138 }
139
140 1;
141
142 __END__
143
144 =head1 MODE
145
146 Check command returns.
147
148 =over 8
149
150 =item B<--manage-returns>
151
152 Set action according command exit code.
153 Example: %(code) == 0,OK,File xxx exist#%(code) == 1,CRITICAL,File xxx not exist#,UNKNOWN,Command problem
154
155 =item B<--separator>
156
157 Set the separator used in --manage-returns (default : #)
158
159 =item B<--exec-command>
160
161 Command to test (default: none).
162 You can use 'sh' to use '&&' or '||'.
163
164 =item B<--exec-command-path>
165
166 Command path (default: none).
167
168 =item B<--exec-command-options>
169
170 Command options (default: none).
171
172 =back
173
174 =cut