基本思路
现在有这么一个需求需要实现自动化:需要实时获取服务器cpu,gpu温度以及传感器信息上报情况,对高低温环境下对于设备运行状态的影响进行测试。基本思路为利用paramiko ssh到服务器上,起一个线程用tail -f命令实时获取日志输出,起另外一个线程用‘cat /sys/class/thermal/thermal_zone0/temp'命令定时获取cpu,gpu温度。
代码
def get_report_info_perid(self, cmd, diff_time, thre_time):
# 发送要执行的命令
pre_time_stamp = [0] * 4
self._channel.send(cmd + '\r')
# 回显很长的命令可能执行较久,通过循环分批次取回回显
time_stamp_arr = []
index = [0] * 4
current_line = b''
line_counter = 0
line_feed_byte = '\n'.encode(self.encoding)
while true:
buffer = self._channel.recv(1)
if len(buffer) == 0:
logger.info('end______________')
break
current_line += buffer
if buffer == line_feed_byte:
line = current_line