Debian Codegraph @

src/database/warp10/sensision/mode/scriptstatistics.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 database::warp10::sensision::mode::scriptstatistics;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::common::monitoring::openmetrics::scrape;
28 use Digest::MD5 qw(md5_hex);
29
30 sub set_counters {
31 my ($self, %options) = @_;
32
33 $self->{maps_counters_type} = [
34 { name => 'global', type => 0 },
35 { name => 'functions', type => 1, cb_prefix_output => 'prefix_output',
36 message_multiple => 'All functions statistics are ok' },
37 ];
38
39 $self->{maps_counters}->{global} = [
40 { label => 'time-total', nlabel => 'time.total.microseconds', set => {
41 key_values => [ { name => 'time', diff => 1 } ],
42 output_template => 'Time Spent: %d us',
43 perfdatas => [
44 { template => '%d', min => 0, unit => 'us' },
45 ],
46 }
47 },
48 { label => 'requests-count', nlabel => 'requests.count', set => {
49 key_values => [ { name => 'requests', diff => 1 } ],
50 output_template => 'Requests: %d',
51 perfdatas => [
52 { template => '%d', min => 0 },
53 ],
54 }
55 },
56 { label => 'requests-persecond', nlabel => 'requests.persecond', set => {
57 key_values => [ { name => 'requests', per_second => 1 } ],
58 output_template => 'Requests (per second): %.2f',
59 perfdatas => [
60 { template => '%.2f', min => 0 },
61 ],
62 }
63 },
64 { label => 'ops-count', nlabel => 'ops.count', set => {
65 key_values => [ { name => 'ops', diff => 1 } ],
66 output_template => 'Ops: %d',
67 perfdatas => [
68 { template => '%d', min => 0 },
69 ],
70 }
71 },
72 { label => 'ops-persecond', nlabel => 'ops.persecond', set => {
73 key_values => [ { name => 'ops', per_second => 1 } ],
74 output_template => 'Ops (per second): %.2f',
75 perfdatas => [
76 { template => '%.2f', min => 0 },
77 ],
78 }
79 },
80 { label => 'errors-count', nlabel => 'errors.count', set => {
81 key_values => [ { name => 'errors', diff => 1 } ],
82 output_template => 'Errors: %d',
83 perfdatas => [
84 { template => '%d', min => 0 },
85 ],
86 }
87 },
88 { label => 'errors-persecond', nlabel => 'errors.persecond', set => {
89 key_values => [ { name => 'errors', per_second => 1 } ],
90 output_template => 'Errors (per second): %.2f',
91 perfdatas => [
92 { template => '%.2f', min => 0 },
93 ],
94 }
95 },
96 { label => 'bootstrap-loads-count', nlabel => 'bootstrap.loads.count', set => {
97 key_values => [ { name => 'bootstrap_loads', diff => 1 } ],
98 output_template => 'Bootstrap Loads: %d',
99 perfdatas => [
100 { template => '%d', min => 0 },
101 ],
102 }
103 },
104 { label => 'bootstrap-loads-persecond', nlabel => 'bootstrap.loads.persecond', set => {
105 key_values => [ { name => 'bootstrap_loads', per_second => 1 } ],
106 output_template => 'Bootstrap Loads (per second): %.2f',
107 perfdatas => [
108 { template => '%.2f', min => 0 },
109 ],
110 }
111 },
112 ];
113
114 $self->{maps_counters}->{functions} = [
115 { label => 'time', nlabel => 'function.time.microseconds', set => {
116 key_values => [ { name => 'time', diff => 1 }, { name => 'display' } ],
117 output_template => 'Time Spent: %d us',
118 perfdatas => [
119 { template => '%d', min => 0, unit => 'us', label_extra_instance => 1, instance_use => 'display' },
120 ],
121 }
122 },
123 { label => 'uses-count', nlabel => 'function.uses.count', set => {
124 key_values => [ { name => 'count', diff => 1 }, { name => 'display' } ],
125 output_template => 'Uses: %d',
126 perfdatas => [
127 { template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display' },
128 ],
129 }
130 },
131 { label => 'uses-persecond', nlabel => 'function.uses.persecond', set => {
132 key_values => [ { name => 'count', per_second => 1 }, { name => 'display' } ],
133 output_template => 'Uses (per second): %.2f',
134 perfdatas => [
135 { template => '%.2f', min => 0, label_extra_instance => 1, instance_use => 'display' },
136 ],
137 }
138 }
139 ];
140 }
141
142 sub prefix_output {
143 my ($self, %options) = @_;
144
145 return "Function '" . $options{instance_value}->{display} . "' ";
146 }
147
148 sub new {
149 my ($class, %options) = @_;
150 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
151 bless $self, $class;
152
153 $options{options}->add_options(arguments => {
154 'filter-name:s' => { name => 'filter_name' }
155 });
156
157 return $self;
158 }
159
160 sub manage_selection {
161 my ($self, %options) = @_;
162
163 $self->{cache_name} = "warp10_" . $self->{mode} . '_' .
164 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' .
165 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
166
167 $self->{functions} = {};
168 $self->{metrics} = centreon::common::monitoring::openmetrics::scrape::parse(%options);
169
170 $self->{global} = {
171 time => $self->{metrics}->{'warp.script.time.us'}->{data}[0]->{value},
172 requests => $self->{metrics}->{'warp.script.requests'}->{data}[0]->{value},
173 ops => $self->{metrics}->{'warp.script.ops'}->{data}[0]->{value},
174 errors => $self->{metrics}->{'warp.script.errors'}->{data}[0]->{value},
175 bootstrap_loads => $self->{metrics}->{'warp.script.bootstrap.loads'}->{data}[0]->{value},
176 };
177
178 foreach my $function (@{$self->{metrics}->{'warp.script.function.count'}->{data}}) {
179 $self->{functions}->{$function->{dimensions}->{function}}->{count} = $function->{value};
180 $self->{functions}->{$function->{dimensions}->{function}}->{display} = $function->{dimensions}->{function};
181 }
182 foreach my $function (@{$self->{metrics}->{'warp.script.function.time.us'}->{data}}) {
183 $self->{functions}->{$function->{dimensions}->{function}}->{time} = $function->{value};
184 $self->{functions}->{$function->{dimensions}->{function}}->{display} = $function->{dimensions}->{function};
185 }
186
187 foreach (keys %{$self->{functions}}) {
188 delete $self->{functions}->{$_} if (defined($self->{option_results}->{filter_name}) &&
189 $self->{option_results}->{filter_name} ne '' &&
190 $_ !~ /$self->{option_results}->{filter_name}/);
191 }
192
193 if (scalar(keys %{$self->{functions}}) <= 0) {
194 $self->{output}->add_option_msg(short_msg => "No functions found.");
195 $self->{output}->option_exit();
196 }
197 }
198
199 1;
200
201 __END__
202
203 =head1 MODE
204
205 Check script and functions statistics.
206
207 =over 8
208
209 =item B<--filter-name>
210
211 Filter function name (can be a regexp).
212
213 =item B<--filter-counters>
214
215 Only display some counters (regexp can be used).
216 Example: --filter-counters='^time$|uses'
217
218 =item B<--warning-*-count/persecond>
219
220 Warning threshold.
221 Can be: 'time-total' (delta), 'requests', 'ops',
222 'errors', 'bootstrap-loads', 'time' (delta), 'uses'.
223
224 =item B<--critical-*-count/persecond>
225
226 Critical threshold.
227 Can be: 'time-total' (delta), 'requests', 'ops',
228 'errors', 'bootstrap-loads', 'time' (delta), 'uses'.
229
230 =back
231
232 =cut