如何利用Ubuntu Trigger监控系统
导读:在 Ubuntu 上,所谓 Trigger 通常指由 systemd 服务、定时任务 cron、一次性任务 at,以及文件系统事件(如 inotify)触发的动作。下面给出一套可落地的监控与告警方案,覆盖服务、任务、日志与文件变化等常见场景...
在 Ubuntu 上,所谓 Trigger 通常指由 systemd 服务、定时任务 cron、一次性任务 at,以及文件系统事件(如 inotify)触发的动作。下面给出一套可落地的监控与告警方案,覆盖服务、任务、日志与文件变化等常见场景。
一 监控思路与工具选型
- 服务与任务触发器
- 用 systemd 管理服务生命周期与“触发器”(如 OnCalendar、ExecStartPre/ExecStartPost、Restart= 等),通过 journalctl 与 systemd 内置状态进行观测与告警。
- 用 cron 与 at 执行定时或一次性任务,结合日志审计其执行结果。
- 文件与日志触发器
- 用 inotifywait(inotify-tools)监听目录/文件的创建、修改、关闭写入等事件,作为“文件触发”的监控与联动入口。
- 性能与健康度
- 用 top/htop/vmstat/iostat/nmon 做即时排障,用 Prometheus + Grafana 做时序与可视化,用 Alertmanager 做告警路由与通知。
二 快速上手 服务与任务触发器监控
- systemd 服务
- 查看与跟踪:sudo systemctl status ;sudo journalctl -u -f
- 自动拉起与重启:在单元中设置 Restart=always/on-failure,并配合 OnFailure= 指向故障处理单元。
- 定时触发:使用 OnCalendar=daily 或 systemd timers(例如:OnCalendar=--* 02:00:00 表示每天 02:00 触发)。
- cron 定时任务
- 记录执行日志:编辑 /etc/rsyslog.d/50-default.conf,取消注释 cron.* 行,指向 /var/log/cron.log,然后 sudo systemctl restart rsyslog;查看 tail -f /var/log/cron.log。
- 任务审计:在脚本中将关键输出与退出码重定向到专用日志,便于后续告警与追溯。
- at 一次性任务
- 队列与详情:atq 查看待执行任务;at -c < job_id> 查看脚本内容。
三 文件与日志触发器监控
- 安装 inotify-tools:sudo apt-get update & & sudo apt-get install inotify-tools
- 监听文件修改并触发动作(示例)
- inotifywait -m -e modify,close_write /var/log/myapp.log | while read path action file; do /usr/local/bin/handle_trigger.sh “$path” “$file” “$action” done
- 建议做法
- 将触发脚本做成 systemd 服务,并用 Restart=on-failure 保证可靠性。
- 对高频事件做 去抖(如按时间窗口合并处理),避免抖动放大。
四 性能与健康度监控及告警
- 即时排障
- 资源与负载:top/htop(进程)、vmstat(虚拟内存/CPU)、iostat(磁盘/CPU)、nmon(综合)。
- 时序与可视化
- 部署 Prometheus 采集节点与应用指标,配置 scrape_configs 指向目标;在 Grafana 中创建仪表盘进行可视化。
- 告警规则与通知
- 在 Prometheus 配置 rule_files(如 rules.yml),示例:
- groups:
- name: host
rules:
- alert: InstanceDown expr: up == 0 for: 1m labels: severity: page annotations: summary: “Instance { { $labels.instance } } down” description: “{ { $labels.instance } } of job { { $labels.job } } down for > 1m”
- name: host
rules:
- groups:
- 配置 Alertmanager 接收 Prometheus 告警,并路由到邮件、企业微信、钉钉或 Slack。
- 在 Prometheus 配置 rule_files(如 rules.yml),示例:
五 落地示例 将“文件触发 + 服务自愈 + 告警”串起来
- 目标:当 /var/log/myapp/error.log 出现新错误行时,自动重启服务,并在持续失败时发出告警。
- 步骤
- 创建触发器脚本 /usr/local/bin/trigger_on_error.sh
- #!/usr/bin/env bash
LOG=“/var/log/myapp/error.log”
inotifywait -q -e modify,close_write “$LOG” |
while read -r path action file;
do
简单去抖:1 分钟内只处理一次
if ! pgrep -x “restart_myapp.timer” > /dev/null; then systemctl start restart_myapp.timer fi done
- #!/usr/bin/env bash
LOG=“/var/log/myapp/error.log”
inotifywait -q -e modify,close_write “$LOG” |
while read -r path action file;
do
- 创建 systemd 定时器(一次性、延迟 60 秒执行,避免抖动)
- /etc/systemd/system/restart_myapp.timer
- [Unit] Description=Debounce and restart myapp on error [Timer] OnActiveSec=60 AccuracySec=1s [Install] WantedBy=timers.target
- /etc/systemd/system/restart_myapp.service
- [Unit] Description=Restart myapp [Service] Type=oneshot ExecStart=/bin/systemctl try-restart myapp.service
- /etc/systemd/system/restart_myapp.timer
- 配置告警
- 在 Prometheus 增加记录规则或告警规则,监测 myapp 的 up 或自定义业务指标;当连续失败触发 Alertmanager 通知。
- 启动与验证
- sudo systemctl daemon-reload
- sudo systemctl enable --now restart_myapp.timer
- 观察:journalctl -u myapp -f、journalctl -u restart_myapp -f、Prometheus/Grafana 告警面板。
- 创建触发器脚本 /usr/local/bin/trigger_on_error.sh
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 如何利用Ubuntu Trigger监控系统
本文地址: https://pptw.com/jishu/768199.html
