File: add-contextual-data.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (325 lines) | stat: -rw-r--r-- 8,845 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2016 Balabit
 *
 * 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 "add-contextual-data.h"
#include "logmsg/logmsg.h"
#include "logpipe.h"
#include "parser/parser-expr.h"
#include "reloc.h"
#include "cfg.h"
#include "contextual-data-record-scanner.h"
#include "add-contextual-data-selector.h"
#include "template/templates.h"
#include "context-info-db.h"
#include "pathutils.h"
#include "scratch-buffers.h"

#include <stdio.h>
#include <string.h>

typedef struct AddContextualData
{
  LogParser super;
  ContextInfoDB *context_info_db;
  AddContextualDataSelector *selector;
  gchar *default_selector;
  gchar *filename;
  gchar *prefix;
  gboolean ignore_case;
} AddContextualData;

void
add_contextual_data_set_filename(LogParser *p, const gchar *filename)
{
  AddContextualData *self = (AddContextualData *) p;

  g_free(self->filename);
  self->filename = g_strdup(filename);
}

void
add_contextual_data_set_prefix(LogParser *p, const gchar *prefix)
{
  AddContextualData *self = (AddContextualData *) p;

  g_free(self->prefix);
  self->prefix = g_strdup(prefix);
}

void
add_contextual_data_set_ignore_case(LogParser *p, gboolean ignore)
{
  AddContextualData *self = (AddContextualData *)p;
  self->ignore_case = ignore;
}

void
add_contextual_data_set_default_selector(LogParser *p, const gchar *default_selector)
{
  AddContextualData *self = (AddContextualData *) p;

  g_free(self->default_selector);
  self->default_selector = g_strdup(default_selector);
}

void
add_contextual_data_set_selector(LogParser *p, AddContextualDataSelector *selector)
{
  AddContextualData *self = (AddContextualData *) p;

  add_contextual_data_selector_free(self->selector);
  self->selector = selector;
}

static gboolean
_is_default_selector_set(const AddContextualData *self)
{
  return (self->default_selector != NULL);
}

static void
_add_context_data_to_message(gpointer pmsg, const ContextualDataRecord *record)
{
  LogMessage *msg = (LogMessage *) pmsg;
  GString *result = scratch_buffers_alloc();
  LogMessageValueType type;

  log_template_format_value_and_type(record->value, msg, &DEFAULT_TEMPLATE_EVAL_OPTIONS, result, &type);
  log_msg_set_value_with_type(msg, record->value_handle, result->str, result->len, type);
}

static gboolean
_process(LogParser *s, LogMessage **pmsg,
         const LogPathOptions *path_options,
         const gchar *input, gsize input_len)
{
  AddContextualData *self = (AddContextualData *) s;
  LogMessage *msg = log_msg_make_writable(pmsg, path_options);
  gchar *resolved_selector = add_contextual_data_selector_resolve(self->selector, msg);
  const gchar *selector = resolved_selector;

  if (!context_info_db_contains(self->context_info_db, selector) && _is_default_selector_set(self))
    selector = self->default_selector;

  msg_trace("add-contextual-data(): message lookup finished",
            evt_tag_str("message", input),
            evt_tag_str("resolved_selector", resolved_selector),
            evt_tag_str("selector", selector),
            evt_tag_msg_reference(*pmsg));

  if (selector)
    context_info_db_foreach_record(self->context_info_db, selector,
                                   _add_context_data_to_message,
                                   (gpointer) msg);

  g_free(resolved_selector);

  return TRUE;
}

static void
_replace_context_info_db(ContextInfoDB **old_db, ContextInfoDB *new_db)
{
  context_info_db_unref(*old_db);
  *old_db = context_info_db_ref(new_db);
}

static LogPipe *
_clone(LogPipe *s)
{
  AddContextualData *self = (AddContextualData *) s;
  AddContextualData *cloned =
    (AddContextualData *) add_contextual_data_parser_new(s->cfg);

  log_parser_clone_settings(&self->super, &cloned->super);

  _replace_context_info_db(&cloned->context_info_db, self->context_info_db);
  add_contextual_data_set_prefix(&cloned->super, self->prefix);
  add_contextual_data_set_filename(&cloned->super, self->filename);
  add_contextual_data_set_default_selector(&cloned->super,
                                           self->default_selector);
  add_contextual_data_set_ignore_case(&cloned->super, self->ignore_case);
  cloned->selector = add_contextual_data_selector_clone(self->selector, s->cfg);

  return &cloned->super.super;
}

