Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. option explicit
  2.  
  3. 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
  4. set oFSO = CreateObject("Scripting.FileSystemObject")
  5. set shell = CreateObject("wscript.shell")
  6. set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
  7. Set connection = CreateObject("ADODB.Connection")
  8. set resultSet = CreateObject("ADODB.recordset")
  9.  
  10. const ForReading = 1
  11. const ForWriting = 2
  12. const ForAppending = 8
  13.  
  14. mainFolderPath = oFSO.GetParentFolderName(WScript.ScriptFullName)
  15.  
  16. inputFilePath = mainFolderPath & "\input"
  17. logFilePath = mainFolderPath & "\logs"
  18. confFilePath = mainFolderPath & "\conf"
  19.  
  20. function setFilesNames(path, ByRef fileName)
  21.     if oFSO.GetFolder(path).files.count = 0 then
  22.         if path = logFilePath then
  23.             set objLogFile = oFSO.CreateTextfile(logFilePath & "\logs.log", ForWriting)
  24.             objLogFile.writeline currentTime & "Creating log file"
  25.         else
  26.             objLogFile.WriteLine currentTime & "Missing file in " & path
  27.             objLogFile.WriteLine currentTime & "Script terminated."
  28.             wscript.Quit
  29.         end if
  30.     end if
  31.  
  32.     for each fileItem in oFSO.GetFolder(path).files
  33.         fileName = fileItem.name
  34.         exit for
  35.     next
  36. end function
  37. call setFilesNames(logFilePath, logFileName)
  38. Set objLogFile = oFSO.OpenTextFile(logFilePath & "\" & logFileName, ForWriting)
  39.  
  40. objlogfile.writeline currentTime & "Starting script"
  41. objlogfile.writeline currentTime & "Checking if all files exist"
  42.  
  43. call setFilesNames(inputFilepath, inputFileName)
  44. call setFilesNames(confFilePath, confFileName)
  45.  
  46. objlogfile.writeline currentTime & "All files exist"
  47.  
  48. set confFile =  oFSO.OpenTextFile(confFilePath & "\" & confFileName, ForReading)
  49.  
  50. For index = 0 To 2
  51.     if index = 0 then
  52.         user = confFile.ReadLine
  53.     elseif index = 1 then
  54.         password = confFile.ReadLine
  55.     elseif index = 2 then
  56.         connection.ConnectionString = confFile.ReadLine
  57.     end if
  58. Next
  59. connection.open
  60.  
  61. class HostObject
  62.     private ip
  63.     private domain
  64.     private state
  65.     private lastAv
  66.     'private lastPerf
  67.     private updated
  68.     private cpu
  69.     private mem
  70.  
  71.     public property let hostName(shostName)
  72.         ip = shostName
  73.         updated = false
  74.     end property
  75.  
  76.     public property get hostName()
  77.         hostName = ip
  78.     end property
  79.  
  80.     public property let domainName(sdomainName)
  81.         domain = sdomainName
  82.     end property
  83.  
  84.     public property get domainName()
  85.         domainName = domain
  86.     end property
  87.  
  88.     private sub pav()
  89.         if updated = false or dateDiff("N", lastAv, time) > 10 then
  90.             objlogfile.writeline currentTime & "Running AV on " & host
  91.            
  92.             Set exec = shell.Exec("ping -n 2 -w 1000 " & ip)
  93.             results = LCase(exec.StdOut.ReadAll)
  94.  
  95.             if InStr(results, "ping request could not find") > 0 then
  96.                 state = "ERR"
  97.             elseif InStr(results, "received = 2") > 0 then
  98.                 state = "UP"
  99.             elseif InStr(results, "received = 2") = 0 then
  100.                 state = "DOWN"
  101.             end if
  102.             lastAv = time
  103.             updated = true
  104.             sql = "insert into MON_AV_NT(FQDN,IP,State)" & _
  105.             " values('" & domain & "','" & ip & "','" & state & "')"
  106.             set resultSet = connection.Execute(sql)
  107.         end if
  108.     end sub
  109.  
  110.     private sub pperf()
  111.         if state = "UP" then
  112.             objlogfile.writeline currentTime & "Running PERF on " & host
  113.             CPUPercentage = 0
  114.             MemoryPercentage = 0
  115.             for index = 0 to 4
  116.                 on error resume next
  117.                 Set objWMIService = objSWbemLocator.ConnectServer(ip, "Root\CIMv2", user, password)
  118.                 if err.number <> 0 then
  119.                     objLogFile.WriteLine currentTime & "server: " & ip & " -  Error Description: """ & err.Description & """"
  120.                 else                                               
  121.                     Set CPUInfo = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48)
  122.                     sumOfCPUInfo = 0
  123.                     count = 0
  124.                     For Each Item in CPUInfo
  125.                         sumOfCPUInfo = sumOfCPUInfo + Item.PercentProcessorTime
  126.                         count = count + 1
  127.                     Next
  128.                     CPUPercentage = CPUPercentage + round(sumOfCPUInfo/count)
  129.  
  130.                     Set colComputer = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
  131.                     for each objItem in colComputer
  132.                         usedPhysicalMemory = objItem.TotalVisibleMemorySize - objItem.FreePhysicalMemory
  133.                         totalPhysicalMemory = objItem.TotalVisibleMemorySize
  134.                         MemoryPercentage = MemoryPercentage + (usedPhysicalMemory/totalPhysicalMemory)*100
  135.                         exit for
  136.                     Next
  137.                 end if
  138.             next
  139.             cpu = round(CPUPercentage/5)
  140.             mem = round(MemoryPercentage/5)
  141.             sql = "insert into MON_PERF_NT(FQDN,IP,CPU_Usage,Memory_Usage)" & _
  142.                 " values('" & domain & "','" & ip & "','" & cpu & "','" & mem & "')"
  143.             set resultSet = connection.Execute(sql)
  144.         end if
  145.     end sub
  146.  
  147.     public sub perf()
  148.         pav
  149.         pperf
  150.     end sub
  151. end class
  152.  
  153. set hosts = CreateObject("scripting.dictionary")
  154.  
  155. function currentTime
  156.     currentTime = "<" & time & "> - "
  157. end function
  158.  
  159. function monWinAdr(inputFilePath)
  160.     objLogFile.writeLine currentTime & "Starting procedure"
  161.     do while 1 = 1
  162.         set textStream = oFSO.OpenTextFile(inputFilePath & "\" & inputFileName, ForReading)
  163.         do until textStream.AtEndOfStream
  164.             line = textStream.ReadLine
  165.             collection = split(line, ",")
  166.  
  167.             host = collection(0)
  168.             domain = collection(1)
  169.  
  170.             if hosts.exists(host) then
  171.                 objlogfile.writeline currentTime & "Host: " & host & "Exists in the collection"
  172.                 set host1 = hosts(host)
  173.                 host1.perf
  174.             else
  175.                 set host1 = new HostObject
  176.                 host1.hostName = host
  177.                 host1.domainName = domain
  178.                 host1.perf
  179.                 hosts.add host,host1
  180.             end if
  181.         loop
  182.     loop
  183. end function
  184. monWinAdr(inputFilePath)
  185. objLogFile.writeline currentTime & "Script ended"
  186. connection.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement