Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pg new metrics, add endpoint getAppMonitorsUnConnect, set date format #21

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add pg new metrics, add endpoint getAppMonitorsUnConnect, set date fo…
…rmat
tomsun28 committed Feb 23, 2023
commit cf5211bd194600773210d6de52ed6c5601c28112
Original file line number Diff line number Diff line change
@@ -95,6 +95,8 @@ public void applyBindAlertDefineMonitors(Long alertId, List<AlertDefineMonitorBi
// todo checks whether the alarm definition and monitoring exist
// todo 校验此告警定义和监控是否存在

alertDefineBinds.forEach(item -> item.setAlertDefineId(alertId));

// Delete all associations of this alarm
// 先删除此告警的所有关联
alertDefineBindDao.deleteAlertDefineBindsByAlertDefineIdEquals(alertId);
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.zmops.open.manager.config;

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* LocalDateTime format
*
* @author tom
* @date 2023/2/23 10:33
*/
@Configuration
@ConditionalOnClass(JavaTimeModule.class)
public class LocalDateTimeFormatConfig {

@Bean
@ConditionalOnProperty("spring.jackson.date-format")
public Jackson2ObjectMapperBuilderCustomizer customizeLocalDateTimeFormat(@Value("${spring.jackson.date-format}") String dateFormat) {
return jacksonObjectMapperBuilder -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);

jacksonObjectMapperBuilder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
jacksonObjectMapperBuilder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
};
}
}
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@

package com.zmops.open.manager.controller;

import com.zmops.open.alert.service.AlertDefineService;
import com.zmops.open.common.entity.alerter.AlertDefineMonitorBind;
import com.zmops.open.common.entity.dto.Message;
import com.zmops.open.common.entity.manager.Monitor;
import com.zmops.open.manager.service.MonitorService;
@@ -40,6 +42,9 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
@@ -58,6 +63,9 @@ public class MonitorsController {
@Autowired
private MonitorService monitorService;

@Autowired
private AlertDefineService alertDefineService;

@GetMapping
@Operation(summary = "Obtain a list of monitoring information based on query filter items",
description = "根据查询过滤项获取监控信息列表")
@@ -131,6 +139,18 @@ public ResponseEntity<Message<List<Monitor>>> getAppMonitors(
return ResponseEntity.ok(message);
}

@GetMapping(path = "/unrelated/{app}/{alertDefineId}")
@Operation(summary = "查询指定APP下的未关联告警定义监控")
public ResponseEntity<Message<List<Monitor>>> getAppMonitorsUnConnect(@PathVariable final Long alertDefineId,
@PathVariable final String app) {
List<Monitor> monitors = monitorService.getAppMonitors(app);
List<AlertDefineMonitorBind> defineBinds = alertDefineService.getBindAlertDefineMonitors(alertDefineId);
Set<Long> connectIds = defineBinds.stream().map(AlertDefineMonitorBind::getMonitorId).collect(Collectors.toSet());
monitors = monitors.stream().filter(item -> !connectIds.contains(item.getId())).collect(Collectors.toList());
Message<List<Monitor>> message = new Message<>(monitors);
return ResponseEntity.ok(message);
}

@DeleteMapping
@Operation(summary = "Delete monitoring items in batches according to the monitoring ID list",
description = "根据监控ID列表批量删除监控项")
1 change: 1 addition & 0 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ spring:
static-path-pattern: /**
jackson:
default-property-inclusion: ALWAYS
date-format: yyyy-MM-dd HH:mm:ss
web:
resources:
static-locations:
180 changes: 178 additions & 2 deletions manager/src/main/resources/define/app/app-postgresql.yml
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ metrics:
# 指标组中的具体监控指标
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: server_version
- field: version
type: 1
instance: true
- field: port
@@ -56,6 +56,15 @@ metrics:
- field: max_connections
type: 0
unit: sbc
aliasFields:
- server_version
- server_encoding
- data_directory
- max_connections
# (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值
# eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime
calculates:
- version=server_version
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
@@ -73,6 +82,173 @@ metrics:
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: status
priority: 1
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: uptime
type: 1
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式: oneRow, multiRow, columns
queryType: oneRow
# sql
sql: select date_trunc('second', current_timestamp-pg_postmaster_start_time()) as uptime;
url: ^_^url^_^

- name: resource_config
priority: 1
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: shared_buffers
type: 0
unit: MB
- field: work_mem
type: 0
unit: MB
- field: shared_buffers
type: 0
unit: MB
- field: autovacuum
type: 1
- field: max_connections
type: 0
- field: effective_cache_size
type: 0
unit: MB
- field: wal_buffers
type: 0
unit: MB
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式: oneRow, multiRow, columns
queryType: columns
# sql
sql: show all;
url: ^_^url^_^

- name: connection
priority: 1
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: active
type: 0
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式: oneRow, multiRow, columns
queryType: oneRow
# sql
sql: select count(1) as active from pg_stat_activity;
url: ^_^url^_^

- name: connection_state
priority: 1
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: state
type: 1
- field: num
type: 0
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式: oneRow, multiRow, columns
queryType: multiRow
# sql
sql: select COALESCE(state, 'other') as state, count(*) as num from pg_stat_activity group by state;
url: ^_^url^_^

- name: connection_db
priority: 1
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: db_name
type: 1
- field: active
type: 0
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式: oneRow, multiRow, columns
queryType: multiRow
# sql
sql: select count(*) as active, COALESCE(datname, 'other') as db_name from pg_stat_activity group by datname;
url: ^_^url^_^

- name: tuple
priority: 1
fields:
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
- field: fetched
type: 0
- field: returned
type: 0
- field: inserted
type: 0
- field: updated
type: 0
- field: deleted
type: 0
protocol: jdbc
jdbc:
# 主机host: ipv4 ipv6 域名
host: ^_^host^_^
# 端口
port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
# SQL查询方式: oneRow, multiRow, columns
queryType: multiRow
# sql
sql: select sum(tup_fetched) as fetched, sum(tup_updated) as updated, sum(tup_deleted) as deleted, sum(tup_inserted) as inserted, sum(tup_returned) as returned from pg_stat_database;
url: ^_^url^_^

- name: state
priority: 1
fields:
@@ -138,4 +314,4 @@ metrics:
queryType: oneRow
# sql
sql: SELECT count(*) as running FROM pg_stat_activity WHERE NOT pid=pg_backend_pid();
url: ^_^url^_^
url: ^_^url^_^