categraf配置监控linux文件是否存在

采集过程:

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的值来判断文件是否存在,并配置相应的告警规则。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值