File: python-logparser.c

package info (click to toggle)
syslog-ng 3.28.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,028 kB
  • sloc: ansic: 132,531; python: 5,838; makefile: 5,195; sh: 4,580; java: 3,555; xml: 3,344; yacc: 1,209; lex: 493; perl: 193; awk: 184
file content (297 lines) | stat: -rw-r--r-- 8,087 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
/*
 * Copyright (c) 2014-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 "python-logparser.h"
#include "python-module.h"
#include "python-logmsg.h"
#include "python-helpers.h"
#include "str-utils.h"
#include "string-list.h"

typedef struct
{
  LogParser super;

  gchar *class;
  GList *loaders;

  GHashTable *options;

  struct
  {
    PyObject *class;
    PyObject *instance;
    PyObject *parser_process;
  } py;
} PythonParser;

void
python_parser_set_class(LogParser *d, gchar *class)
{
  PythonParser *self = (PythonParser *)d;

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

void
python_parser_set_option(LogParser *d, gchar *key, gchar *value)
{
  PythonParser *self = (PythonParser *)d;
  gchar *normalized_key = __normalize_key(key);
  g_hash_table_insert(self->options, normalized_key, g_strdup(value));
}

void
python_parser_set_loaders(LogParser *d, GList *loaders)
{
  PythonParser *self = (PythonParser *)d;

  string_list_free(self->loaders);
  self->loaders = loaders;
}

static gboolean
_pp_py_invoke_bool_function(PythonParser *self, PyObject *func, PyObject *arg)
{
  return _py_invoke_bool_function(func, arg, self->class, self->super.name);
}

static void
_pp_py_invoke_void_method_by_name(PythonParser *self, PyObject *instance, const gchar *method_name)
{
  return _py_invoke_void_method_by_name(instance, method_name, self->class, self->super.name);
}

static gboolean
_pp_py_invoke_bool_method_by_name_with_args(PythonParser *self, PyObject *instance, const gchar *method_name)
{
  return _py_invoke_bool_method_by_name_with_args(instance, method_name, self->options, self->class, self->super.name);
}

static gboolean
_py_invoke_parser_process(PythonParser *self, PyObject *msg)
{
  return _pp_py_invoke_bool_function(self, self->py.parser_process, msg);
}

static gboolean
_py_invoke_init(PythonParser *self)
{
  if (_py_get_attr_or_null(self->py.instance, "init") == NULL)
    return TRUE;
  return _pp_py_invoke_bool_method_by_name_with_args(self, self->py.instance, "init");
}

static void
_py_invoke_deinit(PythonParser *self)
{
  if (_py_get_attr_or_null(self->py.instance, "deinit") != NULL)
    _pp_py_invoke_void_method_by_name(self, self->py.instance, "deinit");
}

static gboolean
_py_init_bindings(PythonParser *self)
{
  self->py.class = _py_resolve_qualified_name(self->class);
  if (!self->py.class)
    {
      gchar buf[256];

      msg_error("Error looking Python parser class",
                evt_tag_str("parser", self->super.name),
                evt_tag_str("class", self->class),
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();
      return FALSE;
    }

  self->py.instance = _py_invoke_function(self->py.class, NULL, self->class, self->super.name);
  if (!self->py.instance)
    {
      gchar buf[256];

      msg_error("Error instantiating Python parser class",
                evt_tag_str("parser", self->super.name),
                evt_tag_str("class", self->class),
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();
      return FALSE;
    }

  /* these are fast paths, store references to be faster */
  self->py.parser_process = _py_get_attr_or_null(self->py.instance, "parse");
  if (!self->py.parser_process)
    {
      msg_error("Error initializing Python parser, class does not have a parse() method",
                evt_tag_str("parser", self->super.name),
                evt_tag_str("class", self->class));
    }
  return self->py.parser_process != NULL;
}

static void
_py_free_bindings(PythonParser *self)
{
  Py_CLEAR(self->py.class);
  Py_CLEAR(self->py.instance);
  Py_CLEAR(self->py.parser_process);
}

static gboolean
_py_init_object(PythonParser *self)
{
  if (!_py_invoke_init(self))
    {
      msg_error("Error initializing Python parser object, init() returned FALSE",
                evt_tag_str("parser", self->super.name),
                evt_tag_str("class", self->class));
      return FALSE;
    }
  return TRUE;
}

static gboolean
python_parser_process(LogParser *s, LogMessage **pmsg, const LogPathOptions *path_options, const gchar *input,
                      gsize input_len)
{
  PythonParser *self = (PythonParser *)s;
  PyGILState_STATE gstate;
  gboolean result;

  gstate = PyGILState_Ensure();
  {
    LogMessage *msg = log_msg_make_writable(pmsg, path_options);

    msg_trace("python-parser message processing started",
              evt_tag_str ("input", input),
              evt_tag_str("parser", self->super.name),
              evt_tag_str("class", self->class),
              evt_tag_printf("msg", "%p", msg));

    PyObject *msg_object = py_log_message_new(msg);
    result = _py_invoke_parser_process(self, msg_object);
    Py_DECREF(msg_object);
  }
  PyGILState_Release(gstate);

  return result;
}

static gboolean
python_parser_init(LogPipe *s)
{
  PythonParser *self = (PythonParser *)s;
  PyGILState_STATE gstate;

  if (!self->class)
    {
      msg_error("Error initializing Python parser: no script specified!",
                evt_tag_str("parser", self->super.name));
      return FALSE;
    }

  if (!log_parser_init_method(s))
    return FALSE;

  gstate = PyGILState_Ensure();

  _py_perform_imports(self->loaders);
  if (!_py_init_bindings(self) ||
      !_py_init_object(self))
    goto fail;

  PyGILState_Release(gstate);

  msg_verbose("Python parser initialized",
              evt_tag_str("parser", self->super.name),
              evt_tag_str("class", self->class));

  return TRUE;
fail:
  PyGILState_Release(gstate);
  return FALSE;
}

static gboolean
python_parser_deinit(LogPipe *d)
{
  PythonParser *self = (PythonParser *)d;
  PyGILState_STATE gstate;

  gstate = PyGILState_Ensure();
  _py_invoke_deinit(self);
  PyGILState_Release(gstate);

  return log_parser_deinit_method(d);
}

static void
python_parser_free(LogPipe *d)
{
  PythonParser *self = (PythonParser *)d;
  PyGILState_STATE gstate;

  gstate = PyGILState_Ensure();
  _py_free_bindings(self);
  PyGILState_Release(gstate);

  g_free(self->class);

  if (self->options)
    g_hash_table_unref(self->options);

  string_list_free(self->loaders);

  log_parser_free_method(d);
}

static LogPipe *
python_parser_clone(LogPipe *s)
{
  PythonParser *self = (PythonParser *) s;
  PythonParser *cloned = (PythonParser *) python_parser_new(log_pipe_get_config(s));
  g_hash_table_unref(cloned->options);
  python_parser_set_class(&cloned->super, self->class);
  cloned->loaders = string_list_clone(self->loaders);
  cloned->options = g_hash_table_ref(self->options);

  return &cloned->super.super;
}

LogParser *
python_parser_new(GlobalConfig *cfg)
{
  PythonParser *self = g_new0(PythonParser, 1);

  log_parser_init_instance(&self->super, cfg);
  self->super.super.init = python_parser_init;
  self->super.super.deinit = python_parser_deinit;
  self->super.super.free_fn = python_parser_free;
  self->super.super.clone = python_parser_clone;
  self->super.process = python_parser_process;
  self->py.class = self->py.instance = self->py.parser_process = NULL;

  self->options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);

  return (LogParser *)self;
}