田源
2025-04-03 9b4433fddf5b401edb0aace8a404ac733b122702
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 *      Copyright (c) 2018-2028, DreamLu All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: DreamLu 卢春梦 (596392912@qq.com)
 */
package com.vci.ubcs.admin.dingtalk;
 
import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.domain.values.Registration;
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.lang.NonNull;
import reactor.core.publisher.Mono;
 
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
 
/**
 * 服务上下线告警
 *
 * <p>
 * 注意:AbstractStatusChangeNotifier 这个事件有毛病
 * </p>
 *
 * @author L.cm
 */
@Slf4j
public class DingTalkNotifier extends AbstractEventNotifier {
    private final DingTalkService dingTalkService;
    private final MonitorProperties properties;
    private final Environment environment;
    public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
    public DingTalkNotifier(DingTalkService dingTalkService, MonitorProperties properties,
                            Environment environment, InstanceRepository repository) {
        super(repository);
        this.dingTalkService = dingTalkService;
        this.properties = properties;
        this.environment = environment;
    }
 
    @NonNull
    @Override
    protected Mono<Void> doNotify(@NonNull InstanceEvent event, @NonNull Instance instance) {
        if (event instanceof InstanceStatusChangedEvent) {
            // 构造请求结构
            return createAndPushMsg(event, instance);
        }
        return Mono.empty();
    }
 
    private Mono<Void> createAndPushMsg(InstanceEvent event, Instance instance) {
        Registration registration = instance.getRegistration();
        // 服务名
        String appName = registration.getName();
        // 服务地址
        String serviceUrl = registration.getServiceUrl();
        StatusInfo status = instance.getStatusInfo();
        // 时间
        LocalDateTime localDateTime = LocalDateTime.ofInstant(event.getTimestamp(), ZoneId.systemDefault());
        MonitorProperties.DingTalk dingTalk = properties.getDingTalk();
        String title = dingTalk.getService().getTitle();
 
        String message = "## **" + title + "**\n" +
            "#### **【服务】** " + appName + "\n" +
            "#### **【环境】** " + environment.getActiveProfiles()[0] + "\n" +
            "#### **【地址】** " + serviceUrl + "\n" +
            "#### **【状态】** " + statusCn(status) + "\n" +
            "#### **【时间】** " + DATETIME_FORMATTER.format(localDateTime) + "\n" +
            "#### **【详情】** " + dingTalk.getLink() + "\n";
 
        return dingTalkService.pushMsg(title, message);
    }
 
    private String statusCn(StatusInfo status) {
        if (status.isUp()) {
            return "应用上线(IS UP)";
        } else if (status.isDown()) {
            return "应用宕机(IS DOWN)";
        } else if (status.isOffline()) {
            return "应用掉线(IS OFFLINE)";
        } else if (status.isUnknown()) {
            return "未知状态(UNKNOWN)";
        } else {
            return "异常状态";
        }
    }
}