为了在 Windows 机器上监控 FSMLabs Timekeeper (TK),我使用 PowerShell 脚本输出系统时间、TK 记录时间(按时区)和 TK 日志的偏移量(以毫秒为单位)。
然后,我在 ITRS Geneos“工具包”采样器中运行此脚本以显示此类指标并设置规则以在偏移 > x 毫秒时触发警报。
现在我想在 Unix 机器上放置相同的监控,所以我决定将 PowerShell 脚本翻译成 Python3。
您能否建议下面的 Python sintax 是否正确,或者是否有更好的方法来实现相同的结果?
电源外壳:
'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)