操作系统:centos7.5
安装inotify-tools:
yum install inotify-tools
vi monitor.sh
#!/bin/bash
# 要监控的目录
DIRECTORY_TO_WATCH="/data/nfs/mservicelogs"
#机器人地址
webhook='https://xxx.xxx.com.cn/gateway/robot/webhook/send?yzjtype=0&yzjtoken=xxxx'
# 使用inotifywait来监控目录及其子目录下的文件创建事件
inotifywait -m -r -e create --format '%w%f' "$DIRECTORY_TO_WATCH" | while read NEW_FILE
do
# 检查文件是否是hprof文件
if [[ "$NEW_FILE" == *.hprof ]]; then
date=`date +"%F %T"`
# 发送通知,可以使用命令行工具如notify-send,或者其他通知方式
curl $webhook -H 'Content-Type: application/json' -d "{
'content': 'sit环境有服务内存溢出,dump文件生成!!!\n时间:${date}\n文件生成位置:${NEW_FILE}\n'}"
fi
done
填写上面的内容到monitor.sh。
chmod 777 monitor.sh
后台运行 nohup ./monitor.sh > /dev/null &