Debian Codegraph @

src/hardware/server/sun/mgmt_cards/mode/showenvironment.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 hardware::server::sun::mgmt_cards::mode::showenvironment;
22
23 use base qw(centreon::plugins::templates::hardware);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28
29 sub set_system {
30 my ($self, %options) = @_;
31
32 $self->{cb_hook2} = 'execute_command';
33
34 $self->{components_exec_load} = 0;
35
36 $self->{thresholds} = {
37 temperature => [
38 ['^(?!(OK)$)', 'CRITICAL'],
39 ['^OK$', 'OK']
40 ],
41 si => [
42 ['^(?!(OFF)$)', 'CRITICAL'],
43 ['^OFF$', 'OK']
44 ],
45 disk => [
46 ['^(?!(OK|NOT PRESENT)$)', 'CRITICAL'],
47 ['^OK|NOT PRESENT$', 'OK']
48 ],
49 fan => [
50 ['^(?!(OK|NOT PRESENT)$)', 'CRITICAL'],
51 ['^OK|NOT PRESENT$', 'OK']
52 ],
53 voltage => [
54 ['^(?!(OK)$)', 'CRITICAL'],
55 ['^OK$', 'OK']
56 ],
57 psu => [
58 ['^(?!(OK|NOT PRESENT)$)', 'CRITICAL'],
59 ['^OK|NOT PRESENT$', 'OK']
60 ],
61 sensors => [
62 ['^(?!(OK)$)', 'CRITICAL'],
63 ['^OK$', 'OK']
64 ]
65 };
66
67 $self->{components_path} = 'hardware::server::sun::mgmt_cards::components::showenvironment';
68 $self->{components_module} = ['temperature', 'si', 'disk', 'fan', 'voltage', 'psu', 'sensors'];
69 }
70
71 sub new {
72 my ($class, %options) = @_;
73 my $self = $class->SUPER::new(package => __PACKAGE__, %options, no_performance => 1, no_absent => 1);
74 bless $self, $class;
75
76 $options{options}->add_options(arguments => {
77 'hostname:s' => { name => 'hostname' },
78 'port:s' => { name => 'port', default => 23 },
79 'username:s' => { name => 'username' },
80 'password:s' => { name => 'password' },
81 'timeout:s' => { name => 'timeout', default => 30 },
82 'command-plink:s' => { name => 'command_plink' },
83 'ssh' => { name => 'ssh' }
84 });
85
86 return $self;
87 }
88
89 sub check_options {
90 my ($self, %options) = @_;
91 $self->SUPER::check_options(%options);
92
93 if (!defined($self->{option_results}->{hostname})) {
94 $self->{output}->add_option_msg(short_msg => "Need to specify a hostname.");
95 $self->{output}->option_exit();
96 }
97 if (!defined($self->{option_results}->{username})) {
98 $self->{output}->add_option_msg(short_msg => "Need to specify a username.");
99 $self->{output}->option_exit();
100 }
101 if (!defined($self->{option_results}->{password})) {
102 $self->{output}->add_option_msg(short_msg => "Need to specify a password.");
103 $self->{output}->option_exit();
104 }
105
106 if (!defined($self->{option_results}->{ssh})) {
107 require hardware::server::sun::mgmt_cards::lib::telnet;
108 }
109
110 centreon::plugins::misc::check_security_command(
111 output => $self->{output},
112 command => $self->{option_results}->{command_plink}
113 );
114
115 $self->{option_results}->{command} = 'plink'
116 if (!defined($self->{option_results}->{command}) || $self->{option_results}->{command} eq '');
117 }
118
119 sub ssh_command {
120 my ($self, %options) = @_;
121
122 my $cmd_in = $self->{option_results}->{username} . '\n' . $self->{option_results}->{password} . '\nshowenvironment\nlogout\n';
123 my $cmd = "echo -e '$cmd_in' | " . $self->{option_results}->{command_plink} . " -batch " . $self->{option_results}->{hostname} . " 2>&1";
124 my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick(
125 command => $cmd,
126 timeout => $self->{option_results}->{timeout},
127 wait_exit => 1
128 );
129
130 $stdout =~ s/\r//g;
131 if ($lerror <= -1000) {
132 $self->{output}->output_add(severity => 'UNKNOWN',
133 short_msg => $stdout);
134 $self->{output}->display();
135 $self->{output}->exit();
136 }
137 if ($exit_code != 0) {
138 $stdout =~ s/\n/ - /g;
139 $self->{output}->output_add(severity => 'UNKNOWN',
140 short_msg => "Command error: $stdout");
141 $self->{output}->display();
142 $self->{output}->exit();
143 }
144
145 if ($stdout !~ /Environmental Status/mi) {
146 $self->{output}->output_add(long_msg => $stdout);
147 $self->{output}->output_add(severity => 'UNKNOWN',
148 short_msg => "Command 'showenvironment' problems (see additional info).");
149 $self->{output}->display();
150 $self->{output}->exit();
151 }
152
153 return $stdout;
154 }
155
156 sub execute_command {
157 my ($self, %options) = @_;
158
159 if (defined($self->{option_results}->{ssh})) {
160 $self->{stdout} = $self->ssh_command();
161 } else {
162 my $telnet_handle = hardware::server::sun::mgmt_cards::lib::telnet::connect(
163 username => $self->{option_results}->{username},
164 password => $self->{option_results}->{password},
165 hostname => $self->{option_results}->{hostname},
166 port => $self->{option_results}->{port},
167 timeout => $self->{option_results}->{timeout},
168 output => $self->{output}
169 );
170 my @lines = $telnet_handle->cmd("showenvironment");
171 $self->{stdout} = join("", @lines);
172 }
173 $self->{stdout} =~ s/\r//msg;
174 }
175
176 1;
177
178 __END__
179
180 =head1 MODE
181
182 Check Sun vXXX (v240, v440, v245,...) Hardware (through ALOM).
183
184 =over 8
185
186 =item B<--hostname>
187
188 Hostname to query.
189
190 =item B<--port>
191
192 telnet port (default: 23).
193
194 =item B<--username>
195
196 telnet username.
197
198 =item B<--password>
199
200 telnet password.
201
202 =item B<--timeout>
203
204 Timeout in seconds for the command (default: 30).
205
206 =item B<--command-plink>
207
208 Plink command (default: plink). Use to set a path.
209
210 =item B<--ssh>
211
212 Use ssh (with plink) instead of telnet.
213
214 =item B<--component>
215
216 Which component to check (default: '.*').
217 Can be: 'temperature', 'si', 'disk', 'fan', 'voltage', 'psu', 'sensors'.
218
219 =item B<--filter>
220
221 Exclude the items given as a comma-separated list (example: --filter=fan).
222 You can also exclude items from specific instances: --filter=fan,F1.RS
223
224 =item B<--no-component>
225
226 Define the expected status if no components are found (default: critical).
227
228 =item B<--threshold-overload>
229
230 Use this option to override the status returned by the plugin when the status label matches a regular expression (syntax: section,status,regexp).
231 Example: --threshold-overload='fan,CRITICAL,^(?!(OK|NOT PRESENT)$)'
232
233 =back
234
235 =cut