File: README.md

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 (662 lines) | stat: -rw-r--r-- 22,316 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# syslog-ng Python modules

syslog-ng has a quite comprehensive support for implementing various logging
components in Python. The goal of this file is to document how to use this
functionality.

## When to use Python

These Python bindinds are useful if the facilities provided by the
syslog-ng configuration language is not sufficient, that is:

  * if there's no native functionality that would implement the integration with
    a specific service (e.g. API based log sources or information sources that
    you want to use for data enrichment purposes)
  * if the configuration language does not have the features to do some
    specific transformation need (but in that case, please let me know about
    your usecase)
  * if you intend to work on some complex data structures (e.g. JSON) which
    is more natural in a real programming language.

While Python is very powerful and you can produce clean and production ready
solutions using it, the drawback is usually performance. Python code tends
to be slower than the native functionality that syslog-ng provides.

To offset this impact of performance degradation, it's a good strategy to
only process a subset of the incoming log stream via Python code and to use
native configuration elements to select which subset is traversing said
Python code.


## Creating and Storing the Python code

syslog-ng is able to work with both Python code directly embedded into its
main configuration file or to work with Python modules.

### Embedding Python into syslog-ng configuration


You can simply use a top-level `python {}` block to embed your Python code,
like this:
```
@version: 4.0

python {

def template_function(msg):
    return b"Hello World from Python! Original message: " + msg['MSGHDR'] + msg['MESSAGE']


};

log {
    source { tcp(port(2000)); };
    destination { file("logfile" template("$ISODATE $(python template_function)")); };
};
```

### Using Python modules

You can also put your code into a proper Python module and then use it
from there. syslog-ng automatically adds `${sysconfdir}/python` to your
PYTHONPATH (normally `/etc/syslog-ng/python`), with that in mind
add the following code to `/etc/syslog-ng/python/mytemplate.py`:


```python
def template_function(msg):
    return b"Hello World from Python! Original message: " + msg['MSGHDR'] + msg['MESSAGE']
```

The Python glue in syslog-ng will automatically import modules when it
encounters an identifier in dotted notation, so if you use this syslog-ng
config:

```
@version: 4.0

log {
    source { tcp(port(2000)); };
    destination { file("logfile" template("$ISODATE $(python mytemplate.template_function)")); };
};
```

syslog-ng would recognize that `mytemplate.template_function` is a qualified
name and would attempt to import `mytemplate` as a module and then look up
`template_function` within that module.

Please note that modules are only imported once, e.g.  you will need to
restart syslog-ng for a change to take effect.

### syslog-ng reload and Python

When you reload syslog-ng (with syslog-ng-ctl reload or systemctl reload
syslog-ng) then the `python` block in your configuration is reloaded along
with the rest of the configuration file.

So any changes you make in Python code directly embedded in your
configuration will be considered as the reload completes.  This also means
that any global variables will be reset, you cannot store state accross
reloads in your `python {}` block.

The same behaviour is however not the case with code you import from modules.
Modules are only imported once and kept across reloads, even if the
syslog-ng configuration is reloaded in the meanwhile.

This means that global state can be stored in modules and they will be kept,
even as syslog-ng reinitializes the configuration.

In case you want to reload a module every time syslog-ng config is
reinitialized, you need to do this explicitly with a code similar to this:

```python
python {

import mymodule
import importlib

# reload mymodule every time syslog-ng reloads
importlib.reload(mymodule)

};
```


### Destination driver

A destination driver in Python needs to be derived from the `LogDestination`
class, as defined by the syslogng module, like the example below:

`mydestination.py`:

```python
from syslogng import LogDestination

class MyDestination(LogDestination):
    def send(self, msg):
	return True

```

The interface of the `LogDestination` class is documented in the
`syslogng.dest` module, which is stored in the file
`modules/python/pylib/syslogng/dest.py` of the source tree.

Once all required methods are implemented, you can use this using the
"python" destination in the syslog-ng configuration language.

```
destination whatever {
    python(class(mydestination.MyDestination));
};
```

There's a more complete example destination in the `python_example()`
destination plugin, that is located in the directory
`modules/python-modules/example/` within the source tree or the same files
installed under `${exec_prefix}/syslog-ng/python/syslogng/modules` in a
production deployment.

### Template Function plugin

Template functions extend the syslog-ng template language. They get a
`LogMessage` object and return a string which gets embedded into the
output of the template.

You can have syslog-ng call a Python function from the template language
using the `$(python)` template function, as you have seen in the previous
chapter.

```
@version: 4.0

python {

def template_function(msg):
    return b"Hello World from Python! Original message: " + msg['MSGHDR'] + msg['MESSAGE']

};

...

destination d_file {
    file("/var/log/whatever" template("$(python template_function)"));
};
```

