Debian Codegraph @

src/hardware/server/cisco/ucs/snmp/mode/auditlogs.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::cisco::ucs::snmp::mode::auditlogs;
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 POSIX;
30 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
31
32 my $map_severity = {
33 0 => 'cleared', 1 => 'info', 2 => 'condition',
34 3 => 'warning', 4 => 'minor', 5 => 'major',
35 6 => 'critical'
36 };
37
38 sub custom_status_output {
39 my ($self, %options) = @_;
40
41 return sprintf(
42 'audit [severity: %s] [dn: %s] [description: %s] %s',
43 $self->{result_values}->{severity},
44 $self->{result_values}->{dn},
45 $self->{result_values}->{description},
46 scalar(localtime($self->{result_values}->{created}))
47 );
48 }
49
50 sub prefix_global_output {
51 my ($self, %options) = @_;
52
53 return 'Audit ';
54 }
55
56 sub set_counters {
57 my ($self, %options) = @_;
58
59 $self->{maps_counters_type} = [
60 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', },
61 { name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { nlabel => 'audit.problems.current.count', min => 0 },
62 group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ]
63 }
64 ];
65
66 $self->{maps_counters}->{global} = [
67 { label => 'audit-total', nlabel => 'audit.total.count', display_ok => 0, set => {
68 key_values => [ { name => 'total' } ],
69 output_template => 'total: %s',
70 perfdatas => [
71 { template => '%s', min => 0 }
72 ]
73 }
74 }
75 ];
76
77 foreach (values %$map_severity) {
78 push @{$self->{maps_counters}->{global}},
79 { label => 'audit-' . $_, nlabel => 'audit.' . $_ . '.count', display_ok => 0, set => {
80 key_values => [ { name => $_ }, { name => 'total' } ],
81 output_template => $_ . ': %s',
82 perfdatas => [
83 { template => '%s', min => 0, max => 'total' }
84 ]
85 }
86 };
87 }
88
89 $self->{maps_counters}->{alarm} = [
90 {
91 label => 'status',
92 type => 2,
93 warning_default => '%{severity} =~ /minor|warning/',
94 critical_default => '%{severity} =~ /major|critical/',
95 set => {
96 key_values => [
97 { name => 'dn' }, { name => 'severity' },
98 { name => 'description' }, { name => 'created' }
99 ],
100 closure_custom_output => $self->can('custom_status_output'),
101 closure_custom_perfdata => sub { return 0; },
102 closure_custom_threshold_check => \&catalog_status_threshold_ng
103 }
104 }
105 ];
106 }
107
108 sub new {
109 my ($class, %options) = @_;
110 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
111 bless $self, $class;
112
113 $options{options}->add_options(arguments => {
114 'filter-message:s' => { name => 'filter_message' },
115 'retention:s' => { name => 'retention' },
116 'memory' => { name => 'memory' }
117 });
118
119 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
120 return $self;
121 }
122
123 sub check_options {
124 my ($self, %options) = @_;
125 $self->SUPER::check_options(%options);
126
127 if (defined($self->{option_results}->{memory})) {
128 $self->{statefile_cache}->check_options(%options);
129 }
130 }
131
132 sub get_timestamp {
133 my ($self, %options) = @_;
134
135 my $value = unpack('H*', $options{value});
136 $value =~ /^([0-9a-z]{4})([0-9a-z]{2})([0-9a-z]{2})([0-9a-z]{2})([0-9a-z]{2})([0-9a-z]{2})/;
137
138 my $currentTmsp = mktime(hex($6), hex($5), hex($4), hex($3), hex($2) - 1, hex($1) - 1900);
139 return $currentTmsp;
140 }
141
142 my $mapping = {
143 dn => { oid => '.1.3.6.1.4.1.9.9.719.1.2.17.1.2' }, # cucsAaaModLRDn
144 description => { oid => '.1.3.6.1.4.1.9.9.719.1.2.17.1.9' }, # cucsAaaModLRDescr
145 created => { oid => '.1.3.6.1.4.1.9.9.719.1.2.17.1.8' }, # cucsAaaModLRCreated
146 severity => { oid => '.1.3.6.1.4.1.9.9.719.1.2.17.1.12', map => $map_severity } # cucsAaaModLRSeverity
147 };
148
149 sub manage_selection {
150 my ($self, %options) = @_;
151
152 my $datas = {};
153 my ($start, $last_instance);
154 if (defined($self->{option_results}->{memory})) {
155 $self->{statefile_cache}->read(statefile => 'cache_ciscoucs_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode});
156 $start = $self->{statefile_cache}->get(name => 'start');
157 $start = $start - 1 if (defined($start));
158 }
159
160 my $snmp_result = $options{snmp}->get_multiple_table(
161 oids => [ map({ oid => $_->{oid}, start => $_->{oid} . (defined($start) ? '.' . $start : '') }, values(%$mapping)) ],
162 return_type => 1
163 );
164
165 $self->{global} = {
166 total => 0, cleared => 0, info => 0, condition => 0,
167 warning => 0, minor => 0, major => 0, critical => 0
168 };
169 $self->{alarms} = { global => { alarm => {} } };
170 my ($i, $current_time) = (1, time());
171 foreach my $oid ($options{snmp}->oid_lex_sort(keys %$snmp_result)) {
172 next if ($oid !~ /^$mapping->{dn}->{oid}\.(\d+)$/);
173 my $instance = $1;
174 if (defined($self->{option_results}->{memory})) {
175 $last_instance = $instance;
176 next if (defined($start) && ($start + 1) >= $instance); # we skip last one from previous check)
177 }
178
179 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
180 $result->{description} = centreon::plugins::misc::trim($result->{description});
181 $result->{created} = $self->get_timestamp(value => $result->{created});
182 if (defined($self->{option_results}->{retention})) {
183 next if ($current_time - $result->{created} > $self->{option_results}->{retention});
184 }
185
186 $self->{global}->{total}++;
187 next if (defined($self->{option_results}->{filter_message}) && $self->{option_results}->{filter_message} ne '' && $result->{description} !~ /$self->{option_results}->{filter_message}/);
188
189 $self->{alarms}->{global}->{alarm}->{$i} = $result;
190 $self->{global}->{ $result->{severity} }++;
191 $i++;
192 }
193
194 if (defined($self->{option_results}->{memory})) {
195 $datas->{start} = $last_instance;
196 $self->{statefile_cache}->write(data => $datas);
197 }
198 }
199
200 1;
201
202 __END__
203
204 =head1 MODE
205
206 Check audit logs.
207
208 =over 8
209
210 =item B<--warning-status>
211
212 Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /minor|warning/')
213 You can use the following variables: %{severity}, %{description}, %{dn}
214
215 =item B<--critical-status>
216
217 Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /major|critical/').
218 You can use the following variables: %{severity}, %{description}, %{dn}
219
220 =item B<--memory>
221
222 Only check new audit.
223
224 =item B<--filter-message>
225
226 Filter on event message. (default: none)
227
228 =item B<--retention>
229
230 Event older (current time - retention time) is not checked (in seconds).
231
232 =back
233
234 =cut
235