自動收集遠端電腦資源資訊

最後一步自動去收集遠端電腦的資訊, 並且存放入資料庫內, 目前收集的資料有 (1) 電腦的實體記憶體 & 使用率, (2) CPU 使用率, (3) 硬碟大小及使用率

同樣的建立 Table 的 Script 如下:
CREATE TABLE OAUSER.DRV_DAILY_RECORD
(
DRV_DATE DATE NOT NULL,
COMPUTER_NAME VARCHAR2(100 CHAR) NOT NULL,
RESOURCE_TYPE VARCHAR2(20 CHAR) NOT NULL,
RESOURCE_NAME VARCHAR2(25 CHAR),
PERCENT_OF_USAGE NUMBER(4,2) NOT NULL,
MAX_VALUE NUMBER(25,2),
MIN_VALUE NUMBER(25,2) DEFAULT 0,
UOM VARCHAR2(20 CHAR)
)

使用的 Script
————————————————-
Option Explicit
On Error Resume Next

Dim objWMIService, colSettings, objComputer
Dim strComputer

‘ 定義 Oracle 連線變數
Dim connection, connectionString, theCommand, CmdStr

‘for standard query
const cnstCommand = 1

‘ 找到幾個 CPU
Dim i

‘ 如果沒有指定電腦則掃描本機
If Wscript.Arguments.Count = 0 then
Set wshShell = WScript.CreateObject(“WScript.Shell” )
strComputer = UCase(wshShell.ExpandEnvironmentStrings(“%COMPUTERNAME%”))
Else
strComputer = UCase(wscript.arguments(0))
End If

‘ 設定連線字串
connectionString = “DRIVER={Microsoft ODBC for Oracle};SERVER=OAUSER;User Id=OAUSER;Password=OAUSER;”
Set connection = CreateObject(“ADODB.Connection”)
Set theCommand = CreateObject(“ADODB.Command”)
connection.Open connectionString

Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootcimv2”)

‘抓取實體記憶體容量
Set colSettings = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”)
For Each objComputer in colSettings
CmdStr = “INSERT INTO WEBAP.DRV_DAILY_RECORD (DRV_DATE, COMPUTER_NAME, RESOURCE_TYPE, RESOURCE_NAME, PERCENT_OF_USAGE, MAX_VALUE, CUR_VALUE, UOM) VALUES (”
CmdStr = CmdStr & ” SYSDATE, ‘” & strComputer & “‘, ‘MEMORY’, ‘PHY_MEMORY’, ” & Round(((objComputer.TotalVisibleMemorySize – objComputer.FreePhysicalMemory) / objComputer.TotalVisibleMemorySize)*100, 2) & “, ” & round(objComputer.TotalVisibleMemorySize / 1024,2) & “, ” & round((objComputer.TotalVisibleMemorySize – objComputer.FreePhysicalMemory) / 1024,2) & “, ‘MB’ )”

‘執行 SQL 指令
thecommand.CommandText = CmdStr
thecommand.CommandType = cnstCommand
thecommand.ActiveConnection = connection
thecommand.Execute

WScript.Echo “Updating Physical Memory Info for ” & strComputer
Next

‘ 抓取 cpu0 的使用率
i = 0
Set colSettings = objWMIService.ExecQuery(“Select * from Win32_Processor”)
For Each objComputer in colSettings

CmdStr = “INSERT INTO WEBAP.DRV_DAILY_RECORD (DRV_DATE, COMPUTER_NAME, RESOURCE_TYPE, RESOURCE_NAME, PERCENT_OF_USAGE, MAX_VALUE, CUR_VALUE, UOM) VALUES (”
CmdStr = CmdStr & ” SYSDATE, ‘” & strComputer & “‘, ‘CPU’, ‘CPU” & i & “‘, ” & objComputer.loadPercentage & “, 100,” & objComputer.loadPercentage & ” , ‘%’ )”

‘執行 SQL 指令
thecommand.CommandText = CmdStr
thecommand.CommandType = cnstCommand
thecommand.ActiveConnection = connection
thecommand.Execute

WScript.Echo “Updating CPUs Usage for ” & strComputer & ” CPU”&i
i = i + 1
Next

‘ 抓取硬碟空間
Set colSettings = objWMIService.ExecQuery(“Select * from Win32_LogicalDisk where DriveType = 3 “)
For Each objComputer in colSettings

CmdStr = “INSERT INTO WEBAP.DRV_DAILY_RECORD (DRV_DATE, COMPUTER_NAME, RESOURCE_TYPE, RESOURCE_NAME, PERCENT_OF_USAGE, MAX_VALUE, CUR_VALUE, UOM) VALUES (”
CmdStr = CmdStr & ” SYSDATE, ‘” & strComputer & “‘, ‘DISK’, ‘” & objComputer.VolumeName & ” – ” & objComputer.Name & “‘, ” & Round(((objComputer.Size-objComputer.FreeSpace)/objComputer.Size) * 100,2) & “, ” & round(objComputer.Size/ (1024*1024*1024),2) & “,” & ROUND((objComputer.Size – objComputer.FreeSpace)/(1024*1024*1024),2) & ” , ‘GB’ )”

‘執行 SQL 指令
thecommand.CommandText = CmdStr
thecommand.CommandType = cnstCommand
thecommand.ActiveConnection = connection
thecommand.Execute

WScript.Echo “Updating Disk Usage for ” & strComputer & “- ” & objComputer.Name

Next

————————————————-
使用方式跟先前幾個 Script 很類似, 只要加入參數 (遠端的電腦名稱即可)

Cscript DrvCheck.vbs “Server_name”

發表迴響