Copyright Notice: You can reprint anything, you must indicate the original source and author information in hyperlink when reprint.
How to write WMI scripts, see "WMI Learning Notes (2) - Learning WMI Resources".
Simple script example, see: "WMI Learning Notes (1) - Write the Script to Monitor the Performance of the Windows Server
'********************************************************** ************************************* The Script name: enum.vbs' 'V1.0' Get CPU, Memory, Disk Information from Read server name from ad. 'Write Performance Data To Console OR a .csv file' 'by fog 2004-08-12' ****************** *********************************************************** **********
Option Explicit'on Error ResMe Next
'********************************************************** ********************************************************? Const valueConst Err_ok ?????????????? = 0const Err_General_Failure = 1
'HelpConst l_empty_text ???? = ""
'Disk infoconst hard_disk = 3
'Append fileconst fortapore = 8
'Server OUConst WEBSRV ?? = "Computers of Web Service" Const BAKSRV ?? = "Computers of Backup Service" Const DBWEBSRV = "Computers of Database Mix Web Service" Const DBSRV ??? = "Computers of Database Service" Const MANSRV? ? = "Computers of Manage Service" const sussrv ?? = "Computers of Sus Service"
'Write Codesconst Write_CON = 0const Write_CSV = 1const Write_SQL = 2
'********************************************************** *************************************************** *********************************************************** *********** DIM INTWRITEOPER, STRSRVTYPE, STREXPORTINFO, ArrdomainName
INTWRITEOPER? = Write_CSVSTRDOMAINNAME = "Wins.xinnet.com" strexportInfo = "SRVTYPE, SRVNAME, CPU1, CPU2, CPU3, CPU4, MEMORY (M), TOTALMEMORY (M), Disk1 (M), Disk2 (M), Disk3 (M ), Disk4 (m), disk5 (m), checktime "& vbcrlfcall exportinfo (strexportinfo)
strSrvType ??? = WEBSRVCall Enum () strSrvType ??? = BAKSRVCall Enum () strSrvType ??? = DBWEBSRVCall Enum () strSrvType ??? = DBSRVCall Enum () strSrvType ??? = MANSRVCall Enum () strSrvType ??? = SUSSRVCALL ENUM ()
'********************************************************** *************************************************** *********************************************************** ************
'********************************************************** ****************************************************************************************** *********************************************************** ************ SUB ENUM () DIM i, J, INTCOUNTER, STRComputer, StrdomainName, StrobjnaMedim Objconfiguration, Objcontainer, Objitmiservice, Colitems, Objitem
J = 0
'Get server name list from ADstrObjName = "LDAP: // ou =" & strSrvType arrDomainName = Split ( "." StrDomainName,) For i = 0 To UBound (arrDomainName) ??? strObjName = strObjName & ", dc =" & ArrdomainName (I) NextWScript.echo strobjnameset objconfiguration = getObject (strobjname)
For Each Objcontainer In Objconfiguration
??? strComputer = objcontainer.name ??? strComputer = Right (Strcomputer, Len (strComputer) - 3) ??? strexportinfo = Replace (strsrvType, "Computers of", ") &", "& strComputer
??? 'Connect to Server ??? set objwmiservice = getObject ("WinMgmts: //" & strcomputer & "/ root / cimv2") ???' Memory / CPU / DiskFree ??? IntCounter = 0 ??? set colItems = objWMIService.ExecQuery ( "Select * from Win32_Processor" ,, 48) ??? For Each objItem in colItems ??????? strExportInfo = strExportInfo & "," & objItem.LoadPercentage ?? If objItem.LoadPercentage> = 85 THEN ??? Call CDONTS ("[Alert] Server-" & Strcomputer & "'S CPU Load IS Too High", "Server" & Strcomputer & "CPU (" & INTCOUNTER & ") LOAD:" & objItem.loadPercentage ) ?? End if ???? INTCOUNTER = INTCOUNTER 1 ??? Next ??? for i = intcounter to 3 ??????? strexportinfo = strexportinfo & "??? next ??? ?? ? Set colItems = objWMIService.ExecQuery ( "Select * from Win32_PerfRawData_PerfOS_Memory" ,, 48) ??? For Each objItem in colItems ??????? strExportInfo = strExportInfo & "," & objItem.AvailableMBytes ??? Next ?? ? Set colorvice.instances @ ??? for Each Objitem in colitems ?? i = clng (Objitem.totalphysic) Almemory / 1024) ?????? strexportinfo = strerexportinfo & "" & i ?? IF i <= 85 THEN ??? Call Cdonts ("[Alert] Server-" & strComputer & "'s Memory IS TOO Low, "Server" & strComputer & "Available Memory:" & I & "M.") ?? End if ??? Next
??? Set colItems = objWMIService.ExecQuery ( "Select * from Win32_LogicalDisk Where DriveType =" & HARD_DISK & "") ??? intCounter = 0 ??? For Each objItem in colItems ?? i = CLng (objItem.FreeSpace / 1048576 ) ?????? strexportinfo = strelixportinfo & "" & i ?? IF i <= 1000 THEN ??? Call cdonts ("[Alert] Server-" & strComputer & "'s Available Disk Space is TOO LOW "," Server "& strComputer &" Disk ("& INTCOUNTER &") free space: "& I &" m. ") ?? End if ??????? IntCounter = intcounter 1 ??? NEXT? ?? for i = intcounter to 4 ??????? strexportinfo = strexportinfo & "" ??? next ??? streowportInfo = strexportinfo & ", '" & now () & "'"? & Vbcrlf ?? ? Call Exportinfo (strexportinfo) ??? j = j 1
NEXTWScript.echo "Total" strsrvType & ":" & jend sub '*********************************************** *************************************************** End '********* *********************************************************** ***********************
'********************************************************** ********************************************************** *********************************************************** *********** SUB EXPORTINFO (STRINFO) DIM OBJFSO, OBJLOGFILE ??? SELECT CASE INTWRITEOPER ??? case write_con ??????? wscript.echo strinfo ??? case write_csv ??? ???? set objfso = createObject ("scripting.filesystemObject") ??????? set objlogfile = objfso.opentextfile ("Performance.csv", FORAPPENDING, TRUE) ?????? Objlogfile.write Strinfo ??????? objlogfile.close ??? Case Write_SQL ??? End SelectenD Sub '************************************ *********************************************************** 'End' ********************************************** ***************************** ****************************** *********************************************************** ********** 'Send Mail Use CDONTS Component' *************************************************** ******************************************** SUB CDONTS (Topic, MailBody)? 'On Error Resume NEXT? DIM OBJCDOMAIL? Set Objcdomail = Createob ject ( "CDONTS.NewMail")? ObjCDOMail.From = ""? ObjCDOMail.To = ""? ObjCDOMail.Subject = Topic? ObjCDOMail.BodyFormat = 0? ObjCDOMail.MailFormat = 0? ObjCDOMail.Body = Mailbody? 'ObjCDOMail. Send? Set objcdomail = Nothingend Sub '******************************************** *********************************** ************* ************ *********************************************************** ******************