cysystemd

systemd wrapper on Cython

https://github.com/mosquito/cysystemd

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 16 committers (6.3%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.5%) to scientific vocabulary

Keywords

cython journald python python-2 python-3 python-3-5 python-3-6 systemd wrapper
Last synced: 6 months ago · JSON representation

Repository

systemd wrapper on Cython

Basic Info
  • Host: GitHub
  • Owner: mosquito
  • License: apache-2.0
  • Language: Cython
  • Default Branch: master
  • Homepage:
  • Size: 188 KB
Statistics
  • Stars: 103
  • Watchers: 4
  • Forks: 24
  • Open Issues: 1
  • Releases: 21
Topics
cython journald python python-2 python-3 python-3-5 python-3-6 systemd wrapper
Created over 9 years ago · Last pushed 7 months ago
Metadata Files
Readme License

README.md

pypi version License

systemd wrapper in Cython

Python systemd wrapper using Cython.

Installation

About Binary Wheels Distribution

Historically, cysystemd was not distributed via wheels due to systemd versioning challenges. While the libsystemd headers remain relatively stable, the ABI can vary between different OS versions and distributions. Previous attempts to distribute wheels resulted in compatibility issues across different Linux systems. Currently, we use the manylinux_2_34 format for wheel distribution, which bundles the necessary shared objects (.so files) required for operation.

This approach should provide compatibility with modern systemd installations.

However, if you encounter any compatibility issues, we strongly recommend installing the package from source code instead.

bash pip install --no-binary=:all: cysystemd

Installation from PyPI

Once the system dependencies are installed, you can install cysystemd: shell pip install cysystemd

Installation from Source

You must install the systemd development headers (libsystemd) before installation! Without these headers, the installation will fail.

For Debian/Ubuntu users: shell apt install build-essential libsystemd-dev

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

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

For CentOS/RHEL: shell yum install gcc systemd-devel

BREAKING CHANGES in v2.0.0

AsyncJournalReader Changes

  1. Major refactoring of the AsyncJournalReader iterator implementation:

    • Removed internal queue and threading-based implementation
    • Now uses direct async iteration through journal events
    • More reliable handling of journal invalidation events
    • Simpler and more efficient implementation
  2. wait() method now returns JournalEvent instead of boolean

    • Returns specific event type (JournalEvent.APPEND, JournalEvent.INVALIDATE, JournalEvent.NOP)
    • Better error handling and event processing

Type Annotations

  • Added comprehensive type hints throughout the codebase
  • Added return type annotations for all public methods
  • Enhanced IDE support and code documentation

API Behavior Changes

  • seek_tail() now automatically calls previous() to ensure cursor is positioned correctly
  • Improved error handling and validation in various methods
  • More consistent return types across the API

Python Support

  • Added support for Python 3.13
  • Maintained support for Python 3.8-3.12

Dependency Changes

  • Requires latest libsystemd development headers
  • Binary wheels are no longer distributed (see "Why binary wheels are no longer distributed" above)

Please ensure your code is updated to handle these changes when upgrading to version 2.0.0.

Usage examples

Writing to journald

Logging handler for python logger

```python 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

```python 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:

```python 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

```python

from cysystemd.reader import JournalReader, JournalOpenMode

journalreader = JournalReader() journalreader.open(JournalOpenMode.SYSTEM) journalreader.seekhead()

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

Read only cron logs

```python from cysystemd.reader import JournalReader, JournalOpenMode, Rule

rules = ( Rule("SYSLOGIDENTIFIER", "CRON") & Rule("SYSTEMDUNIT", "crond.service") | Rule("SYSTEMD_UNIT", "cron.service") )

cronreader = JournalReader() cronreader.open(JournalOpenMode.SYSTEM) cronreader.seekhead() cronreader.addfilter(rules)

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

Polling records

```python 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 - deprecated alias of SYSTEM

```python 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.

```python 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()

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

