Debian Codegraph @

src/apps/inin/ig/snmp/mode/spanusage.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 apps::inin::ig::snmp::mode::spanusage;
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);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 my $msg = 'status : ' . $self->{result_values}->{status};
33 return $msg;
34 }
35
36 sub custom_status_calc {
37 my ($self, %options) = @_;
38
39 $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_i3IgSpanInfoSpanState'};
40 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
41 return 0;
42 }
43
44 sub set_counters {
45 my ($self, %options) = @_;
46
47 $self->{maps_counters_type} = [
48 { name => 'span', type => 1, cb_prefix_output => 'prefix_span_output', message_multiple => 'All spans are ok' }
49 ];
50
51 $self->{maps_counters}->{span} = [
52 { label => 'status', threshold => 0, set => {
53 key_values => [ { name => 'i3IgSpanInfoSpanState' }, { name => 'display' } ],
54 closure_custom_calc => $self->can('custom_status_calc'),
55 closure_custom_output => $self->can('custom_status_output'),
56 closure_custom_perfdata => sub { return 0; },
57 closure_custom_threshold_check => \&catalog_status_threshold,
58 }
59 },
60 { label => 'active-channels', set => {
61 key_values => [ { name => 'i3IgSpanInfoActiveChannels' }, { name => 'display' } ],
62 output_template => 'Current Active Channels : %s',
63 perfdatas => [
64 { label => 'active_channels', value => 'i3IgSpanInfoActiveChannels', template => '%d',
65 min => 0, label_extra_instance => 1, instance_use => 'display' },
66 ],
67 }
68 },
69 ];
70 }
71
72 sub new {
73 my ($class, %options) = @_;
74 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
75 bless $self, $class;
76
77 $options{options}->add_options(arguments =>
78 {
79 "filter-name:s" => { name => 'filter_name' },
80 "warning-status:s" => { name => 'warning_status', default => '' },
81 "critical-status:s" => { name => 'critical_status', default => '%{status} !~ /closed|ready/i' },
82 });
83
84 return $self;
85 }
86
87 sub check_options {
88 my ($self, %options) = @_;
89 $self->SUPER::check_options(%options);
90
91 $self->change_macros(macros => ['warning_status', 'critical_status']);
92 }
93
94 sub prefix_span_output {
95 my ($self, %options) = @_;
96
97 return "Span '" . $options{instance_value}->{display} . "' ";
98 }
99
100 my %map_state = (0 => 'closed', 1 => 'ready', 2 => 'alarm', 3 => 'error');
101 my $mapping = {
102 i3IgSpanInfoSpanId => { oid => '.1.3.6.1.4.1.2793.4.2.3.1.3' },
103 i3IgSpanInfoActiveChannels => { oid => '.1.3.6.1.4.1.2793.4.2.3.1.5' },
104 i3IgSpanInfoSpanState => { oid => '.1.3.6.1.4.1.2793.4.2.3.1.6', map => \%map_state },
105 };
106
107 my $oid_i3IgSpanInfoTableEntry = '.1.3.6.1.4.1.2793.4.2.3.1';
108
109 sub manage_selection {
110 my ($self, %options) = @_;
111
112 $self->{span} = {};
113 my $snmp_result = $options{snmp}->get_table(oid => $oid_i3IgSpanInfoTableEntry,
114 nothing_quit => 1);
115
116 foreach my $oid (keys %{$snmp_result}) {
117 next if ($oid !~ /^$mapping->{i3IgSpanInfoSpanState}->{oid}\.(.*)$/);
118 my $instance = $1;
119 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
120
121 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
122 $result->{i3IgSpanInfoSpanId} !~ /$self->{option_results}->{filter_name}/) {
123 $self->{output}->output_add(long_msg => "skipping '" . $result->{i3IgSpanInfoSpanId} . "': no matching filter.", debug => 1);
124 next;
125 }
126
127 $self->{span}->{$instance} = {
128 display => $result->{i3IgSpanInfoSpanId},
129 %$result
130 };
131 }
132
133 if (scalar(keys %{$self->{span}}) <= 0) {
134 $self->{output}->add_option_msg(short_msg => "No span found.");
135 $self->{output}->option_exit();
136 }
137 }
138
139 1;
140
141 __END__
142
143 =head1 MODE
144
145 Check span usage.
146
147 =over 8
148
149 =item B<--filter-counters>
150
151 Only display some counters (regexp can be used).
152 Example: --filter-counters='^status$'
153
154 =item B<--filter-name>
155
156 Filter span name (can be a regexp).
157
158 =item B<--warning-status>
159
160 Define the conditions to match for the status to be WARNING.
161 You can use the following variables: %{status}, %{display}
162
163 =item B<--critical-status>
164
165 Define the conditions to match for the status to be CRITICAL (default: '%{status} !~ /closed|ready/i').
166 You can use the following variables: %{status}, %{display}
167
168 =item B<--warning-*>
169
170 Warning threshold.
171 Can be: 'active-channels'.
172
173 =item B<--critical-*>
174
175 Critical threshold.
176 Can be: 'active-channels'.
177
178 =back
179
180 =cut