在 Linux 里,syslog 本身只是日志收集和记录系统,它本身不会“主动告警”,但你可以通过配合其他工具或脚本来实现告警。下面按常见场景给你梳理几种做法。
示例:监控 sshd 的失败登录
# /etc/rsyslog.d/ssh.conf
:programname, contains, "sshd" /var/log/ssh.log
& ~
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 接收日志内容的
适合周期性汇总告警
yum install logwatch # CentOS
apt install logwatch # Ubuntu
logwatch --detail High --mailto admin@example.com
可加入 crontab:
0 8 * * * /usr/sbin/logwatch
tail -F /var/log/messages | while read line
do
echo "$line" | grep "error" && \
curl "https://api.dingtalk.com/robot/send?..."
done
适合:
journalctl -f | grep "error"
或:
systemd-cat echo "test alert"
log[] 监控项| 场景 | 方法 |
|---|---|
| SSH 暴力破解 | rsyslog + 脚本 |
| 磁盘错误 | smartd + mail |
| 服务崩溃 | systemd + mail |
| 安全事件 | SIEM / ELK |
如果你能告诉我:
我可以直接给你一套可复制的配置。