mirror of
https://github.com/zeek/zeek.git
synced 2025-10-11 02:58:20 +00:00
Add Supervisor::{stdout,stderr}_hook
These allow capturing/handling the stdout/stderr of child processes via Zeek scripts.
This commit is contained in:
parent
a06ef66edc
commit
10709c627b
8 changed files with 164 additions and 9 deletions
|
@ -0,0 +1,4 @@
|
|||
[supervisor:STDERR] [grault] (stderr) supervised node zeek_init()
|
||||
received termination signal
|
||||
[supervisor:STDERR] [grault] received termination signal
|
||||
[supervisor:STDERR] [grault] (stderr) supervised node zeek_done()
|
|
@ -0,0 +1,5 @@
|
|||
hooked stdout of (grault): (stdout) supervised node zeek_init()
|
||||
hooked stderr of (grault): (stderr) supervised node zeek_init()
|
||||
hooked stdout of (grault): (stdout) supervised node zeek_done()
|
||||
hooked stderr of (grault): received termination signal
|
||||
hooked stderr of (grault): (stderr) supervised node zeek_done()
|
|
@ -0,0 +1,3 @@
|
|||
supervisor zeek_init()
|
||||
destroying node
|
||||
supervisor zeek_done()
|
78
testing/btest/supervisor/output-redirect-hook.zeek
Normal file
78
testing/btest/supervisor/output-redirect-hook.zeek
Normal file
|
@ -0,0 +1,78 @@
|
|||
# @TEST-PORT: BROKER_PORT
|
||||
# @TEST-EXEC: btest-bg-run zeek zeek -j -b %INPUT
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff zeek/supervisor.out
|
||||
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-sort btest-diff zeek/.stdout
|
||||
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-sort btest-diff zeek/.stderr
|
||||
|
||||
# This test checks the default stdout/stderr redirection will get intercepted
|
||||
# by the supervisor process and sent through the hook mechanisms
|
||||
|
||||
# So the supervised node doesn't terminate right away.
|
||||
redef exit_only_after_terminate=T;
|
||||
|
||||
global supervisor_output_file: file;
|
||||
global topic = "test-topic";
|
||||
global stderr = open("/dev/stderr");
|
||||
|
||||
event do_destroy()
|
||||
{
|
||||
print supervisor_output_file, "destroying node";
|
||||
Supervisor::destroy("grault");
|
||||
}
|
||||
|
||||
hook Supervisor::stdout_hook(node: string, msg: string)
|
||||
{
|
||||
print fmt("hooked stdout of (%s): %s", node, msg);
|
||||
break;
|
||||
}
|
||||
|
||||
hook Supervisor::stderr_hook(node: string, msg: string)
|
||||
{
|
||||
print fmt("hooked stderr of (%s): %s", node, msg);
|
||||
}
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
if ( Supervisor::is_supervisor() )
|
||||
{
|
||||
Broker::subscribe(topic);
|
||||
Broker::listen("127.0.0.1", to_port(getenv("BROKER_PORT")));
|
||||
supervisor_output_file = open("supervisor.out");
|
||||
print supervisor_output_file, "supervisor zeek_init()";
|
||||
local sn = Supervisor::NodeConfig($name="grault", $directory="qux");
|
||||
local res = Supervisor::create(sn);
|
||||
|
||||
if ( res != "" )
|
||||
print supervisor_output_file, res;
|
||||
}
|
||||
else
|
||||
{
|
||||
Broker::peer("127.0.0.1", to_port(getenv("BROKER_PORT")));
|
||||
print "(stdout) supervised node zeek_init()";
|
||||
print stderr, "(stderr) supervised node zeek_init()";
|
||||
}
|
||||
}
|
||||
|
||||
event Broker::peer_added(endpoint: Broker::EndpointInfo, msg: string)
|
||||
{
|
||||
if ( Supervisor::is_supervised() )
|
||||
Broker::publish(topic, do_destroy);
|
||||
}
|
||||
|
||||
event Broker::peer_lost(endpoint: Broker::EndpointInfo, msg: string)
|
||||
{
|
||||
# Should only be run by supervisor
|
||||
terminate();
|
||||
}
|
||||
|
||||
event zeek_done()
|
||||
{
|
||||
if ( Supervisor::is_supervised() )
|
||||
{
|
||||
print "(stdout) supervised node zeek_done()";
|
||||
print stderr, "(stderr) supervised node zeek_done()";
|
||||
}
|
||||
else
|
||||
print supervisor_output_file, "supervisor zeek_done()";
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue