Debian Codegraph @

src/hardware/devices/eltek/enexus/snmp/mode/alarms.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::devices::eltek::enexus::snmp::mode::alarms;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
28 use centreon::plugins::misc;
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 return sprintf('status: %s',
34 $self->{result_values}->{status}
35 );
36 }
37
38 sub set_counters {
39 my ($self, %options) = @_;
40
41 $self->{maps_counters_type} = [
42 { name => 'global', type => 0 },
43 { name => 'alarm', type => 1, cb_prefix_output => 'prefix_alarm_output', message_multiple => 'All alarms are ok', skipped_code => { -10 => 1 } }
44 ];
45
46 $self->{maps_counters}->{global} = [
47 { label => 'alarms-active', nlabel => 'alarms.active.count', display_ok => 0, set => {
48 key_values => [ { name => 'active' }, { name => 'total' } ],
49 output_template => 'current active alarms: %d',
50 perfdatas => [
51 { value => 'active', template => '%d', min => 0, max => 'total' }
52 ]
53 }
54 }
55 ];
56
57 $self->{maps_counters}->{alarm} = [
58 { label => 'status', threshold => 0, set => {
59 key_values => [ { name => 'status' }, { name => 'name' } ],
60 closure_custom_calc => \&catalog_status_calc,
61 closure_custom_output => $self->can('custom_status_output'),
62 closure_custom_perfdata => sub { return 0; },
63 closure_custom_threshold_check => \&catalog_status_threshold
64 }
65 }
66 ];
67 }
68
69 sub prefix_alarm_output {
70 my ($self, %options) = @_;
71
72 return "Alarm '" . $options{instance_value}->{name} . "' ";
73 }
74
75 sub new {
76 my ($class, %options) = @_;
77 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
78 bless $self, $class;
79
80 $options{options}->add_options(arguments => {
81 'filter-name:s' => { name => 'filter_name' },
82 'unknown-status:s' => { name => 'unknown_status', default => '' },
83 'warning-status:s' => { name => 'warning_status', default => '' },
84 'critical-status:s' => { name => 'critical_status', default => '%{status} eq "alarm"' },
85 });
86
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 => ['unknown_status', 'warning_status', 'critical_status']);
95 }
96
97 my $map_status = { 0 => 'normal', 1 => 'alarm' };
98
99 my $mapping = {
100 alarmGroupStatus => { oid => '.1.3.6.1.4.1.12148.10.14.1.1.2', map => $map_status },
101 alarmGroupDescription => { oid => '.1.3.6.1.4.1.12148.10.14.1.1.3' }
102 };
103
104 sub manage_selection {
105 my ($self, %options) = @_;
106
107 my $oid_alarmGroupEntry = '.1.3.6.1.4.1.12148.10.14.1.1';
108 my $snmp_result = $options{snmp}->get_table(
109 oid => $oid_alarmGroupEntry,
110 start => $mapping->{alarmGroupStatus}->{oid},
111 nothing_quit => 1
112 );
113
114 $self->{global} = { total => 0, active => 0 };
115 $self->{alarm} = {};
116 foreach my $oid (keys %$snmp_result) {
117 next if ($oid !~ /^$mapping->{alarmGroupStatus}->{oid}\.(.*)$/);
118 my $instance = $1;
119
120 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
121 $result->{alarmGroupDescription} = centreon::plugins::misc::trim($result->{alarmGroupDescription});
122 $result->{alarmGroupDescription} = $instance if ($result->{alarmGroupDescription} eq '');
123
124 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
125 $result->{alarmGroupDescription} !~ /$self->{option_results}->{filter_name}/) {
126 $self->{output}->output_add(long_msg => "skipping alarm '" . $result->{alarmGroupDescription} . "'.", debug => 1);
127 next;
128 }
129
130 $self->{alarm}->{$instance} = {
131 name => $result->{alarmGroupDescription},
132 status => $result->{alarmGroupStatus}
133 };
134 $self->{global}->{total}++;
135 $self->{global}->{active}++ if ($result->{alarmGroupStatus} eq 'alarm');
136 }
137 }
138
139 1;
140
141 __END__
142
143 =head1 MODE
144
145 Check alarms.
146
147 =over 8
148
149 =item B<--filter-name>
150
151 Filter name (can be a regexp).
152
153 =item B<--unknown-status>
154
155 Define the conditions to match for the status to be UNKNOWN (default: '').
156 You can use the following variables: %{state}, %{status}, %{lastOpError}, %{display}
157
158 =item B<--warning-status>
159
160 Define the conditions to match for the status to be WARNING (default: '').
161 You can use the following variables: %{name}, %{status}
162
163 =item B<--critical-status>
164
165 Define the conditions to match for the status to be CRITICAL (default: '%{status} eq "alarm").
166 You can use the following variables: %{name}, %{status}
167
168 =item B<--warning-*> B<--critical-*>
169
170 Thresholds.
171 Can be: 'alarms-active'.
172
173 =back
174
175 =cut