Solved

script to monitor whether application URL available and trigger alert mail on the same

Posted on 2016-08-26
11
81 Views
Last Modified: 2016-08-31
I have two application URL like http://www.sample.com:1200/fyi/logon.html and https://servername.net:1200/fyi/logon_face.html.
After restarting the application services in windows server 2012, we try to load this URL in website to confirm its availability. I'm trying to automate it and send alert mail with URL status like URL is available or URL is down. I'm new to scripting so kindly help me with the code
Also please do note that webpage doesn't load, unless you give full URL with /logon.html etc.
0
Comment
Question by:vishnu adithya
  • 7
  • 4
11 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 41771491
Here's some code to get you started. It takes an url, and shows you the raw code.
What you can do it to check if this source code is what you'd expect from a correctly running server and then fire off an email command.
After you understand it, we can take further steps to tailor it to your needs.

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

dim csURL

check1()
check2()

Function check1()
  csURL             = "http://test.com/"
  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = getURL(csURL)
  msgbox sHTML
	'check something 
End Function

Function check2()
  csURL             = "http://test2.com/"
  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = getURL(csURL)
  msgbox sHTML
	'check something else
End Function

Function getURL(sURL)
  Dim oHTTP : Set oHTTP = CreateObject("Msxml2.XMLHTTP")
  oHTTP.Open "GET", csURL, False
  oHTTP.Send
  If 200 = oHTTP.Status Then
     getURL = oHTTP.responseText
  Else
     ' handle error\
	 'mail command
  End If
End Function ' getURL

Function mailcode(url)

End Function

Open in new window

1
 

Author Comment

by:vishnu adithya
ID: 41773265
Thanks a lot .... i will try it out
0
 

Author Comment

by:vishnu adithya
ID: 41773827
Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

dim csURL

check1()
check2()

Function check1()
  csURL             = "https://www.experts-exchange.com/"
  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = getURL(csURL)
  'msgbox sHTML
	'check something 
End Function

Function check2()
  csURL             = "http://www.aspsnippets.com/Articles/Send-email-using-Gmail-SMTP-Mail-Server-in-ASPNet.aspx"
  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = getURL(csURL)
  'msgbox sHTML
	'check something else
End Function

Function getURL(sURL)
  Dim oHTTP : Set oHTTP = CreateObject("Msxml2.XMLHTTP")
  oHTTP.Open "GET", csURL, False
  oHTTP.Send
  If 200 = oHTTP.Status Then
     'getURL = oHTTP.responseText
     dim status = "Up" 
     mailcode (status)
  Else
     dim status = "Down"
     mailcode (status)
  End If
End Function ' getURL

Function mailcode(url)
dim sta = url
Set MyEmail=CreateObject("CDO.Message")

MyEmail.Subject="alertmail"
MyEmail.From="name@domain.com"
MyEmail.To="vishnuadithya52@gmail.com"
MyEmail.TextBody="Application services is &sta& "

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2

'SMTP Server
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.gmail.com"

'SMTP Port
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=465 

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
'Username
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="" 
'Password
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")=""

MyEmail.Configuration.Fields.Update
MyEmail.Send

set MyEmail=nothing


End Function

Open in new window

0
 

Author Comment

by:vishnu adithya
ID: 41773829
i added a mail function to get triggered based on ping status but unfortunately its not working
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 41774585
While the code is somewhat reliable, I made these two lines for a reason:

  'msgbox sHTML
	'check something 

Open in new window


