Giter Club home page Giter Club logo

jctlfmt's Introduction

jctlfmt

GitHub main workflow PyPI

๐Ÿ Python Journalctl parsing and formatting library.

With this library you can create custom scripts to parse, filter and transform data from Journalctl in a human-friendly format.

Installation

This library is available as a Python package on PyPI:

pip3 install jctlfmt

Usage

You can use the jctlfmt.Entry class in your code to parse Journalctl messages from JSON format and then you can create your own formatter class by extending jctlfmt.BaseFormatter to filter and print the entries in the format you like.

I have put a full usage example in the example folder of this repo. To try it you can use the following commands:

ssh [email protected] "sudo journalctl -ojson --output-fields _SOURCE_REALTIME_TIMESTAMP,__REALTIME_TIMESTAMP,_HOSTNAME,_SYSTEMD_UNIT,SYSLOG_IDENTIFIER,_PID,PRIORITY,MESSAGE -S '1 day ago'" > example/step01-json.txt
python3 example/dedup.py < example/step01-json.txt > example/step02-dedup.txt
python3 example/fmt.py < example/step02-dedup.txt > example/step03-fmt.txt

As you can see I have two custom scripts: dedup.py, which removes duplicate lines based on custom rules, and fmt.py which does the actual filtering and formatting.

In alternative, you can also use fmt.py to explore Journalctl logs on the go:

sudo journalctl -ojson -ussh -S '1 day ago' | python3 example/fmt.py -fs | less

As you can see, this time I invoked the script with -fs to disable filtering and sensitive mode. See jctlfmt.BaseFormatter for details.

You can customize fmt.py as you want and then use it in your setup. If you make modifications to the script and you want to test if the output is consistent, you can generate the output for all the possible invocations and then use sha256sum to check. The following commands may help:

for i in '' -f -fs -s; do
    python3 example/fmt.py $i < example/step02-dedup.txt > "example/step03-arg$i.txt"
done
sha256sum example/step03-arg*.txt | sha256sum

Development

If you want to contribute to this project, you can install the package in editable mode:

pip3 install -e . --user

This will just link the package to the original location, basically meaning any changes to the original package would reflect directly in your environment (source).

jctlfmt's People

Contributors

dependabot[bot] avatar dmotte avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.