跳到主要内容

服务器硬件监控

得益于定时任务和服务器硬件信息oshi,可实现该功能。

当前仅实现了cpu、内存、硬盘空间的监控。

使用前引入依赖:

<!-- 获取系统硬件信息 -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>6.4.0</version>
</dependency>

原理

使用定时任务每隔一段时间进行检测,若检测到服务器异常则通过邮箱向管理员发送异常监控邮件(当monitor-warn-type配置为EMAIL时)或执行自定义方法(当monitor-warn-type配置为CUSTOM时)。

当配置了enable-monitor=true时会自动开启监控。

使用

开启监控配置即可:

示例:

wjjhook:
system-monitor:
# 是否启用监控
enable-monitor: false
monitor-warn-type: EMAIL
# 监控定时任务cron表达式
cron-el: '0 0 9-21/1 * * ?'
# cpu监控值上限
cpu-threshold-values: 90
# 内存监控值上限
memory-threshold-values: 80
# 磁盘监控值阈值
disk-threshold-values: 95
# 预警邮箱列表(可设置多个)
admin-emails:
- wjianjun9@163.com

注意

预警方式说明(monitor-warn-type):默认为EMAIL,可选CUSTOM。 当选CUSTOM,预警时会执行IPlugInMethodService.monitorCustomRun(),所以需要实现该方法来自定义预警方式。