Permission denied when running nagios plugin

nagios

I just wrote a simple bash script that check if filesystem is working writing and deleting a file, but when I put it into Nagios, it never runs as expected.

root@debian:~# cat /usr/lib/nagios/plugins/check_filesys.sh
#!/bin/bash
# Script que checa se file system consegue escrever/ler arquivos
if  touch teste.txt && rm teste.txt; then
    echo OK - Teste OK!
    exit 0
else
    echo CRITICAL - Teste de escrita/leitura falhou!
    exit 2
fi


root@debian:~# ls -l /usr/lib/nagios/plugins/check_filesys.sh
-rwxrwxrwx 1 root root 217 Feb  5 10:40 /usr/lib/nagios/plugins/check_filesys.sh

I also found a debug script to see what is the problem, and that's the output:

 2016-1-5 13:31:16 ------ debugging
 cmd=[/usr/lib/nagios/plugins/check_filesys.sh]
 output=[touch: cannot touch `teste.txt': Permission denied
 CRITICAL - Teste de escrita/leitura falhou!

I tried inserting the nagios user in the /etc/sudoers file as :

nagios ALL:NOPASSWD: /bin/bash -c /usr/lib/nagios/plugins/check_filesys.sh *

but the problem persists. OBS: I'm checking in localhost.

Best Answer

You need to use a full path for the touch/rm, because your script is trying to run in /, and Nagios doesn't have write permission to /.

(You might be able to get away with using ~/test.txt if $HOME is set to something sane.)

You can verify this for yourself:

# pgrep nagios
556
# ls -ls /proc/556/cwd
0 lrwxrwxrwx 1 root root 0 Feb  5 11:10 /proc/556/cwd -> /

This indicates that the Nagios process is running with cwd set to /

Related Topic