Compare commits

...

1 Commits

Author SHA1 Message Date
tomsun28
c42655e9b9 [collector,manager]feature 支持postgresql数据库的监控 2022-03-08 17:13:10 +08:00
5 changed files with 177 additions and 1 deletions

View File

@@ -91,6 +91,12 @@
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!-- postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.3</version>
</dependency>
</dependencies>
</project>

View File

@@ -11,6 +11,7 @@ import com.usthe.common.entity.job.protocol.JdbcProtocol;
import com.usthe.common.entity.message.CollectRep;
import com.usthe.common.util.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.postgresql.util.PSQLException;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -72,7 +73,16 @@ public class JdbcCommonCollect extends AbstractCollect {
} catch (CommunicationsException communicationsException) {
log.warn("Jdbc sql error: {}, code: {}.", communicationsException.getMessage(), communicationsException.getErrorCode());
builder.setCode(CollectRep.Code.UN_REACHABLE);
builder.setMsg("Query Error: " + communicationsException.getMessage() + " Code: " + communicationsException.getErrorCode());
builder.setMsg("Error: " + communicationsException.getMessage() + " Code: " + communicationsException.getErrorCode());
} catch (PSQLException psqlException) {
// for PostgreSQL 08001
if (CollectorConstants.POSTGRESQL_UN_REACHABLE_CODE.equals(psqlException.getSQLState())) {
// 对端链接失败 不可达
builder.setCode(CollectRep.Code.UN_REACHABLE);
} else {
builder.setCode(CollectRep.Code.FAIL);
}
builder.setMsg("Error: " + psqlException.getMessage() + " Code: " + psqlException.getSQLState());
} catch (SQLException sqlException) {
log.warn("Jdbc sql error: {}, code: {}.", sqlException.getMessage(), sqlException.getErrorCode());
builder.setCode(CollectRep.Code.FAIL);
@@ -253,6 +263,10 @@ public class JdbcCommonCollect extends AbstractCollect {
+ "/" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase())
+ "?useUnicode=true&characterEncoding=utf-8&useSSL=false";
break;
case "postgresql":
url = "jdbc:postgresql://" + jdbcProtocol.getHost() + ":" + jdbcProtocol.getPort()
+ "/" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase());
break;
default:
throw new IllegalArgumentException("Not support database platform: " + jdbcProtocol.getPlatform());

View File

@@ -14,4 +14,9 @@ public interface CollectorConstants {
String ERROR_MSG = "errorMsg";
String URL = "url";
/**
* POSTGRESQL状态码 不可达
*/
String POSTGRESQL_UN_REACHABLE_CODE = "08001";
}

View File

@@ -0,0 +1,121 @@
category: db
app: postgresql
name:
zh-CN: PostgreSQL数据库
en-US: PostgreSQL DB
# 参数映射map. type是参数类型: 0-number数字, 1-string明文字符串, 2-secret加密字符串
# 强制固定必须参数 - host
configmap:
- key: host
type: 1
- key: port
type: 0
- key: username
type: 1
- key: password
type: 2
- key: database
type: 1
- key: url
type: 1
# 指标组列表
metrics:
- name: basic
# 指标组调度优先级(0-127)越小优先级越高,优先级低的指标组会等优先级高的指标组采集完成后才会被调度,相同优先级的指标组会并行调度采集
# 优先级为0的指标组为可用性指标组,即它会被首先调度,采集成功才会继续调度其它指标组,采集失败则中断调度
priority: 0
# 指标组中的具体监控指标
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: server_version
type: 1
instance: true
- field: port
type: 1
- field: server_encoding
type: 1
- field: data_directory
type: 1
- field: max_connections
type: 0
unit: 连接数
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式 oneRow, multiRow, columns
queryType: columns
# sql
sql: select name, setting as value from pg_settings where name = 'max_connections' or name = 'server_version' or name = 'server_encoding' or name = 'port' or name = 'data_directory';
url: ^_^url^_^
- name: state
priority: 1
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: name
type: 1
- field: conflicts
type: 0
unit: 次数
- field: deadlocks
type: 0
unit: 个数
- field: blks_read
type: 0
unit: 次数
- field: blks_hit
type: 0
unit: 次数
- field: blk_read_time
type: 0
unit: ms
- field: blk_write_time
type: 0
unit: ms
- field: stats_reset
type: 1
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式 oneRow, multiRow, columns
queryType: multiRow
# sql
sql: SELECT COALESCE(datname,'shared-object') as name, conflicts, deadlocks, blks_read, blks_hit, blk_read_time, blk_write_time, stats_reset from pg_stat_database where (datname != 'template1' and datname != 'template0') or datname is null;
url: ^_^url^_^
- name: activity
priority: 2
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: running
type: 0
unit: 连接数
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式 oneRow, multiRow, columns
queryType: oneRow
# sql
sql: SELECT count(*) as running FROM pg_stat_activity WHERE NOT pid=pg_backend_pid();
url: ^_^url^_^

View File

@@ -0,0 +1,30 @@
app: postgresql
param:
- field: host
name: 主机Host
type: host
required: true
- field: port
name: 端口
type: number
range: '[0,65535]'
required: true
defaultValue: 5432
placeholder: '请输入端口'
- field: database
name: 数据库名称
type: text
required: false
- field: username
name: 用户名
type: text
limit: 20
required: false
- field: password
name: 密码
type: password
required: false
- field: url
name: URL
type: text
required: false