问题
I'm trying to convert a bash script to nagios plugin
The script will run a find command to see if files are created within x minutes:
#!/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
I tried to convert that script to a Nagios plugin here:
#!/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
But I keep getting an error message each time I run it:
./check_nvr.sh: line 9: syntax error near unexpected token `[[$newfiles'
./check_nvr.sh: line 9: `if [[$newfiles -ne 0]]'
I'm not sure how I can resolve this. I'd appreciate any guidance!
EDIT:
I changed the script to:
#!/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
But now I don't get an output after I ran the script
回答1:
@Donald_W
I got it to work by changing the script to:
#!/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
But I noticed that in Nagios Dashboard doesn't show that the host is not recording. I stopped the recording just to test and I'm getting the warning message when I ran the script but it doesn't show as a problem in Nagios.
回答2:
There are a few issues.
- With /bin/bash, you can simply use
[
rather than[[
. - You should leave spaces before and after the expression.
- The
case $newfiles in
is unnecessary.
So, this should work:
#!/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
Here's the output on my system.
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.
来源:https://stackoverflow.com/questions/32657539/nagios-plugin-to-check-files-are-created-within-x-minutes