Debian Codegraph @

src/blockchain/parity/ethpoller/mode/tracking.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 blockchain::parity::ethpoller::mode::tracking;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use bigint;
28 use Math::BigFloat;
29 use Digest::MD5 qw(md5_hex);
30
31 sub set_counters {
32 my ($self, %options) = @_;
33
34 $self->{maps_counters_type} = [
35 { name => 'events', cb_prefix_output => 'prefix_output_events', type => 1, message_multiple => 'Events metrics are ok' },
36 { name => 'mining', cb_prefix_output => 'prefix_output_mining', type => 1, message_multiple => 'Mining metrics are ok' },
37 { name => 'balance', cb_prefix_output => 'prefix_output_balances', type => 1, message_multiple => 'Balances metrics are ok' }
38 ];
39
40 $self->{maps_counters}->{events} = [
41 { label => 'events-frequency', nlabel => 'parity.tracking.events.perminute', set => {
42 key_values => [ { name => 'events_count', per_minute => 1 }, { name => 'display' },
43 { name => 'last_event' }, { name => 'last_event_block' }, { name => 'last_event_ts' } ],
44 closure_custom_output => $self->can('custom_event_output'),
45 perfdatas => [
46 { template => '%.2f', label_extra_instance => 1, instance_use => 'display' }
47 ]
48 }
49 }
50 ];
51
52 $self->{maps_counters}->{mining} = [
53 { label => 'mining-frequency', nlabel => 'parity.tracking.mined.block.perminute', set => {
54 key_values => [ { name => 'mining_count', per_minute => 1 }, { name => 'display' },
55 , { name => 'last_mining' }, { name => 'last_mining_block' }, { name => 'last_mining_ts' } ],
56 closure_custom_output => $self->can('custom_miner_output'),
57 perfdatas => [
58 { template => '%.2f', label_extra_instance => 1, instance_use => 'display' }
59 ]
60 }
61 },
62 { label => 'mining-prct', nlabel => 'parity.tracking.mined.block.prct', display_ok => 0, set => {
63 key_values => [],
64 manual_keys => 1,
65 closure_custom_calc => $self->can('custom_mining_prct_calc'),
66 closure_custom_output => $self->can('custom_mining_prct_output'),
67 threshold_use => 'mining_prct',
68 perfdatas => [
69 { value => 'mining_prct', template => '%.2f', unit => '%',
70 min => 0, label_extra_instance => 1, instance_use => 'display' }
71 ],
72 }
73 }
74 ];
75
76 $self->{maps_counters}->{balance} = [
77 # { label => 'balance-fluctuation-prct', nlabel => 'parity.tracking.balances.fluctuation', display_ok => 0, set => {
78 # key_values => [],
79 # manual_keys => 1,
80 # closure_custom_calc => $self->can('custom_balance_prct_calc'),
81 # closure_custom_output => $self->can('custom_balance_prct_output'),
82 # threshold_use => 'balance_fluctuation_prct',
83 # perfdatas => [
84 # { value => 'balance_fluctuation_prct', template => '%.2f', unit => '%',
85 # min => 0, label_extra_instance => 1, instance_use => 'display' }
86 # ],
87 # }
88 # }
89 { label => 'balance-changes', nlabel => 'parity.tracking.balance.changes.perminute', set => {
90 key_values => [ { name => 'balance_count', per_minute => 1 }, { name => 'display' },
91 { name => 'last_balance' } ],
92 closure_custom_output => $self->can('custom_balance_output'),
93 perfdatas => [
94 { template => '%.2f', label_extra_instance => 1, unit => 'wei', instance_use => 'display' }
95 ]
96 }
97 },
98 ];
99 }
100
101 sub prefix_output_balances {
102 my ($self, %options) = @_;
103
104 return "Balance '" . $options{instance_value}->{display} . "' ";
105 }
106
107 sub prefix_output_events {
108 my ($self, %options) = @_;
109
110 return "Event '" . $options{instance_value}->{display} . "' ";
111 }
112
113 sub prefix_output_mining {
114 my ($self, %options) = @_;
115
116 return "Miner '" . $options{instance_value}->{display} . "' ";
117 }
118
119 sub custom_mining_prct_output {
120 my ($self, %options) = @_;
121
122 return sprintf(
123 "Mined: %d blocks, witch corresponds to %.2f %% of total validated block",
124 $self->{result_values}->{mined_block_count},
125 $self->{result_values}->{mining_prct}
126 );
127 }
128
129 sub custom_mining_prct_calc {
130 my ($self, %options) = @_;
131
132 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
133 $self->{result_values}->{mined_block_count} = Math::BigFloat->new($options{new_datas}->{$self->{instance} . '_mined_block_count'});
134 $self->{result_values}->{total_block} = Math::BigFloat->new($options{new_datas}->{$self->{instance} . '_total_block'});
135 $self->{result_values}->{mining_prct} = (defined($self->{result_values}->{total_block}) && $self->{result_values}->{total_block} != 0) ?
136 $self->{result_values}->{mined_block_count} / $self->{result_values}->{total_block} * 100 : 0;
137 return 0;
138 }
139
140 # sub custom_balance_prct_output {
141 # my ($self, %options) = @_;
142
143 # return sprintf(
144 # "Balance: %s ether, Last fluctuation: %.2f ",
145 # $self->{result_values}->{balance},
146 # $self->{result_values}->{balance_fluctuation_prct}
147 # );
148 # }
149
150 # sub custom_balance_prct_calc {
151 # my ($self, %options) = @_;
152
153 # $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
154 # $self->{result_values}->{balance} = Math::BigFloat->new($options{new_datas}->{$self->{instance} . '_balance'});
155 # $self->{result_values}->{balance_old} = Math::BigFloat->new($options{old_datas}->{$self->{instance} . '_balance'});
156 # $self->{result_values}->{balance_fluctuation_prct} = (defined($self->{result_values}->{balance_old}) && $self->{result_values}->{balance_old} != 0) ?
157 # ($self->{result_values}->{balance} - $self->{result_values}->{balance_old}) /
158 # $self->{result_values}->{balance_old} * 100 : 0;
159
160 # return 0;
161 # }
162
163 sub custom_event_output {
164 my ($self, %options) = @_;
165
166 if (0 eq $self->{result_values}->{last_event}) {
167 return sprintf("No event yet...");
168 } else {
169
170 return sprintf(
171 "Event frequency: %.2f/min, Last event (#%s) was in block #%s",
172 $self->{result_values}->{event_count},
173 $self->{result_values}->{last_event},
174 $self->{result_values}->{last_event_block}
175 );
176 }
177 }
178
179 sub custom_miner_output {
180 my ($self, %options) = @_;
181
182 if (0 eq $self->{result_values}->{last_mining}) {
183 return sprintf("No validation yet...");
184 } else {
185
186 return sprintf(
187 "Mining frequency: %.2f/min, Last validation (#%s) ago for block #%s",
188 $self->{result_values}->{mining_count},
189 $self->{result_values}->{last_mining},
190 $self->{result_values}->{last_mining_block}
191 );
192 }
193 }
194
195 sub custom_balance_output {
196 my ($self, %options) = @_;
197
198 if (0 eq $self->{result_values}->{balance_count}) {
199 return sprintf("No change in balance in last minute. Balance still %s wei",
200 $self->{result_values}->{last_balance});
201 } else {
202 return sprintf(
203 "Balance changes: %.2f/min. New balance: %s",
204 $self->{result_values}->{balance_count},
205 $self->{result_values}->{last_balance}
206 );
207 }
208 }
209
210 sub new {
211 my ($class, %options) = @_;
212 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
213 bless $self, $class;
214
215 $options{options}->add_options(arguments => {
216 "filter-name:s" => { name => 'filter_name' },
217 });
218
219 return $self;
220 }
221
222 sub manage_selection {
223 my ($self, %options) = @_;
224
225 $self->{cache_name} = "parity_ethpoller_" . $self->{mode} . '_' . (defined($self->{option_results}->{hostname}) ? $self->{option_results}->{hostname} : 'me') . '_' .
226 (defined($self->{option_results}->{port}) ? $self->{option_results}->{port} : 'default') . '_' .
227 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
228
229 my $results = $options{custom}->request_api(url_path => '/tracking');
230
231 $self->{events} = {};
232 $self->{mining} = {};
233 $self->{balance} = {};
234
235 foreach my $event (@{$results->{events}}) {
236 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
237 $event->{id} !~ /$self->{option_results}->{filter_name}/) {
238 $self->{output}->output_add(long_msg => "skipping '" . $event->{label} . "': no matching filter name.", debug => 1);
239 next;
240 }
241
242 my $last_event_timestamp = (defined($event->{timestamp}) && $event->{timestamp} != 0) ? $event->{timestamp} : 'NONE';
243
244 $self->{events}->{lc($event->{label})} = {
245 display => lc($event->{label}),
246 events_count => $event->{count},
247 last_event => $event->{count},
248 last_event_block => $event->{block},
249 last_event_ts => $last_event_timestamp
250 };
251 }
252
253 foreach my $miner (@{$results->{miners}}) {
254 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
255 $miner->{id} !~ /$self->{option_results}->{filter_name}/) {
256 $self->{output}->output_add(long_msg => "skipping '" . $miner->{label} . "': no matching filter name.", debug => 1);
257 next;
258 }
259
260 my $last_mining_timestamp = (defined($miner->{timestamp}) && $miner->{timestamp} != 0) ? $miner->{timestamp} : 'NONE';
261
262 $self->{mining}->{lc($miner->{label})} = {
263 display => lc($miner->{label}),
264 mining_count => $miner->{count},
265 last_mining => $miner->{count},
266 last_mining_block => $miner->{block},
267 last_mining_ts => $last_mining_timestamp,
268 total_block => $miner->{currentBlock},
269 mined_block_count => $miner->{count}
270 };
271 }
272
273 foreach my $balance (@{$results->{balances}}) {
274 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
275 $balance->{id} !~ /$self->{option_results}->{filter_name}/) {
276 $self->{output}->output_add(long_msg => "skipping '" . $balance->{label} . "': no matching filter name.", debug => 1);
277 next;
278 }
279
280 $self->{balance}->{lc($balance->{label})} = {
281 display => lc($balance->{label}),
282 balance_count => $balance->{balance},
283 last_balance => $balance->{balance}
284 };
285 }
286
287 }
288
289 1;
290
291 __END__
292
293 =head1 MODE
294
295 Check Parity eth-poller for events, miners and balances tracking
296
297 =cut