Skip to main content

systemd wrapper in Cython

Project description

Latest Version https://img.shields.io/pypi/wheel/cysystemd.svg https://img.shields.io/pypi/pyversions/cysystemd.svg https://img.shields.io/pypi/l/cysystemd.svg

Python systemd wrapper using Cython

Installation

All packages available on github releases.

Installation from binary wheels

  • wheels is now available for Python 3.7, 3.8, 3.9, 3.10, 3.11 for x86_64 and arm64

pythonn3.9 -m pip install \
   https://github.com/mosquito/cysystemd/releases/download/1.4.8/cysystemd-1.4.8-cp39-cp39-manylinux2014_x86_64.whl

Installation from sources

You must install systemd headers

For Debian/Ubuntu users:

apt install build-essential libsystemd-dev

On older versions of Debian/Ubuntu, you might also need to install:

apt install libsystemd-daemon-dev libsystemd-journal-dev

For CentOS/RHEL

yum install gcc systemd-devel

And install it from pypi

pip install cysystemd

Usage examples

Writing to journald

Logging handler for python logger

from cysystemd import journal
import logging
import uuid

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
logger.addHandler(journal.JournaldLogHandler())

try:
    logger.info("Trying to do something")
    raise Exception('foo')
except:
    logger.exception("Test Exception %s", 1)

systemd daemon notification

from cysystemd.daemon import notify, Notification

# Send READY=1
notify(Notification.READY)

# Send status
notify(Notification.STATUS, "I'm fine.")

# Send stopping
notify(Notification.STOPPING)

Write message into systemd journal

from cysystemd import journal


journal.write("Hello Lennart")

# Or send structured data
journal.send(
    message="Hello Lennart",
    priority=journal.Priority.INFO,
    some_field='some value',
)

Reading journald

Reading all systemd records

from cysystemd.reader import JournalReader, JournalOpenMode

journal_reader = JournalReader()
journal_reader.open(JournalOpenMode.SYSTEM)
journal_reader.seek_head()

for record in journal_reader:
   print(record.data['MESSAGE'])

Read only cron logs

from cysystemd.reader import JournalReader, JournalOpenMode, Rule


rules = (
   Rule("SYSLOG_IDENTIFIER", "CRON") &
   Rule("_SYSTEMD_UNIT", "crond.service") |
   Rule("_SYSTEMD_UNIT", "cron.service")
)

cron_reader = JournalReader()
cron_reader.open(JournalOpenMode.SYSTEM)
cron_reader.seek_head()
cron_reader.add_filter(rules)

for record in cron_reader:
   print(record.data['MESSAGE'])

Polling records

from cysystemd.reader import JournalReader, JournalOpenMode


reader = JournalReader()
reader.open(JournalOpenMode.SYSTEM)
reader.seek_tail()

poll_timeout = 255

while True:
   reader.wait(poll_timeout)

   for record in reader:
      print(record.data['MESSAGE'])

journald open modes

  • CURRENT_USER

  • LOCAL_ONLY

  • RUNTIME_ONLY

  • SYSTEM

  • SYSTEM_ONLY

from cysystemd.reader import JournalReader, JournalOpenMode

reader = JournalReader()
reader.open(JournalOpenMode.CURRENT_USER)

journald entry

JournalEntry class has some special properties and methods:

  • data - journal entry content (dict)

  • date - entry timestamp (datetime instance)

  • cursor - systemd identification bytes for this entry

  • boot_id() - returns bootid

  • get_realtime_sec() - entry epoch (float)

  • get_realtime_usec() - entry epoch (int microseconds)

  • get_monotonic_sec() - entry monotonic time (float)

  • get_monotonic_usec() - entry monotonic time (int microseconds)

  • __getitem__(key) - shoutcut for entry.data[key]

journald reader

