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.
[sourcecode language=”powershell”]
$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,nnVirtual center server or its component is down, please investigate!!!nn"
$emailfooter=@"
Regards,
Monitoring Script
"@
foreach ($vc in $vcservers)
{
$emailbody+="nnVirtual Center server: ${vc}n"n Test-connection from mgmt box to $VC is OK.
if ($testcon=Test-Connection $vc -ErrorAction SilentlyContinue){
$emailbody+="n"nService $($qservice.name) is not running
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+="n"nService $($qservice.name) is running
}else{
$emailbody+="n"nCould not query service: ${service}
}
}else {
$status=$false
$emailbody+="n"n Test-connection from mgmt box to $VC has failed, please check whether VC server is up and network is working.`n"
}
}
}else {
$status=$false
$emailbody+="
}
}
$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"
}
[/sourcecode]
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.