Shell Scripts Check URL Status


You can  use these scripts to check  website availability  and send alert  to  HP  Business Service Management.

Powershell  Script:

Powershell script  will take the URL parameter from command line and  check Website availability. You should  run powershell script  with these  parameters  set. <OpenMessageInterfaceName>  used  for  HP Operation  Manager. So  you can define  this  parameter as you wish.

#powershell -ExecutionPolicy ByPass -File URL.ps1 <URL>  <OpenMessageInterfaceName>

KornShell Script:

This Script will take the list of URLs from a text tile (URL.list). So before  running  unix  script create a URL.list  file. First  parameter  is control  type, second  is  URL and  last  one  is  Responsible  Group  information that will give  information about  alert.

#/bin/ksh /unixadmin/URL/URL_Control.sh

PowerShell URL  Control:

#powershell -ExecutionPolicy ByPass -File URL.ps1 <URL>  <OpenMessageInterfaceName>
##############################################################################
##### Abdurrahim YILDIRIM                                               ######
##############################################################################
$url = $args[0]
$Control = $args[1]
$Servername = $env:computername
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
try {
$r = [System.Net.WebRequest]::Create($url)
$r.UserAgent=$userAgent
$r.Timeout=40000;
$resp = $r.GetResponse()
#Write-host "Response Headers:"
foreach ($HeaderKey in $resp.Headers) {
$caption = $HeaderKey.PadLeft(15," ")
#Write-host "$caption`: $($resp.Headers[$HeaderKey])";
}
$reqstream = $resp.GetResponseStream()
$sr = New-Object System.IO.StreamReader $reqstream
$body = $sr.ReadToEnd()
#Write-host "$body"
$resp.StatusCode
}
catch [System.Net.WebException]
{
$resp = $_.Exception.Response
if ($resp -eq $null)
{
#Write-host $_.Exception
}
else
{
$reqstream = $resp.GetResponseStream()
$sr = New-Object System.IO.StreamReader $reqstream
$body = $sr.ReadToEnd()
#Write-host -Text "Response Headers:"
Echo " Status: $([int]$resp.StatusCode) - $($resp.StatusCode)"
#$URL_Status = $([int]$resp.StatusCode)
#$URL_Check = $($resp.StatusCode)
foreach ($HeaderKey in $resp.Headers) {
$caption = $HeaderKey.PadLeft(15," ")
#Write-host "$caption`: $($resp.Headers[$HeaderKey])";
}
#Write-host "$body"
$resp.StatusCode
}
} catch {
#Write-host $_.Exception
}
$URL_Status = $([int]$resp.StatusCode)
$URL_Check = $($resp.StatusCode)
if ($URL_Check -ne $null) {
If ($URL_Status -eq 200 -or $URL_Status -eq 400 -or $URL_Status -eq 401 -or $URL_Status -eq 403 -or $URL_Status -eq 404 -or $URL_Status -eq 405 -or $URL_Status -eq 500 ) {
Write-Host "Site is OK on $Servername "
$path = "C:\URL_Control\$Control"
if ($path | Test-Path) {
del C:\URL_Control\$Control
Write-Host "Site is Cleared for - $URL - $Servername"
opcmsg a=$Control o=$Control s=Normal msg_text="$Servername Server $URL control fixed" node=$Servername
}
}
Else {
$URL_Status | out-file C:\URL_Control\$Control
Write-Host "The Site may be down, please check connection for - $URL - $Servername "
opcmsg a=$Control o=$Control s=Critical msg_text="$Servername Server $URL control failed"  node=$Servername
}
}
Else {
$URL_Status | out-file C:\URL_Control\$Control
Write-Host "The Site may be down, please check connection for - $URL - $Servername "
opcmsg a=$Control o=$Control s=Critical msg_text="$Servername Server $URL control failed"  node=$Servername
}

KornShell URL Control:

# cat /unixadmin/URL/URL.list
CASESUP_BLOG http://casesup.com/blog  CASESUP_ADMIN
#!/bin/ksh
BASE=/unixadmin/URL
Servername=`hostname`
while read i
do
AlertOwner=`echo $i |awk '{print $3}'`
URL=`echo $i |awk '{print $2}'`
URL_Detail=`echo $i |awk '{print $1}'`
if /usr/bin/curl --insecure --connect-timeout 10 --max-time 11 $URL >/dev/null 2>&1
then
if [[ -e $BASE/$URL_Detail ]]
then
/opt/OV/bin/opcmsg a=$URL_Detail o=$URL_Detail s=Normal msg_text="$Servername server $URL control fixed !!!" node=$Servername
rm -rf $BASE/$URL_Detail
fi
else
if [[ -e $BASE/$URL_Detail ]]
then
echo "Dublicated Alert"
else
touch $BASE/$URL_Detail
/opt/OV/bin/opcmsg a=$URL_Detail o=$URL_Detail s=critical msg_text="$Servername server $URL control failed . Call $AlertOwner !!!"  node=$Servername
fi
fi
done < URL.list

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me