JournalReader class has some special properties and methods:

  • open(flags=JournalOpenMode.CURRENT_USER) - opening journald with selected mode

  • open_directory(path) - opening journald from path

  • open_files(*filename) - opening journald from files

  • data_threshold - may be used to get or set the data field size threshold for data returned by fething entry data.

  • closed - returns True when journal reader closed

  • locked - returns True when journal reader locked

  • idle - returns True when journal reader opened

  • seek_head - move reader pointer to the first entry

  • seek_tail - move reader pointer to the last entry

  • seek_monotonic_usec - seeks to the entry with the specified monotonic timestamp, i.e. CLOCK_MONOTONIC. Since monotonic time restarts on every reboot a boot ID needs to be specified as well.

  • seek_realtime_usec - seeks to the entry with the specified realtime (wallclock) timestamp, i.e. CLOCK_REALTIME. Note that the realtime clock is not necessarily monotonic. If a realtime timestamp is ambiguous, it is not defined which position is sought to.

  • seek_cursor - seeks to the entry located at the specified cursor (see JournalEntry.cursor).

  • wait(timeout) - It will synchronously wait until the journal gets changed. The maximum time this call sleeps may be controlled with the timeout_usec parameter.

  • __iter__ - returns JournalReader object

  • __next__ - calls next() or raise StopIteration

  • next(skip=0) - returns the next JournalEntry. The skip parameter skips some entries.

  • previous(skip=0) - returns the previous JournalEntry. The skip parameter skips some entries.

  • skip_next(skip) - skips next entries.

  • skip_previous(skip) - skips next entries.

  • add_filter(rule) - adding filter rule. See read-only-cron-logs as example.

  • clear_filter - reset all filters

  • fd - returns a special file descriptor

  • events - returns EPOLL events

  • timeout - returns internal timeout

  • process_events() - After each poll() wake-up process_events() needs to be called to process events. This call will also indicate what kind of change has been detected.

  • get_catalog() - retrieves a message catalog entry for the current journal entry. This will look up an entry in the message catalog by using the “MESSAGE_ID=” field of the current journal entry. Before returning the entry all journal field names in the catalog entry text enclosed in “@” will be replaced by the respective field values of the current entry. If a field name referenced in the message catalog entry does not exist, in the current journal entry, the “@” will be removed, but the field name otherwise left untouched.

  • get_catalog_for_message_id(message_id: UUID) - works similar to get_catalog() but the entry is looked up by the specified message ID (no open journal context is necessary for this), and no field substitution is performed.

Asyncio support

Initial asyncio support for reading journal asynchronously.

AsyncJournalReader

Blocking methods were wrapped by threads. Method wait() use epoll on journald file descriptor.

import asyncio
import json

from cysystemd.reader import JournalOpenMode
from cysystemd.async_reader import AsyncJournalReader


async def main():
    reader = AsyncJournalReader()
    await reader.open(JournalOpenMode.SYSTEM)
    await reader.seek_tail()

    while await reader.wait():
        async for record in reader:
            print(
                json.dumps(
                    record.data,
                    indent=1,
                    sort_keys=True
                )
            )

if __name__ == '__main__':
    asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cysystemd-1.5.4.tar.gz (209.8 kB view details)

Uploaded Source

Built Distributions

cysystemd-1.5.4-cp311-cp311-manylinux_2_28_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

cysystemd-1.5.4-cp311-cp311-manylinux_2_28_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

cysystemd-1.5.4-cp310-cp310-manylinux_2_28_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

cysystemd-1.5.4-cp310-cp310-manylinux_2_28_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

cysystemd-1.5.4-cp39-cp39-manylinux_2_28_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

cysystemd-1.5.4-cp39-cp39-manylinux_2_28_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ ARM64

cysystemd-1.5.4-cp38-cp38-manylinux_2_28_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

cysystemd-1.5.4-cp38-cp38-manylinux_2_28_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.28+ ARM64

cysystemd-1.5.4-cp37-cp37m-manylinux_2_28_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.28+ x86-64

cysystemd-1.5.4-cp37-cp37m-manylinux_2_28_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.28+ ARM64

File details

Details for the file cysystemd-1.5.4.tar.gz.

File metadata

  • Download URL: cysystemd-1.5.4.tar.gz
  • Upload date:
  • Size: 209.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4.tar.gz
