0

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.

anon
  • 19
  • 1
  • 6

1 Answers1

0

Here you go
hope it helps
PS one more things did it work when u tapped the "check the progress button" cause it didnt work for me. It kept showing me work

#!/usr/bin/env python3
import socket
import requests
localhost = socket.gethostbyname('localhost')
request = requests.get("http://www.google.com")
def check_localhost():
        return  localhost=='127.0.0.1'
def check_connectivity():
        return request.status_code==200
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 15 '21 at 23:31