PoshCode Logo PowerShell Code Repository

Check Server Health by Hemal Pancholi 2 weeks ago (modification of post by Brian English view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6835"></script>download | new post

Testing Report

  1. ########################################################
  2. # Created by Brian English
  3. #   Brian.English@charlottefl.com
  4. #   eddiephoenix@gmail.com
  5. #
  6. # for Charlotte County Government
  7. # No warranty suggested or implied
  8. ########################################################
  9. # Purpose: Check Server Service Health
  10. ########################################################
  11. # Notes:   For checking Vmware to work you must have the VMWare ToolKit for windows installed
  12. #          For checking Citrix to work you must have the MPSSDK intalled
  13. #          For checking DNS to work you must have dcdiag in your path
  14. ########################################################
  15. #input params
  16. param($Server)
  17. #################
  18. #other variables
  19. #################
  20. $GLOBAL:priority="normal"
  21. $FreePercent = 5
  22. $date = date
  23. #################
  24.  
  25. #turn off error handling
  26. #$SavedEA=$Global:ErrorActionPreference
  27. #$Global:ErrorActionPreference="SilentlyContinue"
  28.  
  29. $inAry = @($input)
  30. if($inAry.count -gt 0)
  31. {$server = $inary}
  32.  
  33.  
  34. #############
  35. function checkSystem()
  36. { $status = "`nChecking Server Systems"    
  37.  
  38.   for($i = 0; $i -lt @($Server).count; $i++ )
  39.   { $srv = @($Server)[$i]
  40.     $checkVMWare = $false
  41.     $checkDNS = $false
  42.     $checkDHCP = $false
  43.     $checkShares = $false
  44.     $checkExc = $false
  45.     $checkCitrix = $false
  46.     $checkIIS = $false
  47.    
  48.     write-progress "Checking Server Systems: $srv "-> -perc ($i * (100/@($Server).count))
  49.    
  50.     $status += "`n***$srv***"
  51.     $errcnt = 0
  52.     $error.clear()
  53.    
  54.     if((ping-computer "$srv").protocoladdress -ne "")
  55.     {
  56.       if((gwmi -computername $srv win32_computersystem).dnshostname -ne $srv -or (gwmi -computername $srv win32_computersystem).name -ne $srv)
  57.       { $status += "`n`t! ServerName does not match"
  58.         $errcnt += 1
  59.       }
  60.    
  61.       foreach($drive in(gwmi win32_logicaldisk -computername $srv | where{$_.Drivetype -like "3"}))
  62.       { if((($drive.freespace/1gb)/($drive.size/1gb) * 100 ) -lt $FreePercent)
  63.         { $status += "`n`t! Drive: " + $drive.deviceid + " Free Space: " + ($drive.freespace/ 1GB) + "GB"
  64.           $priority = "high"
  65.           $errcnt += 1
  66.         }
  67.       }
  68.      
  69.       foreach($svc in(gwmi win32_service -computername $srv | where{$_.StartMode -like "Auto" -and $_.name -ne "SysmonLog"}))
  70.       { if($svc.state -ne "Running")
  71.         { $status += "`n`t! Service: " + $svc.DisplayName + " is " + $svc.state
  72.           $errcnt += 1
  73.         }
  74.         if($svc.name -eq "vpxd" -and $svc.state -eq "Running")
  75.         {$checkVMWare = $true}
  76.         if($svc.name -eq "DNS" -and $svc.state -eq "Running")
  77.         {$checkDNS = $true}
  78.         if($svc.name -eq "DHCPServer" -and $svc.state -eq "Running")
  79.         {$checkDHCP = $true}
  80.         if($svc.name -eq "lanmanserver" -and $svc.state -eq "Running")
  81.         {$checkShares = $true}
  82.         if($svc.name -eq "MSExchangeIS" -and $svc.state -eq "Running")
  83.         {$checkExc = $true}
  84.         if($svc.name -eq "cpsvc" -and $svc.state -eq "Running")
  85.         {$checkCitrix = $true}
  86.         if($svc.name -eq "IISADMIN" -and $svc.state -eq "Running")
  87.         {$checkIIS = $true}
  88.       }
  89.          
  90.       if($checkVMWare)
  91.       { $vi = get-esx $srv
  92.        
  93.         $status += "`nHosts"    
  94.         foreach($vmh in get-vmhost)
  95.         { if($vmh.state -ne "Connected")
  96.           { $status += "`n`t! Host: " + $vmh.Name + "; State: " + $vmh.state  }
  97.         }
  98.        
  99.         $status += "`nVMs"
  100.         $vms = (get-vm -server $vi)      
  101.         $greenvms = 0
  102.        
  103.         $status += "`n`tTotal VMs: " + $vms.count
  104.         $status += "`n`tVMs powered Off: " + ($vms | where{$_.PowerState -eq "PoweredOff"}).count
  105.         $status += "`n`tVMs powered On: " + ($vms | where{$_.PowerState -eq "PoweredOn"}).count
  106.            
  107.         foreach($vm in ($vms | where{$_.PowerState -eq "PoweredOn"}))
  108.         { $vmv = get-view $vm.id
  109.          
  110.           if($vmv.Overallstatus -ne "green" -or $vmv.configstatus -ne "green") # -or $vmv.guestheartbeatstatus -ne "green")
  111.           { $status += "`n`t`t" + $vm.name + " Overall: " + $vmv.overallStatus
  112.             $status += "`n`t`t" + $vm.name + " Config: " + $vmv.configStatus
  113.             $status += "`n`t`t" + $vm.name + " Heartbeat: " + $vmv.GuestHeartbeatStatus
  114.            
  115.             foreach($alrm in $vmv.triggeredAlarmState)
  116.             { $av = get-view $alrm.alarm
  117.               $status += "`n`t`t " + $vm.name + " Triggered: " + $av.info.Name
  118.             }
  119.             $priority = "high"
  120.           }
  121.           else
  122.           {$greenvms += 1}              
  123.         }#for
  124.         $status += "`n`tGreen VMs: " + $greenvms + "`n"
  125.       }
  126.      
  127.       if($checkDNS)
  128.       { foreach($ln in (dcdiag /test:DNS /s:$srv /v))
  129.         { if( $ln -like "*warning:*" -or $ln -like "*error:*")
  130.           {$status += "`n`t" + $ln.trim()}
  131.         }
  132.         foreach($ln in (dcdiag /test:CheckSecurityError /s:$srv /v))
  133.         { if($ln -like "*warning:*" -or $ln -like "*error:*")
  134.           {$status += "`n`t" + $ln.trim()}
  135.         }
  136.       }#checkdns
  137.      
  138.       if($checkDHCP)
  139.       {
  140.         ##########################
  141.         #add code to check DHCP data
  142.       }#checkDHCP
  143.      
  144.       if($checkShares)
  145.       { foreach($share in (gwmi win32_share -computername $srv | where {$_.status -ne "OK"}))
  146.         { $status += "`n`t! Share: " + $share.Name + " is " + $share.status
  147.           $errcnt += 1
  148.         }      
  149.       }#checkShares
  150.      
  151.       if($checkExc)
  152.       { foreach($exc in (gwmi ExchangeConnectorState -namespace "root\cimv2\applications\exchange" -computername $srv | where {$_.IsUp -ne $true}))
  153.         { $status += "`n`t! Exchange Connector: " + $exc.Name + " is DOWN"
  154.           $errcnt += 1
  155.         }
  156.        
  157.         foreach($exc in (gwmi ExchangeQueue -namespace "root\cimv2\applications\exchange" -computername $srv | where {$_.NumberOfMessages -gt 1}))
  158.         { $status += "`n`t! Exchange Queue: " + $exc.QueueName + " has " + $exc.NumberofMessages
  159.           $errcnt += 1
  160.         }
  161.        
  162.         foreach($exc in (gwmi Exchange_Logon -namespace "root\microsoftexchangev2" -computername $srv | where {$_.Latency -gt 60000}))
  163.         { $status += "`n`t! Exchange Logons: " + $exc.MailboxDisplayName + " in Store " + $exc.StoreName + " has a latency of " + $exc.Latency
  164.           $errcnt += 1
  165.         }
  166.        
  167.       }#checkExc
  168.      
  169.       if($checkCitrix)
  170.       { $mfserver = New-Object -com "MetaframeCom.MetaFrameServer"
  171.         $mfserver.initialize(6,$srv)
  172.         $status += "`n`t Sesion Count " + $mfserver.SessionCount
  173.        
  174.         switch($mfserver.WinServerObject.EnableLogon)
  175.         {
  176.           "0" { $status += "`n`t! Not Allowing Connections"
  177.                 $errcnt += 1
  178.               }
  179.           "1" {}
  180.         }
  181.       }#checkCitrix
  182.      
  183.       if($checkIIS)
  184.       { $pathToTest = "\\$srv\c$\windows\system32\inetsrv\metabase.xml"
  185.         if(test-path $pathToTest)
  186.         { $mb=[xml](get-content $pathToTest)
  187.        
  188.           $WebSites = $mb.configuration.MBProperty.IIsWebServer
  189.           $WebVDirs = $mb.configuration.MBProperty.IIsWebVirtualDir
  190.          
  191.           If(!($mb.configuration.MBProperty.IIsWebService.Custom | where {$_.name -eq "ServerComment"}).value)
  192.           { $WebServerName = "[UNKNOWN]"  }
  193.           Else
  194.           { $WebServerName = ($mb.configuration.MBProperty.IIsWebService.Custom | where {$_.name -eq "ServerComment"}).value }
  195.          
  196.           If($WebServerName -ne $srv)
  197.           { $status += "`n`t! WebServer Name Mismatch $WebServerName, $srv"  
  198.             $errcnt += 1
  199.           }
  200.        
  201.           ForEach ($Site in $WebSites)
  202.           { if($Site.ServerComment -notlike "Allows*")
  203.             { $sitePath = "$srv" + ($site.location -replace "/LM","")
  204.               $siteState = ([ADSI]("IIS://$sitepath")).serverstate
  205.               if($siteState -ne "2")
  206.               { switch($sitestate)
  207.                 { "1" {$siteState = "starting"}
  208.                   "2" {$siteState = "started"}
  209.                   "3" {$siteState = "stopping"}
  210.                   "4" {$siteState = "stopped"}
  211.                   "5" {$siteState = "pausing"}
  212.                   "6" {$siteState = "paused"}
  213.                   "7" {$siteState = "continuing"}
  214.                 }
  215.                 $status += "`n`t! " + $Site.ServerComment + " is not running: state " + $sitestate
  216.                 $errcnt += 1
  217.               }
  218.             }
  219.           }
  220.        
  221.         }  
  222.         else
  223.         { $status += "`n`t! Unable to verify IIS Metabase"
  224.           $errcnt += 1
  225.         }
  226.       }#checkIIS
  227.     }
  228.     else
  229.     { $status += "`n`t! Server Unreachable"
  230.       $errcnt += 1
  231.     }
  232.    
  233.     if($error[0])
  234.     { $status += "`n`t Errors Occured: "
  235.       foreach($err in $error)
  236.       { $status += "`n`t`!" + $err }
  237.     }
  238.     elseif($errCnt -eq 0)
  239.     {$status += "`n***$srv*** Healthy" }
  240.     else
  241.     {$status += "`n***$srv*** Unhealthy"}
  242.     $status
  243.     $status = ""
  244.   }#for
  245. }#checkSystem
  246. #############
  247.  
  248. ########################################################
  249. #Execute script
  250. ############
  251.  
  252. checkSystem
  253. ########################################################
  254. #reset error handling
  255. #$Global:ErrorActionPreference=$SavedEA

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me