Debian Codegraph @

src/os/aix/local/mode/errpt.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::errpt;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
31 bless $self, $class;
32
33 $options{options}->add_options(arguments => {
34 'error-type:s' => { name => 'error_type' },
35 'error-class:s' => { name => 'error_class' },
36 'error-id:s' => { name => 'error_id' },
37 'retention:s' => { name => 'retention' },
38 'timezone:s' => { name => 'timezone' },
39 'description' => { name => 'description' },
40 'filter-resource:s' => { name => 'filter_resource' },
41 'filter-id:s' => { name => 'filter_id' },
42 'exclude-id:s' => { name => 'exclude_id' },
43 'format-date' => { name => 'format_date' }
44 });
45
46 return $self;
47 }
48
49 sub check_options {
50 my ($self, %options) = @_;
51 $self->SUPER::init(%options);
52
53 if (defined($self->{option_results}->{exclude_id}) && defined($self->{option_results}->{error_id})) {
54 $self->{output}->add_option_msg(short_msg => "Please use --error-id OR --exclude-id, these options are mutually exclusives");
55 $self->{output}->option_exit();
56 }
57 }
58
59 sub manage_selection {
60 my ($self, %options) = @_;
61
62 my $extra_options = '';
63 if (defined($self->{option_results}->{error_type}) && $self->{option_results}->{error_type} ne '') {
64 $extra_options .= ' -T '.$self->{option_results}->{error_type};
65 }
66 if (defined($self->{option_results}->{error_class}) && $self->{option_results}->{error_class} ne '') {
67 $extra_options .= ' -d '.$self->{option_results}->{error_class};
68 }
69 if (defined($self->{option_results}->{error_id}) && $self->{option_results}->{error_id} ne ''){
70 $extra_options.= ' -j '.$self->{option_results}->{error_id};
71 }
72 if (defined($self->{option_results}->{exclude_id}) && $self->{option_results}->{exclude_id} ne ''){
73 $extra_options.= ' -k ' . $self->{option_results}->{exclude_id};
74 }
75 if (defined($self->{option_results}->{retention}) && $self->{option_results}->{retention} ne ''){
76 my $retention = time() - $self->{option_results}->{retention};
77 if (defined($self->{option_results}->{timezone})){
78 $ENV{TZ} = $self->{option_results}->{timezone};
79 }
80 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($retention);
81 $year = $year - 100;
82 if (length($sec) == 1){
83 $sec = '0' . $sec;
84 }
85 if (length($min) == 1){
86 $min = '0' . $min;
87 }
88 if (length($hour) == 1){
89 $hour = '0' . $hour;
90 }
91 if (length($mday) == 1){
92 $mday = '0' . $mday;
93 }
94 $mon = $mon + 1;
95 if (length($mon) == 1){
96 $mon = '0' . $mon;
97 }
98 $retention = $mon . $mday . $hour . $min . $year;
99 $extra_options .= ' -s '.$retention;
100 }
101
102 my ($stdout) = $options{custom}->execute_command(
103 command => 'errpt',
104 command_options => $extra_options
105 );
106
107 my $results = {};
108 my @lines = split /\n/, $stdout;
109 # Header not needed
110 shift @lines;
111 foreach my $line (@lines) {
112 next if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/);
113
114 my ($identifier, $timestamp, $resource_name, $description) = ($1, $2, $5, $6);
115 $results->{ $timestamp . '~' . $identifier . '~' . $resource_name } = { description => $description };
116 }
117
118 return $results;
119 }
120
121 sub run {
122 my ($self, %options) = @_;
123
124 my $extra_message = '';
125 if (defined($self->{option_results}->{retention})) {
126 $extra_message = ' since ' . $self->{option_results}->{retention} . ' seconds';
127 }
128
129 my $results = $self->manage_selection(custom => $options{custom});
130 $self->{output}->output_add(
131 severity => 'OK',
132 short_msg => sprintf("No error found%s.", $extra_message)
133 );
134
135 my $total_error = 0;
136 foreach my $errpt_error (sort(keys %$results)) {
137 my @split_error = split ('~', $errpt_error);
138 my $timestamp = $split_error[0];
139 my $identifier = $split_error[1];
140 my $resource_name = $split_error[2];
141 my $description = $results->{$errpt_error}->{description};
142
143 next if (defined($self->{option_results}->{filter_resource}) && $self->{option_results}->{filter_resource} ne '' &&
144 $resource_name !~ /$self->{option_results}->{filter_resource}/);
145 next if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' &&
146 $identifier !~ /$self->{option_results}->{filter_id}/);
147
148 my $output_date = $split_error[0];
149 if (defined($self->{option_results}->{format_date})) {
150 my ($month, $day, $hour, $minute, $year) = unpack("(A2)*", $output_date);
151 $output_date = sprintf("20%s/%s/%s %s:%s", $year, $month, $day, $hour, $minute);
152 }
153
154 $total_error++;
155 if (defined($description)) {
156 $self->{output}->output_add(
157 long_msg => sprintf(
158 "Error '%s' Date: %s ResourceName: %s Description: %s",
159 $identifier,
160 $output_date,
161 $resource_name,
162 $description
163 )
164 );
165 } else {
166 $self->{output}->output_add(
167 long_msg => sprintf(
168 "Error '%s' Date: %s ResourceName: %s",
169 $identifier,
170 $output_date,
171 $resource_name
172 )
173 );
174 }
175 }
176
177 if ($total_error != 0) {
178 $self->{output}->output_add(
179 severity => 'critical',
180 short_msg => sprintf("%s error(s) found(s)%s", $total_error, $extra_message)
181 );
182 }
183
184 $self->{output}->display();
185 $self->{output}->exit();
186 }
187
188 1;
189
190 __END__
191
192 =head1 MODE
193
194 Check errpt messages.
195 Command used: 'errpt' with dynamic options
196
197 =over 8
198
199 =item B<--error-type>
200
201 Filter error type separated by a coma (INFO, PEND, PERF, PERM, TEMP, UNKN).
202
203 =item B<--error-class>
204
205 Filter error class ('H' for hardware, 'S' for software, '0' for errlogger, 'U' for undetermined).
206
207 =item B<--error-id>
208
209 Filter specific error code (can be a comma separated list).
210
211 =item B<--retention>
212
213 Retention time of errors in seconds.
214
215 =item B<--verbose>
216
217 Print error description in long output. [ Error 'CODE' Date: Timestamp ResourceName: RsrcName Description: Desc ]
218
219 =item B<--filter-resource>
220
221 Filter resource (can use a regexp).
222
223 =item B<--filter-id>
224
225 Filter error code (can use a regexp).
226
227 =item B<--exclude-id>
228
229 Filter on specific error code (can be a comma separated list).
230
231 =item B<--format-date>
232
233 Print the date to format 20YY/mm/dd HH:MM instead of mmddHHMMYY.
234
235 =back
236
237 =cut