我试图将bash脚本转换为nagios插件
该脚本将运行find命令以查看是否在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
但是现在我在运行脚本
后没有得到输出答案 0 :(得分: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中显示为问题。
答案 1 :(得分:-1)
有一些问题。
[
而不是[[
。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.