Debian Codegraph @

src/hardware/server/ibm/mgmt_cards/imm/snmp/mode/eventlog.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::ibm::mgmt_cards::imm::snmp::mode::eventlog;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28 use centreon::plugins::statefile;
29 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
30
31 sub custom_status_output {
32 my ($self, %options) = @_;
33
34 my $msg = sprintf("alarm [severity: %s] [text: %s] %s", $self->{result_values}->{severity},
35 $self->{result_values}->{text}, $self->{result_values}->{generation_time});
36 return $msg;
37 }
38
39 sub custom_status_calc {
40 my ($self, %options) = @_;
41
42 $self->{result_values}->{text} = $options{new_datas}->{$self->{instance} . '_eventLogString'};
43 $self->{result_values}->{severity} = $options{new_datas}->{$self->{instance} . '_eventLogSeverity'};
44 $self->{result_values}->{since} = $options{new_datas}->{$self->{instance} . '_since'};
45 $self->{result_values}->{generation_time} = $options{new_datas}->{$self->{instance} . '_generation_time'};
46 return 0;
47 }
48
49
50 sub set_counters {
51 my ($self, %options) = @_;
52
53 $self->{maps_counters_type} = [
54 { name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { label => 'alerts', min => 0 },
55 group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ]
56 }
57 ];
58
59 $self->{maps_counters}->{alarm} = [
60 { label => 'status', threshold => 0, set => {
61 key_values => [ { name => 'eventLogSeverity' }, { name => 'eventLogString' }, { name => 'since' }, { name => 'generation_time' } ],
62 closure_custom_calc => $self->can('custom_status_calc'),
63 closure_custom_output => $self->can('custom_status_output'),
64 closure_custom_perfdata => sub { return 0; },
65 closure_custom_threshold_check => \&catalog_status_threshold,
66 }
67 },
68 ];
69 }
70
71 sub new {
72 my ($class, %options) = @_;
73 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
74 bless $self, $class;
75
76 $options{options}->add_options(arguments =>
77 {
78 "warning-status:s" => { name => 'warning_status', default => '%{severity} =~ /warning/i' },
79 "critical-status:s" => { name => 'critical_status', default => '%{severity} =~ /error/i' },
80 "memory" => { name => 'memory' },
81 "timezone:s" => { name => 'timezone' },
82 });
83
84 centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'DateTime',
85 error_msg => "Cannot load module 'DateTime'.");
86 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
87 return $self;
88 }
89
90 sub check_options {
91 my ($self, %options) = @_;
92 $self->SUPER::check_options(%options);
93
94 $self->change_macros(macros => ['warning_status', 'critical_status']);
95 if (defined($self->{option_results}->{memory})) {
96 $self->{statefile_cache}->check_options(%options);
97 }
98
99 $self->{option_results}->{timezone} = 'GMT' if (!defined($self->{option_results}->{timezone}) || $self->{option_results}->{timezone} eq '');
100 }
101
102 my %map_severity = (0 => 'error', 1 => 'warning', 2 => 'information', 3 => 'other');
103
104 my $mapping = {
105 eventLogString => { oid => '.1.3.6.1.4.1.2.3.51.3.2.1.1.1.2' },
106 eventLogSeverity => { oid => '.1.3.6.1.4.1.2.3.51.3.2.1.1.1.3', map => \%map_severity },
107 eventLogDate => { oid => '.1.3.6.1.4.1.2.3.51.3.2.1.1.1.4' }, # Month/Day/YEAR
108 eventLogTime => { oid => '.1.3.6.1.4.1.2.3.51.3.2.1.1.1.5' },
109 };
110 my $oid_eventLogEntry = '.1.3.6.1.4.1.2.3.51.3.2.1.1.1';
111
112 sub manage_selection {
113 my ($self, %options) = @_;
114
115 $self->{alarms}->{global} = { alarm => {} };
116 my $snmp_result = $options{snmp}->get_table(oid => $oid_eventLogEntry, nothing_quit => 1);
117
118 my $last_time;
119 if (defined($self->{option_results}->{memory})) {
120 $self->{statefile_cache}->read(statefile => "cache_imm_" . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port(). '_' . $self->{mode});
121 $last_time = $self->{statefile_cache}->get(name => 'last_time');
122 }
123
124 my ($i, $current_time) = (1, time());
125 my $tz = centreon::plugins::misc::set_timezone(name => $self->{option_results}->{timezone});
126 foreach my $oid (keys %{$snmp_result}) {
127 next if ($oid !~ /^$mapping->{eventLogSeverity}->{oid}\.(.*)$/);
128 my $instance = $1;
129 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
130
131 my $date = $result->{eventLogDate} . ' ' . $result->{eventLogTime};
132 $date =~ /^(\d+)\/(\d+)\/(\d+)\s+(\d+)[:\/](\d+)[:\/](\d+)/;
133
134 my $dt = DateTime->new(year => $3, month => $1, day => $2, hour => $4, minute => $5, second => $6, %$tz);
135
136 next if (defined($self->{option_results}->{memory}) && defined($last_time) && $last_time > $dt->epoch);
137
138 my $diff_time = $current_time - $dt->epoch;
139
140 $self->{alarms}->{global}->{alarm}->{$i} = { %$result, since => $diff_time, generation_time => centreon::plugins::misc::change_seconds(value => $diff_time) };
141 $i++;
142 }
143
144 if (defined($self->{option_results}->{memory})) {
145 $self->{statefile_cache}->write(data => { last_time => $current_time });
146 }
147 }
148
149 1;
150
151 __END__
152
153 =head1 MODE
154
155 Check alarms.
156
157 =over 8
158
159 =item B<--warning-status>
160
161 Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /warning/i')
162 You can use the following variables: %{severity}, %{text}, %{since}
163
164 =item B<--critical-status>
165
166 Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /error/i').
167 You can use the following variables: %{severity}, %{text}, %{since}
168
169 =item B<--timezone>
170
171 Timezone of time options. Default is 'GMT'.
172
173 =item B<--memory>
174
175 Only check new alarms.
176
177 =back
178
179 =cut