Skip to content

Commit

Permalink
Merge pull request #10 from flowbehappy/zk-ping-empty
Browse files Browse the repository at this point in the history
Zk ping empty
  • Loading branch information
flowbehappy committed Apr 2, 2015
2 parents 4f3d9ac + 0720b44 commit 03f773d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 336 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ It is written in java, and use [InfluxDB v0.9](https://github.com/influxdb/influ

##Dependences

* Run
* java(1.6 or later)
* [netcat](http://netcat.sourceforge.net/)
* Compilation
* Run & Compile
* java(1.6 or later)

##Installation
Expand Down
149 changes: 0 additions & 149 deletions src/main/java/com/sf/exec/SystemCommandExecutor.java

This file was deleted.

146 changes: 0 additions & 146 deletions src/main/java/com/sf/exec/ThreadedStreamHandler.java

This file was deleted.

63 changes: 26 additions & 37 deletions src/main/java/com/sf/monitor/zk/ZookeeperHosts.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sf.exec.SystemCommandExecutor;
import com.sf.log.Logger;
import com.sf.monitor.Config;
import com.sf.monitor.Resources;
import com.sf.monitor.utils.JsonValues;
import com.sf.monitor.utils.Utils;
import org.apache.commons.io.IOUtils;

import java.util.ArrayList;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -130,44 +132,31 @@ public JsonValues getInfo() {
}

public String command(String command) {
try {
List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add(String.format("echo %s | nc %s %d", command, hostName, port));

SystemCommandExecutor executor = new SystemCommandExecutor(commands);
int result = executor.executeCommand();
String err = executor.getStandardErrorFromCommand();
String res = executor.getStandardOutputFromCommand();
if (result == 0) {
return res.trim();
} else {
log.error(
"send 4LTR command[%s] to [%s:%d] failed, error code: %d, error msg: %s",
command,
hostName,
port,
result,
err
);
return err;
// Try 10 times if we face "Connection reset" error.
String resp = null;
for (int i = 0; i < 10; i++) {
resp = doCommand(command);
if (!"Connection reset".equals(resp)) {
return resp;
}
}
return resp;
}

// The following code could throw "Connection reset" time to time,
// so use system command instead temporary!

//Socket socket = new Socket(hostName, port);
//OutputStream os = socket.getOutputStream();
//IOUtils.write(command + "\n", os);
//os.flush();
//String resp = IOUtils.toString(socket.getInputStream());
//IOUtils.closeQuietly(socket);
//return resp;

} catch (Exception e) {
log.error(e, "send 4LTR command[%s] to [%s:%d] failed", command, hostName, port);
public String doCommand(String command) {
Socket s = null;
try {
s = new Socket();
s.setSoLinger(false, 10);
s.setSoTimeout(20000);
s.connect(new InetSocketAddress(hostName, port));
IOUtils.write(command, s.getOutputStream());
return IOUtils.toString(s.getInputStream());
} catch (IOException e) {
log.warn(e, "send 4TLR command [%s] to [%s:%d] error", command, hostName, port);
return e.getMessage();
} finally {
IOUtils.closeQuietly(s);
}
}
}
Expand Down

0 comments on commit 03f773d

Please sign in to comment.