• Stars
    star
    448
  • Rank 94,122 (Top 2 %)
  • Language
    C
  • License
    GNU Lesser Genera...
  • Created almost 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python wrappers for systemd functionality

python-systemd

Python module for native access to the systemd facilities. Functionality is separated into a number of modules:

  • systemd.journal supports sending of structured messages to the journal and reading journal files,
  • systemd.daemon wraps parts of libsystemd useful for writing daemons and socket activation,
  • systemd.id128 provides functions for querying machine and boot identifiers and a lists of message identifiers provided by systemd,
  • systemd.login wraps parts of libsystemd used to query logged in users and available seats and machines.

Installation

This module should be packaged for almost all Linux distributions. Use

On Fedora:

dnf install python3-systemd

On Debian/Ubuntu/Mint:

apt update
apt install python3-systemd

On openSUSE and SLE:

zypper in python3-systemd

On Arch:

pacman -Sy python-systemd

The project is also available on pypi as systemd-python:

PyPI

To build from source

On CentOS, RHEL, and Fedora:

dnf install git python3-pip gcc python3-devel systemd-devel
pip3 install 'git+https://github.com/systemd/python-systemd.git#egg=systemd-python'

On Debian or Ubuntu:

apt install libsystemd-{journal,daemon,login,id128}-dev gcc python3-dev pkg-config

Usage

Quick example:

from systemd import journal
journal.send('Hello world')
journal.send('Hello, again, world', FIELD2='Greetings!', FIELD3='Guten tag')
journal.send('Binary message', BINARY=b'\xde\xad\xbe\xef')

There is one required argument — the message, and additional fields can be specified as keyword arguments. Following the journald API, all names are uppercase.

The journald sendv call can also be accessed directly:

from systemd import journal
journal.sendv('MESSAGE=Hello world')
journal.sendv('MESSAGE=Hello, again, world', 'FIELD2=Greetings!',
               'FIELD3=Guten tag')
journal.sendv('MESSAGE=Binary message', b'BINARY=\xde\xad\xbe\xef')

The two examples should give the same results in the log.

Reading from the journal is often similar to using the journalctl utility.

Show all entries since 20 minutes ago (journalctl --since "20 minutes ago"):

from systemd import journal
from datetime import datetime, timedelta
j = journal.Reader()
j.seek_realtime(datetime.now() - timedelta(minutes=20))
for entry in j:
    print(entry['MESSAGE'])

Show entries between two timestamps (journalctl --since "50 minutes ago" --until "10 minutes ago"):

from systemd import journal
from datetime import datetime, timedelta
j = journal.Reader()
since = datetime.now() - timedelta(minutes=50)
until = datetime.now() - timedelta(minutes=10)
j.seek_realtime(since)
for entry in j:
  if entry['__REALTIME_TIMESTAMP'] > until:
    break
  print(entry['MESSAGE'])

Show explanations of log messages alongside entries (journalctl -x):

from systemd import journal
j = journal.Reader()
for entry in j:
    print("MESSAGE: ", entry['MESSAGE'])
    try:
        print("CATALOG: ", j.get_catalog())
    except:
        pass

Show entries by a specific executable (journalctl /usr/bin/vim):

from systemd import journal
j = journal.Reader()
j.add_match('_EXE=/usr/bin/vim')
for entry in j:
    print(entry['MESSAGE'])
  • Note: matches can be added from many different fields, for example entries from a specific process ID can be matched with the _PID field, and entries from a specific unit (ie. journalctl -u systemd-udevd.service) can be matched with _SYSTEMD_UNIT. See all fields available at the systemd.journal-fields docs.

Show kernel ring buffer (journalctl -k):

from systemd import journal
j = journal.Reader()
j.add_match('_TRANSPORT=kernel')
for entry in j:
    print(entry['MESSAGE'])

Read entries in reverse (journalctl _EXE=/usr/bin/vim -r):

from systemd import journal
class ReverseReader(journal.Reader):
    def __next__(self):
        ans = self.get_previous()
        if ans:
            return ans
        raise StopIteration()

j = ReverseReader()
j.add_match('_EXE=/usr/bin/vim')
j.seek_tail()
for entry in j:
  print(entry['MESSAGE'])

