2

I have a list of IPs in a json file . My java application should do the following.

  1. Read IPs from the JSON file. [this has been taken care of]
  2. Should check if the machines mentioned in the JSON file are up and running.
  3. If the machine is up, my application should read the processes that are running in that computer.

Other information:

  1. My java application would run on a Linux OS
  2. The machines mentioned in the JSON file are also on Linux
  3. My application is a standalone java application.

Can you please suggest the options I have to perform this task?

Andy
  • 5,433
  • 6
  • 31
  • 38

3 Answers3

4

One option is to install/enable SSH Servers in the remote machines, then

  1. Read IPs from JSON
  2. do something like a ping to each IP to check if its up. In java, you could use something like InetAddress#isReachable() for this.
  3. connect to the IP using SSH and issue linux commands like ps etc to get the list of running processes in the machine. In java you could use any of the available SSH libraries. Eg: jsch

You can even skip the step 2 and straight away try the SSH connection, and determine if host is up that way.. but pinging will be more reliable (but make sure no firewall settings block the remote machines from responding to pings).

Update

Instead of InetAddress#isReachable(), you can use Runtimes's exec command to run a native ping command. check this

Jos
  • 2,015
  • 1
  • 17
  • 22
  • 1
    I think I would skip the `ping`. If the `ssh` responds, the machine is up and running. (I've had to deal with systems where everything was configured to not respond to `ping`.) – bradimus Feb 24 '16 at 16:48
  • thanks for your response.I understand that I can ping the machines to see if they are up and use the command *ps -e* to get the processes that are running. **But I am looking for a way to do the above mentioned points in Java** P.S: I have very limited knowledge in Linux – Andy Feb 24 '16 at 16:55
  • Seems like `InetAddress#isReachable()` [isn't really reliable](http://stackoverflow.com/questions/9922543/why-does-inetaddress-isreachable-return-false-when-i-can-ping-the-ip-address). – Clark Kent Feb 24 '16 at 17:14
  • If the network and remote systems are reliable enough such that `if server is up, we will be able to connect via SSH`, then you can skip step 2, instead determine if system is up or not based on success of SSH connection. – Jos Feb 24 '16 at 17:18
  • @redflar3 `.. but pinging would be more reliable` If by pinging you mean issuing a `ping` command. – Clark Kent Feb 24 '16 at 17:25
  • i mean the underlying mechanism, i understand linux uses ICMP and windows uses some port based echo thing... in linux case since it uses ICMP and ICMP is not port specific, chances of firewall issues are less.. thats why i meant more reliable... also pls check my update. – Jos Feb 24 '16 at 17:29
1

You could try to use Zabbix or some light weight analog with JMX

Retardust
  • 294
  • 1
  • 11
1
  1. For parsing json there are pretty much different json formatters. As like gson, Jackson, boon.
  2. On your computer ping command may not exist. So, more accurate way to check availability is to use ()

    InetAddress address = InetAddress.getByName("127.0.0.1");
    boolean reachable = address.isReachable(10000);
    

    But there might be issues. So, if you doesn't fear platform dependency, you can use UNIX ping command as follow:

    Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
    int returnVal = p1.waitFor();
    boolean reachable = (returnVal==0);
    
  3. For 3rd step you could use JSch

Community
  • 1
  • 1
Solorad
  • 904
  • 9
  • 21