采集过程:
categraf做为采集器,使用input.exec插件采集linux文件是否存在,并将采集结果传至监控系统。
编辑自定义采集配置文件exec.toml,该文件用来配置执行的采集脚本
/opt/categraf/conf/input.exec/exec.toml
配置文件内容:
# # 采集时间,如果设置了全局的采集时间,这里可以无需设置
# interval = 15
[[instances]]
# # 执行脚本的目录
commands = [
"/opt/categraf/conf/input.exec/scripts/*.sh"
]
# # timeout for each command to complete
timeout = 10
# # interval = global.interval * interval_times
# interval_times = 1
# # mesurement,labelkey1=labelval1,labelkey2=labelval2 field1=1.2,field2=2.3
data_format = "influx"
创建判断文件是否存在的脚本:
/opt/categraf/conf/input.exec/scripts/check_files.sh
脚本:
#! /bin/sh
file_dir=/opt/test.txt
status=0
filesize_mb=0
# 检查文件是否存在
if [[ -f "${file_dir}" ]]; then
# 文件存在,设置文件状态变量a为1
status=1
# 获取文件大小(字节)
filesize_bytes=$(stat -c%s "${file_dir}")
# 转换为MB并保留两位小数
filesize_mb=$(printf "%.2f" $(bc <<< "scale=2; $filesize_bytes / 1024 / 1024"))
else
# 文件不存在,设置文件状态为0,文件大小为0
status=0
filesize_mb=0
fi
echo "checkFile fileStatus="$status",filesize="${filesize_mb}
配置完成后,测试结果:
cd /opt/categraf
./categraf --test --inputs exec
监控系统再根据checkFile_fileStatus的值来判断文件是否存在,并配置相应的告警规则。