/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
// UrlWatchdog
// ************ package ******************************************************
package mod.UptimeModule;
// ************ imports ******************************************************
import java.util.Date;
import java.io.*;
import java.net.*;
import KFM.HTML.HtmlLoader2;
import KFM.DateTimeServices.KFM_DateTimeService;
import HTTPClient.*;
/****************************************************************************
* UrlWatchdog
*
* Tests periodically a given URL and checks if the result pages contains the
* given label. Errors will be reported via mail.
*
* A documentation for the watchdog can be found at:
* www-docs/password/Portal/EmployeePortal/Operation/BetreiberDoku.html
* @version 0.1 (00 09 21)
*
****************************************************************************/
public class UrlWatchdog extends Thread implements Watchdog
{
// ************************************************************
// Variables
// ************************************************************
/** Message sent when page not found. */
private String mNotLoadedMessage = null;
/** Message sent when page found. */
private String mLoadedMessage = null;
/** Message sent when page found, but did not contain label. */
private String mLabelNotFoundMessage = null;
/** Message sent if WatchdogObserver terminates UrlWatchdog */
private String mTerminateMessage = null;
/** UptimeTester */
private UptimeTester2 mUptimeTester;
/** WatchdogObserver */
private WatchdogObserver mWatchdogObserver;
/** Protocol */
private String mProtocol;
/** Host */
private String mHost;
/** URL */
private String mUrl;
/** File */
private String mFile;
/** RunsPerInterval */
private int mRunsPerInterval;
/** PauseBetweenIntervals */
private int mPauseBetweenIntervals;
/** NumberOfIntervals */
private int mNumberOfIntervals;
/** TimeoutValue */
private int mTimeoutValue;
/** Port to use */
private int mPort;
/** Timeout mail already sent */
private boolean mProblemMailSent;
/** Time when error occurs */
private long mErrorTime;
/** Label */
private String mLabel;
public UrlWatchdog (
UptimeTester2 aUptimeTester,
String aUrl,
String aLabel,
int aRunsPerInterval,
int aPauseBetweenIntervals,
int aNumberOfIntervals,
int aTimeoutValue,
long aObserverTimeout)
{
// create URL and get port, host, file. These values are required for the
// HTTP get
URL tUrl;
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
try{
tUrl= new URL(aUrl);
mProtocol = tUrl.getProtocol();
mPort = tUrl.getPort();
mHost = tUrl.getHost();
mFile = tUrl.getFile();
}catch (MalformedURLException me){
System.out.println("UrlWatchdog terminates: "+me);
System.exit(1);
}
mUptimeTester = aUptimeTester;
mRunsPerInterval = aRunsPerInterval;
mPauseBetweenIntervals = aPauseBetweenIntervals;
mNumberOfIntervals = aNumberOfIntervals;
mTimeoutValue = aTimeoutValue;
mLabel = aLabel;
mUrl = aUrl;
mNotLoadedMessage =
" UrlWatchdog can't load \n"
+ " " + aUrl + "\n"
+ " Server is down, very slow or URL does not exist.\n";
mLabelNotFoundMessage =
" UrlWatchdog loaded " + aUrl + ",\n"
+ "but Label '" + mLabel + "' was not found.\n";
mTerminateMessage =
"Timeout: WatchdogObserver terminates UrlWatchdog. ";
mLoadedMessage =
"Module UrlWatchdog loaded \n"
+ " " + aUrl +".\n";
// Create a Watchdog Observer which monitors the UrlWatchdog. If watchdog does not
// send sign of life messages the watchdog observer terminates the program.
// This should not happen since we invoke the HTTP get method with a timeout value
// (mTimeoutValue).
mWatchdogObserver = new WatchdogObserver(aObserverTimeout, mTerminateMessage, this);
mWatchdogObserver.start();
System.out.println(KFM_DateTimeService.createTimeStamp()
+ ": UrlWatchdog started.");
}
/** Send terminate message
*/
public void sendTerminateMail ()
{
mUptimeTester.sendMail("Observer terminated UrlWatchdog", mTerminateMessage);
}
/** Runs ServerCheck
*
*/
public void run ()
{
int i=0;
boolean tError;
HtmlLoader2 tHtmlLoad = new HtmlLoader2();
mProblemMailSent = false;
// Loop for mNumberOfIntervals, 0 means unfinite
while((i++ < mNumberOfIntervals) || (mNumberOfIntervals == 0)){
int tCounter = 0;
// In every interval load file mRunsPerInterval times
while(tCounter++ < mRunsPerInterval) {
tError=false;
String tContent=null;
long tEndTime;
long tStartTime = new Date().getTime();
boolean tResult = false;
try
{
tResult = tHtmlLoad.get(mHost, mFile, mPort, mTimeoutValue, mProtocol.equalsIgnoreCase("https"));
}
catch (java.io.InterruptedIOException ioe)
{
tResult = false;
tError = true;
System.out.println(ioe);
}
tEndTime = new Date().getTime();
if (!tResult){
if (!mProblemMailSent){
mErrorTime = tEndTime;
mUptimeTester.sendMail("DOWN", mNotLoadedMessage);
mProblemMailSent=true;
}
System.out.println(KFM_DateTimeService.createTimeStamp()+": test " + i +":" +tCounter+mNotLoadedMessage);
tError=true;
}
// Send sign of life message to the watchdog observer
// if HtmlLoad.get blocks, watchdog observer terminates the program
mWatchdogObserver.setAlive(true);
// load was successful
if(!tError){
// Check if page contains given label
tContent = tHtmlLoad.getContent();
if (tContent.indexOf(mLabel) == -1){
if (!mProblemMailSent){
mErrorTime = tEndTime;
mUptimeTester.sendMail("LABEL not found", mLabelNotFoundMessage);
mProblemMailSent=true;
}
System.out.println(KFM_DateTimeService.createTimeStamp()
+ ": test " + i +":" + tCounter+ mLabelNotFoundMessage);
tError=true;
}
// in case of previously sent problem mail, inform user that problem is solved now
if (!tError && mProblemMailSent){
mUptimeTester.sendMail("AGAIN UP AFTER " +(tEndTime-mErrorTime)/1000 +"s; "+(tEndTime - tStartTime)+" ms for last load", mLoadedMessage);
mProblemMailSent=false;
}
if (!tError){
System.out.println(KFM_DateTimeService.createTimeStamp()
+ ": test " + i+":"+tCounter +" for Url "+ mUrl +" passed in "
+ (tEndTime - tStartTime) + " ms. Server is up.");
}
}
} // while
// Pause between two intervals
try{
sleep(mPauseBetweenIntervals*1000);
} catch (InterruptedException ie){}
} // while
System.exit(0);
} // run
}