[collector,manager]linux监控类型新增内存,磁盘,网络监控指标

This commit is contained in:
tomsun28
2022-03-12 10:51:22 +08:00
parent 25d692cf37
commit f52247df67
3 changed files with 122 additions and 13 deletions

View File

@@ -21,9 +21,9 @@ import java.util.concurrent.TimeUnit;
public class CommonCache {
/**
* 默认缓存时间 30minute
* 默认缓存时间 10minute
*/
private static final long DEFAULT_CACHE_TIMEOUT = 30 * 60 * 1000L;
private static final long DEFAULT_CACHE_TIMEOUT = 10 * 60 * 1000L;
/**
* 默认最大缓存数量
@@ -155,6 +155,15 @@ public class CommonCache {
});
}
/**
* 新增或更新cache
* @param key 存储对象key
* @param value 存储对象
*/
public void addCache(Object key, Object value) {
addCache(key, value, DEFAULT_CACHE_TIMEOUT);
}
/**
* 根据缓存key获取缓存对象
* @param key key

View File

@@ -57,23 +57,23 @@ public class SshCollectImpl extends AbstractCollect {
}
SshProtocol sshProtocol = metrics.getSsh();
// 超时时间默认300毫秒
int timeout = 300;
int timeout = 3000;
try {
timeout = Integer.parseInt(sshProtocol.getTimeout());
} catch (Exception e) {
log.warn(e.getMessage());
}
try {
ClientSession clientSession = getConnectSession(sshProtocol);
ClientSession clientSession = getConnectSession(sshProtocol, timeout);
ClientChannel channel = clientSession.createExecChannel(sshProtocol.getScript());
ByteArrayOutputStream response = new ByteArrayOutputStream();
channel.setOut(response);
if (!channel.open().verify(Integer.parseInt(sshProtocol.getTimeout())).isOpened()) {
if (!channel.open().verify(timeout).isOpened()) {
throw new Exception("open failed");
}
List<ClientChannelEvent> list = new ArrayList<>();
list.add(ClientChannelEvent.CLOSED);
channel.waitFor(list, Integer.parseInt(sshProtocol.getTimeout()));
channel.waitFor(list, timeout);
Long responseTime = System.currentTimeMillis() - startTime;
channel.close();
String result = response.toString();
@@ -109,8 +109,16 @@ public class SshCollectImpl extends AbstractCollect {
log.error("ssh response data not enough: {}", result);
}
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String value : lines) {
valueRowBuilder.addColumns(value);
int aliasIndex = 0;
int lineIndex = 0;
while (aliasIndex < aliasFields.size()) {
if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(aliasFields.get(aliasIndex))) {
valueRowBuilder.addColumns(responseTime.toString());
} else {
valueRowBuilder.addColumns(lines[lineIndex].trim());
lineIndex++;
}
aliasIndex++;
}
builder.addValues(valueRowBuilder.build());
}
@@ -145,7 +153,7 @@ public class SshCollectImpl extends AbstractCollect {
}
}
private ClientSession getConnectSession(SshProtocol sshProtocol) throws IOException {
private ClientSession getConnectSession(SshProtocol sshProtocol, int timeout) throws IOException {
CacheIdentifier identifier = CacheIdentifier.builder()
.ip(sshProtocol.getHost()).port(sshProtocol.getPort())
.username(sshProtocol.getUsername()).password(sshProtocol.getPassword())
@@ -170,15 +178,15 @@ public class SshCollectImpl extends AbstractCollect {
}
SshClient sshClient = CommonSshClient.getSshClient();
clientSession = sshClient.connect(sshProtocol.getUsername(), sshProtocol.getHost(), Integer.parseInt(sshProtocol.getPort()))
.verify(Long.parseLong(sshProtocol.getTimeout()), TimeUnit.MILLISECONDS).getSession();
.verify(timeout, TimeUnit.MILLISECONDS).getSession();
if (StringUtils.hasText(sshProtocol.getPassword())) {
clientSession.addPasswordIdentity(sshProtocol.getPassword());
}
// 进行认证
if (!clientSession.auth().verify(Long.parseLong(sshProtocol.getTimeout()), TimeUnit.MILLISECONDS).isSuccess()) {
if (!clientSession.auth().verify(timeout, TimeUnit.MILLISECONDS).isSuccess()) {
throw new IllegalArgumentException("认证失败");
}
CommonCache.getInstance().addCache(identifier, clientSession, 10000L);
CommonCache.getInstance().addCache(identifier, clientSession);
return clientSession;
}