| 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::fortinet::fortigate::snmp::mode::clusterstatus; |
| 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 Digest::MD5 qw(md5_hex); |
| 29 |
|
| 30 |
sub custom_status_output { |
| 31 |
my ($self, %options) = @_; |
| 32 |
|
| 33 |
return sprintf( |
| 34 |
"status is '%s' [hostname: %s] [role: %s] [checksum: %s]", |
| 35 |
$self->{result_values}->{sync_status}, |
| 36 |
$self->{result_values}->{hostname}, |
| 37 |
$self->{result_values}->{role}, |
| 38 |
$self->{result_values}->{checksum} |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub prefix_global_output { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
return 'Nodes '; |
| 46 |
} |
| 47 |
|
| 48 |
sub node_long_output { |
| 49 |
my ($self, %options) = @_; |
| 50 |
|
| 51 |
return sprintf( |
| 52 |
"checking node '%s'", |
| 53 |
$options{instance_value}->{serial} |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
sub prefix_node_output { |
| 58 |
my ($self, %options) = @_; |
| 59 |
|
| 60 |
return sprintf( |
| 61 |
"node '%s' ", |
| 62 |
$options{instance_value}->{serial} |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
sub custom_status_calc { |
| 67 |
my ($self, %options) = @_; |
| 68 |
|
| 69 |
$self->{result_values}->{roleLast} = $options{old_datas}->{$self->{instance} . '_role'}; |
| 70 |
$self->{result_values}->{role} = $options{new_datas}->{$self->{instance} . '_role'}; |
| 71 |
if (!defined($options{old_datas}->{$self->{instance} . '_role'})) { |
| 72 |
$self->{error_msg} = "buffer creation"; |
| 73 |
return -2; |
| 74 |
} |
| 75 |
|
| 76 |
$self->{result_values}->{sync_status} = $options{new_datas}->{$self->{instance} . '_sync_status'}; |
| 77 |
$self->{result_values}->{hostname} = $options{new_datas}->{$self->{instance} . '_hostname'}; |
| 78 |
$self->{result_values}->{checksum} = $options{new_datas}->{$self->{instance} . '_checksum'}; |
| 79 |
$self->{result_values}->{serial} = $options{new_datas}->{$self->{instance} . '_serial'}; |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
sub set_counters { |
| 84 |
my ($self, %options) = @_; |
| 85 |
|
| 86 |
$self->{maps_counters_type} = [ |
| 87 |
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, |
| 88 |
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_node_output', cb_long_output => 'node_long_output', |
| 89 |
indent_long_output => ' ', message_multiple => 'All cluster nodes are ok', |
| 90 |
group => [ |
| 91 |
{ name => 'status', type => 0, skipped_code => { -10 => 1 } }, |
| 92 |
{ name => 'cpu', type => 0, skipped_code => { -10 => 1 } }, |
| 93 |
{ name => 'memory', type => 0, skipped_code => { -10 => 1 } } |
| 94 |
] |
| 95 |
} |
| 96 |
]; |
| 97 |
|
| 98 |
$self->{maps_counters}->{global} = [ |
| 99 |
{ label => 'total-nodes', nlabel => 'cluster.nodes.count', set => { |
| 100 |
key_values => [ { name => 'total_nodes' } ], |
| 101 |
output_template => 'total: %d', |
| 102 |
perfdatas => [ |
| 103 |
{ template => '%d', min => 0 } |
| 104 |
] |
| 105 |
} |
| 106 |
}, |
| 107 |
{ label => 'synchronized', nlabel => 'cluster.nodes.synchronized.count', set => { |
| 108 |
key_values => [ { name => 'synchronized' } ], |
| 109 |
output_template => 'synchronized: %d', |
| 110 |
perfdatas => [ |
| 111 |
{ template => '%d', min => 0 } |
| 112 |
] |
| 113 |
} |
| 114 |
}, |
| 115 |
{ label => 'unsynchronized', nlabel => 'cluster.nodes.unsynchronized.count',set => { |
| 116 |
key_values => [ { name => 'unsynchronized' } ], |
| 117 |
output_template => 'unsynchronized: %d', |
| 118 |
perfdatas => [ |
| 119 |
{ template => '%d', min => 0 } |
| 120 |
] |
| 121 |
} |
| 122 |
}, |
| 123 |
{ label => 'total-checksums', nlabel => 'cluster.checksums.total.count', display_ok => 0, set => { |
| 124 |
key_values => [ { name => 'total_checksums' } ], |
| 125 |
output_template => 'checksums: %d', |
| 126 |
perfdatas => [ |
| 127 |
{ template => '%d', min => 0 } |
| 128 |
] |
| 129 |
} |
| 130 |
} |
| 131 |
]; |
| 132 |
|
| 133 |
$self->{maps_counters}->{status} = [ |
| 134 |
{ label => 'status', type => 2, critical_default => '%{role} ne %{roleLast} or %{sync_status} =~ /unsynchronized/', set => { |
| 135 |
key_values => [ { name => 'serial' }, { name => 'hostname' }, { name => 'sync_status' }, { name => 'role' }, { name => 'checksum' } ], |
| 136 |
closure_custom_calc => \&custom_status_calc, |
| 137 |
closure_custom_output => $self->can('custom_status_output'), |
| 138 |
closure_custom_perfdata => sub { return 0; }, |
| 139 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 140 |
} |
| 141 |
} |
| 142 |
]; |
| 143 |
|
| 144 |
$self->{maps_counters}->{cpu} = [ |
| 145 |
{ label => 'cpu-utilization', nlabel => 'node.cpu.utilization.percentage', set => { |
| 146 |
key_values => [ { name => 'cpu_usage' }, { name => 'serial' } ], |
| 147 |
output_template => 'cpu usage: %.2f %%', |
| 148 |
perfdatas => [ |
| 149 |
{ template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'serial' } |
| 150 |
] |
| 151 |
} |
| 152 |
} |
| 153 |
]; |
| 154 |
|
| 155 |
$self->{maps_counters}->{memory} = [ |
| 156 |
{ label => 'memory-usage', nlabel => 'node.memory.usage.percentage', set => { |
| 157 |
key_values => [ { name => 'memory_usage' }, { name => 'serial' } ], |
| 158 |
output_template => 'memory used: %.2f %%', |
| 159 |
output_change_bytes => 1, |
| 160 |
perfdatas => [ |
| 161 |
{ template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'serial' } |
| 162 |
] |
| 163 |
} |
| 164 |
} |
| 165 |
]; |
| 166 |
} |
| 167 |
|
| 168 |
sub new { |
| 169 |
my ($class, %options) = @_; |
| 170 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); |
| 171 |
bless $self, $class; |
| 172 |
|
| 173 |
$options{options}->add_options(arguments => { |
| 174 |
'one-node-status:s' => { name => 'one_node_status' } # not used, use --opt-exit instead |
| 175 |
}); |
| 176 |
|
| 177 |
return $self; |
| 178 |
} |
| 179 |
|
| 180 |
my $map_ha_mode = { |
| 181 |
1 => 'standalone', |
| 182 |
2 => 'activeActive', |
| 183 |
3 => 'activePassive' |
| 184 |
}; |
| 185 |
my $map_sync_status = { |
| 186 |
0 => 'unsynchronized', |
| 187 |
1 => 'synchronized' |
| 188 |
}; |
| 189 |
|
| 190 |
my $mapping = { |
| 191 |
cpuUsage => { oid => '.1.3.6.1.4.1.12356.101.13.2.1.1.3' }, # fgHaStatsCpuUsage |
| 192 |
memUsage => { oid => '.1.3.6.1.4.1.12356.101.13.2.1.1.4' }, # fgHaStatsMemUsage |
| 193 |
hostname => { oid => '.1.3.6.1.4.1.12356.101.13.2.1.1.11' }, # fgHaStatsHostname |
| 194 |
syncStatus => { oid => '.1.3.6.1.4.1.12356.101.13.2.1.1.12', map => $map_sync_status }, # fgHaStatsSyncStatus |
| 195 |
globalChecksum => { oid => '.1.3.6.1.4.1.12356.101.13.2.1.1.15' }, # fgHaStatsGlobalChecksum |
| 196 |
masterSerial => { oid => '.1.3.6.1.4.1.12356.101.13.2.1.1.16' } # fgHaStatsMasterSerial |
| 197 |
}; |
| 198 |
my $oid_fgHaSystemMode = '.1.3.6.1.4.1.12356.101.13.1.1.0'; |
| 199 |
|
| 200 |
sub manage_selection { |
| 201 |
my ($self, %options) = @_; |
| 202 |
|
| 203 |
my $mode = $options{snmp}->get_leef(oids => [ $oid_fgHaSystemMode ], nothing_quit => 1); |
| 204 |
|
| 205 |
if ($map_ha_mode->{ $mode->{$oid_fgHaSystemMode} } =~ /standalone/) { |
| 206 |
$self->{output}->add_option_msg(short_msg => "No cluster configuration (standalone mode)"); |
| 207 |
$self->{output}->option_exit(); |
| 208 |
} |
| 209 |
|
| 210 |
$self->{output}->output_add(short_msg => 'High-availibility mode: ' . $map_ha_mode->{ $mode->{$oid_fgHaSystemMode} }); |
| 211 |
|
| 212 |
my $oid_serial = '.1.3.6.1.4.1.12356.101.13.2.1.1.2'; # fgHaStatsSerial |
| 213 |
my $snmp_result = $options{snmp}->get_table( |
| 214 |
oid => $oid_serial, |
| 215 |
nothing_quit => 1 |
| 216 |
); |
| 217 |
|
| 218 |
$self->{global} = { synchronized => 0, unsynchronized => 0, total_nodes => 0 }; |
| 219 |
$self->{nodes} = {}; |
| 220 |
foreach (keys %$snmp_result) { |
| 221 |
/^$oid_serial\.(.*)$/; |
| 222 |
my $instance = $1; |
| 223 |
|
| 224 |
$self->{nodes}->{$instance} = { serial => $snmp_result->{$_} }; |
| 225 |
} |
| 226 |
|
| 227 |
return if (scalar(keys %{$self->{nodes}}) <= 0); |
| 228 |
|
| 229 |
$options{snmp}->load( |
| 230 |
oids => [ map($_->{oid}, values(%$mapping)) ], |
| 231 |
instances => [ map($_, keys(%{$self->{nodes}})) ], |
| 232 |
instance_regexp => '^(.*)$' |
| 233 |
); |
| 234 |
$snmp_result = $options{snmp}->get_leef(); |
| 235 |
|
| 236 |
my $checksums = {}; |
| 237 |
foreach (keys %{$self->{nodes}}) { |
| 238 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_); |
| 239 |
|
| 240 |
$checksums->{ $result->{globalChecksum} } = 1; |
| 241 |
$self->{nodes}->{$_}->{status} = { |
| 242 |
serial => $self->{nodes}->{$_}->{serial}, |
| 243 |
hostname => $result->{hostname}, |
| 244 |
sync_status => $result->{syncStatus}, |
| 245 |
role => ($result->{masterSerial} eq '' || $result->{masterSerial} =~ /$self->{nodes}->{$_}->{serial}/) ? 'master' : 'slave', |
| 246 |
checksum => $result->{globalChecksum} |
| 247 |
}; |
| 248 |
$self->{nodes}->{$_}->{cpu} = { cpu_usage => $result->{cpuUsage}, serial => $self->{nodes}->{$_}->{serial} }; |
| 249 |
$self->{nodes}->{$_}->{memory} = { memory_usage => $result->{memUsage}, serial => $self->{nodes}->{$_}->{serial} }; |
| 250 |
|
| 251 |
$self->{global}->{ $result->{syncStatus} }++; |
| 252 |
$self->{global}->{total_nodes}++; |
| 253 |
} |
| 254 |
|
| 255 |
$self->{global}->{total_checksums} = scalar(keys %$checksums); |
| 256 |
|
| 257 |
$self->{cache_name} = 'fortinet_fortigate_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . |
| 258 |
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); |
| 259 |
} |
| 260 |
|
| 261 |
1; |
| 262 |
|
| 263 |
__END__ |
| 264 |
|
| 265 |
=head1 MODE |
| 266 |
|
| 267 |
Check cluster status. |
| 268 |
|
| 269 |
=over 8 |
| 270 |
|
| 271 |
=item B<--warning-status> |
| 272 |
|
| 273 |
Define the conditions to match for the status to be WARNING. |
| 274 |
You can use the following variables: %{serial}, %{hostname}, %{sync_status}, %{role}, %{roleLast} |
| 275 |
|
| 276 |
=item B<--critical-status> |
| 277 |
|
| 278 |
Define the conditions to match for the status to be CRITICAL (default: '%{role} ne %{roleLast} or %{sync_status} =~ /unsynchronized/'). |
| 279 |
You can use the following variables: %{serial}, %{hostname}, %{sync_status}, %{role}, %{roleLast} |
| 280 |
|
| 281 |
=item B<--warning-*> B<--critical-*> |
| 282 |
|
| 283 |
Set thresholds. |
| 284 |
Can be: 'total-nodes', 'synchronized', 'unsynchronized', |
| 285 |
'total-checksums', 'cpu-utilization', 'memory-usage'. |
| 286 |
|
| 287 |
=back |
| 288 |
|
| 289 |
=cut |