【问题标题】:Nagios plugin to check files are created within x minutes用于检查文件的 Nagios 插件在 x 分钟内创建
【发布时间】:2015-12-15 22:17:37
【问题描述】:

我正在尝试将 bash 脚本转换为 nagios 插件

该脚本将运行一个查找命令来查看是否在 x 分钟内创建了文件:

#!/bin/bash
#
#Check NVR 


newfiles=$(find /srv/unifi-video/videos/* -name '*.ts' -mmin -10 | wc -l)

if [[ $newfiles -eq 0 ]] ; then
    echo "!!!WARNING!!! The NVR has System stopped recording."
fi

我尝试在此处将该脚本转换为 Nagios 插件:

#!/bin/bash
#
#Check NVR
#http://www.netchester.com
#Check if NVR System is recording correctly
newfiles=$(find /srv/unifi-video/videos/* -name '*.ts' -mmin -10 | wc -l)
case $mewfiles in
if [[$newfiles -ne 0]]
then
echo "!!!OK!!! NVR is recording."
exit 0
fi
if [[$newfiles -eq 0]]
then
echo "!!!WARNING!!! NVR is not recording."
exit 1
fi

但每次运行时我都会收到一条错误消息:

./check_nvr.sh: line 9: syntax error near unexpected token `[[$newfiles'
./check_nvr.sh: line 9: `if [[$newfiles -ne 0]]'

我不确定如何解决这个问题。我将不胜感激任何指导!

编辑:

我把脚本改成:

#!/bin/bash
#
#Check NVR
#http://www.netchester.com
#Youssef Karami
#Check if NVR System is recording correctly
newfiles=$(find /srv/unifi-video/videos/ -name '*.ts' -mmin -10 | wc -l)
case $newfiles in
[1]*)
echo "OK - $newfiles$ found."
exit 0
;;
[0]*)
echo "WARNING - $newfiles% found."
exit 1
;;
esac

但是现在我运行脚本后没有得到输出

【问题讨论】:

  • 我不是专家,但您不需要在[[ 之后和]] 之前有空格吗?
  • 无论有没有空格,我都会收到相同的错误消息
  • 请看:shellcheck.net
  • 对不起,我的 if 语句总是写成 if [ $test == 0 ] then ... else .. fi
  • @NoseKnowsAll 它没有改变任何东西......

标签: linux bash find nagios


【解决方案1】:

@Donald_W

我通过将脚本更改为:

#!/bin/bash
#
#Check NVR
#http://www.netchester.com/scripts_sh/check_nvr
#Youssef Karami
#Check if NVR System is recording correctly
newfiles=$(find /srv/unifi-video/videos/ -name '*.ts' -mmin -10 | wc -l)
case $newfiles in
[1-9]*)
echo "!!!OK!!! - NVR is working properly - $newfiles found."
exit 0
;;
[0]*)
echo "!!!WARNING!!! - NVR is not recording - $newfiles found."
exit 1
;;
esac

但我注意到在 Nagios Dashboard 中没有显示主机没有录制。我停止录制只是为了测试,当我运行脚本时收到警告消息,但它在 Nagios 中没有显示为问题。

【讨论】:

    【解决方案2】:

    有几个问题。

    • 使用 /bin/bash,您可以简单地使用 [ 而不是 [[
    • 表达式前后应留有空格。
    • case $newfiles in 是不必要的。

    所以,这应该可行:

    #!/bin/bash
    #
    #Check NVR
    #http://www.netchester.com
    #Check if NVR System is recording correctly
    
    newfiles=$(find . -name '*.ts' -mmin -10 | wc -l)
    
    if [ $newfiles != 0 ]
    then
    echo "!!!OK!!! NVR is recording."
    exit 0
    fi
    if [ $newfiles == 0 ]
    then
    echo "!!!WARNING!!! NVR is not recording."
    exit 1
    fi
    

    这是我系统上的输出。

    vagrantt:puppet donald$ ./check.sh
    !!!WARNING!!! NVR is not recording.
    vagrant:puppet hdonald$ touch blah.ts
    vagrant:puppet donald$ ./check.sh
    !!!OK!!! NVR is recording.
    

    【讨论】:

    • 个人选择三人组。它更便携。 stackoverflow.com/questions/669452/…
    • 如果到其他 shell 的可移植性是个问题,您不会使用 #!/bin/bash,对吗?
    • @Donald_W 我刚刚发布了一个答案,如果你能看一下请
    • 通过运行 echo $? 检查脚本的输出?执行后立即 i
    • NRPE 一直超时,因为 find 命令需要很长时间才能执行...有没有办法更改命令的设置并使其仅搜索 10 分钟内创建的文件和不是所有文件夹?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多