SHARE
TWEET

Untitled

a guest Aug 21st, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. option explicit
  2.  
  3. dim items,collection,MemoryPercentage,CPUPercentage,cacheCreated,cacheIsOld,query,objRecordSet,objConnection,count,currentIPAddress,cacheTextFile, sumOfCPUInfo ,j,i,cachefileOpened,cacheExists,cachefile, cacheFilePath, cacheFileName,outputFile, index, confFile, user, password, outputFileName, objLogFile, logFile, confFileName, confFilePath ,fileitem, mainFolderPath, inputFileName, logFilePath, logFileName, tryPingResult, 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. outputFilePath = mainFolderPath & "\output"
  18. logFilePath = mainFolderPath & "\logs"
  19. confFilePath = mainFolderPath & "\conf"
  20. cacheFilePath = mainFolderPath & "\cache"
  21.  
  22. cacheCreated = false
  23. cacheIsOld = false
  24.  
  25. function setFilesNames(path, ByRef fileName)
  26.     if oFSO.GetFolder(path).files.count = 0 then
  27.         if path = logFilePath then
  28.             set logFile = oFSO.CreateTextfile(logFilePath & "\logs.log", True)
  29.         elseif path = outputFilePath then
  30.             set outputFile = oFSO.CreateTextfile(outputFilePath & "\output.log", True)
  31.         elseif path = cacheFilePath then
  32.             set cacheTextFile = oFSO.CreateTextFile(cacheFilePath & "\cache.txt", ForWriting)
  33.             cacheIsOld = false
  34.             cacheCreated = true
  35.         else
  36.             objLogFile.WriteLine currentTime & "Missing file in " & path
  37.             objLogFile.WriteLine currentTime & "Script terminated."
  38.             wscript.Quit
  39.         end if
  40.     else
  41.         if path = cacheFilePath then
  42.             set cacheFile = oFSO.GetFile(cacheFilePath & "\cache.txt")
  43.             if dateDiff("N", cacheFile.DateLastModified, now) > 10 then
  44.                 cacheIsOld = true
  45.                 cacheCreated = false
  46.             else
  47.                 cacheIsOld = false
  48.                 cacheCreated = false
  49.             end if
  50.         end if
  51.     end if
  52.  
  53.     for each fileItem in oFSO.GetFolder(path).files
  54.         fileName = fileItem.name
  55.         exit for
  56.     next
  57.  
  58. end function
  59. call setFilesNames(logFilePath, logFileName)
  60. Set objLogFile = oFSO.OpenTextFile(logFilePath & "\" & logFileName, ForWriting)
  61.  
  62. objlogfile.writeline currentTime & "Starting script"
  63. objlogfile.writeline currentTime & "Checking if all files exist"
  64.  
  65. call setFilesNames(inputFilepath, inputFileName)
  66. call setFilesNames(outputFilePath, outputFileName)
  67. call setFilesNames(confFilePath, confFileName)
  68. call setFilesNames(cacheFilePath, cacheFileName)
  69.  
  70. objlogfile.writeline currentTime & "All files exist"
  71.  
  72. Set objOutputFile = oFSO.OpenTextFile(outputFilePath & "\" & outputFileName, ForWriting)
  73.  
  74. set confFile =  oFSO.OpenTextFile(confFilePath & "\" & confFileName, ForReading)
  75. For index = 0 To 1
  76.     if index = 0 then
  77.         user = confFile.ReadLine
  78.     elseif index = 1 then
  79.         password = confFile.ReadLine
  80.         exit for
  81.     elseif index = 2
  82.         connection.ConnectionString = confFile.ReadLine
  83.     end if
  84. Next
  85. 'connection.open
  86.  
  87. function currentTime
  88.     currentTime = "<" & time & "> - "
  89. end function
  90.  
  91. function monWinAv(host)
  92.     Set exec = shell.Exec("ping -n 2 -w 1000 " & host)
  93.     results = LCase(exec.StdOut.ReadAll)
  94.    
  95.     if InStr(results, "ping request could not find") > 0 then
  96.         monWinAv = "Ping request could not find host " & host & "."
  97.     elseif InStr(results, "received = 2") > 0 then
  98.         monWinAv = "server: " & host & ", status: UP"
  99.     elseif InStr(results, "received = 2") = 0 then
  100.         monWinAv = "server: " & host & ", status: DOWN"
  101.     end if
  102. end function
  103.  
  104. function monWinPerf(strComputer)
  105.     output = ""
  106.  
  107.     CPUPercentage = 0
  108.     MemoryPercentage = 0
  109.     for index = 0 to 4
  110.         on error resume next
  111.         Set objWMIService = objSWbemLocator.ConnectServer(strComputer, "Root\CIMv2", user, password)
  112.         if err.number <> 0 then
  113.             objLogFile.WriteLine currentTime & "server: " & strComputer & " -  Error Description: """ & err.Description & """"
  114.         else                                               
  115.             Set CPUInfo = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48)
  116.             sumOfCPUInfo = 0
  117.             count = 0
  118.             For Each Item in CPUInfo
  119.                 sumOfCPUInfo = sumOfCPUInfo + Item.PercentProcessorTime
  120.                 count = count + 1
  121.             Next
  122.             CPUPercentage = CPUPercentage + round(sumOfCPUInfo/count)
  123.  
  124.             Set colComputer = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
  125.            
  126.             for each objItem in colComputer
  127.                 usedPhysicalMemory = objItem.TotalVisibleMemorySize - objItem.FreePhysicalMemory
  128.                 totalPhysicalMemory = objItem.TotalVisibleMemorySize
  129.                 MemoryPercentage = MemoryPercentage + (usedPhysicalMemory/totalPhysicalMemory)*100
  130.                 exit for
  131.             Next
  132.  
  133.         end if
  134.     next
  135.     output = "CPU: " & round(CPUPercentage/5) & "% MEM: " & round(MemoryPercentage/5) & "%"
  136.     monWinPerf = output
  137. end function
  138.  
  139. function monWinAdr(inputFilePath)
  140.     set textStream = oFSO.OpenTextFile(inputFilePath & "\" & inputFileName, ForReading)
  141.     objLogFile.writeLine currentTime & "Starting procedure"
  142.     objLogFile.writeLine currentTime & "Checking if cache exists"
  143.  
  144.     if cacheIsOld = false and cacheCreated = false then  
  145.         objLogFile.writeline currentTime & "Cache found"
  146.         set cacheTextFile = oFSO.OpenTextFile(cacheFilePath & "\" & cacheFileName, ForReading)     
  147.         objlogfile.writeline currentTime & "Using cache file"
  148.         objlogfile.writeline currentTime & "Retrieving performance data from servers"
  149.         i = 0
  150.         do until textStream.AtEndOfStream
  151.             line = textStream.ReadLine
  152.            
  153.             collection = split(line, ",")
  154.  
  155.             host = collection(0)
  156.             domain = collection(1)
  157.            
  158.             do until cacheTextFile.AtEndOfStream
  159.                 item = cacheTextFile.ReadLine
  160.                 items = Split(item,"- ")
  161.                 result = monWinPerf(host)
  162.                 if instr(item, "UP") > 0 then
  163.                     objOutputFile.WriteLine currentTime & items(1) & " - " & result
  164.                 else
  165.                     objOutputFile.writeline currentTime & items(1)
  166.                 end if
  167.                 exit do
  168.             loop
  169.         loop
  170.         objlogfile.writeline currentTime &  "End of procedure"
  171.     elseif cacheIsOld = true and cacheCreated = false then
  172.             objlogfile.writeline currentTime & "Overriding cache"
  173.             objlogfile.writeline currentTime & "Running PING"
  174.             objlogfile.writeline currentTime & "Retrieving performance data from servers"
  175.             do until textStream.AtEndOfStream
  176.                 line = textStream.ReadLine
  177.  
  178.                 result = split(line, ",")
  179.                 host = result(0)
  180.            
  181.                 tryPingResult = monWinAv(host)
  182.                 results = InStr(tryPingResult, "UP")
  183.                
  184.                 if results > 0 then
  185.                     objOutputFile.WriteLine tryPingResult & " - " & monWinPerf(host)
  186.                     cacheTextfile.writeLine currentTime & "server: " & host & ", status: UP"    
  187.                 else
  188.                     objOutputFile.WriteLine tryPingResult
  189.                     cacheTextFile.writeLine currentTime & "server: " & host & ", status: DOWN"  
  190.                 end if
  191.                 next
  192.             loop
  193.             objlogfile.writeline currentTime & "End of procedure"
  194.     elseif cacheIsOld = false and cacheCreated = true then
  195.         objLogFile.writeline currentTime & "Cache not found"
  196.         objLogFile.writeline currentTime & "Creating cache file"                           
  197.         objlogfile.writeline currentTime & "Running PING"
  198.         objLogFile.writeline currentTime & "Retrieving performance data from servers"
  199.         objlogfile.writeline currentTime & "Writing the output"
  200.         do until textStream.AtEndOfStream
  201.             line = textStream.ReadLine
  202.  
  203.             result = split(line, ",")
  204.             host = result(0)
  205.  
  206.             tryPingResult = monWinAv(host)
  207.             results = InStr(tryPingResult, "UP")
  208.            
  209.             if results > 0 then
  210.                 objOutputFile.WriteLine tryPingResult & " - " & monWinPerf(host)
  211.                 cacheTextfile.writeLine currentTime & "server: " & host & ", status: UP"    
  212.             else
  213.                 objOutputFile.WriteLine tryPingResult
  214.                 cacheTextFile.writeLine currentTime & "server: " & host & ", status: DOWN"  
  215.             end if
  216.         loop
  217.         objlogfile.writeline currentTime & "End of procedure"
  218.     end if
  219.     objlogfile.writeline currentTime & "Task completed"
  220. end function
  221.  
  222. monWinAdr(inputFilePath)
  223. objLogFile.writeline currentTime & "Script ended"
  224. wscript.echo "Script ended"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top