Debian Codegraph @

src/os/solaris/local/mode/prtdiag.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 # Main code: Comes from Sebastien Phelep ([email protected])
22
23 package os::solaris::local::mode::prtdiag;
24
25 use base qw(centreon::plugins::mode);
26
27 use strict;
28 use warnings;
29 use centreon::plugins::misc;
30 use File::Basename;
31
32 sub new {
33 my ($class, %options) = @_;
34 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
35 bless $self, $class;
36
37 $options{options}->add_options(arguments => {
38 'config-file:s' => { name => 'config_file' },
39 'exclude:s@' => { name => 'exclude' }
40 });
41
42 $self->{conf} = {};
43 $self->{excludes} = {};
44 $self->{syst} = undef;
45 return $self;
46 }
47
48 sub check_options {
49 my ($self, %options) = @_;
50 $self->SUPER::init(%options);
51 if (defined($self->{option_results}->{config_file})) {
52 $self->{config_file} = $self->{option_results}->{config_file};
53 } else {
54 $self->{config_file} = dirname(__FILE__) . '/../conf/prtdiag.conf';
55 }
56
57 foreach (@{$self->{option_results}->{exclude}}) {
58 next if (! /^(.*?),(.*?),(.*)$/);
59 my ($section, $tpl, $filter) = ($1, $2, $3);
60 $self->{excludes}->{$section} = [] if (!defined($self->{excludes}->{$section}));
61 push @{$self->{excludes}->{$section}}, { template => $tpl, filter => $filter };
62 }
63 }
64
65 sub check_exclude {
66 my ($self, %options) = @_;
67
68 return 0 if (!defined($self->{excludes}->{$options{section}}));
69
70 foreach my $exclude (@{$self->{excludes}->{$options{section}}}) {
71 my ($template, $filter) = ($exclude->{template}, $exclude->{filter});
72 foreach my $label (keys %{$options{dataset}}) {
73 $template =~ s/%$label%/$options{dataset}->{$label}/g;
74 }
75
76 if ($template =~ /$filter/) {
77 $self->{output}->output_add(long_msg => " INF - Skipping $template");
78 return 1;
79 }
80 }
81
82 return 0;
83 }
84
85 sub prtdiag {
86 my ($self, %options) = @_;
87
88 my ($stdout, $exit_code) = $options{custom}->execute_command(
89 command => 'prtdiag',
90 command_options => '-v 2>&1',
91 command_path => '/usr/platform/`/sbin/uname -i`/sbin'
92 );
93 if ($exit_code != 0 && $exit_code != 1) {
94 $self->{output}->add_option_msg(short_msg => "Command error: $stdout");
95 $self->{output}->option_exit();
96 }
97
98 my @diag = split(/\n/, $stdout);
99
100 # Look for system type
101 unless( defined($self->{syst}) ) {
102 FSYS:
103 foreach my $section ( keys(%{$self->{conf}}) ) {
104 foreach my $param ( keys(%{ $self->{conf}->{$section} }) ) {
105 next unless( $param eq "system.match" );
106 if( grep(/$self->{conf}->{$section}->{'system.match'}/, @diag) ) {
107 $self->{syst} = $section;
108 last FSYS;
109 }
110 }
111 }
112 }
113
114 # Check for unidentified system type
115 unless( defined($self->{syst}) ) {
116 $self->{output}->output_add(
117 severity => 'UNKNOWN',
118 short_msg => "Unable to identify system type !"
119 );
120 return ;
121 }
122 $self->{output}->output_add(long_msg => "Using system type : $self->{syst}");
123
124 # Further config checks
125 unless( defined($self->{conf}->{$self->{syst}}->{'system.checks'}) ) {
126 $self->{output}->output_add(
127 severity => 'UNKNOWN',
128 short_msg => "Initialization failed - Missing 'system.checks' entry for section '$self->{syst}' in file '$self->{config_file}' !"
129 );
130 return ;
131 }
132 my @checks = split(/\s*,\s*/,$self->{conf}->{$self->{syst}}->{'system.checks'});
133 if( scalar(@checks) == 0 ) {
134 $self->{output}->output_add(
135 severity => 'UNKNOWN',
136 short_msg => "No check defined in 'system.checks' entry for section '$self->{syst}' in file '$self->{config_file}' !"
137 );
138 return ;
139 }
140 foreach my $check ( @checks ) {
141 foreach my $param ( "description", "begin_match", "end_match", "data_match", "data_labels", "ok_condition", "output_string" ) {
142 my $param_name = "checks.$check.$param";
143 unless( defined($self->{conf}->{$self->{syst}}->{$param_name}) ) {
144 $self->{output}->output_add(
145 severity => 'UNKNOWN',
146 short_msg => "Initialization error - Missing '$param_name' entry for section '$self->{syst}' in file '$self->{config_file}' !"
147 );
148 return ;
149 }
150 }
151 }
152
153 # Check'em all
154 my @failed = ();
155 my @passed = ();
156 foreach my $check ( @checks ) {
157 # Get associated data
158 my $description = $self->{conf}->{$self->{syst}}->{"checks.$check.description"};
159 my @labels = split(/\s*,\s*/,$self->{conf}->{$self->{syst}}->{"checks.$check.data_labels"});
160 my $fetch_mode = $self->{conf}->{$self->{syst}}->{"checks.$check.fetch_mode"};
161 my $begin = 0;
162 my $dcount = 0;
163 my $lcount = 0;
164 my %data = ();
165
166 $self->{output}->output_add(long_msg => "Checking $description:");
167
168 # Parse prtdiag output
169 DIAG: foreach( @diag ) {
170 unless( $begin ) {
171 # Looking for begin pattern
172 next DIAG unless( m/$self->{conf}->{$self->{syst}}->{"checks.$check.begin_match"}/ );
173 s/$self->{conf}->{$self->{syst}}->{"checks.$check.begin_match"}//;
174 $begin = 1;
175 } else {
176 # Stop parsing if matched end pattern
177 last DIAG if( m/$self->{conf}->{$self->{syst}}->{"checks.$check.end_match"}/ );
178 }
179
180 # Skip unwanted data
181 if( defined($self->{conf}->{$self->{syst}}->{"checks.$check.skip_match"}) ) {
182 next DIAG if( m/$self->{conf}->{$self->{syst}}->{"checks.$check.skip_match"}/ );
183 }
184
185 # Reinit read values
186 my @values = ();
187
188 # === Fetching data in linear mode === #
189 if( defined($fetch_mode) and ($fetch_mode eq "linear") ) {
190 # Use specified regexp separator, or define a default one
191 my $regexp_separator = $self->{conf}->{$self->{syst}}->{"checks.$check.data_match_regsep"} || '\s*,\s';
192
193 # Extract regular expresssions to be used for data collection
194 my @dmatch = split(/\s*,\s*/,$self->{conf}->{$self->{syst}}->{"checks.$check.data_match"});
195
196 # Take care of counters
197 if( $lcount >= scalar(@labels) ) {
198 $lcount = 0;
199 $dcount = $dcount + scalar(@labels) + 1;
200 }
201
202 # Get all matching values
203 @values = m/$dmatch[$lcount]/g;
204
205 # Update our hash
206 for( my $i=0; $i < scalar(@values); $i++ ) {
207 $data{($dcount+$i)}->{$labels[$lcount]} = centreon::plugins::misc::trim($values[$i]);
208 }
209 $lcount++;
210
211 # Next one
212 next DIAG;
213 }
214 # === Fetching data otherwise (aka tabular mode) === #
215 else {
216 # Next one if this does not match
217 next DIAG unless( @values = m/$self->{conf}->{$self->{syst}}->{"checks.$check.data_match"}/g );
218
219 # Update our hash
220 for( my $i=0; $i < scalar(@values); $i++ ) {
221 # Take care of counters
222 if( $lcount >= scalar(@labels) ) {
223 $lcount = 0;
224 $dcount++;
225 }
226
227 $data{$dcount}->{$labels[$lcount]} = centreon::plugins::misc::trim($values[$i]);
228 $lcount++;
229 }
230 }
231 }
232
233 # Check collected data
234 my $errors = 0;
235 my $tests = 0;
236 foreach my $dataset ( keys(%data) ) {
237 my $test_result = "";
238 my $ok_condition = $self->{conf}->{$self->{syst}}->{"checks.$check.ok_condition"};
239 my $output_string = $self->{conf}->{$self->{syst}}->{"checks.$check.output_string"};
240
241 next if ($self->check_exclude(dataset => $data{$dataset}, section => $check));
242
243 # Substitute labels in condition and output string
244 foreach my $label ( keys( %{ $data{$dataset} } ) ) {
245 $ok_condition =~ s/%$label%/$data{$dataset}->{$label}/g;
246 $output_string =~ s/%$label%/$data{$dataset}->{$label}/g;
247 }
248
249 # Test condition
250 if( eval($ok_condition) ) {
251 # Test passed
252 $test_result = "INF - $output_string";
253 push(@passed,$output_string);
254 } else {
255 # Test failed
256 $test_result = "ERR - $output_string";
257 push(@failed,$output_string);
258 $errors++;
259
260 }
261 $tests++;
262 $self->{output}->output_add(long_msg => " $test_result");
263 }
264 $self->{output}->output_add(long_msg => "Checked $tests component".( $tests le 1 ? "" : "s").", found ".( $errors == 0 ? "no error" : "$errors errors." ));
265 }
266
267 my $checked = scalar(@passed) + scalar(@failed);
268 if( scalar(@failed) > 0 ) {
269 $self->{output}->output_add(
270 severity => 'CRITICAL',
271 short_msg => "Checked $checked component" . ( $checked le 1 ? "" : "s") . ", found " . scalar(@failed) . " errors : " . join(', ',@failed)
272 );
273 $self->{output}->output_add(long_msg => join("\n",@passed));
274 } elsif( $checked == 0 ) {
275 $self->{output}->output_add(
276 severity => 'WARNING',
277 short_msg => "Found nothing to check !"
278 );
279 } else {
280 $self->{output}->output_add(
281 severity => 'OK',
282 short_msg => "Successfully checked $checked component" . ( $checked le 1 ? "" : "s")
283 );
284 $self->{output}->output_add(long_msg => join("\n", @passed));
285 }
286 }
287
288 sub run {
289 my ($self, %options) = @_;
290
291 $self->load_prtdiag_config();
292 $self->prtdiag(custom => $options{custom});
293 $self->{output}->display();
294 $self->{output}->exit();
295 }
296
297 sub load_prtdiag_config {
298 my ($self, %options) = @_;
299
300 unless( open(CONFIG,"<$self->{config_file}") ) {
301 $self->{output}->output_add(
302 severity => 'UNKNOWN',
303 short_msg => "Initialization error - unable to open file '" . $self->{config_file} . "' : $!"
304 );
305 $self->{output}->display();
306 $self->{output}->exit();
307 }
308 my $section = undef;
309 while(<CONFIG>) {
310 chomp();
311
312 # Remove comments
313 s/#.*//;
314
315 # Ignore blank lines
316 next if( m/^\s*$/ );
317
318 if( m/^\s*\[\s*(.*?)\s*\]\s*$/ ) {
319 $section = $1;
320 next;
321 } elsif( m/^\s*(.*?)\s*=\s*(.*?)\s*$/ ) {
322 $self->{conf}->{$section}->{$1} = $2 if( defined($section) );
323 }
324 }
325 close (CONFIG);
326 }
327
328 1;
329
330 __END__
331
332 =head1 MODE
333
334 Check Sun hardware.
335
336 Command used: '/usr/platform/`/sbin/uname -i`/sbin/prtdiag -v 2>&1'
337
338 =over 8
339
340 =item B<--config-file>
341
342 Config file with prtdiag output description (default: Directory 'conf/prtdiag.conf' under absolute mode path).
343
344 =item B<--exclude>
345
346 Exclude some components (multiple) (Syntax: SECTION,INSTANCE,FILTER).
347 SECTION = component type in prtdiag.conf (example: temperature, fan,...
348 INSTANCE = Set the instance (example: %Location%)
349 FILTER = regexp to filter
350
351 =back
352
353 =cut