Skip to main content

使用 Python 与 Linux 看门狗驱动程序交互。

项目描述

看门狗

Python 版本 派皮 代码风格:黑色 执照

什么是看门狗?

看门狗(或看门狗定时器,WDT)是一个硬件组件,如果没有从用户空间定期通知它,它会重新启动您的系统,通常是由一个守护进程通知。如果用户空间因任何原因出现故障,看门狗将停止收到通知,当超时发生时,它将重新启动系统。

例如,当您有一个无法物理访问的远程系统,并且您希望确保它不会冻结并且在您再次获得物理访问之前无限期地变得不可用时,它会很有用。

要求

  • 具有 root 权限的 Linux
  • 看门狗(请参阅我有看门狗吗?以获得基本帮助)
  • Python 3.7+

安装

使用pip

sudo -H pip install pywatchdog

对于特定的 Python 版本,如果您有多个:

sudo -H python3.x -m pip install pywatchdog

直接从存储库中获取最新更改:

sudo -H pip install git+https://github.com/AT0myks/pywatchdog
sudo -H python3.x -m pip install git+https://github.com/AT0myks/pywatchdog

用法

API

推荐的用法是使用上下文管理器:

from pywatchdog import Watchdog
with Watchdog() as wdt:
    ...

Watchdog接受一个默认为 的参数/dev/watchdog,这在大多数情况下应该可以工作。

您也可以手动openclose文件:

wdt = Watchdog()
wdt.open()
...
wdt.close()

一旦文件打开,如果在超时发生前没有 ping 通看门狗,计算机将重新启动。

要 ping 看门狗,请使用以下keep_alive方法:

wdt.keep_alive()

可以查询超时,对于某些驱动程序,它也可以即时修改:

print("The timeout is", wdt.timeout, "seconds")
wdt.timeout = 45  # Has no effect if unsupported.
print("The timeout is now", wdt.timeout, "seconds")

请注意,某些设备的超时时间以分钟为单位。在这种情况下,上面的示例将打印The timeout is now 60 seconds.

一些看门狗驱动程序能够报告系统重启前的剩余时间:

print("Time left before reboot:", wdt.time_left, "seconds")  # Prints None if unsupported.

这是其余的属性。您唯一可以设置的是pretimeout

print(wdt.identity)  # A string identifying the watchdog driver. Example: 'iTCO_wdt'.
print(wdt.firmware_version)  # Shortcut for wdt.support.firmware_version.
print(wdt.options)  # A list of flags describing what the device supports.
print(wdt.pretimeout)  # Returns the pretimeout in seconds.
wdt.pretimeout = 15  # Seconds. Set to 0 to disable. Unsettable for some drivers.
print(wdt.support)  # Returns an instance of watchdog_info.
print(wdt.status)  # Current status, not always supported.
print(wdt.boot_status)  # Status at the last reboot, not always supported.
print(wdt.temperature)  # In Fahrenheit, not always supported.

如果驱动程序不支持某个属性,None则返回。

可以启用和禁用一些看门狗:

wdt.enable()  # Turn on the watchdog timer.
wdt.disable()  # Turn off the watchdog timer.

当系统过热时,一些驱动程序能够导致内核崩溃:

wdt.temp_panic()

启用此选项后,如果不删除内核模块并读取它,就无法禁用它。此外,无法查询其状态。

有关属性和方法的更多信息,请参阅文档字符串。

命令行界面

这个包还提供了一个pywatchdog命令。它有两个子命令:

find-module

获取系统的潜在内核看门狗驱动程序列表。

usage: pywatchdog find-module [-a]

optional arguments:
  -a, --all   only look for the presence of /dev/watchdog

这是通过尝试每个模块/lib/modules/$(uname -r)/kernel/drivers/watchdog并测试哪些模块在插入时会导致 和 的可用性来完成/dev/watchdog/dev/watchdog0。指定选项时--all,它只查找/dev/watchdog.

