Skip to content

Instantly share code, notes, and snippets.

@shanet
Created March 18, 2013 17:18
Show Gist options
  • Select an option

  • Save shanet/5188968 to your computer and use it in GitHub Desktop.

Select an option

Save shanet/5188968 to your computer and use it in GitHub Desktop.

Revisions

  1. shanet created this gist Mar 18, 2013.
    56 changes: 56 additions & 0 deletions Monitor.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    import java.net.InetAddress;
    import java.net.Socket;

    import java.io.IOException;
    import java.net.UnknownHostException;

    public class Monitor {

    public static void main(String[] args) {
    if(args.length < 2) {
    System.out.println("Bad number of arguments.");
    return;
    }

    String server = args[0];
    int port = Integer.valueOf(args[1]);

    try {
    InetAddress ina = InetAddress.getByName(server);

    if(ina.isReachable(3000)) {
    System.out.println("Server is up.");
    } else {
    System.out.println("Server is down!");
    return;
    }

    } catch (IOException ioe) {
    ioe.printStackTrace();
    return;
    }

    Socket socket = null;
    try {
    socket = new Socket(server, port);

    if(socket.isConnected()) {
    System.out.println("Service is up on port " + port + ".");
    } else {
    System.out.println("Service is down on port " + port + "!");
    }
    } catch (IOException ioe) {
    ioe.printStackTrace();
    } finally {
    if(socket != null && socket.isConnected()) {
    try {
    socket.close();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    }

    return;
    }
    }