----------------------------------------------------------------
BEFORE YOU POST, search the faq at <http://java.apache.org/faq/>
WHEN YOU POST, include all relevant version numbers, log files,
and configuration files.  Don't make us guess your problem!!!
----------------------------------------------------------------


Here is what we do:

We have a little java servlet "ping" that returns "I AM ALIVE" whenever
its called.

We have a little java program "fetchURL" that opens up URLs and gets their 
contents as a string.

We wrote a little script that:
 o calls fetchURL, telling it to open the URL to the servlet
 o looks at the text returned
 o if it contains "I AM ALIVE", the server is alive and kicking
 o if it is anything else, jserv is dead
 o if it is dead, it sends an email to our pager

We then put this script in cron to run every 10 mins.

Works like a champ.

I am attaching modified versions of ping, fetchURL and the pingJServ script
that we use. Feel free to use them. You will need to modify the pingJServ
script and put in the URL to the ping servlet and the email of your pager.

If you do use it, please let me know.

Thanks,
naeem


-----------------------
ping.java (the servlet)
-----------------------

import java.lang.*;
import java.util.*;
import java.text.*;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * A little servlet that returns a string upon being called
 * Can be used to check if jserv is alive
 *
 * @author Naeem Bari
 */

public class ping extends HttpServlet
{
  private String str = "I AM ALIVE";

  public void service(HttpServletRequest request, HttpServletResponse
response)
    throws ServletException, IOException
  {
    response.setContentType("text/html");
    PrintWriter out = new PrintWriter(response.getWriter());
    
    out.println(str);
    out.close();
  }
}

===========================================================
fetchURL.java
-------------

import java.io.*;
import java.net.*;

/**
 * Fetches a url and returns the contents as a string
 *
 * @author Naeem Bari
 */

public class fetchURL
{
  /**
   * open a url and fetch its contents
   *
   * @param url to open
   * @return content
   */   
  public String fetchURL(String sURL)
  {
    StringBuffer res = new StringBuffer("");
    String inputLine;
    URL url;
    URLConnection uc;

    if (sURL == null)
    {
      return "Can't open null URL";
    }  
        
    try
    {
      url = new URL(sURL);
      uc = url.openConnection();
      
      BufferedReader br = new BufferedReader(new
InputStreamReader(uc.getInputStream()));
      while((inputLine = br.readLine()) != null)
      {
        res.append(inputLine).append("\n");
      }
      br.close();
    }
    catch (MalformedURLException me)
    {
      me.printStackTrace();
      return me.getMessage();
    }
    catch (IOException ie)
    {
      ie.printStackTrace();
      return ie.getMessage();
    }
        
    return res.toString(); 
  }

  public static void main (String[] args)
  {
    fetchURL f = new fetchURL();
    System.out.println(f.fetchURL(args[0]));
  }
}

===========================================================
pingJserv (the script)
----------------------

#!/bin/tcsh

set url = http://localhost/servlets/ping
set email = [EMAIL PROTECTED]

set reply = `java fetchURL $url | grep -c "I AM ALIVE"`

if ($reply != 1) then
  echo "server down"
  echo "mw server down" | /bin/Mail -s "jserv down" $email
endif


--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives and Other:  <http://java.apache.org/main/mail.html>
Problems?:           [EMAIL PROTECTED]

Reply via email to