pho*_*tal 4 python journal systemd
我试图使用systemd库在Python中模拟这个shell命令 http://www.freedesktop.org/software/systemd/python-systemd/journal.html
我实际上是试图在Python中模拟这个命令.
journalctl --since=-5m --no-pager
Run Code Online (Sandbox Code Playgroud)
我已经看到其他人通过调用日志可执行文件在Python中执行此操作,但这是一个非常糟糕的方法.
我根据上面链接的文档编写了这个简单的脚本
import select
from systemd import journal
j = journal.Reader()
j.log_level(journal.LOG_INFO)
# j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
j.seek_tail()
j.get_next()
while j.get_next():
for entry in j:
if entry['MESSAGE'] != "":
print(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])
Run Code Online (Sandbox Code Playgroud)
这里有一些问题
理想情况下,我希望仅根据一组过滤器/匹配来跟踪此日志,以模拟命令'journalctl -f',但我只需要首先解决此问题.我想最终得到这样的东西,但它也不起作用.
import select
from systemd import journal
j = journal.Reader()
j.log_level(journal.LOG_INFO)
# j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
j.seek_tail()
p = select.poll()
p.register(j, j.get_events())
while p.poll():
while j.get_next():
for entry in j:
if entry['MESSAGE'] != "":
print(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])
Run Code Online (Sandbox Code Playgroud)
我也在研究类似的python模块.
根据以下链接,我们必须调用sd_journal_previous(在python systemd模块中,即journal.Reader().get_previous()).
http://www.freedesktop.org/software/systemd/man/sd_journal_seek_tail.html
https://bugs.freedesktop.org/show_bug.cgi?id=64614
此外,您的示例代码将占用80-100%的CPU负载,因为即使在获得条目后,读取器的状态仍然是"可读"的,这导致了太多的poll().
根据以下链接,似乎我们必须在每次poll()之后调用sd_journal_process(在python systemd模块中,即journal.Reader().process()),以便重置文件描述符的可读状态.
http://www.freedesktop.org/software/systemd/man/sd_journal_get_events.html
总之,您的示例代码是,
import select
from systemd import journal
j = journal.Reader()
j.log_level(journal.LOG_INFO)
# j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
j.seek_tail()
j.get_previous()
# j.get_next() # it seems this is not necessary.
p = select.poll()
p.register(j, j.get_events())
while p.poll():
if j.process() != journal.APPEND:
continue
# Your example code has too many get_next() (i.e, "while j.get_next()" and "for event in j") which cause skipping entry.
# Since each iteration of a journal.Reader() object is equal to "get_next()", just do simple iteration.
for entry in j:
if entry['MESSAGE'] != "":
print(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])
Run Code Online (Sandbox Code Playgroud)
编辑:在j.get_previous()之后删除了"j.get_next()".
| 归档时间: |
|
| 查看次数: |
5064 次 |
| 最近记录: |