Advertisement
Guest User

Untitled

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