| 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 network::opengear::snmp::mode::serialports; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use Digest::MD5 qw(md5_hex); |
| 28 |
|
| 29 |
sub custom_traffic_perfdata { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
my ($warning, $critical); |
| 33 |
if ($self->{instance_mode}->{option_results}->{units_traffic} eq 'percent_delta' && defined($self->{result_values}->{speed})) { |
| 34 |
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1); |
| 35 |
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1); |
| 36 |
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} =~ /bps|counter/) { |
| 37 |
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}); |
| 38 |
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}); |
| 39 |
} |
| 40 |
|
| 41 |
if ($self->{instance_mode}->{option_results}->{units_traffic} eq 'counter') { |
| 42 |
my $nlabel = $self->{nlabel}; |
| 43 |
$nlabel =~ s/bitspersecond/bits/; |
| 44 |
$self->{output}->perfdata_add( |
| 45 |
nlabel => $nlabel, |
| 46 |
unit => 'b', |
| 47 |
instances => $self->{result_values}->{name}, |
| 48 |
value => $self->{result_values}->{traffic_counter}, |
| 49 |
warning => $warning, |
| 50 |
critical => $critical, |
| 51 |
min => 0 |
| 52 |
); |
| 53 |
} else { |
| 54 |
$self->{output}->perfdata_add( |
| 55 |
nlabel => $self->{nlabel}, |
| 56 |
instances => $self->{result_values}->{name}, |
| 57 |
value => sprintf('%.2f', $self->{result_values}->{traffic_per_seconds}), |
| 58 |
warning => $warning, |
| 59 |
critical => $critical, |
| 60 |
min => 0, max => $self->{result_values}->{speed} |
| 61 |
); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
sub custom_traffic_threshold { |
| 66 |
my ($self, %options) = @_; |
| 67 |
|
| 68 |
my $exit = 'ok'; |
| 69 |
if ($self->{instance_mode}->{option_results}->{units_traffic} eq 'percent_delta' && defined($self->{result_values}->{speed})) { |
| 70 |
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); |
| 71 |
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'bps') { |
| 72 |
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_per_seconds}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); |
| 73 |
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'counter') { |
| 74 |
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_counter}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); |
| 75 |
} |
| 76 |
return $exit; |
| 77 |
} |
| 78 |
|
| 79 |
sub custom_traffic_output { |
| 80 |
my ($self, %options) = @_; |
| 81 |
|
| 82 |
my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic_per_seconds}, network => 1); |
| 83 |
return sprintf( |
| 84 |
'traffic %s: %s/s (%s)', |
| 85 |
$self->{result_values}->{label}, $traffic_value . $traffic_unit, |
| 86 |
defined($self->{result_values}->{traffic_prct}) ? sprintf('%.2f%%', $self->{result_values}->{traffic_prct}) : '-' |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
sub custom_traffic_calc { |
| 91 |
my ($self, %options) = @_; |
| 92 |
|
| 93 |
my $diff_traffic = ($options{new_datas}->{ $self->{instance} . '_' . $options{extra_options}->{label_ref} } - $options{old_datas}->{ $self->{instance} . '_' . $options{extra_options}->{label_ref} }); |
| 94 |
$self->{result_values}->{traffic_per_seconds} = $diff_traffic / $options{delta_time}; |
| 95 |
$self->{result_values}->{traffic_counter} = $options{new_datas}->{ $self->{instance} . '_' . $options{extra_options}->{label_ref} }; |
| 96 |
if (defined($options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}}) && |
| 97 |
$options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}} > 0) { |
| 98 |
$self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic_per_seconds} * 100 / $options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}}; |
| 99 |
$self->{result_values}->{speed} = $options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}}; |
| 100 |
} |
| 101 |
|
| 102 |
$self->{result_values}->{label} = $options{extra_options}->{label_ref}; |
| 103 |
$self->{result_values}->{name} = $options{new_datas}->{ $self->{instance} . '_name' }; |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
sub prefix_interface_output { |
| 108 |
my ($self, %options) = @_; |
| 109 |
|
| 110 |
return "Serial port '" . $options{instance_value}->{name} . "' "; |
| 111 |
} |
| 112 |
|
| 113 |
sub set_counters { |
| 114 |
my ($self, %options) = @_; |
| 115 |
|
| 116 |
$self->{maps_counters_type} = [ |
| 117 |
{ name => 'interfaces', type => 1, cb_prefix_output => 'prefix_interface_output', message_multiple => 'All interfaces are ok', cb_init_counters => 'skip_counters', skipped_code => { -10 => 1 } }, |
| 118 |
]; |
| 119 |
|
| 120 |
$self->{maps_counters}->{interfaces} = [ |
| 121 |
{ label => 'traffic-in', nlabel => 'serial_port.traffic.in.bitspersecond', set => { |
| 122 |
key_values => [ { name => 'in', diff => 1 }, { name => 'speed_in' }, { name => 'name' } ], |
| 123 |
closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'in' }, |
| 124 |
closure_custom_output => $self->can('custom_traffic_output'), output_error_template => 'traffic in: %s', |
| 125 |
closure_custom_perfdata => $self->can('custom_traffic_perfdata'), |
| 126 |
closure_custom_threshold_check => $self->can('custom_traffic_threshold') |
| 127 |
} |
| 128 |
}, |
| 129 |
{ label => 'traffic-out', nlabel => 'serial_port.traffic.out.bitspersecond', set => { |
| 130 |
key_values => [ { name => 'out', diff => 1 }, { name => 'speed_out' }, { name => 'name' } ], |
| 131 |
closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'out' }, |
| 132 |
closure_custom_output => $self->can('custom_traffic_output'), output_error_template => 'traffic out: %s', |
| 133 |
closure_custom_perfdata => $self->can('custom_traffic_perfdata'), |
| 134 |
closure_custom_threshold_check => $self->can('custom_traffic_threshold') |
| 135 |
} |
| 136 |
} |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
sub new { |
| 141 |
my ($class, %options) = @_; |
| 142 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); |
| 143 |
bless $self, $class; |
| 144 |
|
| 145 |
$options{options}->add_options(arguments => { |
| 146 |
'filter-name:s' => { name => 'filter_name' }, |
| 147 |
'units-traffic:s' => { name => 'units_traffic', default => 'percent_delta' }, |
| 148 |
'speed:s' => { name => 'speed' } |
| 149 |
}); |
| 150 |
|
| 151 |
return $self; |
| 152 |
} |
| 153 |
|
| 154 |
sub check_options { |
| 155 |
my ($self, %options) = @_; |
| 156 |
$self->SUPER::check_options(%options); |
| 157 |
|
| 158 |
if (defined($self->{option_results}->{speed}) && $self->{option_results}->{speed} ne '') { |
| 159 |
if ($self->{option_results}->{speed} !~ /^[0-9]+(\.[0-9]+){0,1}$/) { |
| 160 |
$self->{output}->add_option_msg(short_msg => "Speed must be a positive number '" . $self->{option_results}->{speed} . "' (can be a float also)."); |
| 161 |
$self->{output}->option_exit(); |
| 162 |
} else { |
| 163 |
$self->{option_results}->{speed} *= 1000000; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$self->{option_results}->{units_traffic} = 'percent_delta' |
| 168 |
if (!defined($self->{option_results}->{units_traffic}) || |
| 169 |
$self->{option_results}->{units_traffic} eq '' || |
| 170 |
$self->{option_results}->{units_traffic} eq '%'); |
| 171 |
if ($self->{option_results}->{units_traffic} !~ /^(?:percent|percent_delta|bps|counter)$/) { |
| 172 |
$self->{output}->add_option_msg(short_msg => 'Wrong option --units-traffic.'); |
| 173 |
$self->{output}->option_exit(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
my $mapping = { |
| 178 |
rxBytes => { oid => '.1.3.6.1.4.1.25049.16.1.1.3' }, # ogSerialPortStatusRxBytes |
| 179 |
txBytes => { oid => '.1.3.6.1.4.1.25049.16.1.1.4' }, # ogSerialPortStatusTxBytes |
| 180 |
speed => { oid => '.1.3.6.1.4.1.25049.16.1.1.5' } # ogSerialPortStatusSpeed |
| 181 |
}; |
| 182 |
|
| 183 |
sub manage_selection { |
| 184 |
my ($self, %options) = @_; |
| 185 |
|
| 186 |
if ($options{snmp}->is_snmpv1()) { |
| 187 |
$self->{output}->add_option_msg(short_msg => 'Need to use SNMP v2c or v3.'); |
| 188 |
$self->{output}->option_exit(); |
| 189 |
} |
| 190 |
|
| 191 |
my $oid_label = '.1.3.6.1.4.1.25049.16.1.1.11'; # ogSerialPortStatusLabel |
| 192 |
my $snmp_result = $options{snmp}->get_table( |
| 193 |
oid => $oid_label, |
| 194 |
nothing_quit => 1 |
| 195 |
); |
| 196 |
|
| 197 |
$self->{interfaces} = {}; |
| 198 |
foreach (keys %$snmp_result) { |
| 199 |
/\.(\d+)$/; |
| 200 |
my $instance = $1; |
| 201 |
|
| 202 |
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 203 |
$snmp_result->{$_} !~ /$self->{option_results}->{filter_name}/); |
| 204 |
|
| 205 |
$self->{interfaces}->{$instance} = { name => $snmp_result->{$_} }; |
| 206 |
} |
| 207 |
|
| 208 |
return if (scalar(keys %{$self->{interfaces}}) <= 0); |
| 209 |
|
| 210 |
$options{snmp}->load( |
| 211 |
oids => [ |
| 212 |
map($_->{oid}, values(%$mapping)) |
| 213 |
], |
| 214 |
instances => [ map($_, keys %{$self->{interfaces}}) ], |
| 215 |
instance_regexp => '^(.*)$' |
| 216 |
); |
| 217 |
$snmp_result = $options{snmp}->get_leef(); |
| 218 |
|
| 219 |
foreach (keys %{$self->{interfaces}}) { |
| 220 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_); |
| 221 |
|
| 222 |
$self->{interfaces}->{$_}->{in} = $result->{rxBytes}; |
| 223 |
$self->{interfaces}->{$_}->{out} = $result->{txBytes}; |
| 224 |
$self->{interfaces}->{$_}->{speed_in} = defined($self->{option_results}->{speed}) ? $self->{option_results}->{speed} : $result->{speed}; |
| 225 |
$self->{interfaces}->{$_}->{speed_out} = defined($self->{option_results}->{speed}) ? $self->{option_results}->{speed} : $result->{speed}; |
| 226 |
} |
| 227 |
|
| 228 |
$self->{cache_name} = 'opengear_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . |
| 229 |
md5_hex( |
| 230 |
(defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : 'all') . |
| 231 |
(defined($self->{option_results}->{filter_name}) ? $self->{option_results}->{filter_name} : 'all') |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
1; |
| 236 |
|
| 237 |
__END__ |
| 238 |
|
| 239 |
=head1 MODE |
| 240 |
|
| 241 |
Check serial ports. |
| 242 |
|
| 243 |
=over 8 |
| 244 |
|
| 245 |
=item B<--units-traffic> |
| 246 |
|
| 247 |
Units of thresholds for the traffic (default: 'percent_delta') ('percent_delta', 'bps', 'counter'). |
| 248 |
|
| 249 |
=item B<--filter-name> |
| 250 |
|
| 251 |
Filter serial port name (regexp can be used). |
| 252 |
|
| 253 |
=item B<--speed> |
| 254 |
|
| 255 |
Set serial port speed (in Mb). |
| 256 |
|
| 257 |
=item B<--warning-*> B<--critical-*> |
| 258 |
|
| 259 |
Thresholds. |
| 260 |
Can be: 'traffic-in', 'traffic-out'. |
| 261 |
|
| 262 |
=back |
| 263 |
|
| 264 |
=cut |