温馨提示×

linux syslog怎样告警

小樊
51
2026-10-16 10:12:38
栏目: 智能运维

在 Linux 里,syslog 本身只是日志收集和记录系统,它本身不会“主动告警”,但你可以通过配合其他工具或脚本来实现告警。下面按常见场景给你梳理几种做法。


一、基础概念

  • syslog / rsyslog / syslog-ng:负责接收、存储日志
  • 告警:通常指:
    • 邮件通知
    • 短信/微信/钉钉
    • 监控平台(Zabbix、Prometheus)
    • 本地触发脚本

二、方式一:rsyslog + 规则触发脚本(最常用)

1. 在 rsyslog 中匹配日志

示例:监控 sshd 的失败登录

# /etc/rsyslog.d/ssh.conf
:programname, contains, "sshd" /var/log/ssh.log
& ~

2. 使用 rsyslog 的 action 触发脚本

:msg, contains, "Failed password" ^/usr/local/bin/alert.sh

alert.sh 示例:

#!/bin/bash
echo "SSH 登录失败告警" | mail -s "SSH Alert" admin@example.com

注意:rsyslog 触发脚本时是通过 stdin 接收日志内容的


三、方式二:logwatch(每日邮件告警)

适合周期性汇总告警

yum install logwatch   # CentOS
apt install logwatch   # Ubuntu
logwatch --detail High --mailto admin@example.com

可加入 crontab:

0 8 * * * /usr/sbin/logwatch

四、方式四:实时监控日志文件(tail + 脚本)

tail -F /var/log/messages | while read line
do
  echo "$line" | grep "error" && \
  curl "https://api.dingtalk.com/robot/send?..." 
done

适合:

  • 简单场景
  • 自定义强

五、方式五:系统级告警(journald + systemd)

journalctl -f | grep "error"

或:

systemd-cat echo "test alert"

六、方式六:对接监控平台(推荐生产环境)

Zabbix

  • 使用 log[] 监控项
  • 触发告警动作

Prometheus + Loki

  • 日志采集
  • 告警规则(Alertmanager)

七、常见告警场景示例

场景 方法
SSH 暴力破解 rsyslog + 脚本
磁盘错误 smartd + mail
服务崩溃 systemd + mail
安全事件 SIEM / ELK

八、总结建议

  • 简单服务器:rsyslog + mail
  • 批量服务器:Zabbix / Prometheus
  • 安全审计:ELK + 告警

如果你能告诉我:

  • 用的发行版(CentOS / Ubuntu)
  • 想告警的具体内容(SSH / 磁盘 / 应用)

我可以直接给你一套可复制的配置。

0 踩