forked from ConSol-Monitoring/mod-gearman-worker-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_gearman_worker_epn.pl
More file actions
executable file
·570 lines (479 loc) · 15.7 KB
/
mod_gearman_worker_epn.pl
File metadata and controls
executable file
·570 lines (479 loc) · 15.7 KB
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/usr/bin/perl
package main;
=head1 NAME
mod_gearman_worker_epn.pl
=head1 SYNOPSIS
Usage: mod_gearman_worker_epn.pl [options] <socket>
=head1 DESCRIPTION
Run perl monitoring plugins with embedded perl interpreter. Usually this script
is started internally by the mod-gearman-worker. It can be started manually
for developing or testing purposes.
=head1 OPTIONS
-v|--verbose print additional debug information
-c|--cache enable perl cache
-r|--run run single plugin for testing purpose
socket path to socket
=head1 USAGE
Start epn server in verbose mode:
./mod_gearman_worker_epn.pl epn.socket -v
then send command lines to the socket:
echo "test.pl arg1 arg2" | nc -U epn.socket
Test single plugin call
./mod_gearman_worker_epn.pl -v --run -- ./plugin.pl <plugin args...>
=head1 AUTHOR
2022, Sven Nierlein, <sven@consol.de>
=cut
use warnings;
no warnings 'redefine';
use strict;
use Time::HiRes;
use Cpanel::JSON::XS;
use Socket ();
use IO::Socket;
use IO::Socket::UNIX;
use Pod::Usage;
use POSIX ":sys_wait_h";
use Getopt::Long;
use Text::ParseWords qw(parse_line);
$| = 1;
###########################################################
# parse and check cmd line arguments
Getopt::Long::Configure('no_ignore_case');
Getopt::Long::Configure('bundling');
Getopt::Long::Configure('pass_through');
my $opt ={
'help' => 0,
'verbose' => 0,
'use_cache' => 0,
'run_only' => 0,
'socket' => [],
};
Getopt::Long::GetOptions(
"h|help" => \$opt->{'help'},
"v|verbose" => sub { $opt->{'verbose'}++ },
"c|cache" => sub { $opt->{'use_cache'}++ },
"r|run" => sub { $opt->{'run_only'}++ },
"<>" => sub { push @{$opt->{'socket'}}, $_[0] },
) || pod2usage( { -verbose => 2, -message => 'error in options', -exit => 3 } );
pod2usage( { -verbose => 2, -exit => 3 } ) if $opt->{'help'};
my $unixsocket;
END {
if($unixsocket) {
unlink($unixsocket);
}
}
my $main_loop_interval = 5;
our $child_procs = {};
###########################################################
# listen on the socket and run the plugins
sub _server {
my($opt) = @_;
my $socketpath = $opt->{'socket'}->[0];
unlink($socketpath);
my $server = IO::Socket::UNIX->new(Local => $socketpath,
Type => SOCK_STREAM,
Listen => Socket::SOMAXCONN,
) || die "Couldn't open unix socket $socketpath: $@\n";
printf("**ePN: listening on %s\n", $socketpath) if $opt->{'verbose'};
$unixsocket = $socketpath;
local $SIG{CHLD} = 'IGNORE';
local $SIG{INT} = sub {
CORE::exit(0);
};
alarm($main_loop_interval);
while(1) {
local $SIG{CHLD} = \&_sigchld_handler;
local $SIG{ALRM} = \&_timeout_handler;
while(my $client = $server->accept()) {
alarm(0);
_handle_connection($client);
alarm($main_loop_interval);
}
}
close($server);
}
###########################################################
# cleanup exited child process
sub _sigchld_handler {
while((my $chldpid = waitpid(-1, &WNOHANG)) > 0) {
printf("**ePN: chld pid %d exited\n", $chldpid) if $opt->{'verbose'} > 1;
delete $child_procs->{$chldpid};
}
$SIG{CHLD} = \&_sigchld_handler;
}
###########################################################
# check if any chld needs to be killed
sub _check_chld_timeouts {
my $now = time();
for my $pid (sort keys %{$child_procs}) {
my $proc = $child_procs->{$pid};
if($proc->{'end_time'} < $now) {
printf("**ePN: killing chld pid %d, %ds timeout (%s) reached but still running\n",
$pid,
$proc->{'timeout'},
scalar localtime $proc->{'end_time'},
) if $opt->{'verbose'};
if($proc->{'end_time'} < $now - 10) {
kill('KILL', $pid);
} else {
kill('INT', $pid);
}
}
}
}
###########################################################
# listen on the socket and run the plugins
sub _timeout_handler {
_check_chld_timeouts();
if(getppid() == 1) {
printf("**ePN: exiting, ppid is 1, this means usually our parent worker has gone away.\n") if $opt->{'verbose'};
_clean_exit();
}
alarm($main_loop_interval);
}
###########################################################
# end children process and exit
sub _clean_exit {
for my $pid (sort keys %{$child_procs}) {
kill('INT', $pid);
}
if($unixsocket) {
unlink($unixsocket);
undef $unixsocket;
}
sleep(0.5) if scalar keys %{$child_procs} > 0;
for my $pid (sort keys %{$child_procs}) {
kill('KILL', $pid);
}
CORE::exit(0);
}
###########################################################
# listen on the socket and run the plugins
sub _handle_connection {
my($client) = @_;
my $res;
eval {
my $req = <$client>;
my $request = _parse_request($req);
die("**ePN: invalid request: ".($req // 'undef')) unless $request->{'bin'};
$res = _handle_request($request);
};
my $err = $@;
if($err) {
printf("**ePN: errored: %s\n", $err) if $opt->{'verbose'};
_send_answer($client, {
rc => 3, # UNKNOWN
stdout => $err,
compile_duration => 0,
run_duration => 0,
});
return;
}
return unless $res; # parent process can handle next request
_send_answer($client, $res);
my $forked = delete $res->{'forked'};
if($forked) {
undef $unixsocket;
CORE::exit(0);
}
}
###########################################################
# handle a single plugin execution
sub _handle_request {
my($request, $skip_fork) = @_;
my $t0 = [Time::HiRes::gettimeofday()];
my($handler, $err) = Embed::Persistent::eval_file($request, $opt->{'use_cache'});
my $elapsed_compile = Time::HiRes::tv_interval($t0);
if($err) {
return({
rc => 3, # UNKNOWN
stdout => $err,
compile_duration => $elapsed_compile,
run_duration => 0,
});
}
# fork now after creating the cache, cache needs to remain in the parent
my $forked = 0;
if(!$skip_fork) {
my $pid = fork();
if($pid == -1) {
die("**ePN: failed to fork: ".$!);
}
if($pid) {
my $entry = {
start_time => time(),
timeout => $request->{'timeout'} // 60,
pid => $pid,
request => $request,
};
$entry->{'end_time'} = $entry->{'start_time'} + $entry->{'timeout'};
$child_procs->{$pid} = $entry;
printf("**ePN: chld pid %d started\n", $pid) if $opt->{'verbose'} > 1;
return;
}
$forked = 1;
}
# continue as child process
my $t1 = [Time::HiRes::gettimeofday()];
my($rc, $res) = Embed::Persistent::run_package($request, $handler);
my $elapsed_run = Time::HiRes::tv_interval($t1);
return({
rc => $rc,
stdout => $res,
compile_duration => $elapsed_compile,
run_duration => $elapsed_run,
forked => $forked,
});
}
###########################################################
# parse text or json request into request object
sub _parse_request {
my($text) = @_;
chomp($text);
$text =~ s/\s+$//;
printf("**ePN: request: %s\n", $text) if $opt->{'verbose'} > 1;
# json request
if($text =~ m/^\s*{/mx) {
return(_request(Cpanel::JSON::XS::decode_json($text)));
} else {
my @line = parse_line('\s+', 0, $text);
my $bin = shift @line;
return(_request({
bin => $bin,
args => \@line,
}));
}
}
###########################################################
sub _request {
my($req) = @_;
if(ref $req ne "HASH") {
die("expected hash, got: ".(ref $req));
}
$req->{'env'} = $req->{'env'} // {};
$req->{'args'} = $req->{'args'} // [];
$req->{'timeout'} = $req->{'timeout'} // 60;
return($req);
}
###########################################################
sub _send_answer {
my($client, $res) = @_;
$res->{'cpu_user'} = POSIX::clock() / 1e6; # value is in microseconds
$res->{'rc'} = int($res->{'rc'});
my $json = Cpanel::JSON::XS->new->utf8->canonical;
$res = $json->encode($res);
print $client $res,"\n";
close($client);
printf("**ePN: done: %s\n", $res) if $opt->{'verbose'} > 1;
return;
}
###########################################################
sub _test_run {
my($args) = @_;
if($args->[0] && $args->[0] eq '--') {
shift @{$args};
}
printf("**ePN: test run: ".join(" ", @{$args})."\n") if $opt->{'verbose'};
my $res;
eval {
my $req = join(" ", @{$args});
my $request = _parse_request($req);
die("**ePN: invalid request: ".($req // 'undef')) unless $request->{'bin'};
$res = _handle_request($request, 1);
};
my $err = $@;
if($err) {
printf("**ePN: errored: %s\n", $err);
return(3);
}
print $res->{'stdout'};
printf("**ePN: compile: %.5fs\n", $res->{'compile_duration'}) if $opt->{'verbose'};
printf("**ePN: runtime: %.5fs\n", $res->{'run_duration'}) if $opt->{'verbose'};
printf("**ePN: exit: %d\n", $res->{'rc'}) if $opt->{'verbose'};
return($res->{'rc'});
}
###########################################################
# one shot mode?
if($opt->{'run_only'}) {
CORE::exit(_test_run(\@ARGV));
}
###########################################################
pod2usage( { -verbose => 2, -message => 'error in options', -exit => 3 } ) if scalar @{$opt->{'socket'}} != 1;
use subs 'CORE::GLOBAL::exit';
sub CORE::GLOBAL::exit { die sprintf("ExitTrap: %d (Redefine exit to trap plugin exit with eval BLOCK)", $_[0]//0) }
_server($opt);
CORE::exit(0);
################################################################################
package Embed::Persistent;
use strict;
my $plugin_cache = {};
# Offsets in $plugin_cache->{$filename}
use constant MTIME => 0;
use constant PLUGIN_ERROR => 1;
use constant PLUGIN_HNDLR => 2;
###########################################################
sub valid_package_name {
local $_ = shift;
s|([^A-Za-z0-9\/])|sprintf("_%2x",unpack("C",$1))|eg;
# second pass only for words starting with a digit
s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
# Dress it up as a real package name
s|/|::|g;
return /^::/ ? "Embed$_" : "Embed::$_";
}
###########################################################
sub eval_file {
my($request, $use_cache) = @_;
my $filename = $request->{'bin'};
my $mtime = -M $filename;
if($plugin_cache->{$filename} && $plugin_cache->{$filename}[MTIME]) {
if($plugin_cache->{$filename}[MTIME] <= $mtime) {
if($plugin_cache->{$filename}[PLUGIN_ERROR]) {
# failed previously, return last error
printf("**ePN: cache hit (compile failed) for: %s\n", $filename) if $opt->{'verbose'} > 2;
return(undef, sprintf("**ePN: failed to compile %s: %s", $filename, $plugin_cache->{$filename}[PLUGIN_ERROR]));
} else {
# cache hit, return compiled plugin reference
printf("**ePN: cache hit for: %s\n", $filename) if $opt->{'verbose'} > 2;
return $plugin_cache->{$filename}[PLUGIN_HNDLR];
}
} else {
printf("**ePN: need to recompile %s\n", $filename) if $opt->{'verbose'} > 2;
}
}
my $sub;
open(my $fh, '<', $filename) || return(undef, sprintf("**ePN: failed to open %s: %s", $filename, $!));
sysread($fh, $sub, -s $fh);
close($fh);
# Wrap the code into a subroutine inside our unique package
# (using $_ here [to save a lexical] is not a good idea since
# the definition of the package is visible to any other Perl
# code that uses [non localised] $_).
my $package = valid_package_name($filename);
my $hndlr = <<EOSUB ;
package $package;
sub hndlr {
\@ARGV = \@_;
local \$^W = 1;
\$ENV{NAGIOS_PLUGIN} = '$filename';
# line 0 $filename
$sub
}
EOSUB
$plugin_cache->{$filename}[MTIME] = $mtime if $use_cache;
# ensure modified Perl plugins get recached by the epn
no strict 'refs';
undef %{ $package . '::' };
use strict 'refs';
printf("**ePN: compiling %s\n", $filename) if $opt->{'verbose'} > 2;
# Compile &$package::hndlr. (will run BEGIN blocks)
# catch prints and add them to the error output
my $stdout = tie(*STDOUT, 'OutputTrap');
my $stderr = tie(*STDERR, 'OutputTrap');
{
eval $hndlr;
};
my $output = <STDOUT>;
undef $stdout;
untie *STDOUT;
$output .= <STDERR>;
undef $stderr;
untie *STDERR;
# $@ is set for any warning and error.
# This guarantees that the plugin will not be run.
my $err = $@;
if($err) {
$err =~ s/^ExitTrap:.*?line\s+\d+\.//gmx;
$plugin_cache->{$filename}[PLUGIN_ERROR] = $err;
# If the compilation fails, leave nothing behind that may affect subsequent compilations.
return(undef, sprintf("**ePN: failed to compile %s: %s%s", $filename, $err, $output));
}
else {
$plugin_cache->{$filename}[PLUGIN_ERROR] = '';
}
# successfully compiled, return reference
no strict 'refs';
return($plugin_cache->{$filename}[PLUGIN_HNDLR] = *{ $package . '::hndlr' }{CODE}, undef);
}
###########################################################
sub run_package {
my($request, $plugin_hndlr_cr) = @_;
my $has_exit = 0;
my $res = 3;
my $filename = $request->{'bin'};
my @plugin_args = @{$request->{'args'}};
my $stdout = tie(*STDOUT, 'OutputTrap');
my $stderr = tie(*STDERR, 'OutputTrap');
$0 = $filename.(scalar @plugin_args > 0 ? " " : '').join(" ", @plugin_args);
local %ENV = (%ENV, %{$request->{'env'}}) if($request->{'env'} && scalar keys %{$request->{'env'}} > 0);
local $SIG{ALRM} = sub { exit(2); };
alarm($request->{'timeout'});
eval {
local $SIG{TERM} = sub { exit(2); };
local $SIG{INT} = sub { exit(2); };
$plugin_hndlr_cr->(@plugin_args);
};
alarm(0);
my $err = $@;
if($err) {
if($err =~ m/^ExitTrap:\s+(-?\d+|)/mx) {
$has_exit = 1;
$res = 0+($1 // 0);
} else {
chomp($err);
printf(STDOUT "**ePN: %s: %s\n", $filename, $err);
}
($@, $_) = ('', ''); # reset global perl variables
}
my $plugin_output = <STDOUT>;
undef $stdout;
untie *STDOUT;
my $errors = <STDERR>;
if($errors) {
$plugin_output .= sprintf("\n[%s]", $errors);
}
undef $stderr;
untie *STDERR;
$plugin_output = "**ePN: $filename: plugin did not call exit()\n".$plugin_output if $has_exit == 0;
return($res, $plugin_output);
}
################################################################################
package OutputTrap;
#
# =head1 NAME
#
# OutputTrap
#
# =head1 DESCRIPTION
#
# Tie STDOUT/STDERR to a scalar and caches values written to it.
#
# =cut
#
sub TIEHANDLE {
my($class) = @_;
my $me = '';
bless \$me, $class;
}
sub PRINT {
my($self, @args) = @_;
$$self .= join('', @args);
}
sub PRINTF {
my($self, $fmt, @args) = @_;
$$self .= sprintf($fmt, @args);
}
sub READLINE {
my $self = shift;
return $$self;
}
sub CLOSE {
my $self = shift;
undef $self;
}
sub DESTROY {
my $self = shift;
undef $self;
}
################################################################################
1;