使用环境:
某应用程序将 数据文件,实时落地到 本地磁盘 中,落地目录为:
D:\record\signal\当天日期\小时时间,例:D:\record\signal\2024-02-26\9\
使用目的:
使用zabbix(6.4.8) 自定义监控, agent调用本地bat脚本:
检测 是否存在 上一个小时的目录 ,存在输出1,不存在输出0
zabbix server收集数据后,对0和1进行判断,输出为0时触发告警!
测试实施:
windows脚本(D:\zabbix\zabbix_agent2.d\plugins.d\SignalPreviousHours.bat)内容:
@echo off
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%date:~0,4%-%date:~5,2%-%date:~8,2%)
::for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set timeShi=%time:~0,2%)
::echo %timeShi%
setlocal enabledelayedexpansion
for /f "tokens=1-3 delims=:." %%a in ("%time%") do (
set "hour=%%a"
set "minute=%%b"
set "second=%%c"
)
set /a "prev_hour=!hour!-1"
if !prev_hour! lss 0 (
set /a "prev_hour=prev_hour+24"
)
::echo %hour%:%minute%:%second%
::echo %prev_hour%:%minute%:%second%
set "dir_path=D:\record\signal\%mydate%\%prev_hour%"
::set "dir_path=D:\sentry\signal\%mydate%\%prev_hour%"
::echo %dir_path%
IF EXIST %dir_path% (
echo 1
) else (
echo 0
)
endlocal
::pause
windows zabbix_agent2.conf 文件配置如下:
LogFile=D:\zabbix\zabbix_agent2.log
Server=127.0.0.1
ServerActive=XXX.XXX.XXX.XXX
Hostname=ROOTWORK
Include=D:\zabbix\zabbix_agent2.d\
ControlSocket=\\.\pipe\agent.sock
Include=.\zabbix_agent2.d\plugins.d\*.conf
UserParameter=SignalPreviousHours,D:\zabbix\zabbix_agent2.d\plugins.d\SignalPreviousHours.bat
自定义监控项名称:SignalPreviousHours
调用脚本位置:D:\zabbix\zabbix_agent2.d\plugins.d\SignalPreviousHours.bat