Skip to content

Commit

Permalink
support email alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
yaojia.lou authored and yaojia.lou committed Mar 14, 2021
1 parent af55036 commit 554fa91
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/.idea/
/dbsync.iml
/src/main/resources/app.yaml
/src/main/resources/logback.yaml
/src/main/resources/logback.xml
3 changes: 0 additions & 3 deletions bin/start.sh

This file was deleted.

72 changes: 31 additions & 41 deletions doc/configuration.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,64 +108,54 @@ monitor:

系统提供`同步状态``工作线程状态`的监控. 详情参看`Endpoints端点`

监控配置可配置1-N个监控策略, 目前支持的动作有: `发送告警``自动重启`
监控配置可配置1-N个监控规则, 目前支持的动作有: `发送邮件通知``通过Webhook发送通知``自动重启`

监控配置参数说明:

- matches.heartbeatLostOver 当工作线程心跳丢失超过N次时执行
- matches.syncBlockedOver 当阻塞的数据量超过N个时执行
- matches.syncErrorOver 当同步失败的数据量超过N个时执行
- matches.syncPendingOver 当同步等待的数据量超过N个时执行
- action 动作类型, 目前支持`restart``webhook`方式. 可扩展
- action 动作类型, 目前支持`email``restart``webhook`方式
- params 动作参数. 针对每种动作类型有不同参数

### 动作`webhook`, 配置如下
发送通知时包括以下几个信息:

- matchedRule 匹配到的监控规则
- reason 告警的原因
- syncStatus 当前同步状态
- components 当前工作线程状态

### `webhook`动作,

配置如下

```
action: webhook
params:
url: http://x.x.x.x:xxxx/path
url: http://x.x.x.x:xxxx/path #接收告警的地址
```

其中`url`为接收告警的地址
### `email`动作

告警数据格式如下:
配置如下

```
> POST /path HTTP/1.1
> Content-Type: application/json
> Content-Length: 1056
{
"reason": "告警原因",
"syncStatus": {
"pending": 0,
"blocked": 0,
"error": 0,
"success": 0,
"others": 0
},
"components": {
"blocked-handler": {
"statistics": {},
"total": 0,
"heartbeatLost": 0,
"heartbeatInterval": 120000,
"lastHeartbeat": "2021-03-13 15:04:59",
"status": "GREEN"
},
"cleanWorker": {
"heartbeatLost": 206,
"heartbeatInterval": 5,
"lastHeartbeat": "2021-03-13 15:04:59",
"status": "RED"
}
}
}
action: email
params:
host: x.x.x.x #邮件服务器地址
port: xxxx #邮件服务器端口
user: xxx #邮件服务器用户名
password: xxxx #邮件服务器密码
from: [email protected] #发件人
subject: xxx #邮件标题
to: #收件人列表
- [email protected]
- [email protected]
```

其中:
### `restart` 动作

- reason 为原因
- syncStatus 为当前同步状态
- components 为当前工作线程状态
```
action: restart
```
32 changes: 11 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.louyj</groupId>
<artifactId>dbsync</artifactId>
<version>1.0</version>
<version>1.1</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -189,45 +189,35 @@
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>make-assembly</id>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
<!--<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
&lt;!&ndash; <configuration>&ndash;&gt;
&lt;!&ndash; <descriptorRefs>&ndash;&gt;
&lt;!&ndash; <descriptorRef>jar-with-dependencies</descriptorRef>&ndash;&gt;
&lt;!&ndash; </descriptorRefs>&ndash;&gt;
&lt;!&ndash; </configuration>&ndash;&gt;
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
<descriptor>src/assembly/bin.xml</descriptor>
<finalName>dbsync-${pom.version}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>-->
</plugin>
</plugins>
</build>

