Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- option explicit
- dim hosts,host1,domain,host,items,collection,MemoryPercentage,CPUPercentage,cacheCreated,cacheIsOld,query,objRecordSet,objConnection,count,currentIPAddress,cacheTextFile, sumOfCPUInfo,cachefile, cacheFilePath, cacheFileName,outputFile, index, confFile, user, password, outputFileName, objLogFile, logFile, confFileName, confFilePath ,fileitem, mainFolderPath, inputFileName, logFilePath, logFileName, result, ipAddress, currentIPNum, colComputer, totalPhysicalMemory, usedPhysicalMemory, inputFilePath, shell, return, exec, results, oFSO, textStream, output, line, command, objWMIService, CPUInfo, objOutputFile, continued, item, MemoryInfo, objItem, strComputer, objRefresher, colItems, outputFilePath, objSWbemLocator, objSWbemServices,connection, sql, resultSet
- set oFSO = CreateObject("Scripting.FileSystemObject")
- set shell = CreateObject("wscript.shell")
- set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
- Set connection = CreateObject("ADODB.Connection")
- set resultSet = CreateObject("ADODB.recordset")
- const ForReading = 1
- const ForWriting = 2
- const ForAppending = 8
- mainFolderPath = oFSO.GetParentFolderName(WScript.ScriptFullName)
- inputFilePath = mainFolderPath & "\input"
- logFilePath = mainFolderPath & "\logs"
- confFilePath = mainFolderPath & "\conf"
- function setFilesNames(path, ByRef fileName)
- if oFSO.GetFolder(path).files.count = 0 then
- if path = logFilePath then
- set objLogFile = oFSO.CreateTextfile(logFilePath & "\logs.log", ForWriting)
- objLogFile.writeline currentTime & "Creating log file"
- else
- objLogFile.WriteLine currentTime & "Missing file in " & path
- objLogFile.WriteLine currentTime & "Script terminated."
- wscript.Quit
- end if
- end if
- for each fileItem in oFSO.GetFolder(path).files
- fileName = fileItem.name
- exit for
- next
- end function
- call setFilesNames(logFilePath, logFileName)
- Set objLogFile = oFSO.OpenTextFile(logFilePath & "\" & logFileName, ForWriting)
- objlogfile.writeline currentTime & "Starting script"
- objlogfile.writeline currentTime & "Checking if all files exist"
- call setFilesNames(inputFilepath, inputFileName)
- call setFilesNames(confFilePath, confFileName)
- objlogfile.writeline currentTime & "All files exist"
- set confFile = oFSO.OpenTextFile(confFilePath & "\" & confFileName, ForReading)
- For index = 0 To 2
- if index = 0 then
- user = confFile.ReadLine
- elseif index = 1 then
- password = confFile.ReadLine
- elseif index = 2 then
- connection.ConnectionString = confFile.ReadLine
- end if
- Next
- connection.open
- class HostObject
- private ip
- private domain
- private state
- private lastAv
- 'private lastPerf
- private updated
- private cpu
- private mem
- public property let hostName(shostName)
- ip = shostName
- updated = false
- end property
- public property get hostName()
- hostName = ip
- end property
- public property let domainName(sdomainName)
- domain = sdomainName
- end property
- public property get domainName()
- domainName = domain
- end property
- private sub pav()
- if updated = false or dateDiff("N", lastAv, time) > 10 then
- objlogfile.writeline currentTime & "Running AV on " & host
- Set exec = shell.Exec("ping -n 2 -w 1000 " & ip)
- results = LCase(exec.StdOut.ReadAll)
- if InStr(results, "ping request could not find") > 0 then
- state = "ERR"
- elseif InStr(results, "received = 2") > 0 then
- state = "UP"
- elseif InStr(results, "received = 2") = 0 then
- state = "DOWN"
- end if
- lastAv = time
- updated = true
- sql = "insert into MON_AV_NT(FQDN,IP,State)" & _
- " values('" & domain & "','" & ip & "','" & state & "')"
- set resultSet = connection.Execute(sql)
- end if
- end sub
- private sub pperf()
- if state = "UP" then
- objlogfile.writeline currentTime & "Running PERF on " & host
- CPUPercentage = 0
- MemoryPercentage = 0
- for index = 0 to 4
- on error resume next
- Set objWMIService = objSWbemLocator.ConnectServer(ip, "Root\CIMv2", user, password)
- if err.number <> 0 then
- objLogFile.WriteLine currentTime & "server: " & ip & " - Error Description: """ & err.Description & """"
- else
- Set CPUInfo = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48)
- sumOfCPUInfo = 0
- count = 0
- For Each Item in CPUInfo
- sumOfCPUInfo = sumOfCPUInfo + Item.PercentProcessorTime
- count = count + 1
- Next
- CPUPercentage = CPUPercentage + round(sumOfCPUInfo/count)
- Set colComputer = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
- for each objItem in colComputer
- usedPhysicalMemory = objItem.TotalVisibleMemorySize - objItem.FreePhysicalMemory
- totalPhysicalMemory = objItem.TotalVisibleMemorySize
- MemoryPercentage = MemoryPercentage + (usedPhysicalMemory/totalPhysicalMemory)*100
- exit for
- Next
- end if
- next
- cpu = round(CPUPercentage/5)
- mem = round(MemoryPercentage/5)
- sql = "insert into MON_PERF_NT(FQDN,IP,CPU_Usage,Memory_Usage)" & _
- " values('" & domain & "','" & ip & "','" & cpu & "','" & mem & "')"
- set resultSet = connection.Execute(sql)
- end if
- end sub
- public sub perf()
- pav
- pperf
- end sub
- end class
- set hosts = CreateObject("scripting.dictionary")
- function currentTime
- currentTime = "<" & time & "> - "
- end function
- function monWinAdr(inputFilePath)
- objLogFile.writeLine currentTime & "Starting procedure"
- do while 1 = 1
- set textStream = oFSO.OpenTextFile(inputFilePath & "\" & inputFileName, ForReading)
- do until textStream.AtEndOfStream
- line = textStream.ReadLine
- collection = split(line, ",")
- host = collection(0)
- domain = collection(1)
- if hosts.exists(host) then
- objlogfile.writeline currentTime & "Host: " & host & "Exists in the collection"
- set host1 = hosts(host)
- host1.perf
- else
- set host1 = new HostObject
- host1.hostName = host
- host1.domainName = domain
- host1.perf
- hosts.add host,host1
- end if
- loop
- loop
- end function
- monWinAdr(inputFilePath)
- objLogFile.writeline currentTime & "Script ended"
- connection.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement