feat: 邮件规则新增服务下线监控
- 监控资源新增「服务下线监控」复选框
- 勾选后出现服务选择器,列出所有Web服务
- 阈值单位为分钟(默认5分钟),范围1~1440
- 触发类型自动锁定为duration,简化配置
- checkEmailRules支持null参数,仅检查服务下线
- loadProjects同步调用,服务状态刷新即时检测
- 离线计时:服务下线时记录时间戳,恢复时自动重置
- 规则列表特殊显示:🔴 服务名 下线 ≥ X分钟
This commit is contained in:
@@ -698,17 +698,32 @@
|
||||
<i class="ri-hard-drive-2-line text-orange-400"></i>
|
||||
<span class="text-gray-200 text-sm">磁盘使用率</span>
|
||||
</label>
|
||||
<label class="flex items-center gap-2 bg-gray-700/50 px-3 py-2 rounded-lg cursor-pointer hover:bg-gray-700 col-span-2">
|
||||
<input type="checkbox" value="service_down" class="email-rule-resource w-4 h-4" onchange="onResourceChange()">
|
||||
<i class="ri-shut-down-line text-yellow-400"></i>
|
||||
<span class="text-gray-200 text-sm">服务下线监控</span>
|
||||
<span class="text-gray-500 text-xs">检测指定服务长期不在线</span>
|
||||
</label>
|
||||
</div>
|
||||
<p class="text-gray-500 text-xs mt-1">勾选多项可随机组合监控,任一资源超标即触发通知</p>
|
||||
</div>
|
||||
|
||||
<!-- 服务选择器(仅当勾选「服务下线监控」时显示) -->
|
||||
<div class="mb-3" id="serviceDownSelector" style="display:none;">
|
||||
<label class="block text-gray-400 text-sm mb-1">监控服务 *</label>
|
||||
<select id="emailRuleServiceId" class="w-full bg-gray-700 text-gray-200 px-3 py-2 rounded-lg" onchange="onServiceDownChange()">
|
||||
<option value="">-- 请选择要监控的服务 --</option>
|
||||
</select>
|
||||
<p class="text-gray-500 text-xs mt-1">选择需要监控下线状态的服务</p>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="block text-gray-400 text-sm mb-1">触发阈值 *</label>
|
||||
<div class="flex items-center gap-2">
|
||||
<input type="number" id="emailRuleThreshold" required min="1" class="w-20 bg-gray-700 text-gray-200 px-3 py-2 rounded-lg" value="80">
|
||||
<span class="text-gray-400" id="emailRuleThresholdUnit">% / °C</span>
|
||||
</div>
|
||||
<p class="text-gray-500 text-xs mt-1">对不同资源类型使用统一阈值(如CPU用%则填百分比,温度用°C则填温度值)</p>
|
||||
<p class="text-gray-500 text-xs mt-1" id="emailRuleThresholdHint">对不同资源类型使用统一阈值(如CPU用%则填百分比,温度用°C则填温度值)</p>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
@@ -1245,6 +1260,8 @@
|
||||
fetch('/api/system/stats').then(r => r.json()).then(sysData => {
|
||||
if (sysData.available) checkThresholds(sysData);
|
||||
}).catch(() => {});
|
||||
// 同时检查服务下线邮件规则
|
||||
checkEmailRules(null);
|
||||
|
||||
} catch (e) {
|
||||
console.error('加载失败:', e);
|
||||
@@ -1561,7 +1578,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
const resourceNames = { 'cpu': 'CPU', 'cpu_temp': 'CPU温度', 'memory': '内存', 'disk': '磁盘' };
|
||||
const resourceNames = { 'cpu': 'CPU', 'cpu_temp': 'CPU温度', 'memory': '内存', 'disk': '磁盘', 'service_down': '服务下线' };
|
||||
const resourceUnits = { 'cpu': '%', 'cpu_temp': '°C', 'memory': '%', 'disk': '%' };
|
||||
|
||||
list.innerHTML = rules.map(rule => {
|
||||
@@ -1575,14 +1592,19 @@
|
||||
|
||||
// 显示资源列表(支持多资源和旧版单资源兼容)
|
||||
const resources = rule.resources || (rule.resource ? [rule.resource] : []);
|
||||
const resourceDisplay = resources.map(r => resourceNames[r] || r).join('+');
|
||||
let resourceDisplay;
|
||||
if (resources.includes('service_down') && rule.serviceName) {
|
||||
resourceDisplay = `🔴 ${rule.serviceName} 下线 ≥ ${rule.threshold}分钟`;
|
||||
} else {
|
||||
resourceDisplay = resources.map(r => resourceNames[r] || r).join('+') + ' ≥ ' + rule.threshold;
|
||||
}
|
||||
|
||||
return `
|
||||
<div class="flex items-center justify-between bg-gray-700/50 px-3 py-2 rounded-lg">
|
||||
<div class="flex items-center gap-3 flex-wrap">
|
||||
<span class="px-2 py-0.5 rounded text-xs ${rule.enabled ? 'bg-green-500/20 text-green-400' : 'bg-gray-500/20 text-gray-400'}">${rule.enabled ? '启用' : '禁用'}</span>
|
||||
<span class="text-gray-200">${rule.name}</span>
|
||||
<span class="text-gray-400 text-xs">${resourceDisplay} ≥ ${rule.threshold}</span>
|
||||
<span class="text-gray-400 text-xs">${resourceDisplay}</span>
|
||||
<span class="text-blue-400/70 text-xs"><i class="ri-flashlight-line"></i> ${triggerDisplay}</span>
|
||||
<span class="text-gray-400 text-xs">→ ${rule.email}</span>
|
||||
${silentInfo ? `<span class="text-yellow-400/70 text-xs"><i class="ri-moon-line"></i> ${silentInfo}</span>` : ''}
|
||||
@@ -1602,9 +1624,13 @@
|
||||
document.getElementById('emailRuleName').value = '';
|
||||
// 重置资源复选框
|
||||
document.querySelectorAll('.email-rule-resource').forEach(cb => cb.checked = false);
|
||||
document.getElementById('serviceDownSelector').style.display = 'none';
|
||||
document.getElementById('emailRuleServiceId').value = '';
|
||||
document.getElementById('emailRuleThreshold').value = 80;
|
||||
document.getElementById('emailRuleThresholdUnit').textContent = '% / °C';
|
||||
document.getElementById('emailRuleThresholdHint').textContent = '对不同资源类型使用统一阈值';
|
||||
document.getElementById('emailRuleTriggerType').value = 'single';
|
||||
document.getElementById('emailRuleTriggerType').disabled = false;
|
||||
document.getElementById('emailRuleTriggerParam').value = 3;
|
||||
document.getElementById('triggerParamsDiv').style.display = 'none';
|
||||
document.getElementById('emailRuleAddress').value = '';
|
||||
@@ -1629,24 +1655,73 @@
|
||||
const selected = getSelectedResources();
|
||||
const unitEl = document.getElementById('emailRuleThresholdUnit');
|
||||
const thresholdEl = document.getElementById('emailRuleThreshold');
|
||||
const hintEl = document.getElementById('emailRuleThresholdHint');
|
||||
const serviceSelector = document.getElementById('serviceDownSelector');
|
||||
const triggerTypeEl = document.getElementById('emailRuleTriggerType');
|
||||
const triggerParamsDiv = document.getElementById('triggerParamsDiv');
|
||||
|
||||
if (selected.length === 0) {
|
||||
unitEl.textContent = '% / °C';
|
||||
} else if (selected.length === 1 && selected[0] === 'cpu_temp') {
|
||||
unitEl.textContent = '°C';
|
||||
if (parseInt(thresholdEl.value) > 120) thresholdEl.value = 80;
|
||||
thresholdEl.max = 120;
|
||||
thresholdEl.min = 30;
|
||||
} else {
|
||||
unitEl.textContent = '%';
|
||||
thresholdEl.max = 100;
|
||||
const hasServiceDown = selected.includes('service_down');
|
||||
|
||||
// 显示/隐藏服务选择器
|
||||
if (hasServiceDown) {
|
||||
serviceSelector.style.display = 'block';
|
||||
populateServiceSelector();
|
||||
// 服务下线使用分钟作为阈值单位
|
||||
unitEl.textContent = '分钟';
|
||||
thresholdEl.value = 5;
|
||||
thresholdEl.max = 1440;
|
||||
thresholdEl.min = 1;
|
||||
if (selected.includes('cpu_temp')) {
|
||||
hintEl.textContent = '服务连续离线超过此分钟数即触发通知';
|
||||
// 服务下线固定为duration类型
|
||||
if (triggerTypeEl) {
|
||||
triggerTypeEl.value = 'duration';
|
||||
triggerTypeEl.disabled = true;
|
||||
}
|
||||
if (triggerParamsDiv) triggerParamsDiv.style.display = 'none';
|
||||
} else {
|
||||
serviceSelector.style.display = 'none';
|
||||
if (triggerTypeEl) triggerTypeEl.disabled = false;
|
||||
updateTriggerParams();
|
||||
|
||||
if (selected.length === 0) {
|
||||
unitEl.textContent = '% / °C';
|
||||
hintEl.textContent = '对不同资源类型使用统一阈值';
|
||||
} else if (selected.length === 1 && selected[0] === 'cpu_temp') {
|
||||
unitEl.textContent = '°C';
|
||||
if (parseInt(thresholdEl.value) > 120) thresholdEl.value = 80;
|
||||
thresholdEl.max = 120;
|
||||
thresholdEl.min = 30;
|
||||
hintEl.textContent = 'CPU温度超过此值即触发通知';
|
||||
} else {
|
||||
unitEl.textContent = '%';
|
||||
thresholdEl.max = 100;
|
||||
thresholdEl.min = 1;
|
||||
hintEl.textContent = '对不同资源类型使用统一阈值';
|
||||
if (selected.includes('cpu_temp')) {
|
||||
unitEl.textContent = '% / °C';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function populateServiceSelector() {
|
||||
const sel = document.getElementById('emailRuleServiceId');
|
||||
const currentVal = sel.value;
|
||||
// 收集所有web类型服务
|
||||
const webServices = projects.filter(p => p.type === 'web');
|
||||
let options = '<option value="">-- 请选择要监控的服务 --</option>';
|
||||
webServices.forEach(p => {
|
||||
const portsStr = p.ports ? p.ports.join(',') : '';
|
||||
const selected = p.id === currentVal ? ' selected' : '';
|
||||
options += `<option value="${p.id}"${selected}>${p.name} (端口: ${portsStr})</option>`;
|
||||
});
|
||||
sel.innerHTML = options;
|
||||
}
|
||||
|
||||
function onServiceDownChange() {
|
||||
// 将来可以在这里做联动
|
||||
}
|
||||
|
||||
function updateTriggerParams() {
|
||||
const triggerType = document.getElementById('emailRuleTriggerType').value;
|
||||
const paramsDiv = document.getElementById('triggerParamsDiv');
|
||||
@@ -1704,6 +1779,10 @@
|
||||
if (cb) cb.checked = true;
|
||||
});
|
||||
onResourceChange();
|
||||
// 服务下线相关
|
||||
if (rule.serviceId) {
|
||||
document.getElementById('emailRuleServiceId').value = rule.serviceId;
|
||||
}
|
||||
document.getElementById('emailRuleThreshold').value = rule.threshold;
|
||||
document.getElementById('emailRuleTriggerType').value = rule.triggerType || 'single';
|
||||
document.getElementById('emailRuleTriggerParam').value = rule.triggerParam || 3;
|
||||
@@ -1732,6 +1811,7 @@
|
||||
const silentEnd = document.getElementById('emailRuleSilentEnd').value;
|
||||
const silentEnabled = document.getElementById('emailRuleSilentEnabled').checked;
|
||||
const enabled = document.getElementById('emailRuleEnabled').checked;
|
||||
const serviceId = document.getElementById('emailRuleServiceId').value;
|
||||
|
||||
if (!name || !email) {
|
||||
alert('请填写完整信息');
|
||||
@@ -1743,23 +1823,32 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// 服务下线必须选择具体服务
|
||||
if (resources.includes('service_down') && !serviceId) {
|
||||
alert('请选择要监控的服务');
|
||||
return;
|
||||
}
|
||||
|
||||
const rules = getEmailRules();
|
||||
|
||||
if (ruleId) {
|
||||
// 编辑
|
||||
const idx = rules.findIndex(r => r.id === ruleId);
|
||||
if (idx >= 0) {
|
||||
rules[idx] = { ...rules[idx], name, resources, threshold, triggerType, triggerParam, email, interval, silentStart, silentEnd, silentEnabled, enabled };
|
||||
rules[idx] = { ...rules[idx], name, resources, threshold, triggerType, triggerParam, email, interval, silentStart, silentEnd, silentEnabled, enabled, serviceId };
|
||||
}
|
||||
} else {
|
||||
// 新增
|
||||
const svc = projects.find(p => p.id === serviceId);
|
||||
rules.push({
|
||||
id: 'rule_' + Date.now(),
|
||||
name, resources, threshold, triggerType, triggerParam, email, interval, silentStart, silentEnd, silentEnabled, enabled,
|
||||
serviceId, serviceName: svc ? svc.name : '',
|
||||
offlineSince: null, // 服务下线开始时间
|
||||
lastSent: 0,
|
||||
exceedCount: 0, // 连续超过次数
|
||||
exceedDuration: 0, // 累计超过时间
|
||||
recentValues: [] // 最近采样值
|
||||
exceedCount: 0,
|
||||
exceedDuration: 0,
|
||||
recentValues: []
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1818,6 +1907,33 @@
|
||||
|
||||
for (const resource of resources) {
|
||||
let value;
|
||||
|
||||
// 服务下线监控 - 特殊处理(不需要data参数)
|
||||
if (resource === 'service_down') {
|
||||
const svcId = rule.serviceId;
|
||||
if (!svcId) continue;
|
||||
const svc = projects.find(p => p.id === svcId);
|
||||
if (!svc) continue;
|
||||
const isOnline = svc.status?.status === 'running';
|
||||
|
||||
if (isOnline) {
|
||||
rule.offlineSince = null;
|
||||
} else {
|
||||
if (!rule.offlineSince) {
|
||||
rule.offlineSince = now;
|
||||
}
|
||||
const offlineMinutes = (now - rule.offlineSince) / 60000;
|
||||
if (offlineMinutes >= rule.threshold) {
|
||||
const svcName = svc.name || rule.serviceName || svcId;
|
||||
exceededResources.push(`🔴 ${svcName} 已离线 ${Math.round(offlineMinutes)} 分钟 (阈值: ${rule.threshold} 分钟)`);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// 系统资源监控需要 data 参数
|
||||
if (!data) continue;
|
||||
|
||||
if (resource === 'cpu_temp') {
|
||||
// CPU温度取两个Package中较高的那个
|
||||
const t0 = data.cpu?.temp_package_0;
|
||||
|
||||
Reference in New Issue
Block a user