Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Created August 15, 2014 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcefoli/431f44cc20d585d389a1 to your computer and use it in GitHub Desktop.
Save jcefoli/431f44cc20d585d389a1 to your computer and use it in GitHub Desktop.
This python script will reboot your DigitalOcean droplet if the hostname is unreachable or returns a status 500 or 503. All you have to do is edit the variables. Can be scheduled as a cron job.
#!/usr/bin/python
import httplib
import subprocess
############################## Site To Check #################################
VAR_hostname = "yoursite.com"
VAR_path = "/"
######################### DigitalOcean Variables #############################
VAR_dropletID = "123456"
VAR_Token = "YOUR_API_TOKEN_HERE"
##############################################################################
def get_status_code(host=VAR_hostname, path=VAR_path):
try:
conn = httplib.HTTPConnection(host,timeout=1)
conn.request("HEAD", path)
return conn.getresponse().status
except StandardError:
return "Err"
checkStatus = get_status_code()
if (checkStatus == "Err" or checkStatus == 500 or checkStatus == 503):
shellCMD = "curl -X POST \"https://api.digitalocean.com/v2/droplets/" + str(VAR_dropletID) + "/actions\" -d'{\"type\":\"reboot\"}' -H \"Authorization: Bearer " + str(VAR_Token) + "\" -H \"Content-Type: application/json\""
p = subprocess.Popen(shellCMD, shell=True, stdout=False, stderr=False)
retval = p.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment