| 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 blockchain::parity::ethpoller::mode::stats; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use bigint; |
| 28 |
use Digest::MD5 qw(md5_hex); |
| 29 |
|
| 30 |
sub set_counters { |
| 31 |
my ($self, %options) = @_; |
| 32 |
|
| 33 |
$self->{maps_counters_type} = [ |
| 34 |
{ name => 'block', cb_prefix_output => 'prefix_output_block', type => 0 }, |
| 35 |
{ name => 'transaction', cb_prefix_output => 'prefix_output_transaction', type => 0 }, |
| 36 |
{ name => 'fork', cb_prefix_output => 'prefix_output_fork', type => 0 } |
| 37 |
]; |
| 38 |
|
| 39 |
$self->{maps_counters}->{block} = [ |
| 40 |
{ label => 'block-frequency', nlabel => 'parity.stats.block.perminute', set => { |
| 41 |
key_values => [ { name => 'block_count', per_minute => 1 }, { name => 'last_block' }, { name => 'last_block_ts' } ], |
| 42 |
closure_custom_output => $self->can('custom_block_output'), |
| 43 |
perfdatas => [ |
| 44 |
{ label => 'block', value => 'block_count', template => '%.2f' } |
| 45 |
], |
| 46 |
} |
| 47 |
} |
| 48 |
]; |
| 49 |
|
| 50 |
$self->{maps_counters}->{transaction} = [ |
| 51 |
{ label => 'transaction-frequency', nlabel => 'parity.stats.transaction.perminute', set => { |
| 52 |
key_values => [ { name => 'transaction_count', per_minute => 1 }, { name => 'last_transaction' }, { name => 'last_transaction_ts' } ], |
| 53 |
closure_custom_output => $self->can('custom_transaction_output'), |
| 54 |
perfdatas => [ |
| 55 |
{ label => 'transaction', value => 'transaction_count', template => '%.2f' } |
| 56 |
], |
| 57 |
} |
| 58 |
} |
| 59 |
]; |
| 60 |
|
| 61 |
$self->{maps_counters}->{fork} = [ |
| 62 |
{ label => 'fork-frequency', nlabel => 'parity.stats.fork.perminute', set => { |
| 63 |
key_values => [ { name => 'fork_count', per_minute => 1 }, { name => 'last_fork' }, { name => 'last_fork_ts' } ], |
| 64 |
closure_custom_output => $self->can('custom_fork_output'), |
| 65 |
perfdatas => [ |
| 66 |
{ label => 'fork', value => 'fork_count', template => '%.2f' } |
| 67 |
], |
| 68 |
} |
| 69 |
} |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
sub custom_block_output { |
| 74 |
my ($self, %options) = @_; |
| 75 |
|
| 76 |
if (0 eq $self->{result_values}->{block_count}) { |
| 77 |
return sprintf("No block yet..."); |
| 78 |
} else { |
| 79 |
return sprintf( |
| 80 |
"Block frequency: '%.2f/min', Last block (#%s)", |
| 81 |
$self->{result_values}->{block_count}, |
| 82 |
$self->{result_values}->{last_block} |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
sub custom_transaction_output { |
| 88 |
my ($self, %options) = @_; |
| 89 |
|
| 90 |
if (0 eq $self->{result_values}->{transaction_count}) { |
| 91 |
return sprintf("No transaction yet..."); |
| 92 |
} else { |
| 93 |
|
| 94 |
return sprintf( |
| 95 |
"Transaction frequency: '%.2f/min', Last transaction (#%s)", |
| 96 |
$self->{result_values}->{transaction_count}, |
| 97 |
$self->{result_values}->{last_transaction} |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
sub custom_fork_output { |
| 103 |
my ($self, %options) = @_; |
| 104 |
|
| 105 |
if (0 eq $self->{result_values}->{fork_count}) { |
| 106 |
return sprintf("No fork occurred yet..."); |
| 107 |
} else { |
| 108 |
|
| 109 |
return sprintf( |
| 110 |
"Fork frequency: '%.2f/min', Last fork (#%s)", |
| 111 |
$self->{result_values}->{fork_count}, |
| 112 |
$self->{result_values}->{last_fork} |
| 113 |
); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
sub prefix_output_block { |
| 119 |
my ($self, %options) = @_; |
| 120 |
|
| 121 |
return "Block stats '"; |
| 122 |
} |
| 123 |
|
| 124 |
sub prefix_output_fork { |
| 125 |
my ($self, %options) = @_; |
| 126 |
|
| 127 |
return "Fork stats '"; |
| 128 |
} |
| 129 |
|
| 130 |
sub prefix_output_transaction { |
| 131 |
my ($self, %options) = @_; |
| 132 |
|
| 133 |
return "Transaction stats '"; |
| 134 |
} |
| 135 |
|
| 136 |
sub new { |
| 137 |
my ($class, %options) = @_; |
| 138 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1, statefile => 1); |
| 139 |
bless $self, $class; |
| 140 |
|
| 141 |
$options{options}->add_options(arguments => { |
| 142 |
"filter-name:s" => { name => 'filter_name' }, |
| 143 |
}); |
| 144 |
|
| 145 |
return $self; |
| 146 |
} |
| 147 |
|
| 148 |
sub manage_selection { |
| 149 |
my ($self, %options) = @_; |
| 150 |
|
| 151 |
$self->{cache_name} = "parity_ethpoller_" . $self->{mode} . '_' . (defined($self->{option_results}->{hostname}) ? $self->{option_results}->{hostname} : 'me') . '_' . |
| 152 |
(defined($self->{option_results}->{port}) ? $self->{option_results}->{port} : 'default') . '_' . |
| 153 |
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); |
| 154 |
|
| 155 |
my $result = $options{custom}->request_api(url_path => '/stats'); |
| 156 |
|
| 157 |
my $last_block_timestamp = (defined($result->{block}->{timestamp}) && $result->{block}->{timestamp} != 0) ? |
| 158 |
$result->{block}->{timestamp} : |
| 159 |
'NONE'; |
| 160 |
my $last_transaction_timestamp = (defined($result->{transaction}->{timestamp}) && $result->{transaction}->{timestamp} != 0) ? |
| 161 |
$result->{transaction}->{timestamp} : |
| 162 |
'NONE'; |
| 163 |
my $last_fork_timestamp = (defined($result->{fork}->{timestamp}) && $result->{fork}->{timestamp} != 0) ? |
| 164 |
$result->{fork}->{timestamp} : |
| 165 |
'NONE'; |
| 166 |
|
| 167 |
$self->{block} = { block_count => $result->{block}->{count}, |
| 168 |
last_block => $result->{block}->{count}, |
| 169 |
last_block_ts => $last_block_timestamp }; |
| 170 |
|
| 171 |
$self->{transaction} = { transaction_count => $result->{transaction}->{count}, |
| 172 |
last_transaction => $result->{transaction}->{count}, |
| 173 |
last_transaction_ts => $last_transaction_timestamp }; |
| 174 |
|
| 175 |
$self->{fork} = { fork_count => $result->{fork}->{count}, |
| 176 |
last_fork => $result->{fork}->{count}, |
| 177 |
last_fork_ts => $last_fork_timestamp }; |
| 178 |
} |
| 179 |
|
| 180 |
1; |
| 181 |
|
| 182 |
__END__ |
| 183 |
|
| 184 |
=head1 MODE |
| 185 |
|
| 186 |
Check Parity eth-poller for statsitics about blocks, transactions and forks |
| 187 |
|
| 188 |
=cut |