Debian Codegraph @

src/hardware/devices/abb/cms700/snmp/mode/listsensors.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::abb::cms700::snmp::mode::listsensors;
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
35 return $self;
36 }
37
38 sub check_options {
39 my ($self, %options) = @_;
40 $self->SUPER::init(%options);
41 }
42
43 my $oid_GroupName = '.1.3.6.1.4.1.51055.1.20';
44 my $oid_BranchNamesens = '.1.3.6.1.4.1.51055.1.19';
45 my $mapping = {
46 Phasesens => { oid => '.1.3.6.1.4.1.51055.1.21' },
47 Groupsens => { oid => '.1.3.6.1.4.1.51055.1.22' },
48 };
49
50 sub manage_selection {
51 my ($self, %options) = @_;
52
53 my %groups;
54 my $snmp_result = $options{snmp}->get_table(oid => $oid_GroupName);
55 foreach my $oid (keys %$snmp_result) {
56 next if ($oid !~ /^$oid_GroupName\.(.*)/);
57 next if ($snmp_result->{$oid} eq '');
58 $groups{$1} = $snmp_result->{$oid};
59 }
60
61 my %sensors;
62 $snmp_result = $options{snmp}->get_table(oid => $oid_BranchNamesens);
63 foreach my $oid (keys %$snmp_result) {
64 next if ($oid !~ /^$oid_BranchNamesens\.(.*)/);
65 next if ($snmp_result->{$oid} eq '');
66 my $instance = $1;
67
68 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
69 $snmp_result->{$oid} !~ /$self->{option_results}->{filter_name}/) {
70 $self->{output}->output_add(long_msg => "skipping sensor '" . $snmp_result->{$oid} . "'.", debug => 1);
71 next;
72 }
73
74 $sensors{$instance} = $snmp_result->{$oid};
75 }
76
77 $options{snmp}->load(
78 oids => [
79 $mapping->{Phasesens}->{oid},
80 $mapping->{Groupsens}->{oid},
81 ],
82 instances => [ keys %sensors ],
83 instance_regexp => '^(.*)$'
84 );
85 my $snmp_result_data = $options{snmp}->get_leef(nothing_quit => 1);
86
87 foreach my $oid (keys %$snmp_result_data) {
88 next if ($oid !~ /^$mapping->{Phasesens}->{oid}\.(.*)/);
89 my $instance = $1;
90 my $result = $options{snmp}->map_instance(
91 mapping => $mapping,
92 results => $snmp_result_data,
93 instance => $instance
94 );
95
96 $self->{sensors}->{$instance}->{name} = $sensors{$instance};
97 $self->{sensors}->{$instance}->{phase} = ($result->{Phasesens} != 0) ? $result->{Phasesens} : '-';
98 $self->{sensors}->{$instance}->{group} =
99 (defined($groups{$result->{Groupsens}})) ? $groups{$result->{Groupsens}} : '-';
100 }
101 }
102
103 sub run {
104 my ($self, %options) = @_;
105
106 $self->manage_selection(%options);
107 foreach my $instance (sort keys %{$self->{sensors}}) {
108 $self->{output}->output_add(
109 long_msg => sprintf("[name = %s] [group = %s] [phase = %s]",
110 $self->{sensors}->{$instance}->{name},
111 $self->{sensors}->{$instance}->{group},
112 $self->{sensors}->{$instance}->{phase})
113 );
114 }
115
116 $self->{output}->output_add(
117 severity => 'OK',
118 short_msg => 'List sensors:'
119 );
120 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
121 $self->{output}->exit();
122 }
123
124 sub disco_format {
125 my ($self, %options) = @_;
126
127 $self->{output}->add_disco_format(elements => ['name', 'group', 'phase']);
128 }
129
130 sub disco_show {
131 my ($self, %options) = @_;
132
133 $self->manage_selection(%options);
134 foreach my $instance (sort keys %{$self->{sensors}}) {
135 $self->{output}->add_disco_entry(
136 name => $self->{sensors}->{$instance}->{name},
137 group => $self->{sensors}->{$instance}->{group},
138 phase => $self->{sensors}->{$instance}->{phase},
139 );
140 }
141 }
142
143 1;
144
145 __END__
146
147 =head1 MODE
148
149 List sensors.
150
151 =over 8
152
153 =back
154
155 =cut