Skip to content

Instantly share code, notes, and snippets.

@nagarjunareddyg
Forked from Kimjunkuk/health_check.py
Created January 5, 2023 10:56
Show Gist options
  • Select an option

  • Save nagarjunareddyg/04cd02c18946ca0462fb2b69c3335f08 to your computer and use it in GitHub Desktop.

Select an option

Save nagarjunareddyg/04cd02c18946ca0462fb2b69c3335f08 to your computer and use it in GitHub Desktop.
Health check
#!/usr/bin/env python3
import socket
import shutil
import psutil
import emails
def check_localhost():
localhost=socket.gethostbyname('localhost')
return localhost=="127.0.0.1"
def check_disk_usage(disk):
du=shutil.disk_usage(disk)
free=du.free / du.total* 100
return free > 20
def check_memory_usage():
mu=psutil.virtual_memory().available
total = mu/(1024.0 ** 2)
return total > 500
def check_cpu_usage():
usage=psutil.cpu_percent(1)
return usage<80
def send_email(subject):
email = emails.generate_email("automation@example.com", "student-01-a8fcc4800a7c@example.com", subject,"Please check your system and resolve the issue as soon as possible.", "") #문제
emails.send_email(email)
if not check_cpu_usage():
subject="Error - CPU usage is over 80%"
print(subject)
send_email(subject) #문제
if not check_memory_usage():
subject="Error - Available memory is less than 500MB"
print(subject)
if not check_disk_usage('/'):
subject="Error - Available disk space is less than 20%"
print(subject)
send_email(subject)
if not check_localhost():
subject="Error - localhost cannot be resolved to 127.0.0.1"
print(subject)
send_email(subject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment