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
|
/*
* Copyright (c) 2014 Balabit
* Copyright (c) 2013 Tihamer Petrovics <tihameri@gmail.com>
* Copyright (c) 2014 Gergely Nagy <algernon@balabit.hu>
* Copyright (c) 2021 Szilárd Parrag
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "redis-worker.h"
#include "redis.h"
#include "messages.h"
#include "scratch-buffers.h"
#include "utf8utils.h"
static LogThreadedResult
_flush(LogThreadedDestWorker *s, LogThreadedFlushMode mode)
{
RedisDestWorker *self = (RedisDestWorker *) s;
if(s->batch_size == 0)
{
return LTR_SUCCESS;
}
if(mode == LTF_FLUSH_EXPEDITE)
{
return LTR_RETRY;
}
if(self->c == NULL || self->c->err)
{
return LTR_ERROR;
}
redisReply *reply;
for(gint i = s->batch_size; i > 0; i--)
{
if(redisGetReply(self->c, (void **)&reply) != REDIS_OK)
{
return LTR_ERROR;
}
freeReplyObject(reply);
}
return LTR_SUCCESS;
}
static inline void
_fill_template(RedisDestWorker *self, LogMessage *msg, LogTemplate *template, gchar **str,
gsize *size)
{
RedisDriver *owner = (RedisDriver *) self->super.owner;
if (log_template_is_trivial(template))
{
gssize unsigned_size;
*str = (gchar *)log_template_get_trivial_value(template, msg, &unsigned_size);
*size = unsigned_size;
}
else
{
GString *buffer = scratch_buffers_alloc();
LogTemplateEvalOptions options = {&owner->template_options, LTZ_SEND,
self->super.seq_num, NULL, LM_VT_STRING
};
log_template_format(template, msg, &options, buffer);
*size = buffer->len;
*str = buffer->str;
}
}
static void
_fill_argv_from_template_list(RedisDestWorker *self, LogMessage *msg)
{
RedisDriver *owner = (RedisDriver *) self->super.owner;
for (gint i = 1; i < self->argc; i++)
{
LogTemplate *redis_command = g_list_nth_data(owner->arguments, i-1);
_fill_template(self, msg, redis_command, &self->argv[i], &self->argvlen[i]);
}
}
static const gchar *
_argv_to_string(RedisDestWorker *self)
{
GString *full_command = scratch_buffers_alloc();
full_command = g_string_append(full_command, self->argv[0]);
for (gint i = 1; i < self->argc; i++)
{
g_string_append(full_command, " \"");
append_unsafe_utf8_as_escaped_text(full_command, self->argv[i], self->argvlen[i], "\"");
g_string_append(full_command, "\"");
}
return full_command->str;
}
static LogThreadedResult
redis_worker_insert_batch(LogThreadedDestWorker *s, LogMessage *msg)
{
RedisDestWorker *self = (RedisDestWorker *)s;
RedisDriver *owner = (RedisDriver *) self->super.owner;
g_assert(owner->super.batch_lines > 0);
ScratchBuffersMarker marker;
scratch_buffers_mark(&marker);
_fill_argv_from_template_list(self, msg);
int retval = redisAppendCommandArgv(self->c, self->argc, (const gchar **)self->argv, self->argvlen);
if(self->c == NULL || self->c->err || retval != REDIS_OK)
{
msg_error("REDIS server error, suspending",
evt_tag_str("driver", owner->super.super.super.id),
evt_tag_str("command", _argv_to_string(self)),
evt_tag_str("error", self->c->errstr),
evt_tag_int("time_reopen", self->super.time_reopen));
scratch_buffers_reclaim_marked(marker);
return LTR_ERROR;
}
msg_debug("REDIS command appended",
evt_tag_str("driver", owner->super.super.super.id),
evt_tag_str("command", _argv_to_string(self)));
scratch_buffers_reclaim_marked(marker);
return LTR_QUEUED;
}
static LogThreadedResult
redis_worker_insert(LogThreadedDestWorker *s, LogMessage *msg)
{
RedisDestWorker *self = (RedisDestWorker *)s;
RedisDriver *owner = (RedisDriver *) self->super.owner;
LogThreadedResult status = LTR_ERROR;
g_assert(owner->super.batch_lines <= 0);
ScratchBuffersMarker marker;
scratch_buffers_mark(&marker);
_fill_argv_from_template_list(self, msg);
redisReply *reply = redisCommandArgv(self->c, self->argc, (const gchar **)self->argv, self->argvlen);
if (!reply)
{
msg_error("REDIS server error, suspending",
evt_tag_str("driver", owner->super.super.super.id),
evt_tag_str("command", _argv_to_string(self)),
evt_tag_str("error", self->c->errstr),
evt_tag_int("time_reopen", self->super.time_reopen));
goto exit;
}
if (reply->type == REDIS_REPLY_ERROR)
{
msg_error("REDIS server error, suspending",
evt_tag_str("driver", owner->super.super.super.id),
evt_tag_str("command", _argv_to_string(self)),
evt_tag_str("error", reply->str),
evt_tag_int("time_reopen", self->super.time_reopen));
goto exit;
}
msg_debug("REDIS command sent",
evt_tag_str("driver", owner->super.super.super.id),
evt_tag_str("command", _argv_to_string(self)));
status = LTR_SUCCESS;
exit:
freeReplyObject(reply);
scratch_buffers_reclaim_marked(marker);
return status;
}
static gboolean
redis_worker_init(LogThreadedDestWorker *d)
{
RedisDestWorker *self = (RedisDestWorker *) d;
RedisDriver *owner = (RedisDriver *) self->super.owner;
self->argc = g_list_length(owner->arguments) + 1;
self->argv = g_malloc(self->argc * sizeof(char *));
self->argvlen = g_malloc(self->argc * sizeof(size_t));
self->argv[0] = owner->command->str;
self->argvlen[0] = owner->command->len;
msg_debug("Worker thread started",
evt_tag_str("driver", self->super.owner->super.super.id));
return log_threaded_dest_worker_init_method(d);
}
static void
redis_worker_disconnect(LogThreadedDestWorker *s)
{
RedisDestWorker *self = (RedisDestWorker *)s;
if (self->c)
redisFree(self->c);
self->c = NULL;
}
static void
redis_worker_deinit(LogThreadedDestWorker *d)
{
RedisDestWorker *self = (RedisDestWorker *)d;
g_free(self->argv);
g_free(self->argvlen);
redis_worker_disconnect(d);
log_threaded_dest_worker_deinit_method(d);
}
static inline void
_trace_reply_message(redisReply *r)
{
if (trace_flag)
{
if (r->elements > 0)
{
msg_trace(">>>>>> REDIS command reply begin",
evt_tag_long("elements", r->elements));
for (gsize i = 0; i < r->elements; i++)
{
_trace_reply_message(r->element[i]);
}
msg_trace("<<<<<< REDIS command reply end");
}
else if (r->type == REDIS_REPLY_STRING || r->type == REDIS_REPLY_STATUS || r->type == REDIS_REPLY_ERROR)
{
msg_trace("REDIS command reply",
evt_tag_str("str", r->str));
}
else
{
msg_trace("REDIS command unhandled reply",
evt_tag_int("type", r->type));
}
}
}
static gboolean
send_redis_command(RedisDestWorker *self, const char *format, ...)
{
va_list ap;
va_start(ap, format);
redisReply *reply = redisvCommand(self->c, format, ap);
va_end(ap);
gboolean retval = reply && (reply->type != REDIS_REPLY_ERROR);
if (reply)
{
_trace_reply_message(reply);
freeReplyObject(reply);
}
return retval;
}
static gboolean
check_connection_to_redis(RedisDestWorker *self)
{
return send_redis_command(self, "ping");
}
static gboolean
authenticate_to_redis(RedisDestWorker *self, const gchar *password)
{
return send_redis_command(self, "AUTH %s", password);
}
static gboolean
redis_worker_connect(LogThreadedDestWorker *s)
{
RedisDestWorker *self = (RedisDestWorker *)s;
RedisDriver *owner = (RedisDriver *) self->super.owner;
if (self->c && check_connection_to_redis(self))
return TRUE;
else if (self->c)
redisReconnect(self->c);
else
self->c = redisConnectWithTimeout(owner->host, owner->port, owner->timeout);
if (self->c == NULL || self->c->err)
{
if (self->c)
{
msg_error("REDIS server error during connection",
evt_tag_str("driver", owner->super.super.super.id),
evt_tag_str("error", self->c->errstr),
evt_tag_int("time_reopen", self->super.time_reopen));
}
else
{
msg_error("REDIS server can't allocate redis context");
}
return FALSE;
}
if (owner->auth)
if (!authenticate_to_redis(self, owner->auth))
{
msg_error("REDIS: failed to authenticate",
evt_tag_str("driver", owner->super.super.super.id));
return FALSE;
}
if (!check_connection_to_redis(self))
{
msg_error("REDIS: failed to connect",
evt_tag_str("driver", owner->super.super.super.id));
return FALSE;
}
if (self->c->err)
return FALSE;
msg_debug("Connecting to REDIS succeeded",
evt_tag_str("driver", owner->super.super.super.id));
return TRUE;
}
LogThreadedDestWorker *redis_worker_new(LogThreadedDestDriver *o, gint worker_index)
{
RedisDestWorker *self = g_new0(RedisDestWorker, 1);
log_threaded_dest_worker_init_instance(&self->super, o, worker_index);
self->super.init = redis_worker_init;
self->super.deinit = redis_worker_deinit;
self->super.connect = redis_worker_connect;
self->super.disconnect = redis_worker_disconnect;
self->super.insert = o->batch_lines > 0 ? redis_worker_insert_batch : redis_worker_insert;
if(o->batch_lines > 0)
self->super.flush = _flush;
return &self->super;
}
|