I am trying to get the network module I imported into the health_checks.py to pass everything is ok but it keeps failing. The module works in its own right, but I'm trying to get the check_localhost and check_cpu_usage to print and it's still not printing anything. So I am currently unable to debug code to fix it and find out what's wrong, as every time I run the health_checks.py script it returns with "network checks failed"
Code for the health_checks.py script:
#!/usr/bin/env python3
from network import *
import shutil
import psutil
def check_disk_usage(disk):
"""Verifies that there's enough free space on disk"""
du = shutil.disk_usage(disk)
free = du.free / du.total * 100
return free > 20
def check_cpu_usage():
"""Verifies that there's enough unused CPU"""
usage = psutil.cpu_percent(1)
return usage < 75
# If there's not enough disk, or not enough CPU, print an error
if not check_disk_usage('/') or not check_cpu_usage():
print("ERROR!")
elif check_localhost() and check_connectivity():
print("Everything ok")
print(check_localhost())
print(check_cpu_usage())
else:
print("network checks failed")
Code for the network.py:
#!/usr/bin/env python3
import requests
import socket
def check_localhost():
localhost = socket.gethostbyname('localhost')
if localhost == '127.0.0.1':
return localhost
print(localhost)
def check_connectivity():
request = requests.get("http://www.google.com")
response = request.status_code
if response == '200':
return response
print(response)
I don't know if it's an import problem or an internal script problem, any help would be appreciated, thanks.