如何通过xdmp:email根据主机状态设置MarkLogic监控邮件重要性?
Got it, let's walk through how to build this dynamic priority monitoring email system for MarkLogic using scheduled tasks and xdmp:email. Here's a practical, step-by-step implementation:
实现方案
1. 编写资源状态检测函数
First, we need a helper function to check if the current host's resources exceed your defined thresholds (we'll use CPU, memory, and disk as examples—adjust these based on your actual needs):
declare function local:check-resource-critical() as xs:boolean { let $current-host := xdmp:host() let $host-metrics := xdmp:host-status($current-host) -- 自定义资源基准阈值,可根据业务调整 let $cpu-threshold := 80 (: CPU使用率阈值,单位% :) let $memory-threshold := 85 (: 内存使用率阈值,单位% :) let $disk-threshold := 90 (: 磁盘分区使用率阈值,单位% :) let $current-cpu := $host-metrics/cpu/usage let $current-memory := $host-metrics/memory/usage -- 如果需要监控特定磁盘分区,可调整partition的索引值 let $current-disk := $host-metrics/disk/partition[1]/usage -- 任意一项资源超过阈值即返回true,标记为高优先级 return ($current-cpu > $cpu-threshold) or ($current-memory > $memory-threshold) or ($current-disk > $disk-threshold) };
这个函数通过xdmp:host-status()获取当前主机的实时状态,和预设阈值对比,只要有一项指标超标,就判定为需要高优先级告警。
2. 构建动态优先级邮件
接下来创建邮件发送函数,根据资源检测结果调整邮件的视觉标记和系统优先级,用HTML格式让红色告警更醒目,同时设置邮件客户端能识别的高优先级标记:
declare function local:send-monitoring-alert() { let $is-critical := local:check-resource-critical() -- 根据状态设置优先级标签,高优先级用红色加粗样式 let $priority-tag := if ($is-critical) then "<span style='color: #dc3545; font-weight: 700;'>【高优先级 - 资源告警】</span>" else "【常规监控报告】" let $email-subject := concat($priority-tag, " MarkLogic主机监控 - ", xdmp:host-name(xdmp:host())) let $email-body := "<html> <body style='font-family: Arial, sans-serif;'> <h2>" || $priority-tag || "</h2> <p><strong>主机名称:</strong> " || xdmp:host-name(xdmp:host()) || "</p> <p><strong>主机ID:</strong> " || xdmp:host() || "</p> <h3>资源使用详情:</h3> <ul> <li>CPU使用率: " || xdmp:host-status(xdmp:host())/cpu/usage || "%</li> <li>内存使用率: " || xdmp:host-status(xdmp:host())/memory/usage || "%</li> <li>磁盘使用率: " || xdmp:host-status(xdmp:host())/disk/partition[1]/usage || "%</li> </ul> </body> </html>" -- 替换为实际收件邮箱,支持多个邮箱组成的序列,比如("user1@domain.com", "user2@domain.com") let $recipient := "monitoring-team@yourdomain.com" let $email-options := <options xmlns="xdmp:email"> <content-type>text/html</content-type> -- 高优先级邮件添加系统级重要性标记,邮件客户端会识别为紧急邮件 { if ($is-critical) then <importance>high</importance> else () } </options> -- 用try/catch包裹发送逻辑,避免邮件发送失败导致定时任务中断 return try { xdmp:email($recipient, $email-subject, $email-body, $email-options) } catch ($e) { xdmp:log(concat("监控邮件发送失败: ", $e/error:message), "error") } };
这里的关键细节:
- 红色标签用了通用的危险色值,你可以根据需求调整颜色和样式
<importance>high</importance>会让Outlook、Gmail等客户端把邮件标记为高优先级(通常显示红色感叹号)- 加入
try/catch捕获异常,避免邮件发送故障影响定时任务的后续执行
3. 创建定时任务
最后把上述代码部署为MarkLogic的定时任务,让它自动定期执行:
- 登录MarkLogic管理控制台(Admin Interface)
- 导航到 Tasks → Create Task
- 填写任务信息:
- Task Name: 比如「MarkLogic主机监控邮件任务」
- Task Type: 选择 XQuery
- XQuery: 粘贴完整的函数代码,或者引用存储在模块库中的
.xqy模块路径 - Schedule: 设置执行频率,比如每小时执行一次(表达式
0 * * * *),或每天早8点执行(0 8 * * *) - Host: 选择「All Hosts」让集群中每个节点监控自身状态,或指定特定主机
- 保存任务即可
额外优化建议
- 阈值参数化: 把资源阈值存储到配置文档中,而非硬编码,后续调整阈值无需修改任务代码
- 扩展监控指标: 可以添加查询队列长度、磁盘IO、副本同步延迟等更多业务关注的指标
- 告警收敛: 如果担心频繁告警打扰,可以添加逻辑,比如连续两次超标才发送高优先级邮件
内容的提问来源于stack exchange,提问作者Amit Gope
相关产品推荐
相关产品推荐

