Skip to content

Commit eba8ebc

Browse files
authored
Merge pull request #92 from zaunist/fix/notification-ip-address-variable
fix(notification): add backward compatibility for ip_address variable
2 parents 27180ce + 6b430d4 commit eba8ebc

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

backend/src/jobs/agent-task.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,18 @@ async function handleAgentOfflineNotification(
105105
}
106106

107107
// 准备通知变量
108+
const formattedIP = getFormattedIPAddresses(agent.ip_addresses);
108109
const variables = {
109110
name: agentName,
110111
status: "offline",
111112
previous_status: "online", // 添加previous_status变量
112113
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
113114
hostname: agent.hostname || "未知",
114-
ip_addresses: getFormattedIPAddresses(agent.ip_addresses),
115+
ip_addresses: formattedIP,
116+
ip_address: formattedIP, // 兼容旧模板
115117
os: agent.os || "未知",
116118
error: "客户端连接超时 🔴",
117-
details: `主机名: ${
118-
agent.hostname || "未知"
119-
}\nIP地址: ${getFormattedIPAddresses(
120-
agent.ip_addresses
121-
)}\n操作系统: ${agent.os || "未知"}\n最后连接时间: ${new Date(
122-
agent.updated_at
123-
).toLocaleString("zh-CN")}`,
119+
details: `主机名: ${agent.hostname || "未知"}\nIP地址: ${formattedIP}\n操作系统: ${agent.os || "未知"}\n最后连接时间: ${new Date(agent.updated_at).toLocaleString("zh-CN")}`,
124120
};
125121

126122
// 发送通知
@@ -186,20 +182,18 @@ export async function handleAgentOnlineNotification(
186182
}
187183

188184
// 准备通知变量
185+
const formattedIP = getFormattedIPAddresses(agent.ip_addresses);
189186
const variables = {
190187
name: agentName,
191188
status: "online",
192189
previous_status: "offline",
193190
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
194191
hostname: agent.hostname || "未知",
195-
ip_addresses: getFormattedIPAddresses(agent.ip_addresses),
192+
ip_addresses: formattedIP,
193+
ip_address: formattedIP, // 兼容旧模板
196194
os: agent.os || "未知",
197195
error: "客户端连接已恢复 🟢",
198-
details: `主机名: ${
199-
agent.hostname || "未知"
200-
}\nIP地址: ${getFormattedIPAddresses(
201-
agent.ip_addresses
202-
)}\n操作系统: ${agent.os || "未知"}\n恢复时间: ${new Date().toLocaleString("zh-CN")}`,
196+
details: `主机名: ${agent.hostname || "未知"}\nIP地址: ${formattedIP}\n操作系统: ${agent.os || "未知"}\n恢复时间: ${new Date().toLocaleString("zh-CN")}`,
203197
};
204198

205199
// 发送通知
@@ -336,22 +330,18 @@ export async function handleAgentThresholdNotification(
336330
);
337331

338332
// 准备通知变量
333+
const formattedIP = getFormattedIPAddresses(agent.ip_addresses);
339334
const variables = {
340335
name: agent.name,
341336
status: `${metricName}告警`,
342337
previous_status: "normal", // 添加previous_status变量
343338
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
344339
hostname: agent.hostname || "未知",
345-
ip_addresses: getFormattedIPAddresses(agent.ip_addresses),
340+
ip_addresses: formattedIP,
341+
ip_address: formattedIP, // 兼容旧模板
346342
os: agent.os || "未知",
347343
error: `${metricName}(${value.toFixed(2)}%)超过阈值(${threshold}%)`,
348-
details: `${metricName}: ${value.toFixed(
349-
2
350-
)}%\n阈值: ${threshold}%\n主机名: ${
351-
agent.hostname || "未知"
352-
}\nIP地址: ${getFormattedIPAddresses(agent.ip_addresses)}\n操作系统: ${
353-
agent.os || "未知"
354-
}`,
344+
details: `${metricName}: ${value.toFixed(2)}%\n阈值: ${threshold}%\n主机名: ${agent.hostname || "未知"}\nIP地址: ${formattedIP}\n操作系统: ${agent.os || "未知"}`,
355345
};
356346

357347
// 发送通知

0 commit comments

Comments
 (0)