static void
_free(LogPipe *s)
{
  AddContextualData *self = (AddContextualData *) s;

  context_info_db_unref(self->context_info_db);
  g_free(self->filename);
  g_free(self->prefix);
  g_free(self->default_selector);
  add_contextual_data_selector_free(self->selector);
  log_parser_free_method(s);
}

static gboolean
_is_relative_path(const gchar *filename)
{
  return (filename[0] != '/');
}

static gchar *
_complete_relative_path_with_config_path(const gchar *filename)
{
  return
    g_build_filename(get_installation_path_for(SYSLOG_NG_PATH_SYSCONFDIR),
                     filename, NULL);
}

static FILE *
_open_data_file(const gchar *filename)
{
  FILE *f = NULL;

  if (_is_relative_path(filename))
    {
      gchar *absolute_path =
        _complete_relative_path_with_config_path(filename);
      f = fopen(absolute_path, "r");
      g_free(absolute_path);
    }
  else
    {
      f = fopen(filename, "r");
    }

  return f;
}

static ContextualDataRecordScanner *
_get_scanner(AddContextualData *self)
{
  const gchar *type = get_filename_extension(self->filename);

  if (g_strcmp0(type, "csv") != 0)
    {
      msg_error("add-contextual-data(): unknown file extension, only files with a .csv extension are supported",
                evt_tag_str("filename", self->filename));
      return NULL;
    }

  return contextual_data_record_scanner_new(log_pipe_get_config(&self->super.super), self->prefix);
}

static gboolean
_load_context_info_db(AddContextualData *self)
{
  ContextualDataRecordScanner *scanner;
  FILE *f = NULL;
  gboolean result = FALSE;

  if (!(scanner = _get_scanner(self)))
    goto error;

  f = _open_data_file(self->filename);
  if (!f)
    {
      msg_error("add-contextual-data(): Error opening database",
                evt_tag_str("filename", self->filename),
                evt_tag_error("error"));
      goto error;
    }

  if (!context_info_db_import(self->context_info_db, f, self->filename, scanner))
    {
      msg_error("add-contextual-data(): Error while parsing database",
                evt_tag_str("filename", self->filename));
      goto error;
    }
  result = TRUE;

error:
  if (scanner)
    contextual_data_record_scanner_free(scanner);
  if (f)
    fclose(f);

  return result;
}

static gboolean
_init_context_info_db(AddContextualData *self)
{
  /* are we reinitializing after an unsuccessful config reload?  in that
   * case we already have the context_info_db */

  if (self->context_info_db)
    return TRUE;

  if (self->filename == NULL)
    {
      msg_error("add-contextual-data(): No database file set, specifying the database() option is mandatory");
      return FALSE;
    }

  self->context_info_db = context_info_db_new(self->ignore_case);

  if (self->selector && add_contextual_data_selector_is_ordering_required(self->selector))
    context_info_db_enable_ordering(self->context_info_db);

  return _load_context_info_db(self);
}

static gboolean
_init_selector(AddContextualData *self)
{
  return add_contextual_data_selector_init(self->selector, context_info_db_ordered_selectors(self->context_info_db));
}

static gboolean
_init(LogPipe *s)
{
  AddContextualData *self = (AddContextualData *)s;

  if (!_init_context_info_db(self))
    return FALSE;
  if (!_init_selector(self))
    return FALSE;
  if (!log_parser_init_method(s))
    return FALSE;

  return TRUE;
}

LogParser *
add_contextual_data_parser_new(GlobalConfig *cfg)
{
  AddContextualData *self = g_new0(AddContextualData, 1);

  log_parser_init_instance(&self->super, cfg);

  self->super.process = _process;
  self->selector = NULL;

  self->super.super.clone = _clone;
  self->super.super.free_fn = _free;
  self->super.super.init = _init;
  self->default_selector = NULL;
  self->prefix = NULL;

  return &self->super;
}