Debian Codegraph @

src/network/fritzbox/upnp/mode/system.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::system;
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_ng);
28 use POSIX;
29
30 my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 };
31 my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' };
32
33 sub custom_uptime_perfdata {
34 my ($self, %options) = @_;
35
36 $self->{output}->perfdata_add(
37 nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} },
38 unit => $self->{instance_mode}->{option_results}->{unit},
39 value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
40 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
41 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
42 min => 0
43 );
44 }
45
46 sub custom_uptime_threshold {
47 my ($self, %options) = @_;
48
49 return $self->{perfdata}->threshold_check(
50 value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
51 threshold => [
52 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
53 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
54 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
55 ]
56 );
57 }
58
59 sub custom_status_output {
60 my ($self, %options) = @_;
61
62 return sprintf(
63 'physical link status is %s and %s is %s',
64 $self->{result_values}->{link_status},
65 $self->{result_values}->{wan_access_type},
66 $self->{result_values}->{connection_status}
67 );
68 }
69
70 sub set_counters {
71 my ($self, %options) = @_;
72
73 $self->{maps_counters_type} = [
74 { name => 'global', type => 0 }
75 ];
76
77 $self->{maps_counters}->{global} = [
78 {
79 label => 'connection-status',
80 type => 2,
81 critical_default => '%{link_status} !~ /^up$/i and %{connection_status} !~ /^connected$/i',
82 set => {
83 key_values => [
84 { name => 'connection_status' }, { name => 'link_status' }, { name => 'wan_access_type' }
85 ],
86 closure_custom_output => $self->can('custom_status_output'),
87 closure_custom_perfdata => sub { return 0; },
88 closure_custom_threshold_check => \&catalog_status_threshold_ng
89 }
90 },
91 { label => 'uptime', nlabel => 'system.uptime', set => {
92 key_values => [ { name => 'uptime' }, { name => 'uptime_human' } ],
93 output_template => 'uptime: %s',
94 output_use => 'uptime_human',
95 closure_custom_perfdata => $self->can('custom_uptime_perfdata'),
96 closure_custom_threshold_check => $self->can('custom_uptime_threshold')
97 }
98 }
99 ];
100 }
101
102 sub new {
103 my ($class, %options) = @_;
104 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
105 bless $self, $class;
106
107 $options{options}->add_options(arguments => {
108 'unit:s' => { name => 'unit', default => 's' }
109 });
110
111 return $self;
112 }
113
114 sub check_options {
115 my ($self, %options) = @_;
116 $self->SUPER::check_options(%options);
117
118 if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
119 $self->{option_results}->{unit} = 's';
120 }
121 }
122
123 sub manage_selection {
124 my ($self, %options) = @_;
125
126 $self->{global} = {};
127
128 my $infos = $options{custom}->request(url => 'WANCommonIFC1', ns => 'WANCommonInterfaceConfig', verb => 'GetCommonLinkProperties');
129 $self->{global}->{wan_access_type} = $infos->{'s:Body'}->{'u:GetCommonLinkPropertiesResponse'}->{NewWANAccessType};
130 $self->{global}->{link_status} = $infos->{'s:Body'}->{'u:GetCommonLinkPropertiesResponse'}->{NewPhysicalLinkStatus};
131
132 $infos = $options{custom}->request(url => 'WANIPConn1', ns => 'WANIPConnection', verb => 'GetStatusInfo');
133 $self->{global}->{connection_status} = $infos->{'s:Body'}->{'u:GetStatusInfoResponse'}->{NewConnectionStatus};
134 $self->{global}->{uptime} = $infos->{'s:Body'}->{'u:GetStatusInfoResponse'}->{NewUptime}; # seconds
135 $self->{global}->{uptime_human} = centreon::plugins::misc::change_seconds(value => $self->{global}->{uptime});
136 }
137
138 1;
139
140 __END__
141
142 =head1 MODE
143
144 Check system.
145
146 =over 8
147
148 =item B<--filter-counters>
149
150 Only display some counters (regexp can be used).
151 Example: --filter-counters='uptime'
152
153 =item B<--warning-status>
154
155 Define the conditions to match for the status to be WARNING.
156 Can use special variables like: %{connection_status}, %{link_status}
157
158 =item B<--critical-status>
159
160 Define the conditions to match for the status to be CRITICAL (default: '%{link_status} !~ /^up$/i and %{connection_status} !~ /^connected$/i').
161 Can use special variables like: %{connection_status}, %{link_status}
162
163 =item B<--unit>
164
165 Select the unit for uptime threshold. May be 's' for seconds, 'm' for minutes,
166 'h' for hours, 'd' for days, 'w' for weeks. Default is days.
167
168 =item B<--warning-*> B<--critical-*>
169
170 Thresholds.
171 Can be: 'uptime'.
172
173 =back
174
175 =cut