Debian Codegraph @

src/hardware/devices/camera/hikvision/snmp/mode/memory.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::camera::hikvision::snmp::mode::memory;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28
29 sub custom_mem_output {
30 my ($self, %options) = @_;
31
32 my $msg = sprintf("Memory Total: %s %s Used: %s %s (%.2f%%) Free: %s %s (%.2f%%)",
33 $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}),
34 $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}),
35 $self->{result_values}->{prct_used},
36 $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}),
37 $self->{result_values}->{prct_free}
38 );
39 return $msg;
40 }
41
42 sub set_counters {
43 my ($self, %options) = @_;
44
45 $self->{maps_counters_type} = [
46 { name => 'memory', type => 0 }
47 ];
48
49 $self->{maps_counters}->{memory} = [
50 { label => 'usage', nlabel => 'memory.usage.bytes', set => {
51 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
52 closure_custom_output => $self->can('custom_mem_output'),
53 perfdatas => [
54 { value => 'used', template => '%d', min => 0, max => 'total',
55 unit => 'B', cast_int => 1 },
56 ],
57 }
58 },
59 { label => 'usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => {
60 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
61 closure_custom_output => $self->can('custom_mem_output'),
62 perfdatas => [
63 { value => 'free', template => '%d', min => 0, max => 'total',
64 unit => 'B', cast_int => 1 },
65 ],
66 }
67 },
68 { label => 'usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => {
69 key_values => [ { name => 'prct_used' } ],
70 output_template => 'Memory Used : %.2f %%',
71 perfdatas => [
72 { value => 'prct_used', template => '%.2f', min => 0, max => 100, unit => '%' },
73 ],
74 }
75 },
76 ];
77 }
78
79 sub new {
80 my ($class, %options) = @_;
81 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
82 bless $self, $class;
83
84 $options{options}->add_options(arguments => {
85 });
86
87 return $self;
88 }
89
90 sub manage_selection {
91 my ($self, %options) = @_;
92
93 my $oid_memSize = '.1.3.6.1.4.1.39165.1.10.0';
94 my $oid_memUsed = '.1.3.6.1.4.1.39165.1.11.0';
95 my $snmp_result = $options{snmp}->get_leef(
96 oids => [$oid_memSize, $oid_memUsed],
97 nothing_quit => 1
98 );
99
100 if ($snmp_result->{$oid_memSize} !~ /([0-9\.]+)\s+(\S+)/) {
101 $self->{output}->add_option_msg(short_msg => 'cannot parse memory size: ' . $snmp_result->{$oid_memSize});
102 $self->{output}->option_exit();
103 }
104 my ($total, $unit) = ($1, $2);
105 $total = centreon::plugins::misc::convert_bytes(value => $total, unit => $unit);
106
107 if ($snmp_result->{$oid_memUsed} !~ /([0-9\.]+)/) {
108 $self->{output}->add_option_msg(short_msg => 'cannot parse memory used: ' . $snmp_result->{$oid_memUsed});
109 $self->{output}->option_exit();
110 }
111 my $prct_used = $1;
112
113 $self->{memory} = {
114 free => $total * (100 - $prct_used) / 100,
115 used => $total * $prct_used / 100,
116 prct_used => $prct_used,
117 prct_free => 100 - $prct_used,
118 total => $total,
119 };
120 }
121
122 1;
123
124 __END__
125
126 =head1 MODE
127
128 Check memory usage.
129
130 =over 8
131
132 =item B<--warning-*> B<--critical-*>
133
134 Thresholds.
135 Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%).
136
137 =back
138
139 =cut