Notes

  • Unlike the native C version of journald's sd_journal_send(), printf-style substitution is not supported. Perform any substitution using Python's f-strings first (or .format() or the % operator).
  • A ValueError is raised if sd_journald_sendv() results in an error. This might happen if there are no arguments or one of them is invalid.

A handler class for the Python logging framework is also provided:

import logging
from systemd import journal
logger = logging.getLogger('custom_logger_name')
logger.addHandler(journal.JournalHandler(SYSLOG_IDENTIFIER='custom_unit_name'))
logger.warning("Some message: %s", 'detail')

libsystemd version compatibility

This module may be compiled against any version of libsystemd. At compilation time, any functionality that is not available in that version is disabled, and the resulting binary module will depend on symbols that were available at compilation time. This means that the resulting binary module is compatible with that or any later version of libsystemd. To obtain maximum possible functionality, this module must be compile against suitably recent libsystemd.

Documentation

Online documentation can be found at freedesktop.org

To build it locally run:

make sphinx-html

Or use any other builder, see man sphinx-build for a list. The compiled docs will be e.g. in docs/html.

Viewing Output

Quick way to view output with all fields as it comes in:

sudo journalctl -f --output=json

Test Builds (for Development)

python setup.py build_ext -i
python
>>> from systemd import journal
>>> journal.send("Test")

Build Status

More Repositories

1

systemd

The systemd System and Service Manager
C
12,406
star
2

casync

Content-Addressable Data Synchronization Tool
C
1,451
star
3

mkosi

💽 Build Bespoke OS Images
Python
1,005
star
4

zram-generator

Systemd unit generator for zram devices
Rust
514
star
5

pystemd

A thin Cython-based wrapper on top of libsystemd, focused on exposing the dbus API via sd-bus in an automated and easy to consume way.
Python
399
star
6

systemd-stable

Backports of patches from systemd git to stable distributions
C
113
star
7

journal2gelf

Ships new systemd journal entries to a remote destination in Graylog Extended Log Format (GELF)
Python
64
star
8

systemd-netlogd

Forwards messages from the journal to other hosts over the network using syslog format RFC 5424
C
63
star
9

node-systemd

V8 engine extension for Node allowing native interaction with systemd and its journal
JavaScript
57
star
10

kdbus

Out-of-tree kdbus module
C
50
star
11

systemd-bootchart

Boot performance graphing tool
C
50
star
12

portable-walkthrough-go

A simple yet complete example for a trivial portable service — Go Edition
Go
43
star
13

php-systemd

PHP extension allowing native interaction with systemd and its journal
C
43
star
14

node-sd-notify

wrapper around sd_notify for using systemd as a node process manager
C++
39
star
15

mkosi-initrd

Build initrd images using mkosi and distro packages
Python
39
star
16

portable-walkthrough

A simple yet complete example for a trivial portable service
C
36
star
17

ejournald

A Erlang binding to the systemd journal C API
Erlang
32
star
18

erlang-sd_notify

Erlang Bindings for sd_notify()
Erlang
18
star
19

package-notes

Tools to add packaging metadata to ELF files
14
star
20

brand.systemd.io

Website with systemd brand assets
CSS
13
star
21

systemd-coredump-python

Log Python exceptions in the journal via systemd-coredump
Python
8
star
22

systemd-centos-ci

CI scripts for systemd upstream/downstream testing using the CentOS CI infrastructure
Shell
7
star
23

journald-cat

Tool to take piped JSON and log the fields to the systemd journal
Python
5
star
24

oci-runtime-tests

Test configs to validate OCI runtime config support in systemd
Shell
5
star
25

systemd-initctl

initctl support for systemd
C
4
star
26

systemd-rhel-testsuite

RHEL downstream testsuite (in progress)
Shell
3
star
27

particleos

Shell
3
star
28

systemd-centos-ci-specs

Spec files for systemd CentOS CI build dependencies
2
star
29

systemd-dfuzzer

Fork of https://github.com/matusmarhefka/dfuzzer/ with custom functionality & improvements. It was moved back to the original repository.
C
1
star
30

systemd-fedora-ci

This project contains code to test systemd
Python
1
star