Algorithm Hash digest
SHA256 8c08ff5b4ec2bef8abbca72ea99f1fb180ce7786fbfd72003cca50709ee1c3c3
MD5 cfc9e02b12ec07cbfa74d7809c38a652
BLAKE2b-256 a6ec77e3f95c40498b3eac1126a40af1a8ef75d07ad32c3f4b016001bec0dd61

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp311-cp311-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e36f2b3766e7e2a6fb682591fe74d298be352ab4384d0b1105cff447d9b07214
MD5 2a8fee8a57c093b7bb6273cd60e150af
BLAKE2b-256 42b675cf78231a2419b834056e5ff911193dbe0c0a93da7ae0ebb4cc8101bc79

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp311-cp311-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp311-cp311-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 2.5 MB
  • Tags: CPython 3.11, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 bd1db82dfa5a6b8020b32547fbd03dd745fca3019e2b66c7c3d8fb2793cc9e77
MD5 ce03ce44188dbcfa3a276a3c37a205ed
BLAKE2b-256 f5117391956bd1f5386af13a1be7df638b7463d6b60d46208f4f5e2d8b9cc37e

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp310-cp310-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.10, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 47fc80de81fa72a7a292c7997d232c8d06cbef145d317b504fa0569088c0058e
MD5 b3bc49c05ad74ca77d9e2f45e296140b
BLAKE2b-256 2a532535c53f899f804c26acb27feee4d83f68383cdc10edbace5fec24155dce

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp310-cp310-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp310-cp310-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 2.5 MB
  • Tags: CPython 3.10, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a7fc9b2758100735b199104e2a88cdd67a0e0d01fb12d152f05cd81cbdc72ee5
MD5 25b49d7eb58fa46a5e355706159e117d
BLAKE2b-256 fd374c78768f3e1755d09e035da4df1d7a13fe31bd518aaa89f9c60de174061d

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp39-cp39-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.9, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5d5c88574c6d85f58c14c3c5c97b6149d94a2c332eba7bc3df4da7ece72f9fa0
MD5 f0fbd9ec6de009877bd968bdf41c1281
BLAKE2b-256 a48019b9d51a325b5789f09cd7282b2e870887c8faae1841ee5f65b4ea791b08

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp39-cp39-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp39-cp39-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 2.5 MB
  • Tags: CPython 3.9, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 453e99dc8f0394a00418b6b27b166831e15a4351ca942e17c5d0f21679f7210e
MD5 b85d2081b8180df462ade76e28c7d6d6
BLAKE2b-256 d83f08638daff14657eb77aeda6a2ed2f40cabb74f2c9c6f6a08795d0b66c16a

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp38-cp38-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp38-cp38-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: CPython 3.8, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1c5809a6046b971be81aa131310386fa57c46a0b4eb1a4f6f4a348fbbda46685
MD5 3808844e8d716c2bf7ae2584921e39c8
BLAKE2b-256 7784ec6fd1a487fd49194851d4c5a11e9defc9c6dc938c1ec9da89cb7a54e40e

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp38-cp38-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp38-cp38-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 2.5 MB
  • Tags: CPython 3.8, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp38-cp38-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 192b2617c7ba5941fde72b5714216573e9ecb9d25c94bc37d8a74119e38b86c7
MD5 77502b84ee46624cc9f861e607cc6546
BLAKE2b-256 2fffdfbdccb0711762578076c93abcf2d932e7b7f62c6bd4819cdeae8935edb4

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp37-cp37m-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp37-cp37m-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp37-cp37m-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 46b8f0ac26f41e7f1e1a40332e0cfbeb1f4db6348e13fb7c40a6c73a7ff551ba
MD5 d68bfd8b5ebcd6c0c63054e8459136dc
BLAKE2b-256 b9b4184fb009385ed8e32155ecac47c81abf19d8016571a316b713ac1b5d3ee7

See more details on using hashes here.

File details

Details for the file cysystemd-1.5.4-cp37-cp37m-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: cysystemd-1.5.4-cp37-cp37m-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 2.5 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cysystemd-1.5.4-cp37-cp37m-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5106222b6e5819dd58d6b3ded4240f757c94777cd1bcf0e2c09ac444320acf75
MD5 52dafe56cd9a5bde9c623bed281277ae
BLAKE2b-256 25e81b775d768ed529785c720a265060b11709e0a722b92f6d51515ec706dee9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page