Created
May 1, 2017 17:18
-
-
Save FergusInLondon/6e3defea076fb0666af6862db0375740 to your computer and use it in GitHub Desktop.
Use dbus to parse system problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fergus In London - https://fergus.london <[email protected]> | |
# | |
# Uses the org.freedesktop.problems dbus channel to read out reported problems. | |
# | |
# Requires pydbus from pip. | |
# Requires python-gobject and glib installed on your distro. | |
# | |
import sys | |
import argparse | |
from pydbus import SystemBus | |
from datetime import datetime | |
from gi.repository import GLib | |
# | |
# Display a problem in the following format: | |
# "[TIME - USERNAME - PID] EXECUTABLE_LOCATION" | |
# | |
def parse_problem(problem_data): | |
time = problem_data.get("time", ("","", False))[2] | |
executable = problem_data.get("executable", ("","", False))[2] | |
pid = problem_data.get("global_pid", ("", "", "Unavailable"))[2] | |
user = problem_data.get("username", ("", "", "Unavailable"))[2] | |
time_string = "Unavailable" if not time else datetime.fromtimestamp( | |
int(time) | |
).strftime('%Y-%m-%d %H:%M:%S') | |
if executable: | |
print("[%s - %s - %s]\t %s" % (time_string, user, pid, executable)) | |
# | |
# GLib listener - pass object to parse_problem() | |
# | |
def problem_listener(pr): | |
print("Recieved new error.") | |
parse_problem(pr) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Problem Browser") | |
parser.add_argument('-d', '--display', action="store_true", help="display all logged problems") | |
parser.add_argument('-l', '--listen', action="store_true", help="listen for new problems") | |
args = parser.parse_args() | |
bus = SystemBus() | |
problems = bus.get('.problems') | |
# Iterate over all available problems and output them to the user. | |
if args.display or len(sys.argv) <= 1: | |
print("Previously logged errors:") | |
for problem in problems.GetAllProblems(): | |
parse_problem( problems.GetProblemData(problem) ) | |
# Configure GLib event loop, and listen for dbus errors | |
if args.listen: | |
print("Listening for new errors:") | |
problems.onJobNew = problem_listener | |
GLib.MainLoop().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment