0

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

Youssef Karami
  • 75
  • 1
  • 13

2 Answers2

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.

Youssef Karami
  • 75
  • 1
  • 13
-1

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.
Donald_W
  • 1,773
  • 21
  • 35
  • Personal choice tripleee. It's more portable. http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts – Donald_W Sep 21 '15 at 05:31
  • If portability to other shells were an issue, you would not use `#!/bin/bash`, would you? – tripleee Sep 21 '15 at 06:20
  • @Donald_W I just posted an answer if you can take a look please – Youssef Karami Sep 21 '15 at 14:16
  • Check the output of your script by running echo $? Immediately after executing it i – Donald_W Sep 21 '15 at 16:46
  • NRPE keeps timing out since the find command takes a lot of time to execute... Is there's a way to change the settings of the command and make it search only for the files created withing 10 minutes and not all the folder ? – Youssef Karami Sep 22 '15 at 16:04
  • I'd be surprised if it was timing out. How big is this folder? What's the output of `find . | wc -l` in the relevant directory? There is a useful guide to avoiding NRPE timeouts here: https://deadlockprocess.wordpress.com/2010/07/11/how-to-fix-service-check-time-outs-in-nagios-nrpe-deployed-in-centosrhel-5/ – Donald_W Sep 22 '15 at 20:08
  • PS Since you are new to Stack Overflow I thought it might be useful to point out that if you find answer useful, it's the convention to up vote it. :) – Donald_W Sep 22 '15 at 20:09