SHARE
TWEET

Untitled

a guest Aug 21st, 2019 105 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. 'confFile content:
  75. 'PIT\bozhidar.iliev
  76. 'QAZ123wsx
  77. 'Provider=SQLOLEDB;Server=172.16.42.106;Database=labmon;Trusted_Connection=True;User ID=sa;Password=QAZ123wsx
  78.  
  79. set confFile =  oFSO.OpenTextFile(confFilePath & "\" & confFileName, ForReading)
  80. For index = 0 To 2
  81.     if index = 0 then
  82.         user = confFile.ReadLine
  83.     elseif index = 1 then
  84.         password = confFile.ReadLine
  85.     elseif index = 2
  86.         connection.ConnectionString = confFile.ReadLine
  87.     end if
  88. Next
  89. 'connection.open
  90.  
  91. function currentTime
  92.     currentTime = "<" & time & "> - "
  93. end function
  94.  
  95. function monWinAv(host)
  96.     Set exec = shell.Exec("ping -n 2 -w 1000 " & host)
  97.     results = LCase(exec.StdOut.ReadAll)
  98.    
  99.     if InStr(results, "ping request could not find") > 0 then
  100.         monWinAv = "Ping request could not find host " & host & "."
  101.     elseif InStr(results, "received = 2") > 0 then
  102.         monWinAv = "server: " & host & ", status: UP"
  103.     elseif InStr(results, "received = 2") = 0 then
  104.         monWinAv = "server: " & host & ", status: DOWN"
  105.     end if
  106. end function
  107.  
  108. function monWinPerf(strComputer)
  109.     output = ""
  110.  
  111.     CPUPercentage = 0
  112.     MemoryPercentage = 0
  113.     for index = 0 to 4
  114.         on error resume next
  115.         Set objWMIService = objSWbemLocator.ConnectServer(strComputer, "Root\CIMv2", user, password)
  116.         if err.number <> 0 then
  117.             objLogFile.WriteLine currentTime & "server: " & strComputer & " -  Error Description: """ & err.Description & """"
  118.         else                                               
  119.             Set CPUInfo = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48)
  120.             sumOfCPUInfo = 0
  121.             count = 0
  122.             For Each Item in CPUInfo
  123.                 sumOfCPUInfo = sumOfCPUInfo + Item.PercentProcessorTime
  124.                 count = count + 1
  125.             Next
  126.             CPUPercentage = CPUPercentage + round(sumOfCPUInfo/count)
  127.  
  128.             Set colComputer = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
  129.            
  130.             for each objItem in colComputer
  131.                 usedPhysicalMemory = objItem.TotalVisibleMemorySize - objItem.FreePhysicalMemory
  132.                 totalPhysicalMemory = objItem.TotalVisibleMemorySize
  133.                 MemoryPercentage = MemoryPercentage + (usedPhysicalMemory/totalPhysicalMemory)*100
  134.                 exit for
  135.             Next
  136.         end if
  137.     next
  138.     output = "CPU: " & round(CPUPercentage/5) & "% MEM: " & round(MemoryPercentage/5) & "%"
  139.     monWinPerf = output
  140. end function
  141.  
  142. function monWinAdr(inputFilePath)
  143.     set textStream = oFSO.OpenTextFile(inputFilePath & "\" & inputFileName, ForReading)
  144.     objLogFile.writeLine currentTime & "Starting procedure"
  145.     objLogFile.writeLine currentTime & "Checking if cache exists"
  146.  
  147.     if cacheIsOld = false and cacheCreated = false then  
  148.         objLogFile.writeline currentTime & "Cache found"
  149.         set cacheTextFile = oFSO.OpenTextFile(cacheFilePath & "\" & cacheFileName, ForReading)     
  150.         objlogfile.writeline currentTime & "Using cache file"
  151.         objlogfile.writeline currentTime & "Retrieving performance data from servers"
  152.         i = 0
  153.         do until textStream.AtEndOfStream
  154.             line = textStream.ReadLine
  155.            
  156.             collection = split(line, ",")
  157.  
  158.             host = collection(0)
  159.             domain = collection(1)
  160.            
  161.             do until cacheTextFile.AtEndOfStream
  162.                 item = cacheTextFile.ReadLine
  163.                 items = Split(item,"- ")
  164.                 result = monWinPerf(host)
  165.                 if instr(item, "UP") > 0 then
  166.                     objOutputFile.WriteLine currentTime & items(1) & " - " & result
  167.                 else
  168.                     objOutputFile.writeline currentTime & items(1)
  169.                 end if
  170.                 exit do
  171.             loop
  172.         loop
  173.         objlogfile.writeline currentTime &  "End of procedure"
  174.     elseif cacheIsOld = true and cacheCreated = false then
  175.             objlogfile.writeline currentTime & "Overriding cache"
  176.             objlogfile.writeline currentTime & "Running PING"
  177.             objlogfile.writeline currentTime & "Retrieving performance data from servers"
  178.             do until textStream.AtEndOfStream
  179.                 line = textStream.ReadLine
  180.  
  181.                 result = split(line, ",")
  182.                 host = result(0)
  183.            
  184.                 tryPingResult = monWinAv(host)
  185.                 results = InStr(tryPingResult, "UP")
  186.                
  187.                 if results > 0 then
  188.                     objOutputFile.WriteLine tryPingResult & " - " & monWinPerf(host)
  189.                     cacheTextfile.writeLine currentTime & "server: " & host & ", status: UP"    
  190.                 else
  191.                     objOutputFile.WriteLine tryPingResult
  192.                     cacheTextFile.writeLine currentTime & "server: " & host & ", status: DOWN"  
  193.                 end if
  194.                 next
  195.             loop
  196.             objlogfile.writeline currentTime & "End of procedure"
  197.     elseif cacheIsOld = false and cacheCreated = true then
  198.         objLogFile.writeline currentTime & "Cache not found"
  199.         objLogFile.writeline currentTime & "Creating cache file"                           
  200.         objlogfile.writeline currentTime & "Running PING"
  201.         objLogFile.writeline currentTime & "Retrieving performance data from servers"
  202.         objlogfile.writeline currentTime & "Writing the output"
  203.         do until textStream.AtEndOfStream
  204.             line = textStream.ReadLine
  205.  
  206.             result = split(line, ",")
  207.             host = result(0)
  208.  
  209.             tryPingResult = monWinAv(host)
  210.             results = InStr(tryPingResult, "UP")
  211.            
  212.             if results > 0 then
  213.                 objOutputFile.WriteLine tryPingResult & " - " & monWinPerf(host)
  214.                 cacheTextfile.writeLine currentTime & "server: " & host & ", status: UP"    
  215.             else
  216.                 objOutputFile.WriteLine tryPingResult
  217.                 cacheTextFile.writeLine currentTime & "server: " & host & ", status: DOWN"  
  218.             end if
  219.         loop
  220.         objlogfile.writeline currentTime & "End of procedure"
  221.     end if
  222.     objlogfile.writeline currentTime & "Task completed"
  223. end function
  224.  
  225. monWinAdr(inputFilePath)
  226. objLogFile.writeline currentTime & "Script ended"
  227. 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