Skip to content

Instantly share code, notes, and snippets.

@moyix
Created February 13, 2019 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moyix/7a2a2f891aef177196442f071a178099 to your computer and use it in GitHub Desktop.
Save moyix/7a2a2f891aef177196442f071a178099 to your computer and use it in GitHub Desktop.
Small parser using Construct for the Linux kernel log buffer
#!/usr/bin/env python
from datetime import timedelta
import sys
from construct import *
Message = Aligned(4, Struct(
"ts_nsec" / Int64ul,
"length" / Int16ul,
"text_len" / Int16ul,
"dict_len" / Int16ul,
"facility" / Int8ul,
"bits" / Bitwise(Struct(
"flags" / BitsInteger(5),
"level" / BitsInteger(3),
)),
"text" / PaddedString(this.text_len, "utf8"),
"dictionary" / PaddedString(this.dict_len, "utf8"),
))
dmesg = GreedyRange(Message)
if __name__ == "__main__":
for entry in dmesg.parse_file(sys.argv[1]):
if entry.text_len:
print timedelta(seconds=entry.ts_nsec/1000000000), entry.text
if entry.dict_len:
for kvp in entry.dictionary.split("\x00"):
print " ",kvp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment