Advertisement
Journeym

web.monitoring

Jan 10th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $URL='192.168.0.1' #URL or IP of a web service
  2. $vm='test VM'      #VCenter vm name
  3. $logpath="E:\log\web.monitoring.txt" #log file
  4. $VCenterserver = "vcenter.contoso.com"
  5. $VCenteruser= "CONTOSO\www-mon"
  6. $VCenterpassword= "Pa$$w0rd"
  7.  
  8. $time = try
  9. {
  10. $request = $null
  11. $timeout_msg = $null
  12. $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $URL }
  13. $result1.TotalMilliseconds
  14. }
  15. catch
  16. {
  17. $request = $_.Exception.Response
  18. $timeout_msg = $_.Exception.Message
  19. $time = -1
  20. }
  21. $date=(Get-Date).ToString()
  22. if ($timeout_msg -ne $null)
  23. {
  24. Write-Output "Time = $($date);StatusCode = $($timeout_msg)"| Out-File  -filepath $logpath -Append -noClobber
  25. }
  26. else
  27. {
  28. Write-Output "Time = $($date);StatusCode = $($request.StatusCode);StatusDescription = $($request.StatusDescription);ResponseLength = $($request.RawContentLength);TimeTaken = $($time)" | Out-File  -filepath $logpath -Append -noClobber
  29. }
  30. if ($request.StatusCode -ne 200)
  31. {
  32. Add-PSSnapin VMware.VimAutomation.Core
  33. Add-PSSnapin VMware.VimAutomation.Vds
  34. Connect-VIServer -Server $VCenterserver -User $VCenteruser -Password $VCenterpassword -Force -Protocol https
  35. if (((Get-VM -Name $vm).PowerState) -like 'PoweredOff')
  36. {
  37. Write-Output "VM is currently Powered off" | Out-File  -filepath $logpath -Append -noClobber
  38. }
  39. Else
  40. {
  41. Restart-VM -VM $vm -RunAsync -Confirm:$false
  42. Write-Output "VM was on, web server unresponsive, Reseted VM" | Out-File  -filepath $logpath -Append -noClobber
  43. }
  44. Disconnect-VIServer -Server $VCenterserver -Force -Confirm:$false
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement