Debian Codegraph @

src/network/fritzbox/upnp/mode/traffic.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 network::fritzbox::upnp::mode::traffic;
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}->{unit} 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}->{unit} =~ /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}->{unit} eq 'counter') {
42 my $nlabel = $self->{nlabel};
43 $nlabel =~ s/bitspersecond/bits/;
44 $self->{output}->perfdata_add(
45 nlabel => $nlabel,
46 unit => 'b',
47 value => $self->{result_values}->{traffic_counter},
48 warning => $warning,
49 critical => $critical,
50 min => 0
51 );
52 } else {
53 $self->{output}->perfdata_add(
54 nlabel => $self->{nlabel},
55 unit => 'b/s',
56 value => sprintf('%.2f', $self->{result_values}->{traffic_per_seconds}),
57 warning => $warning,
58 critical => $critical,
59 min => 0, max => $self->{result_values}->{speed}
60 );
61 }
62 }
63
64 sub custom_traffic_threshold {
65 my ($self, %options) = @_;
66
67 my $exit = 'ok';
68 if ($self->{instance_mode}->{option_results}->{unit} eq 'percent_delta' && defined($self->{result_values}->{speed})) {
69 $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' } ]);
70 } elsif ($self->{instance_mode}->{option_results}->{unit} eq 'bps') {
71 $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' } ]);
72 } elsif ($self->{instance_mode}->{option_results}->{unit} eq 'counter') {
73 $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' } ]);
74 }
75 return $exit;
76 }
77
78 sub custom_traffic_output {
79 my ($self, %options) = @_;
80
81 my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic_per_seconds}, network => 1);
82 return sprintf(
83 'Traffic %s: %s/s (%s)',
84 ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit,
85 defined($self->{result_values}->{traffic_prct}) ? sprintf('%.2f%%', $self->{result_values}->{traffic_prct}) : '-'
86 );
87 }
88
89 sub custom_traffic_calc {
90 my ($self, %options) = @_;
91
92 my $diff_traffic = ($options{new_datas}->{ $self->{instance} . '_total_' . $options{extra_options}->{label_ref} } - $options{old_datas}->{ $self->{instance} . '_total_' . $options{extra_options}->{label_ref} });
93 $self->{result_values}->{traffic_per_seconds} = $diff_traffic / $options{delta_time};
94 $self->{result_values}->{traffic_counter} = $options{new_datas}->{ $self->{instance} . '_total_' . $options{extra_options}->{label_ref} };
95 if (defined($options{new_datas}->{$self->{instance} . '_max_' . $options{extra_options}->{label_ref}}) &&
96 $options{new_datas}->{$self->{instance} . '_max_' . $options{extra_options}->{label_ref}} > 0) {
97 $self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic_per_seconds} * 100 / $options{new_datas}->{$self->{instance} . '_max_' . $options{extra_options}->{label_ref}};
98 $self->{result_values}->{speed} = $options{new_datas}->{$self->{instance} . '_max_' . $options{extra_options}->{label_ref}};
99 }
100
101 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
102 return 0;
103 }
104
105 sub set_counters {
106 my ($self, %options) = @_;
107
108 $self->{maps_counters_type} = [
109 { name => 'global', type => 0 }
110 ];
111
112 $self->{maps_counters}->{global} = [
113 { label => 'traffic-in', nlabel => 'system.interface.wan.traffic.in.bitspersecond', set => {
114 key_values => [ { name => 'total_in', diff => 1 }, { name => 'max_in'} ],
115 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'in' },
116 closure_custom_output => $self->can('custom_traffic_output'), output_error_template => 'Traffic In : %s',
117 closure_custom_perfdata => $self->can('custom_traffic_perfdata'),
118 closure_custom_threshold_check => $self->can('custom_traffic_threshold')
119 }
120 },
121 { label => 'traffic-out', nlabel => 'system.interface.wan.traffic.out.bitspersecond', set => {
122 key_values => [ { name => 'total_out', diff => 1 }, { name => 'max_out'} ],
123 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'out' },
124 closure_custom_output => $self->can('custom_traffic_output'), output_error_template => 'Traffic Out : %s',
125 closure_custom_perfdata => $self->can('custom_traffic_perfdata'),
126 closure_custom_threshold_check => $self->can('custom_traffic_threshold')
127 }
128 }
129 ];
130 }
131
132 sub new {
133 my ($class, %options) = @_;
134 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
135 bless $self, $class;
136
137 $options{options}->add_options(arguments => {
138 'unit:s' => { name => 'unit', default => 'percent_delta' }
139 });
140
141 return $self;
142 }
143
144 sub check_options {
145 my ($self, %options) = @_;
146 $self->SUPER::check_options(%options);
147
148 $self->{option_results}->{unit} = 'percent_delta'
149 if (!defined($self->{option_results}->{unit}) ||
150 $self->{option_results}->{unit} eq '' ||
151 $self->{option_results}->{unit} eq '%');
152 if ($self->{option_results}->{unit} !~ /^(?:percent_delta|bps|counter)$/) {
153 $self->{output}->add_option_msg(short_msg => 'Wrong option --unit');
154 $self->{output}->option_exit();
155 }
156 }
157
158 sub manage_selection {
159 my ($self, %options) = @_;
160
161 $self->{cache_name} = 'fritzbox_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' .
162 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
163 $self->{global} = {};
164
165 =pod
166 <?xml version="1.0" encoding="utf-8"?>
167 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
168 <s:Body>
169 <u:GetAddonInfosResponse xmlns:u="urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1">
170 <NewByteSendRate>6686</NewByteSendRate> <--- upload
171 <NewByteReceiveRate>1414078</NewByteReceiveRate> <--- download
172 <NewPacketSendRate>0</NewPacketSendRate>
173 <NewPacketReceiveRate>0</NewPacketReceiveRate>
174 <NewTotalBytesSent>819115474</NewTotalBytesSent>
175 <NewTotalBytesReceived>2080148768</NewTotalBytesReceived>
176 <NewAutoDisconnectTime>0</NewAutoDisconnectTime>
177 <NewIdleDisconnectTime>0</NewIdleDisconnectTime>
178 <NewDNSServer1>83.169.185.161</NewDNSServer1>
179 <NewDNSServer2>83.169.185.225</NewDNSServer2>
180 <NewVoipDNSServer1>83.169.185.132</NewVoipDNSServer1>
181 <NewVoipDNSServer2>83.169.185.127</NewVoipDNSServer2>
182 <NewUpnpControlEnabled>1</NewUpnpControlEnabled>
183 <NewRoutedBridgedModeBoth>1</NewRoutedBridgedModeBoth>
184 <NewX_AVM_DE_TotalBytesSent64>819115474</NewX_AVM_DE_TotalBytesSent64>
185 <NewX_AVM_DE_TotalBytesReceived64>2080148768</NewX_AVM_DE_TotalBytesReceived64>
186 <NewX_AVM_DE_WANAccessType>Cable</NewX_AVM_DE_WANAccessType>
187 </u:GetAddonInfosResponse>
188 </s:Body>
189 =cut
190 my $infos = $options{custom}->request(url => 'WANCommonIFC1', ns => 'WANCommonInterfaceConfig', verb => 'GetAddonInfos');
191 $self->{global}->{total_out} = $infos->{'s:Body'}->{'u:GetAddonInfosResponse'}->{NewTotalBytesSent} * 8;
192 $self->{global}->{total_in} = $infos->{'s:Body'}->{'u:GetAddonInfosResponse'}->{NewTotalBytesReceived} * 8;
193
194 $infos = $options{custom}->request(url => 'WANCommonIFC1', ns => 'WANCommonInterfaceConfig', verb => 'GetCommonLinkProperties');
195 $self->{global}->{max_out} = $infos->{'s:Body'}->{'u:GetCommonLinkPropertiesResponse'}->{NewLayer1UpstreamMaxBitRate};
196 $self->{global}->{max_in} = $infos->{'s:Body'}->{'u:GetCommonLinkPropertiesResponse'}->{NewLayer1DownstreamMaxBitRate};
197 }
198
199 1;
200
201 __END__
202
203 =head1 MODE
204
205 Checks your FritzBox traffic on WAN interface.
206
207 =over 8
208
209 =item B<--filter-counters>
210
211 Only display some counters (regexp can be used).
212 Example: --filter-counters='^connections$'
213
214 =item B<--unit>
215
216 Unit of thresholds for the traffic (default: 'percent_delta') ('percent_delta', 'bps', 'counter').
217
218 =item B<--warning-*> B<--critical-*>
219
220 Thresholds.
221 Can be: 'traffic-in', 'traffic-out'.
222
223 =back
224
225 =cut