进行系统reboot的python脚本,清除dmesg/message/mce/sel. 检查cpu,内存,pcie设备。做os reboot ,reboot回来后保存dmesg/message/mcelog/sel和cpu,内存,pcie设备信息。清除dmesg等,做os reset 一直循环200圈。最后打印运行的结果 和pass fail情况
时间: 2024-01-22 14:03:17 浏览: 103
Python调用adb命令实现对多台设备同时进行reboot的方法
5星 · 资源好评率100%
以下是一个可能的实现,包括了对于清除和保存相关信息的函数以及主要的测试逻辑。为了方便测试,每次重启后会等待一段时间再进行检查,这个等待时间可以根据具体环境进行调整。
```python
import os
import subprocess
import time
def clear_logs():
subprocess.run(["dmesg", "-c"])
subprocess.run(["echo", ">", "/var/log/messages"])
subprocess.run(["echo", ">", "/var/log/mcelog"])
subprocess.run(["echo", ">", "/var/log/audit/audit.log"])
subprocess.run(["ipmitool", "sel", "clear"])
def save_logs(path):
subprocess.run(["dmesg", "-T", "--level=err,warn,info,debug", "-s", "1000000", ">", os.path.join(path, "dmesg.log")])
subprocess.run(["cp", "/var/log/messages", os.path.join(path, "messages.log")])
subprocess.run(["cp", "/var/log/mcelog", os.path.join(path, "mcelog.log")])
subprocess.run(["ipmitool", "sel", "elist", ">", os.path.join(path, "sel.log")])
subprocess.run(["lscpu", ">", os.path.join(path, "cpu.log")])
subprocess.run(["lspci", "-vvv", ">", os.path.join(path, "pcie.log")])
def check_system():
# 检查CPU和内存
# TODO: 实现检查CPU和内存的代码
# 检查PCIe设备
# TODO: 实现检查PCIe设备的代码
def reboot_and_check():
clear_logs()
check_system()
subprocess.run(["reboot"])
# 等待重启完成后再进行检查
time.sleep(60)
check_system()
save_logs("reboot_logs")
def reset_and_check():
clear_logs()
check_system()
subprocess.run(["systemctl", "reset", "cold"])
# 等待重置完成后再进行检查
time.sleep(60)
check_system()
save_logs("reset_logs")
def run_tests():
num_pass = 0
num_fail = 0
for i in range(200):
try:
reboot_and_check()
reset_and_check()
num_pass += 1
print(f"Iteration {i}: PASS")
except Exception as e:
num_fail += 1
print(f"Iteration {i}: FAIL")
print(str(e))
print(f"Results: {num_pass} PASS, {num_fail} FAIL")
if __name__ == "__main__":
run_tests()
```
需要注意的是,由于具体检查CPU、内存和PCIe设备的方法可能因系统而异,上述代码中并没有实现这些部分的代码。这部分需要根据具体的环境和需求进行编写。
阅读全文