Created
February 13, 2019 04:57
-
-
Save moyix/7a2a2f891aef177196442f071a178099 to your computer and use it in GitHub Desktop.
Small parser using Construct for the Linux kernel log buffer
This file contains 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
#!/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