The Python function needs to be any kind of callable, it receives a
`LogMessage` instance and returns a string (`str` or `bytes`).

The message passed to a template function is read-only, if you are trying to
change a name-value pair, you will receive an exception.

### Parser plugin

A parser plugin in Python needs to be derived from the `LogParser` class as
this example shows:


```python

from syslogng import LogParser

class MyParser(LogParser):

    def parse(self, msg):
        msg['name'] = 'value'
        return True
```

In contrast to template functions, parsers receive a read-writable
`LogMessage` object, so you are free to modify its contents.

### Source driver based on LogFetcher

There are two kinds of source plugins that can be implemented in Python: (a)
`LogFetcher` and (b) `LogSource`.  Both of which are defined by the
`syslogng.source` module.

The main entry point that you will need to implement for a `LogFetcher`
class is the `fetch()` method, which is automatically invoked by syslog-ng,
whenever it is able to consume incoming messages.

```
@version: 4.0

python {
from syslogng import LogFetcher
from syslogng import LogMessage
import time

class MyFetcher(LogFetcher):
    def fetch(self):
        time.sleep(1)
        msg = LogMessage.parse("<5>2022-02-02T10:23:45+02:00 HOST program[pid]: foobar", self.parse_options)
        return self.SUCCESS, msg

};


log {
    source { python-fetcher(class(MyFetcher)); };
    destination { file("messages"); };
};
```

This example would generate one message every second, based on a literal
string that is parsed as a syslog message.

Our source in this case is running in a dedicated thread, so it is free to
block.

Please note the `time.sleep(1)` call as the very first line in our `fetch()`
method.  As you can see we were sleeping 1 second between invocations of our
method, thereby limiting the rate the source is producing messages.  If that
sleep wasn't there, we would be producing somewhere between 100-110k
messages per second, depending on the speed of your CPU, the performance of
the Python interpreter and the syslog-ng core.

Usually, when our fetcher would be connecting to an external API, the sleep
would not be needed and the response times of the API would be the limiting
factor.

#### Adding persistent state

Usually, if we are fetching messages from an API, we will need to keep track
where we are in fetching messages. If we were to store the position in a
variable, that value would be lost when syslog-ng is reloaded or restarted
(depending on where we store that variable, in our `python {}` block or in a
module).

A better option is to use the `Persist()` class that ties into syslog-ng's
own persistent state handling functionality.  This allows you to store
variables persisted in a file that gets stored in
`${localstatedir}/syslog-ng.persist` along with the rest of syslog-ng's
states.

```python
class MyFetcher(LogFetcher):

    def init(self, options):
        self.persist = Persist("MyFetcher_persistent_data", defaults={"counter": 1})
        return True

    def fetch(self):
        time.sleep(1)
        counter = self.persist['counter']
        self.persist['counter'] += 1
        msg = LogMessage.parse("<5>2022-02-02T10:23:45+02:00 HOST program[pid]: foobar %d" % counter, self.parse_options)
        return self.SUCCESS, msg

```

Once initialized, a `Persist()` instance behaves as a dict where you can
store Python values (currently only `str`, `bytes` and `int` are supported).

Anything you store in a persist instance will be remembered even across
restarts.

The entries themselves are backed up to disk immediately after changing
them (using an `mmap()`-ped file), so no need to care about committing them to
disk explicitly.

Albeit you can store position information in a `Persist()` entry, it's not
always the best choice.  In syslog-ng, producing of messages and their
delivery is decoupled: sometimes a message is still in-flight for a while
before being delivered.  This time can be significant, if a destination
consumes messages at a slow rate.  In this case, if you store the position
once fetched, the message would still be sitting in a queue waiting to be
delivered. If the queue is not backed by disk (e.g. disk-buffer), then these
messages would be lost, once syslog-ng is restarted.

To anticipate this case, you will need to use bookmarks, as described in the
next section.

#### Using bookmarks in a source

The bookmarking mechanism allows messages to carry individual markers that
uniquely identify a message and its position in a source stream.

In a source file for example, the bookmark would contain the position of the
message within that file. An API may have a similar mechanism in place in
which the source API associates an opaque to each message, which signifies
its position in the repository.

A specific example for bookmarks is systemd-journald, which has a "cursor"
indicating the position of each journal record. The cursor can then be used
to restart the reading of the log stream.

Once you identified what mechanism the source offers that would map to the
bookmark concept, you can decide how you want syslog-ng to track these
bookmarks.

Which one you will need from syslog-ng's selection of bookmark
tracking strategies is again up to the API specifics.

Sometimes an API is sequential in nature, thus you can only acknowledge the
"last fetch position" in that sequence.  In other cases the API allows you
to acknowledge messages individually. These are both supported by syslog-ng.

But first, here's a Python example that only updates the current position in
a source stream when the messages in sequence were acknowledged by syslog-ng
destinations (e.g.  they were properly sent).

```python

class MyFetcher(LogFetcher):
    counter = 0

    def init(self, options):
        self.persist = Persist("MyFetcher_persistent_data", defaults={"position": 0})
        self.counter = self.persist['position']

	# pass self.message_acked method as ACK callback
        self.ack_tracker = ConsecutiveAckTracker(ack_callback=self.message_acked)
        return True

    def message_acked(self, acked_message_bookmark):
	# update current persisted position when syslog-ng delivered the
	# message, but only then.
        self.persist['position'] = acked_message_bookmark

    def fetch(self):
        time.sleep(1)
        self.counter += 1

	# depending on the speed of our consumer and the setting of
	# flags(flow-control), the current counter and the acked value may
	# differ in the messages generated.
        msg = LogMessage.parse("<5>2022-02-02T10:23:45+02:00 HOST program[pid]: foobar %d (acked so far %d)" % (self.counter, self.persist['position']), self.parse_options)

	# this is where we set the bookmark for the message
        msg.set_bookmark(self.counter)
        return self.SUCCESS, msg

```

#### Acknowledgement tracking strategies

As mentioned some APIs will provide simple others somewhat more complex ways
to track messages that are processed. There are three strategies within
syslog-ng to cope with them.

  * instant tracking (`InstantAckTracker`): messages are considered delivered
    as soon as our destination driver (or the disk based queue) acknowledges
    them.  Out-of-order deliveries are reported as they happen, so an
    earlier message may be acknowledged later than a message originally
    encountered later in the source stream.

  * consecutive tracking (`ConsecutiveAckTracker`): messages are assumed to
    form a stream and the bookmark to be a position in that stream.
    Unordered deliveries are properly handled by only acknowledging messages
    that were delivered in order.  If an unordered delivery happens, the
    tracker waits for the sequence to fill up, e.g.  waits all preceeding
    messages to be delivered as well.

  * batched tracking (`BatchedAckTracker`): messages are assumed to be
    independent, not forming a sequence of events.  Each message is
    individually tracked, the source driver has the means to get delivery
    notifications of each and every message independently.  The
    acknowledgements are accumulated until a timeout happens, at which point
    they get reported as a single batch.

You can initialize your ack_tracker in the `init` method, like this:

```python

class MyFetcher(LogFetcher):

    ...

    def init(self, options):
        # pass self.message_acked method as ACK callback
        self.ack_tracker = ConsecutiveAckTracker(ack_callback=self.message_acked)
        return True

    def message_acked(self, acked_message_bookmark):
        pass

    def fetch(self):
        ...
        msg.set_bookmark("whatever-bookmark-value-that-denotes-position")
    ...

```

In the case above, we were using `ConsecutiveAckTracker`, e.g.  we would only
get acknowledgements in the order messages were generated.  The argument of
the `message_acked` callback would be the "bookmark" value that we set using
`set_bookmark()`.

Using `InstantAckTracker` is very similar, just replace
`ConsecutiveAckTracker` with `InstantAckTracker`. In this case you'd get a
callback as soon as a message is delivered without preserving the original
ordering.

```python

class MyFetcher(LogFetcher):

    ...

    def init(self, options):
        self.ack_tracker = InstantAckTracker(ack_callback=self.message_acked)
        return True

    def message_acked(self, acked_message_bookmark):
        pass
```

While `ConsecutiveAckTracker()` seems to provide a much more useful service,
but `InstantAckTracker()` performs better, as it does not have to track
acknowledgements of individual messages.

The most complex scenario is implemented by `BatchedAckTracker`, this allows
you to track the acks for individual messages, as they happen, not enforcing
any kind of ordering.

```python

class MyFetcher(LogFetcher):

    ...

    def init(self, options):
        self.ack_tracker = BatchedAckTracker(timeout=500, batch_size=100,
                                             batched_ack_callback=self.messages_acked)
        return True

    def messages_acked(self, acked_message_bookmarks):
        pass
```

`BatchedAckTracker` would call your callback every now and then, as specified
by the `timeout` argument in milliseconds. `batch_size` specifies the number of
outstanding messages at a time. With this interface it's quite easy to
perform acknowledgements back to the source interface where per-message
acknowledgements are needed (e.g. Google PubSub).

#### Accessing the `flags()` option

The state of the `flags()` option is mapped to the `self.flags` variable, which is
a `Dict[str, bool]`, for example:
```python
{
    'parse': True,
    'check-hostname': False,
    'syslog-protocol': True,
    'assume-utf8': False,
    'validate-utf8': False,
    'sanitize-utf8': False,
    'multi-line': True,
    'store-legacy-msghdr': True,
    'store-raw-message': False,
    'expect-hostname': True,
    'guess-timezone': False,
    'header': True,
    'rfc3164-fallback': True,
}
```

