| 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::cisco::standard::snmp::mode::memoryflash; |
| 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 |
|
| 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_mem_output { |
| 37 |
my ($self, %options) = @_; |
| 38 |
|
| 39 |
my $msg = sprintf("Total: %s %s Used: %s %s (%.2f%%) Free: %s %s (%.2f%%)", |
| 40 |
$self->{perfdata}->change_bytes(value => $self->{result_values}->{total}), |
| 41 |
$self->{perfdata}->change_bytes(value => $self->{result_values}->{used}), |
| 42 |
$self->{result_values}->{prct_used}, |
| 43 |
$self->{perfdata}->change_bytes(value => $self->{result_values}->{free}), |
| 44 |
$self->{result_values}->{prct_free}); |
| 45 |
return $msg; |
| 46 |
} |
| 47 |
|
| 48 |
sub set_counters { |
| 49 |
my ($self, %options) = @_; |
| 50 |
|
| 51 |
$self->{maps_counters_type} = [ |
| 52 |
{ name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memory flash partitions are ok', skipped_code => { -10 => 1 } } |
| 53 |
]; |
| 54 |
|
| 55 |
$self->{maps_counters}->{memory} = [ |
| 56 |
{ label => 'status', threshold => 0, set => { |
| 57 |
key_values => [ { name => 'status' }, { name => 'display' } ], |
| 58 |
closure_custom_calc => \&catalog_status_calc, |
| 59 |
closure_custom_output => $self->can('custom_status_output'), |
| 60 |
closure_custom_perfdata => sub { return 0; }, |
| 61 |
closure_custom_threshold_check => \&catalog_status_threshold, |
| 62 |
} |
| 63 |
}, |
| 64 |
{ label => 'usage', nlabel => 'memory.flash.usage.bytes', set => { |
| 65 |
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ], |
| 66 |
closure_custom_output => $self->can('custom_mem_output'), |
| 67 |
perfdatas => [ |
| 68 |
{ value => 'used', template => '%d', min => 0, max => 'total', |
| 69 |
unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' }, |
| 70 |
], |
| 71 |
} |
| 72 |
}, |
| 73 |
{ label => 'usage-free', display_ok => 0, nlabel => 'memory.flash.free.bytes', set => { |
| 74 |
key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ], |
| 75 |
closure_custom_output => $self->can('custom_mem_output'), |
| 76 |
perfdatas => [ |
| 77 |
{ value => 'free', template => '%d', min => 0, max => 'total', |
| 78 |
unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' }, |
| 79 |
], |
| 80 |
} |
| 81 |
}, |
| 82 |
{ label => 'usage-prct', display_ok => 0, nlabel => 'memory.flash.usage.percentage', set => { |
| 83 |
key_values => [ { name => 'prct_used' }, { name => 'display' } ], |
| 84 |
output_template => 'Used : %.2f %%', |
| 85 |
perfdatas => [ |
| 86 |
{ value => 'prct_used', template => '%.2f', min => 0, max => 100, unit => '%', |
| 87 |
label_extra_instance => 1, instance_use => 'display' }, |
| 88 |
], |
| 89 |
} |
| 90 |
}, |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
sub new { |
| 95 |
my ($class, %options) = @_; |
| 96 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 97 |
bless $self, $class; |
| 98 |
|
| 99 |
$options{options}->add_options(arguments => { |
| 100 |
'filter-name:s' => { name => 'filter_name' }, |
| 101 |
'unknown-status:s' => { name => 'unknown_status', default => '%{status} =~ /readOnly/i' }, |
| 102 |
'warning-status:s' => { name => 'warning_status', default => '' }, |
| 103 |
'critical-status:s' => { name => 'critical_status', default => '' }, |
| 104 |
}); |
| 105 |
|
| 106 |
return $self; |
| 107 |
} |
| 108 |
|
| 109 |
sub check_options { |
| 110 |
my ($self, %options) = @_; |
| 111 |
$self->SUPER::check_options(%options); |
| 112 |
|
| 113 |
$self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']); |
| 114 |
} |
| 115 |
|
| 116 |
sub prefix_memory_output { |
| 117 |
my ($self, %options) = @_; |
| 118 |
|
| 119 |
return "Partition '" . $options{instance_value}->{display} . "' "; |
| 120 |
} |
| 121 |
|
| 122 |
my $map_status = { 1 => 'readOnly', 2 => 'runFromFlash', 3 => 'readWrite' }; |
| 123 |
|
| 124 |
my $mapping = { |
| 125 |
ciscoFlashPartitionSize => { oid => '.1.3.6.1.4.1.9.9.10.1.1.4.1.1.4' }, |
| 126 |
ciscoFlashPartitionFreeSpace => { oid => '.1.3.6.1.4.1.9.9.10.1.1.4.1.1.5' }, |
| 127 |
ciscoFlashPartitionStatus => { oid => '.1.3.6.1.4.1.9.9.10.1.1.4.1.1.8', map => $map_status }, |
| 128 |
ciscoFlashPartitionName => { oid => '.1.3.6.1.4.1.9.9.10.1.1.4.1.1.10' }, |
| 129 |
ciscoFlashPartitionSizeExtended => { oid => '.1.3.6.1.4.1.9.9.10.1.1.4.1.1.13' }, |
| 130 |
ciscoFlashPartitionFreeSpaceExtended => { oid => '.1.3.6.1.4.1.9.9.10.1.1.4.1.1.14' }, |
| 131 |
}; |
| 132 |
|
| 133 |
sub manage_selection { |
| 134 |
my ($self, %options) = @_; |
| 135 |
|
| 136 |
my $ciscoFlashPartitionEntry = '.1.3.6.1.4.1.9.9.10.1.1.4.1.1'; |
| 137 |
my $snmp_result = $options{snmp}->get_table( |
| 138 |
oid => $ciscoFlashPartitionEntry, |
| 139 |
nothing_quit => 1 |
| 140 |
); |
| 141 |
|
| 142 |
$self->{memory} = {}; |
| 143 |
foreach my $oid (keys %$snmp_result) { |
| 144 |
next if ($oid !~ /^$mapping->{ciscoFlashPartitionSize}->{oid}\.(.*)/); |
| 145 |
my $instance = $1; |
| 146 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance); |
| 147 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 148 |
$result->{ciscoFlashPartitionName} !~ /$self->{option_results}->{filter_name}/) { |
| 149 |
$self->{output}->output_add(long_msg => "skipping '" . $result->{ciscoFlashPartitionName} . "': no matching filter.", debug => 1); |
| 150 |
next; |
| 151 |
} |
| 152 |
|
| 153 |
my $total = $result->{ciscoFlashPartitionSize}; |
| 154 |
if (defined($result->{ciscoFlashPartitionSizeExtended})) { |
| 155 |
$total = $result->{ciscoFlashPartitionSizeExtended}; |
| 156 |
} |
| 157 |
my $free = $result->{ciscoFlashPartitionFreeSpace}; |
| 158 |
if (defined($result->{ciscoFlashPartitionFreeSpaceExtended})) { |
| 159 |
$free = $result->{ciscoFlashPartitionFreeSpaceExtended}; |
| 160 |
} |
| 161 |
|
| 162 |
my ($used, $prct_used, $prct_free); |
| 163 |
if ($total > 0) { |
| 164 |
$used = $total - $free; |
| 165 |
$prct_used = $used * 100 / $total; |
| 166 |
$prct_free = 100 - $prct_used; |
| 167 |
} |
| 168 |
$self->{memory}->{$instance} = { |
| 169 |
display => $result->{ciscoFlashPartitionName}, |
| 170 |
status => $result->{ciscoFlashPartitionStatus}, |
| 171 |
free => $free, |
| 172 |
used => $used, |
| 173 |
prct_used => $prct_used, |
| 174 |
prct_free => $prct_free, |
| 175 |
total => $total, |
| 176 |
}; |
| 177 |
} |
| 178 |
|
| 179 |
if (scalar(keys %{$self->{memory}}) <= 0) { |
| 180 |
$self->{output}->add_option_msg(short_msg => 'No flash memory found.'); |
| 181 |
$self->{output}->option_exit(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
1; |
| 186 |
|
| 187 |
__END__ |
| 188 |
|
| 189 |
=head1 MODE |
| 190 |
|
| 191 |
Check memory flash usages. |
| 192 |
|
| 193 |
=over 8 |
| 194 |
|
| 195 |
=item B<--unknown-status> |
| 196 |
|
| 197 |
Define the conditions to match for the status to be UNKNOWN (default: '%{status} =~ /readOnly/i'). |
| 198 |
You can use the following variables: %{status}, %{display} |
| 199 |
|
| 200 |
=item B<--warning-status> |
| 201 |
|
| 202 |
Define the conditions to match for the status to be WARNING (default: ''). |
| 203 |
You can use the following variables: %{status}, %{display} |
| 204 |
|
| 205 |
=item B<--critical-status> |
| 206 |
|
| 207 |
Define the conditions to match for the status to be CRITICAL (default: ''). |
| 208 |
You can use the following variables: %{status}, %{display} |
| 209 |
|
| 210 |
=item B<--warning-*> B<--critical-*> |
| 211 |
|
| 212 |
Thresholds. |
| 213 |
Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). |
| 214 |
|
| 215 |
=item B<--filter-name> |
| 216 |
|
| 217 |
Filter partition name (can be a regexp). |
| 218 |
|
| 219 |
=back |
| 220 |
|
| 221 |
=cut |