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
|
/*
* Copyright (c) 2014 Balabit
* Copyright (c) 2013 Tihamer Petrovics <tihameri@gmail.com>
* Copyright (c) 2014 Gergely Nagy <algernon@balabit.hu>
*
* 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 <hiredis/hiredis.h>
#include "redis.h"
#include "redis-parser.h"
#include "plugin.h"
#include "messages.h"
#include "stats/stats-registry.h"
#include "logqueue.h"
#include "driver.h"
#include "plugin-types.h"
#include "logthrdestdrv.h"
typedef struct
{
LogThrDestDriver super;
gchar *host;
gint port;
LogTemplateOptions template_options;
GString *command;
LogTemplate *key;
GString *key_str;
LogTemplate *param1;
GString *param1_str;
LogTemplate *param2;
GString *param2_str;
redisContext *c;
} RedisDriver;
/*
* Configuration
*/
void
redis_dd_set_host(LogDriver *d, const gchar *host)
{
RedisDriver *self = (RedisDriver *)d;
g_free(self->host);
self->host = g_strdup(host);
}
void
redis_dd_set_port(LogDriver *d, gint port)
{
RedisDriver *self = (RedisDriver *)d;
self->port = (int)port;
}
void
redis_dd_set_command(LogDriver *d, const gchar *command,
LogTemplate *key,
LogTemplate *param1, LogTemplate *param2)
{
RedisDriver *self = (RedisDriver *)d;
g_string_assign(self->command, command);
log_template_unref(self->key);
self->key = log_template_ref(key);
log_template_unref(self->param1);
self->param1 = log_template_ref(param1);
log_template_unref(self->param2);
self->param2 = log_template_ref(param2);
}
LogTemplateOptions *
redis_dd_get_template_options(LogDriver *d)
{
RedisDriver *self = (RedisDriver *)d;
return &self->template_options;
}
/*
* Utilities
*/
static gchar *
redis_dd_format_stats_instance(LogThrDestDriver *d)
{
RedisDriver *self = (RedisDriver *)d;
static gchar persist_name[1024];
if (d->super.super.super.persist_name)
g_snprintf(persist_name, sizeof(persist_name), "redis,%s", d->super.super.super.persist_name);
else
g_snprintf(persist_name, sizeof(persist_name), "redis,%s,%u", self->host, self->port);
return persist_name;
}
static const gchar *
redis_dd_format_persist_name(const LogPipe *s)
{
const RedisDriver *self = (const RedisDriver *)s;
static gchar persist_name[1024];
if (s->persist_name)
g_snprintf(persist_name, sizeof(persist_name), "redis.%s", s->persist_name);
else
g_snprintf(persist_name, sizeof(persist_name), "redis(%s,%u)", self->host, self->port);
return persist_name;
}
static gboolean
redis_dd_connect(RedisDriver *self, gboolean reconnect)
{
redisReply *reply;
if (reconnect && (self->c != NULL))
{
reply = redisCommand(self->c, "ping");
if (reply)
freeReplyObject(reply);
if (!self->c->err)
return TRUE;
else
self->c = redisConnect(self->host, self->port);
}
else
self->c = redisConnect(self->host, self->port);
if (self->c->err)
{
msg_error("REDIS server error, suspending",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_str("error", self->c->errstr),
evt_tag_int("time_reopen", self->super.time_reopen));
return FALSE;
}
else
msg_debug("Connecting to REDIS succeeded",
evt_tag_str("driver", self->super.super.super.id));
return TRUE;
}
static void
redis_dd_disconnect(LogThrDestDriver *s)
{
RedisDriver *self = (RedisDriver *)s;
if (self->c)
redisFree(self->c);
self->c = NULL;
}
/*
* Worker thread
*/
static worker_insert_result_t
redis_worker_insert(LogThrDestDriver *s, LogMessage *msg)
{
RedisDriver *self = (RedisDriver *)s;
redisReply *reply;
const char *argv[5];
size_t argvlen[5];
int argc = 2;
if (!redis_dd_connect(self, TRUE))
return WORKER_INSERT_RESULT_NOT_CONNECTED;
if (self->c->err)
return WORKER_INSERT_RESULT_ERROR;
log_template_format(self->key, msg, &self->template_options, LTZ_SEND,
self->super.seq_num, NULL, self->key_str);
if (self->param1)
log_template_format(self->param1, msg, &self->template_options, LTZ_SEND,
self->super.seq_num, NULL, self->param1_str);
if (self->param2)
log_template_format(self->param2, msg, &self->template_options, LTZ_SEND,
self->super.seq_num, NULL, self->param2_str);
argv[0] = self->command->str;
argvlen[0] = self->command->len;
argv[1] = self->key_str->str;
argvlen[1] = self->key_str->len;
if (self->param1)
{
argv[2] = self->param1_str->str;
argvlen[2] = self->param1_str->len;
argc++;
}
if (self->param2)
{
argv[3] = self->param2_str->str;
argvlen[3] = self->param2_str->len;
argc++;
}
reply = redisCommandArgv(self->c, argc, argv, argvlen);
if (!reply)
{
msg_error("REDIS server error, suspending",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_str("command", self->command->str),
evt_tag_str("key", self->key_str->str),
evt_tag_str("param1", self->param1_str->str),
evt_tag_str("param2", self->param2_str->str),
evt_tag_str("error", self->c->errstr),
evt_tag_int("time_reopen", self->super.time_reopen));
return WORKER_INSERT_RESULT_ERROR;
}
msg_debug("REDIS command sent",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_str("command", self->command->str),
evt_tag_str("key", self->key_str->str),
evt_tag_str("param1", self->param1_str->str),
evt_tag_str("param2", self->param2_str->str));
freeReplyObject(reply);
return WORKER_INSERT_RESULT_SUCCESS;
}
static void
redis_worker_thread_init(LogThrDestDriver *d)
{
RedisDriver *self = (RedisDriver *)d;
msg_debug("Worker thread started",
evt_tag_str("driver", self->super.super.super.id));
self->key_str = g_string_sized_new(1024);
self->param1_str = g_string_sized_new(1024);
self->param2_str = g_string_sized_new(1024);
redis_dd_connect(self, FALSE);
}
static void
redis_worker_thread_deinit(LogThrDestDriver *d)
{
RedisDriver *self = (RedisDriver *)d;
g_string_free(self->key_str, TRUE);
g_string_free(self->param1_str, TRUE);
g_string_free(self->param2_str, TRUE);
}
/*
* Main thread
*/
static gboolean
redis_dd_init(LogPipe *s)
{
RedisDriver *self = (RedisDriver *)s;
GlobalConfig *cfg = log_pipe_get_config(s);
if (!log_dest_driver_init_method(s))
return FALSE;
log_template_options_init(&self->template_options, cfg);
msg_verbose("Initializing Redis destination",
evt_tag_str("driver", self->super.super.super.id),
evt_tag_str("host", self->host),
evt_tag_int("port", self->port));
return log_threaded_dest_driver_start(s);
}
static void
redis_dd_free(LogPipe *d)
{
RedisDriver *self = (RedisDriver *)d;
log_template_options_destroy(&self->template_options);
g_free(self->host);
g_string_free(self->command, TRUE);
log_template_unref(self->key);
log_template_unref(self->param1);
log_template_unref(self->param2);
if (self->c)
redisFree(self->c);
log_threaded_dest_driver_free(d);
}
/*
* Plugin glue.
*/
LogDriver *
redis_dd_new(GlobalConfig *cfg)
{
RedisDriver *self = g_new0(RedisDriver, 1);
log_threaded_dest_driver_init_instance(&self->super, cfg);
self->super.super.super.super.init = redis_dd_init;
self->super.super.super.super.free_fn = redis_dd_free;
self->super.super.super.super.generate_persist_name = redis_dd_format_persist_name;
self->super.worker.thread_init = redis_worker_thread_init;
self->super.worker.thread_deinit = redis_worker_thread_deinit;
self->super.worker.disconnect = redis_dd_disconnect;
self->super.worker.insert = redis_worker_insert;
self->super.format.stats_instance = redis_dd_format_stats_instance;
self->super.stats_source = SCS_REDIS;
redis_dd_set_host((LogDriver *)self, "127.0.0.1");
redis_dd_set_port((LogDriver *)self, 6379);
self->command = g_string_sized_new(32);
log_template_options_defaults(&self->template_options);
return (LogDriver *)self;
}
extern CfgParser redis_dd_parser;
static Plugin redis_plugin =
{
.type = LL_CONTEXT_DESTINATION,
.name = "redis",
.parser = &redis_parser,
};
gboolean
redis_module_init(GlobalConfig *cfg, CfgArgs *args)
{
plugin_register(cfg, &redis_plugin, 1);
return TRUE;
}
const ModuleInfo module_info =
{
.canonical_name = "redis",
.version = SYSLOG_NG_VERSION,
.description = "The afredis module provides Redis destination support for syslog-ng.",
.core_revision = SYSLOG_NG_SOURCE_REVISION,
.plugins = &redis_plugin,
.plugins_len = 1,
};
|