### Creating a syslog-ng LogSource based Source plugin

While `LogFetcher` gives us a convinient interface for fetching messages from
backend services via blocking APIs, it is limited to performing the fetching
operation in a sequential manner: you fetch a batch of messages, feed the
syslog-ng pipeline with those messages and then repeat.

`LogSource` is more low-level but allows the use of an asynchronous framework
(`asyncio`, etc) to perform message fetching along multiple threads of
execution.

This sample uses `asyncio` to generate two independent sequences of messages,
one is generated every second, the other is every 1.5 seconds, running
concurrently via an `asyncio` event loop.

It is also pretty easy to create a source that implements a HTTP server,
and which injects messages coming via HTTP to the syslog-ng pipeline.

```python

from syslogng import LogSource
from syslogng import LogMessage
import asyncio

class MySource(LogSource):
    cancelled = False

    def run(self):
        asyncio.run(self.main())

    async def main(self):
        await asyncio.gather(self.sequence1(), self.sequence2())

    async def sequence1(self):
        while not self.cancelled:
            await asyncio.sleep(1)
            self.post_message(LogMessage("message1"))

    async def sequence2(self):
        while not self.cancelled:
            await asyncio.sleep(1.5)
            self.post_message(LogMessage("message2"))

    def request_exit(self):
        self.cancelled = True

```

Acknowledgement mechanisms (`ConsecutiveAckTracker`, `BatchedAckTracker`) and
`flags()` mapping can be used similarly to how it was described at `LogFetcher`.

## Making it more native config-wise

All the examples above used some form of `python()` driver to be actually
used, for instance, this was our Python based destination driver:

```
destination whatever {
    python(class(mydestination.MyDestination)
           options(option1 => value,
                   option2 => value));
};
```

while this works, this syntax is a bit foreign from a syslog-ng perspective
and somewhat hard to read. The usual syntax for referencing regular drivers
is something like this:

```
destination whatever {
    my-destination(option1(value) option2(value));
};
```

To make the syntax more native, you can use syslog-ng's block functionality
to wrap your Python driver, hide that it's actually Python and provide a
syntax to users of your code that is more convinient to use.

```
block destination my-destination(option1(value)
                                 option2(value)) {
    python(class(mydestination.MyDestination)
                 options(option1 => `option1`,
                         option2 => `option2`));
};
```

This block allows the use of the more "native" syntax, completely hiding the
fact that the implementation is Python based, concentrating on
functionality.

You should add this wrapper to your Python module in an "scl" subdirectory
as a file with a .conf extension. syslog-ng will automatically include these
files along the rest of the SCL.

## Adding the code to syslog-ng

To add Python based modules to syslog-ng, create a Python package (e.g.  a
directory with `__init__.py` and anything that file references).  Add these
files to `modules/python-modules/<subdirectory>` and open a pull request.

With that a "make install" command should install your module along the
rest of the syslog-ng binaries.

You will also need to add your files to the source tarball by listing them
in the EXTRA_DIST variable of the `modules/python-modules/Makefile.am` file.

## External dependencies

If your Python code depends on third party libraries, those need to be
installed on the system where your code is deployed.

If your deployment mechanism is based on packages (deb or rpm), make sure
that you add these OS level dependencies to the packages generated.

In deb this usually means adding the right package to your Depends line.

In rpm this means adding the right package as a Requires line in the .spec
file.

If you would like to use pip/requirements.txt to deploy dependencies, you
can choose to: invoke pip at "make install" time so that syslog-ng's private
Python directory would contain all the dependencies that you require.

## Adding Python code to syslog-ng deb package

To add your module to the syslog-ng deb package, create a new file in
`packaging/debian/` with a name like `syslog-ng-mod-<yourmodule>.install`.
Populate this file with wildcard patterns that capture the files of your
package after installation.

For example:

```
usr/lib/syslog-ng/python/syslogng/modules/<yourmodule>/*
```

You will also need to add an entry to `packaging/debian/control`:

```
Package: syslog-ng-mod-<yourmodule>
Architecture: any
Multi-Arch: foreign
Depends: ${shlibs:Depends}, ${misc:Depends}, syslog-ng-core (>= ${source:Version}), syslog-ng-core (<< ${source:Version}.1~), syslog-ng-mod-python
Description: The short description of the package
 This is a longer description with dots separating paragraphs.
 .
 This package provides a collection of example plugins.
```

Make sure that your .install file is included in the tarball by adding it to
EXTRA_DIST in the Makefile.am

## Adding Python code to syslog-ng RPM package

The RPM package is less modular than the Debian one and it automatically
captures all Python modules in the `syslog-ng-python` package without having
to list them explicitly.

If you need to customize installation, you can find our spec file in
`packaging/rhel/syslog-ng.spec` which is populated and copied to the root at
tarball creation.