Advertisement
Guest User

Untitled

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