将 Timekeeper Windows 主机 PowerShell 脚本转换为 Linux 主机的 Python3

问题描述

为了在 Windows 机器上监视 FSMLabs Timekeeper (TK),我使用 PowerShell 脚本从 TK 日志中输出系统时间、TK 记录时间(按时区)和偏移量(以毫秒为单位)。

然后我在 ITRS Geneos“工具包”采样器中运行此脚本以显示此类指标并设置规则以在偏移量 > x 毫秒时触发警报。

现在我想在 Unix 机器上进行相同的监控,所以我决定将 PowerShell 脚本翻译成 python3

您能否建议以下 Python 语法是否正确,或者是否有更好的方法来实现相同的结果?

PowerShell:

'SysTimestamp,TK_Timestamp,Offset';
$source = Get-Content "C:\Program Files\timekeeper\var\lib\timekeeper" | Select-String "Primary Source Number:"
$source = $source -replace "Primary Source Number:\s+"
 
$log = (Get-Content "C:\Program Files\timekeeper\var\log\timekeeper_$source.data" -Tail 1) -split " "
$offset = [double]$log[1]
$offset = $offset * 1000
$tkstamp = $log[0]
$tktime = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($tkstamp))
 
$Now=Get-Date
Write-Host "$Now,$tktime,$offset "

python3

import pathlib
import time
import subprocess
from datetime import datetime,timedelta

source=pathlib.Path('/var/lib/timekeeper')
pattern = "Primary Source Number:"

for file in source.glob(pattern)
                source = source + ""

logfile = "/var/log/timekeeper/timekeeper_"+source+".data"
lastline = subprocess.check_output(['tail','-1',logfile])
log = lastline.split('" "')

offset= float(log[1])

offset= offset * 1000

tkstamp = log[0]

Now = datetime.Now()

tktime= Now + datetime.timedelta(0,tkstamp)

print(Now,tktime,offset)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)