Skip to content

Instantly share code, notes, and snippets.

@kabir-rab
Created October 8, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kabir-rab/9edc9bbda74bdf6ce267e415549be2a9 to your computer and use it in GitHub Desktop.
Save kabir-rab/9edc9bbda74bdf6ce267e415549be2a9 to your computer and use it in GitHub Desktop.
Checks the Qlik Sense site availability and reboots all services if proxy service is not accessible
$Today = Get-Date -UFormat "%Y%m%d %H%M"
$LogFile = "\\[LOG_DIRECTORY]\SiteCheck.txt"
# Drop all the restrictions on script execution
Set-ExecutionPolicy Unrestricted
# URL to check
$uri = "[SITE_URL]"
$Result = @()
# Capturing the time taken for the request to complete
$time =
try{
$request = $null
# Request the URI, and measure how long the response took.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliseconds
}
catch{
# If the request generated an exception (i.e.: 500 server
# error or 404 not found), we can pull the status code from the
# Exception.Response property
$request = $_.Exception.Response
$time = -1
}
# Adding all the properties to result object
$result += [PSCustomObject] @{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
# Checking the status code to determine if the site is reachable with no issues. (code 200),
# If there are any issues (anything apart from code 200) which will result in services being
# restarted.
if($result -ne $null)
{ # For future, this will allow us to loop mutiple URLs and create one result set and loop
# through it.
Foreach($Entry in $Result)
{
# When its not reachable
if($Entry.StatusCode -ne "200")
{
write-Output "$Today - Site is not reachable - $Entry" | out-file $LogFile -Append
write-Output "$Today - Stoping Qlik Services......" | out-file $LogFile -Append
stop-service QlikSenseProxyService -Force -WarningAction SilentlyContinue
stop-service QlikSenseEngineService -Force -WarningAction SilentlyContinue
stop-service QlikSenseSchedulerService -Force -WarningAction SilentlyContinue
stop-service QlikSensePrintingService -Force -WarningAction SilentlyContinue
stop-service QlikSenseServiceDispatcher -Force -WarningAction SilentlyContinue
stop-service QlikLoggingService -Force -WarningAction SilentlyContinue
stop-service QlikSenseRepositoryService -Force -WarningAction SilentlyContinue
stop-service QlikSenseRepositoryDatabase -Force -WarningAction SilentlyContinue
sleep 10
write-Output "$Today - Starting Qlik Services......" | out-file $LogFile -Append
start-service QlikLoggingService -WarningAction SilentlyContinue
start-service QlikSenseRepositoryDatabase -WarningAction SilentlyContinue
start-service QlikSenseRepositoryService -WarningAction SilentlyContinue
start-service QlikSenseEngineService -WarningAction SilentlyContinue
start-service QlikSenseSchedulerService -WarningAction SilentlyContinue
start-service QlikSensePrintingService -WarningAction SilentlyContinue
start-service QlikSenseServiceDispatcher -WarningAction SilentlyContinue
start-service QlikSenseProxyService -WarningAction SilentlyContinue
sleep 10
write-Output "$Today - Checking Qlik Services......" | out-file $LogFile -Append
# Calling a custom function with service name to check if they have started ok. If they
# havent, the function below will aim to restart it.
CheckService QlikSenseRepositoryDatabase "$LogFile" "$Today"
CheckService QlikSenseRepositoryService "$LogFile" "$Today"
CheckService QlikSenseEngineService "$LogFile" "$Today"
CheckService QlikSenseSchedulerService "$LogFile" "$Today"
CheckService QlikSensePrintingService "$LogFile" "$Today"
CheckService QlikSenseServiceDispatcher "$LogFile" "$Today"
CheckService QlikSenseProxyService "$LogFile" "$Today"
CheckService QlikLoggingService "$LogFile" "$Today"
}
else
{
write-Output "$Today - Site is reachable - :$Entry" | out-file $LogFile -Append
}
}
}
write-Output "$Today - Completed Check" | out-file $LogFile -Append
#Custom function to see if the service is running, if not, restart it.
function CheckService
{
param($ServiceName,$LogFile,$Today)
$arrService = get-service -Name $ServiceName
if ($arrService.Status -ne "Running")
{
start-service $ServiceName -WarningAction SilentlyContinue
write-output "$Today - Starting " $ServiceName "..." | out-file $LogFile -Append
sleep 10
$result = if (($_ | get-service).Status -eq "Running") {"success"} else {"failure"}
write-output "$Today - Service $($_.Name) has been restarted with $result." | out-file $LogFile -Append
}
if ($arrService.Status -eq "running")
{
write-output "$Today - $ServiceName service has already started" | out-file $LogFile -Append
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment