Skip to content

Commit

Permalink
Fix TCP socket false-positive
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Tinard committed May 19, 2018
1 parent 07796e4 commit 13c6e24
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/edraens/monitoring/BatchCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ private static void Service() {
Object obj = parser.parse(new FileReader("config.json"));
JSONObject jsonObject = (JSONObject) obj;
String servername = (String) jsonObject.get("servername");
int timeout = ((Long) jsonObject.get("timeout")).intValue();

// Parsing host list
JSONArray hostsList = (JSONArray) jsonObject.get("services");
for (Object host : hostsList) {
JSONObject jsonHost = (JSONObject) host;
// Checking host status
Service item = new Service((String) jsonHost.get("host"), (String) jsonHost.get("name"), ((Long)jsonHost.get("port")).intValue());
Service item = new Service((String) jsonHost.get("host"), (String) jsonHost.get("name"), ((Long)jsonHost.get("port")).intValue(), timeout);
if (item.getStatus()) {
// Host is up
System.out.println("[OK] Service up : " + item.getName() + " (" + item.getHost() + " TCP " + item.getPort() + ")");
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/edraens/monitoring/Service.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.edraens.monitoring;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.apache.commons.validator.routines.InetAddressValidator;

/**
* Service
*/
Expand All @@ -11,17 +15,22 @@ public class Service {
private String host;
private String name;
private int port;
private int timeout;

public Service(String host, String name, int port) {
public Service(String host, String name, int port, int timeout) {
this.host = host;
this.port = port;
this.name = name;
this.timeout = timeout;
}

public boolean getStatus() {
boolean result = false;
try {
(new Socket(host, port)).close();
InetAddress ip = InetAddress.getByName(host);
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip.getHostAddress(), port), timeout*1000);
socket.close();
result = true;
} catch (IOException e) {
result = false;
Expand Down

0 comments on commit 13c6e24

Please sign in to comment.