if name == 'main': asyncio.run(main()) ```

Owner

  • Name: Mosquito
  • Login: mosquito
  • Kind: user
  • Company: Yandex

GitHub Events

Total
  • Create event: 4
  • Release event: 1
  • Issues event: 9
  • Watch event: 1
  • Delete event: 1
  • Issue comment event: 5
  • Push event: 13
  • Pull request review event: 1
  • Pull request event: 6
  • Fork event: 2
Last Year
  • Create event: 4
  • Release event: 1
  • Issues event: 9
  • Watch event: 1
  • Delete event: 1
  • Issue comment event: 5
  • Push event: 13
  • Pull request review event: 1
  • Pull request event: 6
  • Fork event: 2

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 138
  • Total Committers: 16
  • Avg Commits per committer: 8.625
  • Development Distribution Score (DDS): 0.181
Past Year
  • Commits: 6
  • Committers: 1
  • Avg Commits per committer: 6.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Dmitry Orlov me@m****u 113
Nikolay Agarin n****n@a****m 5
Jeff Dairiki d****i@d****g 3
Brett Smith b****h@b****g 3
Andrew Grigorev a****w@e****u 2
mmill m****r@g****m 2
glM26 3****6 1
drfruct d****t@g****m 1
Yoan Blanc y****n@d****h 1
Mudrykaa m****a@g****m 1
Karthikeyan Singaravelan t****i@g****m 1
Joerg Behrmann b****n@p****e 1
Chris Oelerich c****s@o****h 1
Sébastien Barthélémy s****y@a****m 1
Sergey Naumenko S****o@a****m 1
Michael Klemm M****m@g****m 1

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 35
  • Total pull requests: 37
  • Average time to close issues: about 1 year
  • Average time to close pull requests: 22 days
  • Total issue authors: 35
  • Total pull request authors: 19
  • Average comments per issue: 2.03
  • Average comments per pull request: 0.81
  • Merged pull requests: 33
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 6
  • Average time to close issues: 2 days
  • Average time to close pull requests: 14 days
  • Issue authors: 2
  • Pull request authors: 2
  • Average comments per issue: 1.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 5
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • mike-twa (1)
  • UrbanskiDawid (1)
  • hahai-vaisala (1)
  • 4l1fe (1)
  • mastykin (1)
  • alex-zywicki (1)
  • greut (1)
  • riwoodward (1)
  • p-doyle (1)
  • Cap-JaTu (1)
  • multani (1)
  • jcormier (1)
  • shatteredsword (1)
  • aj-anet (1)
  • BoboTiG (1)
Pull Request Authors
  • mosquito (13)
  • brettcs (3)
  • greut (3)
  • nagarin (2)
  • mudrykaa (2)
  • smurfix (2)
  • sbarthelemy (1)
  • agronholm (1)
  • tirkarthi (1)
  • sapristi (1)
  • MichaelKlemm (1)
  • ei-grad (1)
  • xray33 (1)
  • glM26 (1)
  • ioistired (1)
Top Labels
Issue Labels
bug (1) enhancement (1)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 69,485 last-month
  • Total dependent packages: 6
    (may contain duplicates)
  • Total dependent repositories: 58
    (may contain duplicates)
  • Total versions: 54
  • Total maintainers: 1
pypi.org: cysystemd

systemd wrapper in Cython

  • Versions: 22
  • Dependent Packages: 6
  • Dependent Repositories: 10
  • Downloads: 46,570 Last month
Rankings
Downloads: 1.7%
Dependent packages count: 2.4%
Dependent repos count: 4.6%
Average: 4.8%
Stargazers count: 7.3%
Forks count: 8.1%
Maintainers (1)
Last synced: 6 months ago
pypi.org: systemd

systemd wrapper in Cython

  • Versions: 32
  • Dependent Packages: 0
  • Dependent Repositories: 48
  • Downloads: 22,915 Last month
Rankings
Downloads: 2.1%
Dependent repos count: 2.1%
Average: 5.9%
Stargazers count: 7.3%
Forks count: 8.1%
Dependent packages count: 10.1%
Maintainers (1)
Last synced: 6 months ago

Dependencies

setup.py pypi