[collector,manager]feature 支持microsoft sqlserver数据库监控类型 (#37)
This commit is contained in:
@@ -103,6 +103,12 @@
|
||||
<artifactId>sshd-core</artifactId>
|
||||
<version>2.8.0</version>
|
||||
</dependency>
|
||||
<!-- sql server -->
|
||||
<dependency>
|
||||
<groupId>com.microsoft.sqlserver</groupId>
|
||||
<artifactId>mssql-jdbc</artifactId>
|
||||
<version>10.2.0.jre8</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -56,7 +56,9 @@ public class JdbcCommonCollect extends AbstractCollect {
|
||||
int timeout = 3000;
|
||||
try {
|
||||
// 获取查询语句超时时间
|
||||
timeout = Integer.parseInt(jdbcProtocol.getTimeout());
|
||||
if (jdbcProtocol.getTimeout() != null) {
|
||||
timeout = Integer.parseInt(jdbcProtocol.getTimeout());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn(e.getMessage());
|
||||
}
|
||||
@@ -140,7 +142,9 @@ public class JdbcCommonCollect extends AbstractCollect {
|
||||
Connection connection = DriverManager.getConnection(url, username, password);
|
||||
statement = connection.createStatement();
|
||||
// 设置查询超时时间10秒
|
||||
statement.setQueryTimeout(timeout);
|
||||
int timeoutSecond = timeout / 1000;
|
||||
timeoutSecond = timeoutSecond <= 0 ? 1 : timeoutSecond;
|
||||
statement.setQueryTimeout(timeoutSecond);
|
||||
// 设置查询最大行数1000行
|
||||
statement.setMaxRows(1000);
|
||||
JdbcConnect jdbcConnect = new JdbcConnect(connection);
|
||||
@@ -200,7 +204,7 @@ public class JdbcCommonCollect extends AbstractCollect {
|
||||
HashMap<String, String> values = new HashMap<>(columns.size());
|
||||
while (resultSet.next()) {
|
||||
if (resultSet.getString(1) != null) {
|
||||
values.put(resultSet.getString(1).toLowerCase(), resultSet.getString(2));
|
||||
values.put(resultSet.getString(1).toLowerCase().trim(), resultSet.getString(2));
|
||||
}
|
||||
}
|
||||
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
||||
@@ -277,6 +281,10 @@ public class JdbcCommonCollect extends AbstractCollect {
|
||||
url = "jdbc:postgresql://" + jdbcProtocol.getHost() + ":" + jdbcProtocol.getPort()
|
||||
+ "/" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase());
|
||||
break;
|
||||
case "sqlserver":
|
||||
url = "jdbc:sqlserver://" + jdbcProtocol.getHost() + ":" + jdbcProtocol.getPort()
|
||||
+ ";" + (jdbcProtocol.getDatabase() == null ? "" : "DatabaseName=" + jdbcProtocol.getDatabase());
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Not support database platform: " + jdbcProtocol.getPlatform());
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -172,14 +173,23 @@ public class MetricsCollect implements Runnable, Comparable<MetricsCollect> {
|
||||
if (metrics.getCalculates() == null) {
|
||||
metrics.setCalculates(Collections.emptyList());
|
||||
}
|
||||
// eg: database_pages=Database pages 非常规映射
|
||||
Map<String, String> fieldAliasMap = new HashMap<>(8);
|
||||
Map<String, Expression> fieldExpressionMap = metrics.getCalculates()
|
||||
.stream()
|
||||
.map(cal -> {
|
||||
int splitIndex = cal.indexOf("=");
|
||||
String field = cal.substring(0, splitIndex);
|
||||
String expressionStr = cal.substring(splitIndex + 1);
|
||||
Expression expression = AviatorEvaluator.compile(expressionStr, true);
|
||||
Expression expression = null;
|
||||
try {
|
||||
expression = AviatorEvaluator.compile(expressionStr, true);
|
||||
} catch (Exception e) {
|
||||
fieldAliasMap.put(field, expressionStr);
|
||||
return null;
|
||||
}
|
||||
return new Object[]{field, expression}; })
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toMap(arr -> (String)arr[0], arr -> (Expression) arr[1]));
|
||||
|
||||
List<Metrics.Field> fields = metrics.getFields();
|
||||
@@ -226,7 +236,12 @@ public class MetricsCollect implements Runnable, Comparable<MetricsCollect> {
|
||||
}
|
||||
} else {
|
||||
// 不存在 则映射别名值
|
||||
value = aliasFieldValueMap.get(realField);
|
||||
String aliasField = fieldAliasMap.get(realField);
|
||||
if (aliasField != null) {
|
||||
value = aliasFieldValueMap.get(aliasField);
|
||||
} else {
|
||||
value = aliasFieldValueMap.get(realField);
|
||||
}
|
||||
}
|
||||
if (value == null) {
|
||||
value = CommonConstants.NULL_VALUE;
|
||||
|
||||
Reference in New Issue
Block a user