Merge pull request #165 from AkkiaS7/enhance-notification

update: #SERVER.IP#仅返回一个IP(优先返回ipv4地址)| 新增获取ipv4与ipv6地址的占位符

Co-authored-by: AkkiaS7 <68485070+AkkiaS7@users.noreply.github.com>
This commit is contained in:
naiba 2022-04-24 21:30:13 +08:00 committed by GitHub
commit 318986b0e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 5 deletions

View File

@ -4,7 +4,7 @@
<br>
<small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small>
<br><br>
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.12.23&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.8.2-brightgreen?style=for-the-badge&logo=linux">
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.12.24&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.8.2-brightgreen?style=for-the-badge&logo=linux">
<br>
<br>
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。</p>

View File

@ -159,11 +159,37 @@ func (ns *NotificationServerBundle) Send(message string) error {
// replaceParamInString 替换字符串中的占位符
func replaceParamsInString(s *Server, str string, message string, mod func(string) string) string {
// IP处理
ipv4 := ""
ipv6 := ""
validIP := ""
if s != nil {
ipList := strings.Split(s.Host.IP, "/")
if len(ipList) > 1 {
// 双栈
ipv4 = ipList[0]
ipv6 = ipList[1]
validIP = ipv4
} else if len(ipList) == 1 {
// 仅ipv4|ipv6
if strings.Contains(ipList[0], ":") {
ipv6 = ipList[0]
validIP = ipv6
} else {
ipv4 = ipList[0]
validIP = ipv4
}
}
}
if mod != nil {
str = strings.ReplaceAll(str, "#NEZHA#", mod(message))
if s != nil {
str = strings.ReplaceAll(str, "#SERVER.NAME#", mod(s.Name))
str = strings.ReplaceAll(str, "#SERVER.IP#", mod(s.Host.IP))
str = strings.ReplaceAll(str, "#SERVER.IP#", mod(validIP))
str = strings.ReplaceAll(str, "#SERVER.IPV4#", mod(ipv4))
str = strings.ReplaceAll(str, "#SERVER.IPV6#", mod(ipv6))
str = strings.ReplaceAll(str, "#SERVER.CPU#", mod(fmt.Sprintf("%f", s.State.CPU)))
str = strings.ReplaceAll(str, "#SERVER.MEM#", mod(fmt.Sprintf("%d", s.State.MemUsed)))
str = strings.ReplaceAll(str, "#SERVER.SWAP#", mod(fmt.Sprintf("%d", s.State.SwapUsed)))
@ -182,7 +208,9 @@ func replaceParamsInString(s *Server, str string, message string, mod func(strin
str = strings.ReplaceAll(str, "#NEZHA#", message)
if s != nil {
str = strings.ReplaceAll(str, "#SERVER.NAME#", s.Name)
str = strings.ReplaceAll(str, "#SERVER.IP#", s.Host.IP)
str = strings.ReplaceAll(str, "#SERVER.IP#", validIP)
str = strings.ReplaceAll(str, "#SERVER.IPV4#", ipv4)
str = strings.ReplaceAll(str, "#SERVER.IPV6#", ipv6)
str = strings.ReplaceAll(str, "#SERVER.CPU#", fmt.Sprintf("%f", s.State.CPU))
str = strings.ReplaceAll(str, "#SERVER.MEM#", fmt.Sprintf("%d", s.State.MemUsed))
str = strings.ReplaceAll(str, "#SERVER.SWAP#", fmt.Sprintf("%d", s.State.SwapUsed))

View File

@ -36,7 +36,7 @@
</div>
<div class="secret field">
<label>Body</label>
<textarea name="RequestBody" placeholder='{&#13;&#10; "content":"#NEZHA#",&#13;&#10; "ServerName":"#SERVER.NAME#",&#13;&#10; "ServerIP":"#SERVER.IP#",&#13;&#10; "CPU":"#SERVER.CPU#",&#13;&#10; "MEM":"#SERVER.MEM#",&#13;&#10; "SWAP":"#SERVER.SWAP#",&#13;&#10; "DISK":"#SERVER.DISK#",&#13;&#10; "NetInSpeed":"#SERVER.NETINSPEED#",&#13;&#10; "NetOutSpeed":"#SERVER.NETOUTSPEED#",&#13;&#10; "TransferIn":"#SERVER.TRANSFERIN#",&#13;&#10; "TranferOut":"#SERVER.TRANSFEROUT#",&#13;&#10; "Load1":"#SERVER.LOAD1#",&#13;&#10; "Load5":"#SERVER.LOAD5#",&#13;&#10; "Load15":"#SERVER.LOAD15#"&#13;&#10; "TCP_CONN_COUNT":"#SERVER.TCPCONNCOUNT"&#13;&#10; "UDP_CONN_COUNT":"#SERVER.UDPCONNCOUNT"&#13;&#10;}'></textarea>
<textarea name="RequestBody" placeholder='{&#13;&#10; "content":"#NEZHA#",&#13;&#10; "ServerName":"#SERVER.NAME#",&#13;&#10; "ServerIP":"#SERVER.IP#",&#13;&#10; "ServerIPV4":"#SERVER.IPV4#",&#13;&#10; "ServerIPV6":"#SERVER.IPV6#",&#13;&#10; "CPU":"#SERVER.CPU#",&#13;&#10; "MEM":"#SERVER.MEM#",&#13;&#10; "SWAP":"#SERVER.SWAP#",&#13;&#10; "DISK":"#SERVER.DISK#",&#13;&#10; "NetInSpeed":"#SERVER.NETINSPEED#",&#13;&#10; "NetOutSpeed":"#SERVER.NETOUTSPEED#",&#13;&#10; "TransferIn":"#SERVER.TRANSFERIN#",&#13;&#10; "TranferOut":"#SERVER.TRANSFEROUT#",&#13;&#10; "Load1":"#SERVER.LOAD1#",&#13;&#10; "Load5":"#SERVER.LOAD5#",&#13;&#10; "Load15":"#SERVER.LOAD15#"&#13;&#10; "TCP_CONN_COUNT":"#SERVER.TCPCONNCOUNT"&#13;&#10; "UDP_CONN_COUNT":"#SERVER.UDPCONNCOUNT"&#13;&#10;}'></textarea>
</div>
<div class="field">
<div class="ui nf-ssl checkbox">

View File

@ -12,7 +12,7 @@ import (
"github.com/naiba/nezha/pkg/utils"
)
var Version = "v0.12.23" // !!记得修改 README 中的 badge 版本!!
var Version = "v0.12.24" // !!记得修改 README 中的 badge 版本!!
var (
Conf *model.Config