We have previously faced issue monitoring our windows service and websites. Whenever the service is down, functionality which is dependent on the service stops working. Continuously monitoring the services is difficult and not feasible. To solve this problem we came up with PowerShell script which will monitor the service, alerts when service is down and automatically restart the service. This blog post will explain you how you can create similar Service Monitor application which will make your work easier.
Create folder with below folder structure:
Logs Folder
It will have logs files after execution of PowerShell script
Settings.xml File
It will contain the configuration details of the PowerShell script
ServiceMonitor.ps1 File
It will contain actual code for monitoring service
Code Snippets
Settings.xml
<?xml version="1.0"?> <Settings> <SolrSettings> <SolrURL>https://localhost:8984/solr/#/</SolrURL> <SolrServiceName>Solr-Service</SolrServiceName> </SolrSettings> <EmailSettings> <ToEmail>"noreply@gmail.com"</ToEmail> <FromEmail>ServiceMonitor@gmail.com</FromEmail> <EmailSubject>Service is not working</EmailSubject> <SMTPServer>smpt.gmail.com</SMTPServer> <EmailPort>25</EmailPort> </EmailSettings> <LogSettings> <LogFileFolder>C:\User\Projects\Restart Service\Logs\</LogFileFolder> </LogSettings> </Settings>
ServiceMonitor.ps1
#Import settings from config file $MyDir = Split-Path -Parent $MyInvocation.MyCommand.Path [xml]$ConfigFile = Get-Content "$MyDir\Settings.xml" #Email Configuration [string[]]$ToEmail = $ConfigFile.Settings.EmailSettings.ToEmail $FromEmail = $ConfigFile.Settings.EmailSettings.FromEmail $EmailSubject = $ConfigFile.Settings.EmailSettings.EmailSubject $EmailSmtpServer = $ConfigFile.Settings.EmailSettings.SMTPServer $EmailPort = $ConfigFile.Settings.EmailSettings.EmailPort $ErrorEmailMessage = "" $ExceptionErrorEmailMessage="" #LogFile Details $TodayDate = Get-Date -format "dd-MM-yyyy" $LogFileFolder = $ConfigFile.Settings.LogSettings.LogFileFolder $LogFileName = $LogFileFolder+"ServiceMonitor-"+$TodayDate+".txt" $LogMessage = "" #Solr Details $SolrURL = $ConfigFile.Settings.SolrSettings.SolrURL $SolrServiceName = $ConfigFile.Settings.SolrSettings.SolrServiceName try{ $LogMessage +="`nStart - $((Get-Date).ToString())" $HTTP_Request = [System.Net.WebRequest]::Create($SolrURL) $HTTP_Response = $HTTP_Request.GetResponse() $HTTP_Status = [int]$HTTP_Response.StatusCode if($HTTP_Status -eq 200) { Write-Host "Solr Site is working" $LogMessage +="Solr Site is working`n" } else { throw "Solr website Error Occured" } } catch{ Write-host "Solr Site is down Error Occured: $_" $LogMessage +="Solr Site is down Error Occured: $_`n" #Restart Service Invoke-Command -ScriptBlock { Restart-Service -Name $args } -ArgumentList $SolrServiceName Write-Host "Service Restarted" $LogMessage +="Service Restarted`n" #Send Email $ErrorEmailMessage = "URL: "+$solrURL+"`nError Details:"+$_+"`nSolr site was down`nService Restared" Send-MailMessage -To $ToEmail -From $FromEmail -Subject $EmailSubject -Body $ErrorEmailMessage -SmtpServer $EmailSmtpServer -Port $EmailPort Write-Host "Email Sent" $LogMessage +="Email Sent`n" } finally{ # Closing Objects If ($HTTP_Response -eq $null) { } Else { $HTTP_Response.Close() } } $LogMessage +="End - $((Get-Date).ToString())" Add-Content $LogFileName $LogMessage
Sample Log File
No comments:
Post a Comment