mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 08:38:20 +00:00
Measurement framework tests all pass now.
This commit is contained in:
parent
6dc204b385
commit
53f9948b02
22 changed files with 544 additions and 381 deletions
|
@ -0,0 +1,83 @@
|
|||
# @TEST-SERIALIZE: comm
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager-1 BROPATH=$BROPATH:.. CLUSTER_NODE=manager-1 bro %INPUT
|
||||
# @TEST-EXEC: sleep 1
|
||||
# @TEST-EXEC: btest-bg-run worker-1 BROPATH=$BROPATH:.. CLUSTER_NODE=worker-1 bro %INPUT
|
||||
# @TEST-EXEC: btest-bg-run worker-2 BROPATH=$BROPATH:.. CLUSTER_NODE=worker-2 bro %INPUT
|
||||
# @TEST-EXEC: btest-bg-wait 15
|
||||
|
||||
# @TEST-EXEC: btest-diff manager-1/.stdout
|
||||
|
||||
@TEST-START-FILE cluster-layout.bro
|
||||
redef Cluster::nodes = {
|
||||
["manager-1"] = [$node_type=Cluster::MANAGER, $ip=127.0.0.1, $p=37757/tcp, $workers=set("worker-1", "worker-2")],
|
||||
["worker-1"] = [$node_type=Cluster::WORKER, $ip=127.0.0.1, $p=37760/tcp, $manager="manager-1", $interface="eth0"],
|
||||
["worker-2"] = [$node_type=Cluster::WORKER, $ip=127.0.0.1, $p=37761/tcp, $manager="manager-1", $interface="eth1"],
|
||||
};
|
||||
@TEST-END-FILE
|
||||
|
||||
redef Log::default_rotation_interval = 0secs;
|
||||
|
||||
global n = 0;
|
||||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
local r1: Measurement::Reducer = [$stream="test.metric", $apply=set(Measurement::SUM, Measurement::MIN, Measurement::MAX, Measurement::AVERAGE, Measurement::STD_DEV, Measurement::VARIANCE, Measurement::UNIQUE)];
|
||||
Measurement::create([$epoch=5secs,
|
||||
$reducers=set(r1),
|
||||
$epoch_finished(rt: Measurement::ResultTable) =
|
||||
{
|
||||
for ( key in rt )
|
||||
{
|
||||
local r = rt[key]["test.metric"];
|
||||
print fmt("Host: %s - num:%d - sum:%.1f - avg:%.1f - max:%.1f - min:%.1f - var:%.1f - std_dev:%.1f - unique:%d", key$host, r$num, r$sum, r$average, r$max, r$min, r$variance, r$std_dev, r$unique);
|
||||
}
|
||||
|
||||
terminate();
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
event remote_connection_closed(p: event_peer)
|
||||
{
|
||||
terminate();
|
||||
}
|
||||
|
||||
global ready_for_data: event();
|
||||
redef Cluster::manager2worker_events += /^ready_for_data$/;
|
||||
|
||||
event ready_for_data()
|
||||
{
|
||||
if ( Cluster::node == "worker-1" )
|
||||
{
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=34]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=30]);
|
||||
Measurement::add_data("test.metric", [$host=6.5.4.3], [$num=1]);
|
||||
Measurement::add_data("test.metric", [$host=7.2.1.5], [$num=54]);
|
||||
}
|
||||
if ( Cluster::node == "worker-2" )
|
||||
{
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=75]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=30]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=3]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=57]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=52]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=61]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=95]);
|
||||
Measurement::add_data("test.metric", [$host=6.5.4.3], [$num=5]);
|
||||
Measurement::add_data("test.metric", [$host=7.2.1.5], [$num=91]);
|
||||
Measurement::add_data("test.metric", [$host=10.10.10.10], [$num=5]);
|
||||
}
|
||||
}
|
||||
|
||||
@if ( Cluster::local_node_type() == Cluster::MANAGER )
|
||||
|
||||
global peer_count = 0;
|
||||
event remote_connection_handshake_done(p: event_peer) &priority=-5
|
||||
{
|
||||
++peer_count;
|
||||
if ( peer_count == 2 )
|
||||
event ready_for_data();
|
||||
}
|
||||
|
||||
@endif
|
34
testing/btest/scripts/base/frameworks/measurement/basic.bro
Normal file
34
testing/btest/scripts/base/frameworks/measurement/basic.bro
Normal file
|
@ -0,0 +1,34 @@
|
|||
# @TEST-EXEC: bro %INPUT
|
||||
# @TEST-EXEC: btest-diff .stdout
|
||||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
local r1: Measurement::Reducer = [$stream="test.metric",
|
||||
$apply=set(Measurement::SUM,
|
||||
Measurement::VARIANCE,
|
||||
Measurement::AVERAGE,
|
||||
Measurement::MAX,
|
||||
Measurement::MIN,
|
||||
Measurement::STD_DEV,
|
||||
Measurement::UNIQUE)];
|
||||
Measurement::create([$epoch=3secs,
|
||||
$reducers=set(r1),
|
||||
$epoch_finished(data: Measurement::ResultTable) =
|
||||
{
|
||||
for ( key in data )
|
||||
{
|
||||
local r = data[key]["test.metric"];
|
||||
print fmt("Host: %s - num:%d - sum:%.1f - var:%.1f - avg:%.1f - max:%.1f - min:%.1f - std_dev:%.1f - unique:%d", key$host, r$num, r$sum, r$variance, r$average, r$max, r$min, r$std_dev, r$unique);
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=5]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=22]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=94]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=50]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=50]);
|
||||
|
||||
Measurement::add_data("test.metric", [$host=6.5.4.3], [$num=2]);
|
||||
Measurement::add_data("test.metric", [$host=7.2.1.5], [$num=1]);
|
||||
}
|
|
@ -19,14 +19,20 @@ redef Log::default_rotation_interval = 0secs;
|
|||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
Metrics::add_filter("test.metric",
|
||||
[$every=1hr,
|
||||
$measure=set(Metrics::SUM),
|
||||
local r1: Measurement::Reducer = [$stream="test.metric", $apply=set(Measurement::SUM)];
|
||||
Measurement::create([$epoch=1hr,
|
||||
$reducers=set(r1),
|
||||
$threshold_val(key: Measurement::Key, result: Measurement::Result) =
|
||||
{
|
||||
return double_to_count(result["test.metric"]$sum);
|
||||
},
|
||||
$threshold=100,
|
||||
$threshold_crossed(index: Metrics::Index, val: Metrics::ResultVal) = {
|
||||
print "A test metric threshold was crossed!";
|
||||
$threshold_crossed(key: Measurement::Key, result: Measurement::Result) =
|
||||
{
|
||||
print fmt("A test metric threshold was crossed with a value of: %.1f", result["test.metric"]$sum);
|
||||
terminate();
|
||||
}]);
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
event remote_connection_closed(p: event_peer)
|
||||
|
@ -39,13 +45,16 @@ event do_metrics(i: count)
|
|||
# Worker-1 will trigger an intermediate update and then if everything
|
||||
# works correctly, the data from worker-2 will hit the threshold and
|
||||
# should trigger the notice.
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=i]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=i]);
|
||||
}
|
||||
|
||||
event bro_init()
|
||||
event remote_connection_handshake_done(p: event_peer)
|
||||
{
|
||||
if ( Cluster::node == "worker-1" )
|
||||
schedule 2sec { do_metrics(99) };
|
||||
if ( Cluster::node == "worker-2" )
|
||||
event do_metrics(1);
|
||||
if ( p$descr == "manager-1" )
|
||||
{
|
||||
if ( Cluster::node == "worker-1" )
|
||||
schedule 0.1sec { do_metrics(1) };
|
||||
if ( Cluster::node == "worker-2" )
|
||||
schedule 0.5sec { do_metrics(99) };
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
# @TEST-EXEC: bro %INPUT
|
||||
# @TEST-EXEC: btest-diff .stdout
|
||||
|
||||
redef enum Notice::Type += {
|
||||
Test_Notice,
|
||||
};
|
||||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
local r1: Measurement::Reducer = [$stream="test.metric", $apply=set(Measurement::SUM)];
|
||||
Measurement::create([$epoch=3secs,
|
||||
$reducers=set(r1),
|
||||
#$threshold_val = Measurement::sum_threshold("test.metric"),
|
||||
$threshold_val(key: Measurement::Key, result: Measurement::Result) =
|
||||
{
|
||||
return double_to_count(result["test.metric"]$sum);
|
||||
},
|
||||
$threshold=5,
|
||||
$threshold_crossed(key: Measurement::Key, result: Measurement::Result) =
|
||||
{
|
||||
local r = result["test.metric"];
|
||||
print fmt("THRESHOLD: hit a threshold value at %.0f for %s", r$sum, Measurement::key2str(key));
|
||||
}
|
||||
]);
|
||||
|
||||
local r2: Measurement::Reducer = [$stream="test.metric", $apply=set(Measurement::SUM)];
|
||||
Measurement::create([$epoch=3secs,
|
||||
$reducers=set(r2),
|
||||
#$threshold_val = Measurement::sum_threshold("test.metric"),
|
||||
$threshold_val(key: Measurement::Key, result: Measurement::Result) =
|
||||
{
|
||||
return double_to_count(result["test.metric"]$sum);
|
||||
},
|
||||
$threshold_series=vector(3,6,800),
|
||||
$threshold_crossed(key: Measurement::Key, result: Measurement::Result) =
|
||||
{
|
||||
local r = result["test.metric"];
|
||||
print fmt("THRESHOLD_SERIES: hit a threshold series value at %.0f for %s", r$sum, Measurement::key2str(key));
|
||||
}
|
||||
]);
|
||||
|
||||
local r3: Measurement::Reducer = [$stream="test.metric", $apply=set(Measurement::SUM)];
|
||||
local r4: Measurement::Reducer = [$stream="test.metric2", $apply=set(Measurement::SUM)];
|
||||
Measurement::create([$epoch=3secs,
|
||||
$reducers=set(r3, r4),
|
||||
$threshold_val(key: Measurement::Key, result: Measurement::Result) =
|
||||
{
|
||||
# Calculate a ratio between sums of two reducers.
|
||||
if ( "test.metric2" in result && "test.metric" in result &&
|
||||
result["test.metric"]$sum > 0 )
|
||||
return double_to_count(result["test.metric2"]$sum / result["test.metric"]$sum);
|
||||
else
|
||||
return 0;
|
||||
},
|
||||
# Looking for metric2 sum to be 5 times the sum of metric
|
||||
$threshold=5,
|
||||
$threshold_crossed(key: Measurement::Key, result: Measurement::Result) =
|
||||
{
|
||||
local thold = result["test.metric2"]$sum / result["test.metric"]$sum;
|
||||
print fmt("THRESHOLD WITH RATIO BETWEEN REDUCERS: hit a threshold value at %.0fx for %s", thold, Measurement::key2str(key));
|
||||
}
|
||||
]);
|
||||
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=3]);
|
||||
Measurement::add_data("test.metric", [$host=6.5.4.3], [$num=2]);
|
||||
Measurement::add_data("test.metric", [$host=7.2.1.5], [$num=1]);
|
||||
Measurement::add_data("test.metric", [$host=1.2.3.4], [$num=3]);
|
||||
Measurement::add_data("test.metric", [$host=7.2.1.5], [$num=1000]);
|
||||
Measurement::add_data("test.metric2", [$host=7.2.1.5], [$num=10]);
|
||||
Measurement::add_data("test.metric2", [$host=7.2.1.5], [$num=1000]);
|
||||
Measurement::add_data("test.metric2", [$host=7.2.1.5], [$num=54321]);
|
||||
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
# @TEST-SERIALIZE: comm
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager-1 BROPATH=$BROPATH:.. CLUSTER_NODE=manager-1 bro %INPUT
|
||||
# @TEST-EXEC: btest-bg-run proxy-1 BROPATH=$BROPATH:.. CLUSTER_NODE=proxy-1 bro %INPUT
|
||||
# @TEST-EXEC: sleep 1
|
||||
# @TEST-EXEC: btest-bg-run worker-1 BROPATH=$BROPATH:.. CLUSTER_NODE=worker-1 bro %INPUT
|
||||
# @TEST-EXEC: btest-bg-run worker-2 BROPATH=$BROPATH:.. CLUSTER_NODE=worker-2 bro %INPUT
|
||||
# @TEST-EXEC: btest-bg-wait 15
|
||||
# @TEST-EXEC: btest-diff manager-1/metrics.log
|
||||
|
||||
@TEST-START-FILE cluster-layout.bro
|
||||
redef Cluster::nodes = {
|
||||
["manager-1"] = [$node_type=Cluster::MANAGER, $ip=127.0.0.1, $p=37757/tcp, $workers=set("worker-1", "worker-2")],
|
||||
["proxy-1"] = [$node_type=Cluster::PROXY, $ip=127.0.0.1, $p=37758/tcp, $manager="manager-1", $workers=set("worker-1", "worker-2")],
|
||||
["worker-1"] = [$node_type=Cluster::WORKER, $ip=127.0.0.1, $p=37760/tcp, $manager="manager-1", $proxy="proxy-1", $interface="eth0"],
|
||||
["worker-2"] = [$node_type=Cluster::WORKER, $ip=127.0.0.1, $p=37761/tcp, $manager="manager-1", $proxy="proxy-1", $interface="eth1"],
|
||||
};
|
||||
@TEST-END-FILE
|
||||
|
||||
redef Log::default_rotation_interval = 0secs;
|
||||
|
||||
global n = 0;
|
||||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
Metrics::add_filter("test.metric",
|
||||
[$every=3secs,
|
||||
$measure=set(Metrics::SUM, Metrics::MIN, Metrics::MAX, Metrics::AVG, Metrics::STD_DEV, Metrics::VARIANCE, Metrics::UNIQUE),
|
||||
$period_finished(ts: time, metric_name: string, filter_name: string, data: Metrics::MetricTable) =
|
||||
{
|
||||
Metrics::write_log(ts, metric_name, filter_name, data);
|
||||
if ( ++n == 3 )
|
||||
{
|
||||
terminate_communication();
|
||||
terminate();
|
||||
}
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
event remote_connection_closed(p: event_peer)
|
||||
{
|
||||
terminate();
|
||||
}
|
||||
|
||||
global ready_for_data: event();
|
||||
|
||||
redef Cluster::manager2worker_events += /ready_for_data/;
|
||||
|
||||
@if ( Cluster::local_node_type() == Cluster::WORKER )
|
||||
|
||||
event ready_for_data()
|
||||
{
|
||||
if ( Cluster::node == "worker-1" )
|
||||
{
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=34]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=30]);
|
||||
Metrics::add_data("test.metric", [$host=6.5.4.3], [$num=1]);
|
||||
Metrics::add_data("test.metric", [$host=7.2.1.5], [$num=54]);
|
||||
}
|
||||
if ( Cluster::node == "worker-2" )
|
||||
{
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=75]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=30]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=3]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=57]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=52]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=61]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=95]);
|
||||
Metrics::add_data("test.metric", [$host=6.5.4.3], [$num=5]);
|
||||
Metrics::add_data("test.metric", [$host=7.2.1.5], [$num=91]);
|
||||
}
|
||||
}
|
||||
|
||||
@endif
|
||||
|
||||
@if ( Cluster::local_node_type() == Cluster::MANAGER )
|
||||
|
||||
global peer_count = 0;
|
||||
|
||||
event remote_connection_handshake_done(p: event_peer)
|
||||
{
|
||||
++peer_count;
|
||||
if ( peer_count == 3 )
|
||||
event ready_for_data();
|
||||
}
|
||||
|
||||
@endif
|
|
@ -1,20 +0,0 @@
|
|||
# @TEST-EXEC: bro %INPUT
|
||||
# @TEST-EXEC: btest-diff metrics.log
|
||||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
Metrics::add_filter("test.metric",
|
||||
[$name="foo-bar",
|
||||
$every=3secs,
|
||||
$measure=set(Metrics::SUM, Metrics::VARIANCE, Metrics::AVG, Metrics::MAX, Metrics::MIN, Metrics::STD_DEV),
|
||||
$period_finished=Metrics::write_log]);
|
||||
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=5]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=22]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=94]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=50]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=50]);
|
||||
|
||||
Metrics::add_data("test.metric", [$host=6.5.4.3], [$num=2]);
|
||||
Metrics::add_data("test.metric", [$host=7.2.1.5], [$num=1]);
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
# @TEST-EXEC: bro %INPUT
|
||||
# @TEST-EXEC: btest-diff .stdout
|
||||
|
||||
|
||||
redef enum Notice::Type += {
|
||||
Test_Notice,
|
||||
};
|
||||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
Metrics::add_filter("test.metric",
|
||||
[$name="foobar",
|
||||
$every=3secs,
|
||||
$measure=set(Metrics::SUM),
|
||||
$threshold=5,
|
||||
$threshold_crossed(index: Metrics::Index, val: Metrics::ResultVal) = {
|
||||
print fmt("THRESHOLD: hit a threshold value at %.0f for %s", val$sum, Metrics::index2str(index));
|
||||
}]);
|
||||
|
||||
Metrics::add_filter("test.metric",
|
||||
[$name="foobar2",
|
||||
$every=3secs,
|
||||
$measure=set(Metrics::SUM),
|
||||
$threshold_series=vector(3,6,800),
|
||||
$threshold_crossed(index: Metrics::Index, val: Metrics::ResultVal) = {
|
||||
print fmt("THRESHOLD_SERIES: hit a threshold series value at %.0f for %s", val$sum, Metrics::index2str(index));
|
||||
}]);
|
||||
Metrics::add_filter("test.metric",
|
||||
[$every=3secs,
|
||||
$measure=set(Metrics::SUM),
|
||||
$threshold_func(index: Metrics::Index, val: Metrics::ResultVal) = {
|
||||
# This causes any data added to be cross the threshold.
|
||||
return T;
|
||||
},
|
||||
$threshold_crossed(index: Metrics::Index, val: Metrics::ResultVal) = {
|
||||
print fmt("THRESHOLD_FUNC: hit a threshold function value at %.0f for %s", val$sum, Metrics::index2str(index));
|
||||
}]);
|
||||
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=3]);
|
||||
Metrics::add_data("test.metric", [$host=6.5.4.3], [$num=2]);
|
||||
Metrics::add_data("test.metric", [$host=7.2.1.5], [$num=1]);
|
||||
Metrics::add_data("test.metric", [$host=1.2.3.4], [$num=3]);
|
||||
Metrics::add_data("test.metric", [$host=7.2.1.5], [$num=1000]);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue