simple java server monitor

Circa 2004

Update: Check out Nagios which is an open source service monitor. This looks like a good solution. Wish I had found it before I did my little project!

ServerMonitor is a small java program that monitors a server and sends email to the administrator when the server is not available.I have been having some reliability problems with a server that I depend on and wanted an easy way to be notified if there server had stopped running. This program is very simple to use.

Use Case Diagram

Deployment Diagram

The program consists of a single java file [text] and a script to start and stop the monitor.

I need feedback regarding a service running on Server. I run a ServerMonitor daemon on Monitor that periodically checks to see if it can connect to Server on a specific port. If it can, I assume all is well. If not, ServerMonitor checks to see if Reference Host is visible. This is to distinguish between an actual failure in Server and a local problem with the connectivity of Monitor to either Server or Reference Host. If there is any failure to connect with Server then email is sent through Notification Server to the administrator.

Create a directory serverMon on Monitor and put and in that directory. Then cd to that directory and follow these directions.

Set to be executable with $chmod u+x
Compile with $javac
You should now have a directory that looks something like this.

[drew@drewsG5 serveMon]$ chmod u+x [drew@drewsG5 serveMon]$ javac [drew@drewsG5 serveMon]$ ls -la total 16 drwxr-xr-x 5 drew drew 170 May 2 22:22 . drwxr-xr-x 11 drew drew 374 May 2 22:20 .. -rw-r–r– 1 drew drew 3617 May 2 22:22 ServerMonitor.class -rw-r–r– 1 drew drew 5794 May 2 14:22 -rwxr–r– 1 drew drew 493 May 2 22:18 [drew@drewsG5 serveMon]$

Now you need to edit to set the relevant parameters.

# Edit these lines for your particular setup PORT=80 INTERVAL=10 REFHOST=refhost REFPORT=81 ADMIN=you@localhost SMTPHOST=mail.localhost

To start ServerMonitor, $./ start
To stop the monitor, $./

To see that everything is running correctly $tail -f log.out
You should see something like this:

[drew@drewsG5 serverMon]$ ./ start [drew@drewsG5 serverMon]$ tail -f log.out Starting ServerMonitor. port:25 refPort:80 interval:10 adminEmail:you@localhost smtpHost:mail.smtp.localhost [Mon May 03 17:18:31 PDT 2004] seems to be running.

Note that only one email will be sent on server failure. When the message is received, you should examine log.out for details about the failure. Then I suggest that you stop and start ServerMonitor again to check for the next problem.

# TODO: check is pid file already exists – and ServerMonitor is already running
# so that we don’t start twice.
# Check if running before we try to stop the process. i.e. make sure calling
# stop twice is not a problem. Would be nice to verify that the pid we are killing
# is actually the ServerMonitor process!

Leave a Reply

Your email address will not be published. Required fields are marked *