Debian Codegraph @

src/centreon/common/huawei/standard/snmp/mode/hardware.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 centreon::common::huawei::standard::snmp::mode::hardware;
22
23 use base qw(centreon::plugins::templates::hardware);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::statefile;
28
29 sub set_system {
30 my ($self, %options) = @_;
31
32 $self->{regexp_threshold_numeric_check_section_option} = '^(fan|temperature)$';
33
34 $self->{cb_hook1} = 'init_cache';
35 $self->{cb_hook2} = 'snmp_execute';
36
37 $self->{thresholds} = {
38 fan => [
39 ['abnormal', 'CRITICAL'],
40 ['normal', 'OK']
41 ]
42 };
43
44 $self->{components_path} = 'centreon::common::huawei::standard::snmp::mode::components';
45 $self->{components_module} = ['fan', 'temperature'];
46 }
47
48 sub snmp_execute {
49 my ($self, %options) = @_;
50
51 $self->{snmp} = $options{snmp};
52 $self->{results} = $self->{snmp}->get_multiple_table(oids => $self->{request});
53 $self->write_cache();
54 }
55
56 sub init_cache {
57 my ($self, %options) = @_;
58
59 $self->check_cache(%options);
60 }
61
62 sub new {
63 my ($class, %options) = @_;
64 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
65 bless $self, $class;
66
67 $options{options}->add_options(arguments => {
68 'reload-cache-time:s' => { name => 'reload_cache_time', default => 180 },
69 'short-name' => { name => 'short_name' }
70 });
71
72 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
73 return $self;
74 }
75
76 sub check_options {
77 my ($self, %options) = @_;
78 $self->SUPER::check_options(%options);
79
80 $self->{short_name} = (defined($self->{option_results}->{short_name})) ? 1 : 0;
81 $self->{statefile_cache}->check_options(%options);
82 }
83
84 my $oid_entPhysicalEntry = '.1.3.6.1.2.1.47.1.1.1.1';
85 my $oid_entPhysicalDescr = '.1.3.6.1.2.1.47.1.1.1.1.2';
86 my $oid_entPhysicalContainedIn = '.1.3.6.1.2.1.47.1.1.1.1.4';
87 my $oid_entPhysicalClass = '.1.3.6.1.2.1.47.1.1.1.1.5';
88 my $oid_entPhysicalName = '.1.3.6.1.2.1.47.1.1.1.1.7';
89
90 sub check_cache {
91 my ($self, %options) = @_;
92
93 $self->{hostname} = $options{snmp}->get_hostname();
94 $self->{snmp_port} = $options{snmp}->get_port();
95
96 # init cache file
97 $self->{write_cache} = 0;
98 my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_huawei_entity_' . $self->{hostname} . '_' . $self->{snmp_port});
99 my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp');
100 if ($has_cache_file == 0 ||
101 !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{option_results}->{reload_cache_time}) * 60)) && $self->{option_results}->{reload_cache_time} != '-1') {
102 push @{$self->{request}}, { oid => $oid_entPhysicalEntry, start => $oid_entPhysicalDescr, end => $oid_entPhysicalName };
103 $self->{write_cache} = 1;
104 }
105 }
106
107 sub write_cache {
108 my ($self, %options) = @_;
109
110 if ($self->{write_cache} == 1) {
111 my $datas = {};
112 $datas->{last_timestamp} = time();
113 $datas->{oids} = $self->{results}->{$oid_entPhysicalEntry};
114 $self->{statefile_cache}->write(data => $datas);
115 } else {
116 $self->{results}->{$oid_entPhysicalEntry} = $self->{statefile_cache}->get(name => 'oids');
117 }
118 }
119
120 sub get_short_name {
121 my ($self, %options) = @_;
122
123 return $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalName . '.' . $options{instance}};
124 }
125
126 sub get_long_name {
127 my ($self, %options) = @_;
128
129 my @names = ($self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $options{instance}});
130 my %loop = ($options{instance} => 1);
131 my $child = $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalContainedIn . '.' . $options{instance}};
132 while (1) {
133 last if (!defined($child) || defined($loop{$child}) || !defined($self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $child}));
134
135 unshift @names, $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $child};
136 $loop{$child} = 1;
137 $child = $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalContainedIn . '.' . $child};
138 }
139
140 return join(' > ', @names);
141 }
142
143 1;
144
145 __END__
146
147 =head1 MODE
148
149 Check hardware.
150
151 =over 8
152
153 =item B<--component>
154
155 Which component to check (default: '.*').
156 Can be: 'fan'.
157
158 =item B<--filter>
159
160 Exclude the items given as a comma-separated list (example: --filter=fan --filter=psu).
161 You can also exclude items from specific instances: --filter=fan,1.0
162
163 =item B<--absent-problem>
164
165 Return an error if an entity is not 'present' (default is skipping) (comma separated list)
166 Can be specific or global: --absent-problem=fan,1.0
167
168 =item B<--no-component>
169
170 Define the expected status if no components are found (default: critical).
171
172
173 =item B<--threshold-overload>
174
175 Use this option to override the status returned by the plugin when the status label matches a regular expression (syntax: section,[instance,]status,regexp).
176 Example: --threshold-overload='fan,WARNING,abnormal'
177
178 =item B<--warning>
179
180 Set warning threshold for 'fan', 'temperature' (syntax: type,regexp,threshold)
181 Example: --warning='fan,.*,40'
182
183 =item B<--critical>
184
185 Set critical threshold for 'fan', 'temperature' (syntax: type,regexp,threshold)
186 Example: --critical='fan,.*,45'
187
188 =item B<--reload-cache-time>
189
190 Time in seconds before reloading cache file (default: 180).
191 Use '-1' to disable cache reload.
192
193 =back
194
195 =cut