Link to home
Start Free TrialLog in
Avatar of LeanMoreTryMore
LeanMoreTryMore

asked on

java program monitors the Web Server

Anyone who has any idea of how to write a java program to monitor the Web Application Server.
For exmaple, if the web server is down, it notifies adminsitrator by sending an email.

Notes. I know how to send e-mail. I don't know how to check whether the WAS is up and running.

Please advise.

Giving me an sample codes is greatly appricate
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LeanMoreTryMore
LeanMoreTryMore

ASKER

Thanks.

Do you have a simple codes how to using URLConnection and check response, thanks
http://javaalmanac.com/egs/java.net/Post.html

may be help ful to you for doing what objetc has suggested you.That was a simple & good suggestion.
another example here, showing how to check response code

http://java.sun.com/developer/TechTips/txtarchive/2003/Apr03_JohnZ.txt
Or just try a TCP/ IP socket connection to the port on which the web server listens. For example, if it listens on 8080:

Socket s = new Socket ( "localhost", 8080 ) ;

If an exception is thrown and it does not connect, then you know that the server is down. Keep the port-number in a properties-file. Then you can send an e-mail using:

http://www.javaalmanac.com/egs/javax.mail/SendApp.html

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class TestServer {

            public static void main(String args[]) {
                  String urlString = "http://localhost:7001/console";
                  try {
                    URL url = new URL(urlString);
                    URLConnection connection =
                        url.openConnection();
                    if (connection instanceof HttpURLConnection) {
                        HttpURLConnection httpConnection =
                         (HttpURLConnection)connection;
                        httpConnection.connect();
                        int response =
                         httpConnection.getResponseCode();
                         System.out.println(response);
                        if(response>=400)
                        System.out.println("Error: Server is not  active");
                        else
                        if(response >=200&&response<400)
                        System.out.println("Server is active");
                    }
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
              }
            }

I tested the above example on WebLogic you can change the URL & use suitably.
It works fine.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How exactly is the code posted by shivaspk any different to what I posted earlier?
You and hsivaspk gave me the same questin but i think i missed out your name.

Unfortunately I have closed this question. In my next question, i will give the points back to you. thanks
Any reason why you ignored the simplest approach described using:

>> Socket s = new Socket ( "localhost", 8080 ) ;

All you need to do is check if the web-server is up or not. URLs wil keep changing because you might keep adding/ moving/ removing web-applications and pages from the server so a URL which gives you one status-code today will give you another status-code tomorrow and then you have to make sure you handle all kinds of status-codes. Its a lot easier to just connect to the port and check if the server is connected or not.
That doesn’t guarantee that the SERVICES are running though. When we first started out looking for web server monitor s/w we were using GFI, but the email check only telneted into port 25 and checked that the server responded. In our particular case, our SMTP server was failing but was responding to the initial connection, so GFI didn’t catch the downtime and we were none the wiser.

With the code above, you can place the text "Server Up" in a database that is presented via a web page, so if you get the text "Server Up" returned, you know 100% that the web server and your database server are alive. Likewise, the only way to guarantee that your email server is working is to send and retrieve an email through it, as a 'dry' test is never going to be 100% effective.

colr__
Yes, it does not guarantee for services, and services/ their URLs can change (better to keep it in a properties-file if that's the case). The Q is simply about the web-server and about an individual service so I'll keep it to that.
What exactly does that mean?

colr__
> You know that we cannot see how the points are splitted :)

web server *is* a service :)
Is there 2 threads going on in here or something? Why has this been reopened? Why have I had my points stripped when I was already awarded them and by correct definition of the problem, I was the only one to answer?

colr__
Right, sorry, Ive never seen this before either.

colr__
:) It's ok :)
I am just trying to fix the things when needed :)

Venabili
Yes. object gave me the idea how to solve this problem. So i can start working on it.
colr gave me the same answer as obejct gave me, but colr gave me a complete source codes so that it save me alot of time. So that colr should get more points than object. I think this is fair for everyone.

By the way I do think you ppl help