My webspace provider offers a simple ssh access to upload and download files. One cannot install any software there and has a limited set of commands. I wanted to check the free space on that server because it often runs out of free space and then new uploads fail.
The script on the server
The server does not have a “quota” command and “df” is about the whole filesystem, not just about my webspace. So I used “du -ms .” to check how much data is saved in my webspace. I have creates a small shell script “checkfreespace.sh” and saved it on the server:
#!/bin/bash export USEDSPACE=`du -ms . | sed "s/[^0-9]*//g"`; if [ "$USEDSPACE" -lt "9000" ] ; then echo "OK - $USEDSPACE MB used" ; exit 0; else echo "CRITICAL - $USEDSPACE MB used"; exit 2; fi
It checks if less than 9000MB are used and returns “OK” with exit code 0. Otherwise it returns “CRITICAL” with exit code 2. These are keywords that are used by Nagios.
SSH configuration
Then it is necessary to make it possible to login using public key authentication (so that Nagios does not need a password).
- Use “chsh nagios” to set the shell of nagios to “/bin/bash”.
- Use “su – nagios” to switch to the home directory of nagios.
- Create a key pair if necessary e.g. using
ssh-keygen -t rsa -b 4096 -C your@email.com
in the .ssh directory of the nagios user and change the owner to the nagios user if necessary.
- Append the contents of the file .ssh/id_rsa.pub to the file .ssh/authorized_keys on the server.
- Use “chsh nagios” to set the shell of nagios back to “/bin/false”.
Nagios configuration
Finally you have to configure Nagios. Add this to the commands.cfg file:
define command{ command_name check_my_free_space command_line /usr/lib/nagios/plugins/check_by_ssh -H ssh.yourserver.com -l yourusername -C "bash checkfreespace.sh" -t 30 }
Afterward you have to use this new command in your services file, e.g. add this:
define service { service_description Free webspace; check_command check_my_free_space use generic-service; host_name android.calengoo.com; notification_interval 0 ; set > 0 if you want to be renotified normal_check_interval 60 }