/
processcount.pm
467 lines (376 loc) · 21.2 KB
/
processcount.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#
# Copyright 2024 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package snmp_standard::mode::processcount;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use centreon::plugins::statefile;
use Digest::MD5 qw(md5_hex);
my %map_process_status = (
1 => 'running',
2 => 'runnable',
3 => 'notRunnable',
4 => 'invalid',
);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'process-status:s' => { name => 'process_status', default => 'running|runnable' },
'process-name:s' => { name => 'process_name' },
'regexp-name' => { name => 'regexp_name' },
'process-path:s' => { name => 'process_path' },
'regexp-path' => { name => 'regexp_path' },
'process-args:s' => { name => 'process_args' },
'regexp-args' => { name => 'regexp_args' },
'warning:s' => { name => 'warning' },
'critical:s' => { name => 'critical' },
'memory' => { name => 'memory' },
'warning-mem-each:s' => { name => 'warning_mem_each' },
'critical-mem-each:s' => { name => 'critical_mem_each' },
'warning-mem-total:s' => { name => 'warning_mem_total' },
'critical-mem-total:s' => { name => 'critical_mem_total' },
'warning-mem-avg:s' => { name => 'warning_mem_avg' },
'critical-mem-avg:s' => { name => 'critical_mem_avg' },
'cpu' => { name => 'cpu' },
'warning-cpu-total:s' => { name => 'warning_cpu_total' },
'critical-cpu-total:s' => { name => 'critical_cpu_total' },
'top' => { name => 'top' },
'top-num:s' => { name => 'top_num', default => 5 },
'top-size:s' => { name => 'top_size', default => 52428800 }, # 50MB
});
$self->{statefile_cache} = centreon::plugins::statefile->new(%options);
$self->{filter4md5} = '';
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'warning-mem-each', value => $self->{option_results}->{warning_mem_each})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning-mem-each threshold '" . $self->{warning_mem_each} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'critical-mem-each', value => $self->{option_results}->{critical_mem_each})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical-mem-each threshold '" . $self->{critical_mem_each} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'warning-mem-total', value => $self->{option_results}->{warning_mem_total})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning-mem-total threshold '" . $self->{warning_mem_total} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'critical-mem-total', value => $self->{option_results}->{critical_mem_total})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical-mem-total threshold '" . $self->{critical_mem_total} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'warning-mem-avg', value => $self->{option_results}->{warning_mem_avg})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning-mem-avg threshold '" . $self->{warning_mem_avg} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'critical-mem-avg', value => $self->{option_results}->{critical_mem_avg})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical-mem-avg threshold '" . $self->{critical_mem_avg} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'warning-cpu-total', value => $self->{option_results}->{warning_cpu_total})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning-cpu-total threshold '" . $self->{warning_cpu_total} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'critical-cpu-total', value => $self->{option_results}->{critical_cpu_total})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical-cpu-total threshold '" . $self->{critical_cpu_total} . "'.");
$self->{output}->option_exit();
}
if (defined($self->{option_results}->{cpu})) {
$self->{statefile_cache}->check_options(%options);
# Construct filter for file cache (avoid one check erase one other)
my @labels = ('process_name', 'regexp_name', 'process_path', 'regexp_path', 'process_args', 'regexp_args', 'process_status');
foreach (@labels) {
if (defined($self->{option_results}->{$_})) {
$self->{filter4md5} .= ',' . $self->{option_results}->{$_};
}
}
}
}
my $filters = {
status => { oid => '.1.3.6.1.2.1.25.4.2.1.7', default => 1, value => '', regexp => 1 }, # hrSWRunStatus
name => { oid => '.1.3.6.1.2.1.25.4.2.1.2', default => 0, value => '' }, # hrSWRunName (Warning: it's truncated. (15 characters))
path => { oid => '.1.3.6.1.2.1.25.4.2.1.4', default => 0, value => '' }, # hrSWRunPath
args => { oid => '.1.3.6.1.2.1.25.4.2.1.5', default => 0, value => '' }, # hrSWRunParameters (Warning: it's truncated. (128 characters))
};
my $oid_hrSWRunName = '.1.3.6.1.2.1.25.4.2.1.2';
my $oid_hrSWRunStatus = '.1.3.6.1.2.1.25.4.2.1.7';
my $oid_hrSWRunPerfMem = '.1.3.6.1.2.1.25.5.1.1.2';
my $oid_hrSWRunPerfCPU = '.1.3.6.1.2.1.25.5.1.1.1';
sub check_top {
my ($self, %options) = @_;
my $data;
foreach my $key (keys %{$self->{snmp_response}->{$oid_hrSWRunName}}) {
if ($key =~ /\.([0-9]+)$/ && defined($self->{snmp_response}->{$oid_hrSWRunPerfMem}->{$oid_hrSWRunPerfMem . '.' . $1})) {
$data->{$self->{snmp_response}->{$oid_hrSWRunName}->{$oid_hrSWRunName . '.' . $1}}->{memory} += $self->{snmp_response}->{$oid_hrSWRunPerfMem}->{$oid_hrSWRunPerfMem . '.' . $1} * 1024;
push @{$data->{$self->{snmp_response}->{$oid_hrSWRunName}->{$oid_hrSWRunName . '.' . $1}}->{pids}}, $1;
}
}
my $i = 1;
foreach my $name (sort { $data->{$b}->{memory} <=> $data->{$a}->{memory} } keys %$data) {
last if ($i > $self->{option_results}->{top_num});
last if ($data->{$name}->{memory} < $self->{option_results}->{top_size});
my ($mem_value, $mem_unit) = $self->{perfdata}->change_bytes(value => $data->{$name}->{memory});
$self->{output}->output_add(long_msg => sprintf("Top %d '%s' [pids: %s] memory usage: %s %s", $i, $name, join(", ", @{$data->{$name}->{pids}}), $mem_value, $mem_unit));
$self->{output}->perfdata_add(label => 'top_' . $name, unit => 'B',
value => $data->{$name}->{memory}, min => 0);
$i++;
}
}
sub run {
my ($self, %options) = @_;
$self->{snmp} = $options{snmp};
my $extra_oids = [];
foreach my $filter (keys %$filters) {
if (defined($self->{option_results}->{'process_' . $filter}) && $self->{option_results}->{'process_' . $filter} ne '') {
$filters->{$filter}->{value} = $self->{option_results}->{'process_' . $filter};
if ($filters->{$filter}->{default} == 0) {
push @{$extra_oids}, $filters->{$filter}->{oid};
}
}
if (defined($self->{option_results}->{'regexp_' . $filter})) {
$filters->{$filter}->{regexp} = 1;
}
}
if (defined($self->{option_results}->{memory})) {
push @{$extra_oids}, $oid_hrSWRunPerfMem;
}
if (defined($self->{option_results}->{cpu})) {
push @{$extra_oids}, $oid_hrSWRunPerfCPU;
}
my $oids_multiple_table = [ { oid => $oid_hrSWRunStatus } ];
if (defined($self->{option_results}->{top})) {
push @{$oids_multiple_table}, { oid => $oid_hrSWRunName };
push @{$oids_multiple_table}, { oid => $oid_hrSWRunPerfMem };
}
# First lookup on name and status
$self->{snmp_response} = $self->{snmp}->get_multiple_table(oids => $oids_multiple_table);
foreach my $key ($self->{snmp}->oid_lex_sort(keys %{$self->{snmp_response}->{$oid_hrSWRunStatus}})) {
$key =~ /\.([0-9]+)$/;
my $pid = $1;
$self->{results}->{$pid}->{status} = $map_process_status{$self->{snmp_response}->{$oid_hrSWRunStatus}->{$oid_hrSWRunStatus . '.' . $pid}};
foreach my $filter (keys %$filters) {
next if !defined($self->{results}->{$pid}) || $filters->{$filter}->{value} eq '' || $filters->{$filter}->{default} == 0;
if ((defined($filters->{$filter}->{regexp}) && $self->{results}->{$pid}->{$filter} !~ /$filters->{$filter}->{value}/)
|| (!defined($filters->{$filter}->{regexp}) && $self->{results}->{$pid}->{$filter} ne $filters->{$filter}->{value})) {
delete $self->{results}->{$pid};
}
}
}
# Second lookup on extra oids
if (scalar(keys %{$self->{results}}) > 0) {
if (scalar(@$extra_oids) > 0) {
$self->{snmp}->load(oids => $extra_oids, instances => [ keys %{$self->{results}} ]);
$self->{snmp_response_extra} = $self->{snmp}->get_leef();
foreach my $pid (keys %{$self->{results}}) {
foreach my $filter (keys %$filters) {
next if !defined($self->{results}->{$pid}) || $filters->{$filter}->{value} eq '' || $filters->{$filter}->{default} == 1;
if ((defined($filters->{$filter}->{regexp}) && $self->{snmp_response_extra}->{$filters->{$filter}->{oid} . '.' . $pid} !~ /$filters->{$filter}->{value}/)
|| (!defined($filters->{$filter}->{regexp}) && $self->{snmp_response_extra}->{$filters->{$filter}->{oid} . '.' . $pid} ne $filters->{$filter}->{value})) {
delete $self->{results}->{$pid};
} else {
$self->{results}->{$pid}->{$filter} = $self->{snmp_response_extra}->{$filters->{$filter}->{oid} . '.' . $pid};
}
}
}
}
}
my $num_processes_match = scalar(keys(%{$self->{results}}));
my $exit = $self->{perfdata}->threshold_check(value => $num_processes_match,
threshold => [ { label => 'critical', exit_litteral => 'critical' },
{ label => 'warning', exit_litteral => 'warning' } ]);
$self->{output}->output_add(severity => $exit,
short_msg => "Number of current processes running: $num_processes_match");
$self->{output}->perfdata_add(label => 'nbproc',
value => $num_processes_match,
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
min => 0);
# Check memory
if (defined($self->{option_results}->{memory}) && $num_processes_match > 0) {
my $total_memory = 0;
foreach my $pid (keys %{$self->{results}}) {
$total_memory += ($self->{snmp_response_extra}->{$oid_hrSWRunPerfMem . "." . $pid} * 1024);
my ($memory_value, $memory_unit) = $self->{perfdata}->change_bytes(value => $self->{snmp_response_extra}->{$oid_hrSWRunPerfMem . '.' . $pid} * 1024);
$self->{results}->{$pid}->{memory} = $memory_value . " " . $memory_unit;
$exit = $self->{perfdata}->threshold_check(value => $self->{snmp_response_extra}->{$oid_hrSWRunPerfMem . '.' . $pid} * 1024,
threshold => [ { label => 'critical-mem-each', exit_litteral => 'critical' },
{ label => 'warning-mem-each', exit_litteral => 'warning' } ]);
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("Process '%s' memory usage: %.2f %s", $pid, $memory_value, $memory_unit));
}
}
my $exit = $self->{perfdata}->threshold_check(value => $total_memory,
threshold => [ { label => 'critical-mem-total', exit_litteral => 'critical' },
{ label => 'warning-mem-total', exit_litteral => 'warning' } ]);
my ($total_mem_value, $total_mem_unit) = $self->{perfdata}->change_bytes(value => $total_memory);
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("Total memory usage: %s", $total_mem_value . " " . $total_mem_unit));
$self->{output}->perfdata_add(label => 'mem_total', unit => 'B',
value => $total_memory,
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-mem-total'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-mem-total'),
min => 0);
$exit = $self->{perfdata}->threshold_check(value => $total_memory / $num_processes_match,
threshold => [ { label => 'critical-mem-avg', exit_litteral => 'critical' },
{ label => 'warning-mem-avg', exit_litteral => 'warning' } ]);
my ($avg_mem_value, $avg_mem_unit) = $self->{perfdata}->change_bytes(value => $total_memory / $num_processes_match);
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("Average memory usage: %.2f %s", $avg_mem_value, $avg_mem_unit));
$self->{output}->perfdata_add(label => 'mem_avg', unit => 'B',
value => sprintf("%.2f", $total_memory / $num_processes_match),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-mem-avg'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-mem-avg'),
min => 0);
}
# Check CPU
if (defined($self->{option_results}->{cpu}) && $num_processes_match > 0) {
my $datas = {};
$datas->{last_timestamp} = time();
$self->{statefile_cache}->read(statefile => "snmpstandard_" . $self->{snmp}->get_hostname() . '_' .
$self->{snmp}->get_port() . '_' . $self->{mode} . '_' . md5_hex($self->{filter4md5}));
my $old_timestamp = $self->{statefile_cache}->get(name => 'last_timestamp');
my $total_cpu = 0;
my $checked = 0;
foreach my $pid (keys %{$self->{results}}) {
$datas->{'cpu_' . $pid} = $self->{snmp_response_extra}->{$oid_hrSWRunPerfCPU . "." . $pid};
my $old_cpu = $self->{statefile_cache}->get(name => 'cpu_' . $pid);
# No value added
if (!defined($old_cpu) || !defined($old_timestamp)) {
$self->{results}->{$pid}->{cpu} = 'Buffer creation...';
next;
}
# Go back to zero
if ($old_cpu > $datas->{'cpu_' . $pid}) {
$old_cpu = 0;
}
my $time_delta = ($datas->{last_timestamp} - $old_timestamp);
# At least one seconds.
if ($time_delta == 0) {
$time_delta = 1;
}
$total_cpu += (($datas->{'cpu_' . $pid} - $old_cpu) / $time_delta);
$self->{results}->{$pid}->{cpu} = sprintf("%.2f %%", ($datas->{'cpu_' . $pid} - $old_cpu) / $time_delta);
$checked = 1;
}
if ($checked == 1) {
$exit = $self->{perfdata}->threshold_check(value => $total_cpu, threshold => [ { label => 'critical-cpu-total', exit_litteral => 'critical' },
{ label => 'warning-cpu-total', exit_litteral => 'warning' } ]);
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("Total CPU usage: %.2f %%", $total_cpu));
$self->{output}->perfdata_add(label => 'cpu_total', unit => '%',
value => sprintf("%.2f", $total_cpu),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-cpu-total'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-cpu-total'),
min => 0);
}
$self->{statefile_cache}->write(data => $datas);
}
$self->check_top() if (defined($self->{option_results}->{top}));
foreach my $pid (keys %{$self->{results}}) {
my $long_msg = sprintf("Process '%s'", $pid);
foreach my $key (keys %{$self->{results}->{$pid}}) {
$long_msg .= sprintf(" [%s: %s]", $key, $self->{results}->{$pid}->{$key});
}
$self->{output}->output_add(long_msg => $long_msg);
}
$self->{output}->display();
$self->{output}->exit();
}
1;
__END__
=head1 MODE
Check system number of processes.
Can also check memory usage and CPU usage.
=over 8
=item B<--process-status>
Filter process status. Can be a regexp.
(default: 'running|runnable').
=item B<--process-name>
Filter process name.
=item B<--regexp-name>
Allows to use regexp to filter process
name (with option --process-name).
=item B<--process-path>
Filter process path.
=item B<--regexp-path>
Allows to use regexp to filter process
path (with option --process-path).
=item B<--process-args>
Filter process arguments.
=item B<--regexp-args>
Allows to use regexp to filter process
arguments (with option --process-args).
=item B<--warning>
Warning threshold of matching processes count.
=item B<--critical>
Critical threshold of matching processes count.
=item B<--memory>
Check memory usage.
=item B<--warning-mem-each>
Warning threshold of memory
used by each matching processes (in Bytes).
=item B<--critical-mem-each>
Critical threshold of memory
used by each matching processes (in Bytes).
=item B<--warning-mem-total>
Warning threshold of total
memory used by matching processes (in Bytes).
=item B<--critical-mem-total>
Critical threshold of total
memory used by matching processes (in Bytes).
=item B<--warning-mem-avg>
Warning threshold of average
memory used by matching processes (in Bytes).
=item B<--critical-mem-avg>
Critical threshold of average
memory used by matching processes (in Bytes).
=item B<--cpu>
Check CPU usage. Should be used with fix processes.
If processes pid changes too much, the plugin can't compute values.
=item B<--warning-cpu-total>
Warning threshold of CPU usage for all processes (in percent).
CPU usage is in % of one CPU, so maximum can be 100% * number of CPU
and a process can have a value greater than 100%.
=item B<--critical-cpu-total>
Critical threshold of CPU usage for all processes (in percent).
CPU usage is in % of one CPU, so maximum can be 100% * number of CPU
and a process can have a value greater than 100%.
=item B<--top>
Enable top memory usage display.
=item B<--top-num>
Number of processes in top memory display (default: 5).
=item B<--top-size>
Minimum memory usage to be in top memory display
(default: 52428800 -> 50 MB).
=back
=cut