Monitoring virtual center server services


If you want to monitor from your management box work of virtual center server, check if services are working, if connection is up, you could use this script.
You can still modify it in different ways. For example if you want to monitor more services, add them in $ServicesToCheck.
I am using an ‘if’ there, as i am not using default services for all vc servers. Email message is build by expanding $emailbody variable. I don’t use smtp authentication here, if you need it, you have to add part with obtaining credentials and passing them to send-mailmessage command.

$SMTPserver="xxxx.com"
[String]$SMTPfrom="greg@xxxx.com"
#if $status will be false, then it will trigger send-mailmessage
[bool]$status=$true
$vcservers="server1","server2","server3","serverN"
[String[]]$SMTPto="greg@xxx.com","your_friend@yyyy.com","nfriend@xxx.com"
#vctomcat           VMware VirtualCenter Management Web...
#vmware-ufad-vci    VMware vSphere Update Manager Service
#vpxd               VMware VirtualCenter Server
#vimQueryService    vCenter Inventory Service
$logfile="c:\scripts\vc_monitoring\vcmonitoring.log"
#checking if log file is not too big, if it is , delete 
if(Test-Path $logfile){if ((Get-ChildItem $logfile).length -gt 100KB) { rm $logfile }}
$subject="Virtual center server is down. Investigation is needed!"
$emailbody="Hello,`n`nVirtual center server or its component is down, please investigate!!!`n`n"
$emailfooter=@"


Regards,
Monitoring Script
"@
foreach ($vc in $vcservers)
{
	$emailbody+="`n`nVirtual Center server: ${vc}`n"
		if ($testcon=Test-Connection $vc -ErrorAction SilentlyContinue){
			$emailbody+="`n Test-connection from mgmt box to $VC is OK.`n"
			if($vc -eq "server1"){$ServicesToCheck="vctomcat","vpxd","vimQueryService"}
			if($vc -eq "serverN"){$ServicesToCheck="vmware-ufad-vci","vctomcat","vpxd"}
				foreach($service in $ServicesToCheck){
					if($qservice=Get-Service $service -ComputerName $vc -ErrorAction SilentlyContinue){
						if($qservice.status -ne 'Running'){
    						$status=$false
							$emailbody+="`nService $($qservice.name) is not running`n"
						}else{
							$emailbody+="`nService $($qservice.name) is running`n"
						}
					}else {
						$status=$false
						$emailbody+="`nCould not query service: ${service}`n"	
					}
				}
		}else {
			$status=$false
			$emailbody+="`n Test-connection from mgmt box to $VC has failed, please check whether VC server is up and network is working.`n"
		}
}
$emailbody+=$emailfooter
if(-not($status)){
Send-MailMessage -Subject $subject -From $SMTPfrom -SmtpServer $SMTPserver -To $SMTPto -Body  $emailbody
"VC IS DOWN at $(get-date)" >> "c:\scripts\vc_monitoring\vcmonitoring.log"
}

Make sure that the user from which this script will run, has proper rights on target VC server. If he is missing rights then the get-service will not work.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s