Skip to content

Commit

Permalink
support new parse type 'log' in ssh collect protocol (#1410)
Browse files Browse the repository at this point in the history
Signed-off-by: tomsun28 <tomsun28@outlook.com>
  • Loading branch information
tomsun28 committed Mar 10, 2024
1 parent abad5aa commit 57f4f98
Showing 1 changed file with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class SshCollectImpl extends AbstractCollect {
private static final String PARSE_TYPE_ONE_ROW = "oneRow";
private static final String PARSE_TYPE_MULTI_ROW = "multiRow";
private static final String PARSE_TYPE_NETCAT = "netcat";
private static final String PARSE_TYPE_LOG = "log";

private static final int DEFAULT_TIMEOUT = 10_000;

Expand Down Expand Up @@ -110,6 +111,9 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
return;
}
switch (sshProtocol.getParseType()) {
case PARSE_TYPE_LOG:
parseResponseDataByLog(result, metrics.getAliasFields(), builder, responseTime);
break;
case PARSE_TYPE_NETCAT:
parseResponseDataByNetcat(result, metrics.getAliasFields(), builder, responseTime);
break;
Expand All @@ -120,7 +124,8 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
parseResponseDataByMulti(result, metrics.getAliasFields(), builder, responseTime);
break;
default:
parseResponseDataByMulti(result, metrics.getAliasFields(), builder, responseTime);
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg("Ssh collect not support this parse type: " + sshProtocol.getParseType());
break;
}
} catch (ConnectException connectException) {
Expand Down Expand Up @@ -169,6 +174,24 @@ public String supportProtocol() {
return DispatchConstants.PROTOCOL_SSH;
}

private void parseResponseDataByLog(String result, List<String> aliasFields, CollectRep.MetricsData.Builder builder, Long responseTime) {
String[] lines = result.split("\n");
if (lines.length + 1 < aliasFields.size()) {
log.error("ssh response data not enough: {}", result);
return;
}
for (String line : lines) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String alias : aliasFields) {
if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
valueRowBuilder.addColumns(responseTime.toString());
} else {
valueRowBuilder.addColumns(line);
}
}
builder.addValues(valueRowBuilder.build());
}
}

private void parseResponseDataByNetcat(String result, List<String> aliasFields, CollectRep.MetricsData.Builder builder, Long responseTime) {
String[] lines = result.split("\n");
Expand Down Expand Up @@ -267,9 +290,9 @@ private void removeConnectSessionCache(SshProtocol sshProtocol) {
private ClientSession getConnectSession(SshProtocol sshProtocol, int timeout, boolean reuseConnection)
throws IOException, GeneralSecurityException {
CacheIdentifier identifier = CacheIdentifier.builder()
.ip(sshProtocol.getHost()).port(sshProtocol.getPort())
.username(sshProtocol.getUsername()).password(sshProtocol.getPassword())
.build();
.ip(sshProtocol.getHost()).port(sshProtocol.getPort())
.username(sshProtocol.getUsername()).password(sshProtocol.getPassword())
.build();
ClientSession clientSession = null;
if (reuseConnection) {
Optional<Object> cacheOption = CommonCache.getInstance().getCache(identifier, true);
Expand All @@ -288,7 +311,7 @@ private ClientSession getConnectSession(SshProtocol sshProtocol, int timeout, bo
}
if (clientSession != null) {
return clientSession;
}
}
}
SshClient sshClient = CommonSshClient.getSshClient();
clientSession = sshClient.connect(sshProtocol.getUsername(), sshProtocol.getHost(), Integer.parseInt(sshProtocol.getPort()))
Expand All @@ -300,15 +323,15 @@ private ClientSession getConnectSession(SshProtocol sshProtocol, int timeout, bo
SecurityUtils.loadKeyPairIdentities(null, () -> resourceKey, new FileInputStream(resourceKey), null)
.forEach(clientSession::addPublicKeyIdentity);
} // else auth with localhost private public key certificates

// auth
if (!clientSession.auth().verify(timeout, TimeUnit.MILLISECONDS).isSuccess()) {
clientSession.close();
throw new IllegalArgumentException("ssh auth failed.");
}
if (reuseConnection) {
SshConnect sshConnect = new SshConnect(clientSession);
CommonCache.getInstance().addCache(identifier, sshConnect);
CommonCache.getInstance().addCache(identifier, sshConnect);
}
return clientSession;
}
Expand Down

0 comments on commit 57f4f98

Please sign in to comment.