由于我不知道拥有两者之间的区别,因此提供/dev/watchdog了该--all选项以防万一。

softdog模块总是被忽略,因为它是一个软件看门狗,无论如何都应该适用于所有系统。如果可能,您应该始终使用硬件看门狗。

test-reboot

测试看门狗是否正确地重新启动您的计算机。

usage: pywatchdog test-reboot [-t TIMEOUT] [-d DEVICE]

optional arguments:
  -t TIMEOUT, --timeout TIMEOUT    the timeout that will be set for the test
  -d DEVICE, --device DEVICE       default: /dev/watchdog

这将在不通知看门狗的情况下打开设备文件,这意味着当超时发生时,计算机应该重新启动。

将显示重新启动前的倒计时。确保在使用此命令之前保存您的工作。

例子

此示例假设您使用的是systemd.

提供了两个文件来创建示例守护程序:脚本systemd 服务文件。该脚本是一个简单的无限循环,每秒 ping 看门狗。服务文件以 root 身份运行脚本,并确保它在失败时重新启动,它也可用于在启动时加载看门狗模块。

在这个例子中,我们在用户的主目录中ben,看门狗模块是iTCO_wdt.

下载两个文件:

wget https://github.com/AT0myks/pywatchdog/blob/main/example.py
wget https://github.com/AT0myks/pywatchdog/blob/main/example.service

让我们将它们重命名为更有意义的名称:

mv example.py pywatchdog.py
mv example.service pywatchdog.service

使脚本可执行:

chmod +x pywatchdog.py

将服务文件移动到/etc/systemd/system/

sudo mv -i pywatchdog.service /etc/systemd/system/

编辑[Service]服务文件的部分

sudo systemctl edit --full pywatchdog

并指定脚本的正确路径:

ExecStart=/home/ben/pywatchdog.py

如果在启动时没有加载看门狗模块(对于 Raspberry Pi 来说应该不是这种情况),您可以通过在下面添加这两行来使用服务文件来执行此操作(请参阅本节底部的注释)[Service]

ExecStartPre=/usr/sbin/modprobe iTCO_wdt
ExecStopPost=/usr/sbin/modprobe -r iTCO_wdt

你可以找到modprobewith的绝对路径which modprobe

现在启用(在启动时自动启动)并启动看门狗守护进程:

sudo systemctl enable pywatchdog
sudo systemctl start pywatchdog

您现在可以看到守护程序的状态:

systemctl status pywatchdog

及其日志:

journalctl -u pywatchdog

注意:由于看门狗模块的硬黑名单(至少在 Ubuntu 上),我们使用服务文件在启动时加载模块,而不是常规方式(如/etc/modules)。例如,请参见此处此处

我有看门狗吗?

如果你有一个/dev/watchdog文件,那么是的。例如,如果您正在使用 Raspberry Pi,则应该已经是这种情况。您也可以通过键入命令进行检查sudo wdctl。如果你得到wdctl: cannot open /dev/watchdog: No such file or directory,请继续阅读。

如果您没有/dev/watchdog文件,则很可能您有一个看门狗,但需要找到合适的内核模块来为您的特定硬件加载。在我的情况下工作的模块适用于sp5100_tco具有(相当近的,如果重要的话)AMD硬件的计算机和iTCO_wdt另一个具有英特尔硬件的计算机。找到模块后,使用sudo modprobe <module>.

您也可以使用该pywatchdog find-module命令。

如果您没有守护程序馈送看门狗模块,则应避免加载它,以防止意外重启。

贡献

欢迎您为这个项目做出贡献。如果您遇到任何错误或想要添加/修复某些内容,请随时打开问题或拉取请求。

基于

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

pywatchdog-1.0.0.tar.gz (19.3 kB 查看哈希

已上传 source

内置分布

pywatchdog-1.0.0-py3-none-any.whl (17.3 kB 查看哈希

已上传 py3