Expand Down
28 changes: 17 additions & 11 deletions src/main/resources/assembly.xml → src/assembly/bin.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>distribution</id>
<assembly>
<id>bin</id>
<formats>
<format>zip</format>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>bin/</directory>
<outputDirectory>/bin/</outputDirectory>
<directory>src/bin</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet>
<directory>target/</directory>
<outputDirectory>/lib/</outputDirectory>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*with-dependencies.jar</include>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources/production/</directory>
<outputDirectory>/config/</outputDirectory>
<directory>target/dependency</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/config</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>**/*</include>
</includes>
Expand Down
3 changes: 3 additions & 0 deletions src/bin/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BASEDIR=$(dirname "$0")
cd ${BASEDIR}/..
nohup java -Dapp=dbsync -cp lib/* com.louyj.dbsync.DbSyncLauncher config/app.yaml > logs/dbsync.out 2>&1
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/main/scala/com/louyj/dbsync/job/CleanWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ class CleanWorker(ctx: SystemContext)
}
}

override def heartbeatInterval(): Long = 5000
override def heartbeatInterval(): Long = 5
}
45 changes: 29 additions & 16 deletions src/main/scala/com/louyj/dbsync/monitor/EmailActionHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@ class EmailActionHandler extends ActionHandler {
val logger = LoggerFactory.getLogger(getClass)

private val jackson = JsonUtils.jackson()
private val yaml = JsonUtils.yaml()

override def name(): String = "email"

override def doAction(monitorConfig: MonitorConfig, ctx: SystemContext,
components: mutable.Map[String, mutable.Map[String, Any]], syncStatus: SyncState, reason: String): Unit = {
val content =
var content =
s"""
matched rule: ${monitorConfig.name}
reason: $reason
sync status: ${jackson.writeValueAsString(syncStatus)}
components status: ${jackson.writeValueAsString(components)}
"""
matched rule: ${monitorConfig.name}
reason: $reason"""
if (syncStatus != null) {
content = content +
s"""
sync status:
${appendBlank(yaml.writeValueAsString(syncStatus), " ")}"""
}
if (components != null) {
content = content +
s"""
components status:
${appendBlank(yaml.writeValueAsString(components), " ")}"""
}
val params = monitorConfig.params
val emailParams = jackson.convertValue(params, classOf[EmailParams])
val email = new SimpleEmail
Expand All @@ -39,15 +49,18 @@ class EmailActionHandler extends ActionHandler {
email.send();
}

case class EmailParams(
host: String,
port: Int,
user: String,
password: String,
from: String,
subject: String,
to: List[String]
)

def appendBlank(content: String, blank: String) = {
content.split("\n").map(blank + _).mkString("\n")
}

}

case class EmailParams(
host: String,
port: Int,
user: String,
password: String,
from: String,
subject: String,
to: List[String]
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ class WebhookActionHandler extends ActionHandler {
val webhookParams = jackson.convertValue(monitorConfig.params, classOf[WebhookParams])
val content = Map("matchedRule" -> monitorConfig.name, "reason" -> reason,
"syncStatus" -> syncStatus, "components" -> components)
basicRequest.post(uri"${webhookParams.url}")
val response = basicRequest.post(uri"${webhookParams.url}")
.body(jackson.writeValueAsString(content))
.header("Content-Type", "application/json")
.send()
logger.info(s"Url ${webhookParams.url} response code ${response.code} content ${response.body}")
}

case class WebhookParams(url: String)

}

case class WebhookParams(url: String)

6 changes: 6 additions & 0 deletions src/main/scala/com/louyj/dbsync/util/JsonUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.louyj.dbsync.util

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility
import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES
import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.scala.DefaultScalaModule
Expand All @@ -15,11 +17,15 @@ object JsonUtils {

def jackson() = {
val jackson = new ObjectMapper()
jackson.configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
jackson.configure(ALLOW_SINGLE_QUOTES, true)
jackson.registerModule(DefaultScalaModule)
}

def jacksonWithFieldAccess() = {
val jackson = new ObjectMapper()
jackson.configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
jackson.configure(ALLOW_SINGLE_QUOTES, true)
jackson.setVisibility(PropertyAccessor.FIELD, Visibility.ANY)
jackson.registerModule(DefaultScalaModule)
}
Expand Down

0 comments on commit 554fa91

Please sign in to comment.