簡體   English   中英

輸出正常運行時間以歸檔和累積

[英]Output Uptime to file and acumulate

大家好,我即時通訊試圖輕松獲取我的網絡計算機的累積正常運行時間數據。

本質上,我們需要將正常運行時間的總和保存到一個文件中,因此在季度末我們可以對此進行報告。

運行腳本時,我可以很容易地獲得正常運行時間,但是不確定如何節省每天機器的正常運行時間,因此我可以對其進行報告。

任何幫助表示贊賞。

如果要使用計算機本身監視的正常運行時間來執行此操作,則需要(或多或少)不斷更新日志,因為在系統崩潰或重啟后計數器會重置,因此您將丟失上次寫入日志之間的正常運行時間信息和崩潰/重啟。 更好的方法是實施系統監視解決方案(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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM