diff --git a/collector/src/main/java/com/zmops/open/collector/collect/database/JdbcCommonCollect.java b/collector/src/main/java/com/zmops/open/collector/collect/database/JdbcCommonCollect.java index 74c5182..b9fd3e6 100644 --- a/collector/src/main/java/com/zmops/open/collector/collect/database/JdbcCommonCollect.java +++ b/collector/src/main/java/com/zmops/open/collector/collect/database/JdbcCommonCollect.java @@ -70,8 +70,9 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a String databaseUrl = constructDatabaseUrl(jdbcProtocol); // 查询超时时间默认6000毫秒 int timeout = CollectUtil.getTimeout(jdbcProtocol.getTimeout()); + Statement statement = null; try { - Statement statement = getConnection(jdbcProtocol.getUsername(), + statement = getConnection(jdbcProtocol.getUsername(), jdbcProtocol.getPassword(), databaseUrl, timeout); switch (jdbcProtocol.getQueryType()) { case QUERY_TYPE_ONE_ROW: @@ -110,6 +111,14 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a log.error("Jdbc error: {}.", errorMessage, e); builder.setCode(CollectRep.Code.FAIL); builder.setMsg("Query Error: " + errorMessage); + } finally { + if (statement != null) { + try { + statement.close(); + } catch (Exception e) { + log.error("Jdbc close statement error: {}", e.getMessage()); + } + } } } diff --git a/manager/src/main/resources/define/app/app-mariadb.yml b/manager/src/main/resources/define/app/app-mariadb.yml index f0b0a75..19ec24d 100644 --- a/manager/src/main/resources/define/app/app-mariadb.yml +++ b/manager/src/main/resources/define/app/app-mariadb.yml @@ -89,6 +89,21 @@ metrics: priority: 1 fields: # 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位 + - field: uptime + type: 0 + unit: s + - field: com_select + type: 0 + - field: com_insert + type: 0 + - field: com_update + type: 0 + - field: com_delete + type: 0 + - field: com_commit + type: 0 + - field: com_rollback + type: 0 - field: threads_created type: 0 - field: threads_connected @@ -97,26 +112,33 @@ metrics: type: 0 - field: threads_running type: 0 + - field: qps + type: 0 # (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换 aliasFields: + - uptime + - com_select + - com_insert + - com_update + - com_delete + - com_commit + - com_rollback - threads_created - threads_connected - threads_cached - threads_running + - questions # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值 # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime calculates: - - threads_created=threads_created - - threads_connected=threads_connected - - threads_cached=threads_cached - - threads_running=threads_running + - qps=uptime / questions protocol: jdbc jdbc: # 主机host: ipv4 ipv6 域名 host: ^_^host^_^ # 端口 port: ^_^port^_^ - platform: mariadb + platform: mysql username: ^_^username^_^ password: ^_^password^_^ database: ^_^database^_^ @@ -124,7 +146,56 @@ metrics: # SQL查询方式: oneRow, multiRow, columns queryType: columns # sql - sql: show global status where Variable_name like 'thread%' or Variable_name = 'com_commit' or Variable_name = 'com_rollback' or Variable_name = 'questions' or Variable_name = 'uptime'; + sql: show global status where Variable_name like 'thread%' or Variable_name = 'com_select' or Variable_name = 'com_insert' or Variable_name = 'com_update' or Variable_name = 'com_delete' or Variable_name = 'com_commit' or Variable_name = 'com_rollback' or Variable_name = 'questions' or Variable_name = 'uptime'; + url: ^_^url^_^ + + - name: cache + priority: 1 + fields: + # 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位 + - field: query_cache_hit_rate + type: 0 + unit: '%' + - field: cache_hits + type: 0 + - field: cache_inserts + type: 0 + - field: cache_free_blocks + type: 0 + - field: cache_free_memory + type: 0 + unit: KB + # (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换 + aliasFields: + - Qcache_hits + - Qcache_inserts + - Qcache_free_blocks + - Qcache_free_memory + # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值 + # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime + calculates: + - query_cache_hit_rate= (Qcache_hits + 1) / (Qcache_hits + Qcache_inserts + 1) * 100 + - cache_hits=Qcache_hits + - cache_inserts=Qcache_inserts + - cache_free_blocks=Qcache_free_blocks + - cache_free_memory=Qcache_free_memory + units: + - cache_free_memory=B->KB + protocol: jdbc + jdbc: + # 主机host: ipv4 ipv6 域名 + host: ^_^host^_^ + # 端口 + port: ^_^port^_^ + platform: mysql + username: ^_^username^_^ + password: ^_^password^_^ + database: ^_^database^_^ + timeout: ^_^timeout^_^ + # SQL查询方式: oneRow, multiRow, columns + queryType: columns + # sql + sql: show global status like 'QCache%'; url: ^_^url^_^ - name: innodb @@ -133,16 +204,38 @@ metrics: # 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位 - field: innodb_data_reads type: 0 - unit: 次数 + unit: Times - field: innodb_data_writes type: 0 - unit: 次数 + unit: Times - field: innodb_data_read type: 0 - unit: kb + unit: MB - field: innodb_data_written type: 0 - unit: kb + unit: MB + - field: innodb_buffer_hit_rate + type: 0 + unit: '%' + aliasFields: + - Innodb_data_reads + - Innodb_data_writes + - Innodb_data_read + - Innodb_data_written + - Innodb_buffer_pool_read_requests + - Innodb_buffer_pool_read_ahead + - Innodb_buffer_pool_reads + # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值 + # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime + calculates: + - innodb_buffer_hit_rate= (Innodb_buffer_pool_read_requests + 1) / (Innodb_buffer_pool_read_requests + Innodb_buffer_pool_read_ahead + Innodb_buffer_pool_reads + 1) * 100 + - innodb_data_reads=Innodb_data_reads + - innodb_data_writes=Innodb_data_writes + - innodb_data_read=Innodb_data_read + - innodb_data_written=Innodb_data_written + units: + - innodb_data_read=KB->MB + - innodb_data_written=KB->MB protocol: jdbc jdbc: # 主机host: ipv4 ipv6 域名 @@ -158,4 +251,4 @@ metrics: queryType: columns # sql sql: show global status where Variable_name like 'innodb%'; - url: ^_^url^_^ \ No newline at end of file + url: ^_^url^_^ diff --git a/manager/src/main/resources/define/app/app-mysql.yml b/manager/src/main/resources/define/app/app-mysql.yml index 7f7b819..158d804 100644 --- a/manager/src/main/resources/define/app/app-mysql.yml +++ b/manager/src/main/resources/define/app/app-mysql.yml @@ -53,7 +53,7 @@ metrics: type: 1 - field: max_connections type: 0 -# (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换 + # (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换 aliasFields: - version - version_compile_os @@ -61,8 +61,8 @@ metrics: - port - datadir - max_connections -# (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值 -# eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime + # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值 + # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime calculates: - port=port - datadir=datadir @@ -74,7 +74,7 @@ metrics: host: ^_^host^_^ # 端口 port: ^_^port^_^ - platform: mysql + platform: mariadb username: ^_^username^_^ password: ^_^password^_^ database: ^_^database^_^ @@ -89,6 +89,21 @@ metrics: priority: 1 fields: # 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位 + - field: uptime + type: 0 + unit: s + - field: com_select + type: 0 + - field: com_insert + type: 0 + - field: com_update + type: 0 + - field: com_delete + type: 0 + - field: com_commit + type: 0 + - field: com_rollback + type: 0 - field: threads_created type: 0 - field: threads_connected @@ -97,19 +112,26 @@ metrics: type: 0 - field: threads_running type: 0 + - field: qps + type: 0 # (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换 aliasFields: + - uptime + - com_select + - com_insert + - com_update + - com_delete + - com_commit + - com_rollback - threads_created - threads_connected - threads_cached - threads_running + - questions # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值 # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime calculates: - - threads_created=threads_created - - threads_connected=threads_connected - - threads_cached=threads_cached - - threads_running=threads_running + - qps=uptime / questions protocol: jdbc jdbc: # 主机host: ipv4 ipv6 域名 @@ -124,7 +146,56 @@ metrics: # SQL查询方式: oneRow, multiRow, columns queryType: columns # sql - sql: show global status where Variable_name like 'thread%' or Variable_name = 'com_commit' or Variable_name = 'com_rollback' or Variable_name = 'questions' or Variable_name = 'uptime'; + sql: show global status where Variable_name like 'thread%' or Variable_name = 'com_select' or Variable_name = 'com_insert' or Variable_name = 'com_update' or Variable_name = 'com_delete' or Variable_name = 'com_commit' or Variable_name = 'com_rollback' or Variable_name = 'questions' or Variable_name = 'uptime'; + url: ^_^url^_^ + + - name: cache + priority: 1 + fields: + # 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位 + - field: query_cache_hit_rate + type: 0 + unit: '%' + - field: cache_hits + type: 0 + - field: cache_inserts + type: 0 + - field: cache_free_blocks + type: 0 + - field: cache_free_memory + type: 0 + unit: KB + # (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换 + aliasFields: + - Qcache_hits + - Qcache_inserts + - Qcache_free_blocks + - Qcache_free_memory + # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值 + # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime + calculates: + - query_cache_hit_rate= (Qcache_hits + 1) / (Qcache_hits + Qcache_inserts + 1) * 100 + - cache_hits=Qcache_hits + - cache_inserts=Qcache_inserts + - cache_free_blocks=Qcache_free_blocks + - cache_free_memory=Qcache_free_memory + units: + - cache_free_memory=B->KB + protocol: jdbc + jdbc: + # 主机host: ipv4 ipv6 域名 + host: ^_^host^_^ + # 端口 + port: ^_^port^_^ + platform: mysql + username: ^_^username^_^ + password: ^_^password^_^ + database: ^_^database^_^ + timeout: ^_^timeout^_^ + # SQL查询方式: oneRow, multiRow, columns + queryType: columns + # sql + sql: show global status like 'QCache%'; url: ^_^url^_^ - name: innodb @@ -133,23 +204,45 @@ metrics: # 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位 - field: innodb_data_reads type: 0 - unit: 次数 + unit: Times - field: innodb_data_writes type: 0 - unit: 次数 + unit: Times - field: innodb_data_read type: 0 - unit: kb + unit: MB - field: innodb_data_written type: 0 - unit: kb + unit: MB + - field: innodb_buffer_hit_rate + type: 0 + unit: '%' + aliasFields: + - Innodb_data_reads + - Innodb_data_writes + - Innodb_data_read + - Innodb_data_written + - Innodb_buffer_pool_read_requests + - Innodb_buffer_pool_read_ahead + - Innodb_buffer_pool_reads + # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值 + # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime + calculates: + - innodb_buffer_hit_rate= (Innodb_buffer_pool_read_requests + 1) / (Innodb_buffer_pool_read_requests + Innodb_buffer_pool_read_ahead + Innodb_buffer_pool_reads + 1) * 100 + - innodb_data_reads=Innodb_data_reads + - innodb_data_writes=Innodb_data_writes + - innodb_data_read=Innodb_data_read + - innodb_data_written=Innodb_data_written + units: + - innodb_data_read=KB->MB + - innodb_data_written=KB->MB protocol: jdbc jdbc: # 主机host: ipv4 ipv6 域名 host: ^_^host^_^ # 端口 port: ^_^port^_^ - platform: mysql + platform: mariadb username: ^_^username^_^ password: ^_^password^_^ database: ^_^database^_^