Your check for the http 200 status, only tells you if the website is up. Not if it's displaying the correct page (could be a bandwidth alert, hacked site, script error etc etc). The 'check something' is to capture, for instance, the login form, or the correct header for the website. While still not always 100% correct (for instance, only after logging in you will get an error), it's far superior than just checking for the http 200 status.
Anyway, you can now continue finishing the script as you wish (or not, since it's working properly right now with below code).


Here's the updated code:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim status, sta, csURL, MyEmail

check1()
check2()

Function check1()
  csURL             = "https://www.experts-exchange.com/"
  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = getURL(csURL)
  'msgbox sHTML
	'check something 
End Function

Function check2()
  csURL             = "http://www.aspsnippets.com/Articles/Send-email-using-Gmail-SMTP-Mail-Server-in-ASPNet.aspx"
  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = getURL(csURL)
  'msgbox sHTML
	'check something else
End Function

Function getURL(sURL)
  Dim oHTTP : Set oHTTP = CreateObject("Msxml2.XMLHTTP")
  oHTTP.Open "GET", csURL, False
  oHTTP.Send
  If 200 = oHTTP.Status Then
     'getURL = oHTTP.responseText
     status = "Up" 
     mailcode status, sURL
  Else
     status = "Down"
     mailcode status, sURL
  End If
End Function ' getURL

Function mailcode(status, url)

Set MyEmail=CreateObject("CDO.Message")

MyEmail.Subject="alertmail"
MyEmail.From="name@domain.com"
MyEmail.To="vishnuadithya52@gmail.com"
MyEmail.TextBody="Application services " & url & " is " & status

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2

'SMTP Server
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.gmail.com"

'SMTP Port
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=465 

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
'Username
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="" 
'Password
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")=""

MyEmail.Configuration.Fields.Update
MyEmail.Send

set MyEmail=nothing

End Function

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:vishnu adithya
ID: 41774820
Thanks Kim for your feedback and i agree, but i'm not sure on how to compare the webpage contents  ??
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 41774890
First, open the webpage in your browser (using anonymous/incognito mode), and view the source.
Find what is unique about this working page and use a string from it. That's all!

Adjust script again, as needed (right now you'll get two updates, one if the site is up/down, one if the site returns what you'd expect or not). If a site gets redesigned, you'll probably have to adjust your script again.

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim status, sta, csURL, MyEmail

check1()
check2()

Function check1()
  csURL             = "https://www.experts-exchange.com/"
  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = getURL(csURL)
  	if Instr(sHTML, "/plansAndPricing.jsp?cid=topnavjoin") then
		mailcode "working properly", csURL
	else
		mailcode "probably not working properly", csURL
	end if
	
End Function

Function check2()
  csURL             = "http://www.aspsnippets.com/Articles/Send-email-using-Gmail-SMTP-Mail-Server-in-ASPNet.aspx"
  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = getURL(csURL)
  
	if Instr(sHTML, "__doPostBack") then
		mailcode "working properly", csURL
	else
		mailcode "probably not working properly", csURL
	end if
	
End Function

Function getURL(sURL)
  Dim oHTTP : Set oHTTP = CreateObject("Msxml2.XMLHTTP")
  oHTTP.Open "GET", csURL, False
  oHTTP.Send
  If 200 = oHTTP.Status Then
     getURL = oHTTP.responseText
     status = "Up" 
     mailcode status, sURL
  Else
     status = "Down"
     mailcode status, sURL
  End If
End Function ' getURL

Function mailcode(status, url)

Set MyEmail=CreateObject("CDO.Message")

MyEmail.Subject="alertmail"
MyEmail.From="name@domain.com"
MyEmail.To="vishnuadithya52@gmail.com"
MyEmail.TextBody="Application services " & url & " is " & status

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2

'SMTP Server
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.gmail.com"

'SMTP Port
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=465 

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
'Username
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="" 
'Password
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")=""

MyEmail.Configuration.Fields.Update
MyEmail.Send

set MyEmail=nothing

End Function

Open in new window

0
 

Author Comment

by:vishnu adithya
ID: 41774993
Thanks a lot Kim, i will try and let you know
0
 

Author Comment

by:vishnu adithya
ID: 41775041
(sHTML, "/plansAndPricing.jsp?cid=topnavjoin") in this you are looking for planandpricing.jsp page if so below action take places. so what it cid=topnavjoin ?
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 41775869
No, I said, you are looking for something unique on a working page. A non-user watching Expers Exchange, will always see this link if the page is working properly (until the page is redesigned).
It could also have been
	if Instr(sHTML, "Search Experts Exchange") then

Open in new window

As long as it's something unique on that page. Since a non-working page will have simple text like "Database down" or "Bandwidth problems" etc, when you get the error that the page isn't working, even though there's also a status the site is up, just visit the site to check what the error is.
0
 

Author Comment

by:vishnu adithya
ID: 41776057
Thanks a lot for your help & support and i understood.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Map JavaScript API 4 39
Need to click button twice to get results 2 32
Check if Object property exists? 3 18
Message Logon Windows 2012 R2 Server 16 11
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now