Monitor Servers and Reboot SolusVM via Cronjob Bash Script

Monitor Servers and Reboot SolusVM via Cronjob Bash ScriptNathan WellsBlockedUnblockFollowFollowingApr 11I’ve been looking for a way to monitor my various VPS web servers and to automatically reboot them if something goes wrong but I was never able to find a simple solution.

I’ve used Monit — but at times it has failed to keep my servers up and running.

So I’ve created my own bash script to do the job and thought I would share it.

First, you will obviously need a seperate VPS you can use to monitor your other VPS servers (I’m using an easy TurnKey Linux on mine which is Debian-based so you may need to modify the script based on what you are running).

Then, create API credentials in SolusVM for each of the servers you would like to monitor:You’ll then need to copy the API KEY and the API HASH that is generated for each server so you can use it in the bash script we will create to run in crontab.

Next, connect to the server you are going to use to monitor your other servers via SSH and create a new bash script (you can name it whatever you want, for example: monitor.

sh) somewhere on your server — preferably not somewhere that is exposed to the wider internet (a good place might be in your etc folder)touch monitor.

shThen open the file with your favorite command line editor and copy and paste this script into the file (I prefer using nano to edit files but you can use whatever you’d like), and edit the script to match your VPS information as well as the location of your server’s SolusVM API’s command.

php, etc.

(you can ask your host for this location).

#!/bin/bash#Email address where you would like to receive notification that your server was rebootedYOUREMAIL="example@email.

com"#Location of your host's SolusVM API command.

php so you can reboot your server via API – you can request this from your hostAPICOMMANDURL="https://YOURHOSTSOLUSVMAPIURL.

something/api/command.

php"declare -A SERVER #declare our associative array for server information# Create an associative array with server info – replace the NAME part with a descriptive name for each server and use the same name for the matching KEY and HASH.

If you have more servers just add a new line with that server information.

Also replace "customurltotest" with the url you want to test on that server.

If you use PHP on your server, select a url that will test and make sure PHP is running correctly (and if you are using MySQL – a PHP file that will test the MySQL connection).

SERVER[NAME1]=$((curl -Is 'CUSTOMURLTOTEST' | head -n 1)|tr -d '
')SERVER[NAME2]=$((curl -Is 'CUSTOMURLTOTEST' | head -n 1)|tr -d '
')declare -A KEY #declare associative array for the API keys# Create an associative array with the API key for each serverKEY[NAME1]="REPLACEWITHAPIKEY"KEY[NAME2]="REPLACEWITHAPIKEY"declare -A HASH #declare associative array for API hashes# Create an associative array with the API hash for each serverHASH[NAME1]="REPLACEWITHAPIHASH"HASH[NAME2]="REPLACEWITHAPIHASH"#Now we cycle through each server – test if the server returns a 200 OK header and if not, reboot the server via the SolusVM API and also send a notification emailfor V in "${!SERVER[@]}" do if [ "${SERVER[${V}]}" == "HTTP/1.

1 200 OK" ] then echo "$V is up" else echo "$V is down" curl "$APICOMMANDURL?key=${KEY[${V}]}&hash=${HASH[${V}]}&action=reboot"> /dev/null 2>&1 echo "$V was rebooted because of some issue" | mail -s "$V Rebooted" $YOUREMAIL fidoneSave the script and then change the permission on the script so only the owner can run it (read it and edit it):chmod 0700 monitor.

shThen you can test the script to make sure everything is working (try shutting down one of the servers you want to monitor and then run the script to make sure everything is working) by running:bash monitor.

shOnce you varify the script is working, you can then create a new cronjob to run the script at various intervals (maybe every 15 minuites) to ensure your servers will keep up and running.

Personally, I like using nano as my editor rather than vim — so if you are like me you can run this command so you can edit the cronjob in nano:export VISUAL=nano; crontab -eOtherwise if you are fine with the default editor you can just use:crontab -eAnd add this line (edit it to match the script’s location and how often you want to run the job — in this example, every 15 minutes).

*/15 * * * * /bin/bash /YOURPATH/monitor.

shAnd there you have it!.. More details

Leave a Reply