Hi, I am trying to create a plugin which sends logs from zeek to redis database. Already we have many plugins available on the internet which sends logs from zeek to kafka, but i couldnt find anything to that on redis. I have shared my work below. Can anyone help me with this task?
redis-writer.zeek:
module Redis;
export {
## Configuration options for the Redis writer.
##
## To use this writer, add the following to your script:
##
## @load aux/redis-writer
##
## redef Redis::servers = {
## ["redis://127.0.0.1:6379"],
## };
##
## Log::add_writer(REDIS_LOG, [$writer=REDIS_WRITER]);
##
## redef Log::default_writer = REDIS_LOG;
##
## The default log stream will then be written to Redis.
##
redef record Redis::options += {
## The Redis server(s) to write to. This should be a set of Redis URIs,
## where each URI is a string of the form "redis://<hostname>:<port>".
servers: set[string] = [
"redis://127.0.0.1:6379"
],
## The Redis key prefix to use when writing logs. This allows you to
## namespace your logs in Redis.
key_prefix: string = ""
};
}
export {
## //Constants for the Redis writer.
const REDIS_WRITER: Log::WriterInfo = Log::WriterInfo($name="Redis::WRITER", $priority=5);
const REDIS_LOG: Log::Log = Log::Log([$columns=Log::default_columns,
$name="redis", $id=Log::default_id,
$path="", $writer=REDIS_WRITER]);
}
type RedisWriter: Log::FilterWriter &optionals += {
## //The Redis server(s) to write to. This should be a set of Redis URIs,
## //where each URI is a string of the form "redis://<hostname>:<port>".
servers: set[string] &optional;
## //The Redis key prefix to use when writing logs. This allows you to
## //namespace your logs in Redis.
key_prefix: string &default="";
## //The Redis connection pool to use. This is an internal option and should
## //not be set by the user.
conn_pool: Redis::ConnectionPool &default=Redis::ConnectionPool(Redis::options$servers);
};
global redis_writer: RedisWriter;
event bro_init()
{
if ( ! redis_writer?$writer )
redis_writer$writer = Log::WRITER_ASYNC;
if ( ! redis_writer?$filters )
redis_writer$filters = Log::default_filters;
if ( ! redis_writer?$prefix )
redis_writer$prefix = Redis::options$key_prefix;
if ( ! redis_writer?$servers )
redis_writer$servers = Redis::options$servers;
if ( ! redis_writer?$conn_pool )
redis_writer$conn_pool = Redis::ConnectionPool(redis_writer$servers);
Log::add_filter_writer(REDIS_WRITER, [$writer=redis_writer]);
}
event bro_done()
{
redis_writer$conn_pool$terminate();
}
function bro_redis_write(log: Log::Info, writer: RedisWriter): bool
{
local Redis::Connection conn = writer$conn_pool$acquire();
local string key = fmt("%s%s:%s", writer$prefix, log$path, log$id);
conn$publish(key, log$to_json(), conn$reconnect_on_error);
writer$conn_pool$release(conn);
return T;
}
lines i added in locak.zeek:
@load redis-writer
redef Redis::servers = {
["redis://127.0.0.1:6379"],
};
Log::add_writer(REDIS_LOG, [$writer=REDIS_WRITER]);
redef Log::default_writer = REDIS_LOG;