| 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 apps::voip::asterisk::ami::mode::sippeersusage; |
| 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 |
|
| 29 |
sub custom_status_output { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
return sprintf('status : %s', $self->{result_values}->{status}); |
| 33 |
} |
| 34 |
|
| 35 |
sub custom_status_calc { |
| 36 |
my ($self, %options) = @_; |
| 37 |
|
| 38 |
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; |
| 39 |
$self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'}; |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
sub prefix_sip_output { |
| 44 |
my ($self, %options) = @_; |
| 45 |
|
| 46 |
return "Peer '" . $options{instance_value}->{name} . "' "; |
| 47 |
} |
| 48 |
|
| 49 |
sub set_counters { |
| 50 |
my ($self, %options) = @_; |
| 51 |
|
| 52 |
$self->{maps_counters_type} = [ |
| 53 |
{ name => 'global', type => 0 }, |
| 54 |
{ name => 'sip', type => 1, cb_prefix_output => 'prefix_sip_output', message_multiple => 'All SIP peers are ok' }, |
| 55 |
]; |
| 56 |
|
| 57 |
$self->{maps_counters}->{global} = [ |
| 58 |
{ label => 'total-peers', nlabel => 'sip.peers.total.count', set => { |
| 59 |
key_values => [ { name => 'total_peers' } ], |
| 60 |
output_template => 'total peers: %s', |
| 61 |
perfdatas => [ |
| 62 |
{ label => 'total_peers', template => '%s', min => 0 } |
| 63 |
] |
| 64 |
} |
| 65 |
}, |
| 66 |
{ label => 'monitor-online-peers', nlabel => 'sip.peers.monitor.online.count', set => { |
| 67 |
key_values => [ { name => 'monitor_online_peers' } ], |
| 68 |
output_template => 'monitor online peers: %s', |
| 69 |
perfdatas => [ |
| 70 |
{ label => 'monitor_online_peers', template => '%s', min => 0 } |
| 71 |
] |
| 72 |
} |
| 73 |
}, |
| 74 |
{ label => 'monitor-offline-peers', nlabel => 'sip.peers.monitor.offline.count', set => { |
| 75 |
key_values => [ { name => 'monitor_offline_peers' } ], |
| 76 |
output_template => 'monitor offline peers: %s', |
| 77 |
perfdatas => [ |
| 78 |
{ label => 'monitor_offline_peers', template => '%s', min => 0 } |
| 79 |
] |
| 80 |
} |
| 81 |
}, |
| 82 |
{ label => 'unmonitor-online-peers', nlabel => 'sip.peers.unmonitor.online.count', set => { |
| 83 |
key_values => [ { name => 'unmonitor_online_peers' } ], |
| 84 |
output_template => 'unmonitor online peers: %s', |
| 85 |
perfdatas => [ |
| 86 |
{ label => 'unmonitor_online_peers', template => '%s', min => 0 } |
| 87 |
] |
| 88 |
} |
| 89 |
}, |
| 90 |
{ label => 'unmonitor-offline-peers', nlabel => 'sip.peers.unmonitor.offline.count', set => { |
| 91 |
key_values => [ { name => 'unmonitor_offline_peers' } ], |
| 92 |
output_template => 'unmonitor offline peers: %s', |
| 93 |
perfdatas => [ |
| 94 |
{ label => 'unmonitor_offline_peers', template => '%s', min => 0 } |
| 95 |
] |
| 96 |
} |
| 97 |
} |
| 98 |
]; |
| 99 |
$self->{maps_counters}->{sip} = [ |
| 100 |
{ |
| 101 |
label => 'status', |
| 102 |
type => 2, |
| 103 |
warning_default => '%{status} =~ /LAGGED|UNKNOWN/i', |
| 104 |
critical_default => '%{status} =~ /UNREACHABLE/i', |
| 105 |
set => { |
| 106 |
key_values => [ { name => 'name' }, { name => 'status' } ], |
| 107 |
closure_custom_calc => $self->can('custom_status_calc'), |
| 108 |
closure_custom_output => $self->can('custom_status_output'), |
| 109 |
closure_custom_perfdata => sub { return 0; }, |
| 110 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 111 |
} |
| 112 |
} |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
sub new { |
| 117 |
my ($class, %options) = @_; |
| 118 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 119 |
bless $self, $class; |
| 120 |
|
| 121 |
$options{options}->add_options(arguments => { |
| 122 |
"filter-name:s" => { name => 'filter_name' } |
| 123 |
}); |
| 124 |
|
| 125 |
return $self; |
| 126 |
} |
| 127 |
|
| 128 |
sub manage_selection { |
| 129 |
my ($self, %options) = @_; |
| 130 |
|
| 131 |
# Status can be: UNREACHABLE, LAGGED (%d ms), OK (%d ms), UNKNOWN, Unmonitored |
| 132 |
|
| 133 |
#Name/username Host Dyn Forcerport Comedia ACL Port Status Description |
| 134 |
#02l44k/02l44k 10.9.0.61 D No No 5060 Unmonitored |
| 135 |
#0rafkw/0rafkw 10.9.0.28 D No No 5060 Unmonitored |
| 136 |
#... |
| 137 |
#55 sip peers [Monitored: 0 online, 0 offline Unmonitored: 43 online, 12 offline] |
| 138 |
my $result = $options{custom}->command(cmd => 'sip show peers'); |
| 139 |
|
| 140 |
$self->{sip} = {}; |
| 141 |
foreach my $line (split /\n/, $result) { |
| 142 |
if ($line =~ /^(.*?)\s+.*(UNREACHABLE|LAGGED|OK|UNKNOWN|Unmonitored)\s/msg) { |
| 143 |
my ($name, $status) = ($1, $2); |
| 144 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 145 |
$name !~ /$self->{option_results}->{filter_name}/) { |
| 146 |
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); |
| 147 |
next; |
| 148 |
} |
| 149 |
|
| 150 |
$self->{sip}->{$name} = { |
| 151 |
name => $name, |
| 152 |
status => $status, |
| 153 |
}; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
$self->{global} = { |
| 158 |
total_peers => $1, |
| 159 |
monitor_online_peers => $2, monitor_offline_peers => $3, |
| 160 |
unmonitor_online_peers => $4, unmonitor_offline_peers => $5, |
| 161 |
} if ($result =~ /(\d+) sip peers \[Monitored: (\d+) online, (\d+) offline Unmonitored: (\d+) online, (\d+) offline]/msi); |
| 162 |
|
| 163 |
if (scalar(keys %{$self->{sip}}) <= 0) { |
| 164 |
$self->{output}->add_option_msg(short_msg => "No sip peers found."); |
| 165 |
$self->{output}->option_exit(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
1; |
| 170 |
|
| 171 |
__END__ |
| 172 |
|
| 173 |
=head1 MODE |
| 174 |
|
| 175 |
Check SIP peers usage. |
| 176 |
|
| 177 |
=over 8 |
| 178 |
|
| 179 |
=item B<--filter-name> |
| 180 |
|
| 181 |
Filter sip peer name (can be a regexp). |
| 182 |
|
| 183 |
=item B<--warning-status> |
| 184 |
|
| 185 |
Define the conditions to match for the status to be WARNING (default: '%{status} =~ /LAGGED|UNKNOWN/i'). |
| 186 |
You can use the following variables: %{name}, %{status} |
| 187 |
|
| 188 |
=item B<--critical-status> |
| 189 |
|
| 190 |
Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /UNREACHABLE/i'). |
| 191 |
You can use the following variables: %{name}, %{status} |
| 192 |
|
| 193 |
=item B<--warning-*> |
| 194 |
|
| 195 |
Warning threshold. |
| 196 |
Can be: 'total-peers', 'monitor-online-peers', 'monitor-offline-peers', |
| 197 |
'unmonitor-online-peers', 'unmonitor-offline-peers'. |
| 198 |
|
| 199 |
=item B<--critical-*> |
| 200 |
|
| 201 |
Critical threshold. |
| 202 |
Can be: 'total-peers', 'monitor-online-peers', 'monitor-offline-peers', |
| 203 |
'unmonitor-online-peers', 'unmonitor-offline-peers'. |
| 204 |
|
| 205 |
=back |
| 206 |
|
| 207 |
=cut |