【问题标题】:Systemd journal access with python API使用 python API 访问 Systemd 日志
【发布时间】:2019-11-07 17:04:05
【问题描述】:

我使用 Ubuntu 18.04,我想在 python 脚本中阅读我的日记。 作为用户,我可以使用命令阅读日志

journaltctl -f -u foo.service

我想用python读取最后一行:

第一次尝试:

$ python
>>> from systemd import journal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'systemd'

好的,我当然需要 API:

$ sudo apt install python-systemd
$ python
>>> from systemd import journal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'journal'

从这里,我无法弄清楚为什么它不起作用。

第三次尝试:

$ sudo python
>>> from systemd import journal
>>>

好的,这似乎是一个权限问题。然后我将当前用户添加到systemd-journal 组:

$ sudo usermod -aG systemd-journal user
<logout/login>
$ groups user
user : user adm cdrom sudo dip plugdev systemd-journal lpadmin sambashare docker

然后

$ python
>>> from systemd import journal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'journal'

来自documentation

请注意,为了访问系统日志,非 root 用户必须具有必要的权限,详情请参阅 journalctl(1)。非特权用户只能访问自己的日志。

所以至少我希望能够导入journal 模块并阅读user 日志?

【问题讨论】:

    标签: python systemd


    【解决方案1】:

    你确定python指的是系统python吗?如果您在虚拟环境或其他 Python 二进制文件中运行,它不会看到使用 apt 安装的包。

    我有一个 Ubuntu 18.04 系统:

    root@cff463fdd322:/# cat /etc/os-release
    NAME="Ubuntu"
    VERSION="18.04.3 LTS (Bionic Beaver)"
    [...]
    

    如果我安装python-systemd 包:

    root@cff463fdd322:/# apt install python-systemd
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following additional packages will be installed:
      file libexpat1 libmagic-mgc libmagic1 libpython-stdlib libpython2.7-minimal
      libpython2.7-stdlib libreadline7 libsqlite3-0 libssl1.1 mime-support python
      python-minimal python2.7 python2.7-minimal readline-common xz-utils
    Suggested packages:
      python-doc python-tk python2.7-doc binutils binfmt-support readline-doc
    The following NEW packages will be installed:
      file libexpat1 libmagic-mgc libmagic1 libpython-stdlib libpython2.7-minimal
      libpython2.7-stdlib libreadline7 libsqlite3-0 libssl1.1 mime-support python
      python-minimal python-systemd python2.7 python2.7-minimal readline-common xz-utils
    0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded.
    Need to get 6440 kB of archives.
    After this operation, 29.0 MB of additional disk space will be used.
    Do you want to continue? [Y/n] y
    [...]
    

    我可以毫无问题地导入systemd.journal 模块:

    root@cff463fdd322:/# python
    Python 2.7.15+ (default, Oct  7 2019, 17:39:04)
    [GCC 7.4.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from systemd import journal
    >>>
    

    【讨论】:

    • 确实,我是在虚拟环境中运行的,有可能吗?我可以导入systemd,但不能导入systemd.journal
    • 在文档中,它提到“该项目也可以在 pypi 上作为 systemd-python 使用”所以我希望在虚拟环境中使用 pip 安装它时能够导入包?
    【解决方案2】:

    终于找到解决办法了!

    正如@larsks 所指出的,我尝试在虚拟环境中导入包。如文档中所述:

    该项目也可以在 pypi 上作为 systemd-python 使用。

    但为了能够正确安装,我也需要安装libsystemd-dev

    $ sudo apt install libsystemd-dev
    

    然后我可以安装 PyPi 包

    $ pip install systemd-python
    

    【讨论】:

      【解决方案3】:

      可能就像安装python3-systemd而不是python-systemd一样简单

      $ python
      Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
      [GCC 8.3.0] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> from systemd import journal
      >>> j = journal.Reader()
      >>> j.this_boot()
      >>> j.log_level(journal.LOG_INFO)
      >>> j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
      >>> for entry in j:
      ...     print(entry['MESSAGE'])
      ... 
      checking bus 1, device 9: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4.4/1-4.4.2"
      bus: 1, device: 9 was not an MTP device
      link_config: autonegotiation is unset or enabled, the speed and duplex are not writable.
      link_config: autonegotiation is unset or enabled, the speed and duplex are not writable.
      checking bus 1, device 2: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1"
      bus: 1, device: 2 was not an MTP device
      checking bus 1, device 3: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-2"
      bus: 1, device: 3 was not an MTP device
      

      【讨论】:

      • 两者都安装了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 2014-12-07
      • 2022-12-28
      • 2010-12-08
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多