嘿伙计们,我正在尝试轻松获取我的网络机器的累积正常运行时间数据。
本质上,我们需要将正常运行时间的总和保存到一个文件中,因此在本季度末我们可以对此进行报告。
我可以在运行脚本时轻松获得正常运行时间,但不确定如何去做一个可以每天节省机器正常运行时间的方法,以便我可以报告它。
任何帮助表示赞赏。
如果您想使用机器本身监控的正常运行时间来执行此操作,您需要(或多或少)持续更新您的日志,因为计数器在系统崩溃或重新启动后重置,您将丢失上次日志写入之间的正常运行时间信息和崩溃/重启。更好的方法是实施系统监控解决方案(Nagios、Zabbix、WhatsUp、SCOM 等)。这些工具持续跟踪系统正常运行时间并已提供您可以使用的报告。
如果您仍想走“本地脚本”路线,则每 5 分钟运行一次类似以下的计划任务可能会起作用:
Set fso = CreateObject("Scripting.FileSystemObject")
Set wmi = GetObject("winmgmts://./root/cimv2")
Set re = New RegExp
logfile = "C:\path\to\uptime.log"
uptimes = fso.OpenTextFile(logfile, 1, True).ReadAll
'extract last uptime from log
re.Pattern = "(\d+)\s*$"
For m In re.Execute(uptimes)
lastUptime = CLng(m.SubMatches(0))
Next
'get current uptime
qry = "SELECT * FROM Win32_PerfFormattedData_PerfOS_System"
For Each v In wmi.ExecQuery(qry)
currentUptime = CLng(v.SystemUpTime)
Next
Set f = fso.OpenTextFile(logfile, 2)
If IsEmpty(lastUptime) Or lastUptime >= currentUptime Then
'append current uptime if the last uptime was greater or equal to than the
'current uptime (=> a crash or reboot occurred) or no uptime was logged before
f.WriteLine uptimes & currentUptime
Else
'update last uptime otherwise
f.WriteLine re.Replace(uptimes, currentUptime)
End If
f.Close