VLT-Website-Heading

How to complete the Working with Python Scripts lab

Aug 11, 2021 11:00:00 AM / by Vu Long Tran

If you are working through the "Working with Python Scripts"  lab quest and are stuck, here are some of my solution to the quest to help you, please do try attempt it yourself progressively, as it is important to continue to learn what you might be missing, and view what I am sharing as one of the the possible solutions to solve it.

Essentially the goal of this lab is to fix problems in a Python script, then create another Python script module to utilise it.

How to complete the "Working with Python Scripts" lab

For the "Working with Python Scripts" lab, you'll first have to fix an incorrect Python script. This includes:

  1. Fixing the file permissions to make it executable.
  2. Fixing a bug in the code.
  3. After that, you'll write your own Python module
  4. Use Python module from the original script.

working-with-python-scripts

1. Fix file permissions

Solution: here is to check for script and make it executable.

You can do this by doing the following:

cd scripts

ls

cat health_checks.py

sudo chmod +x health_checks.py

./health_checks.py

 

2. Debug and fix the issue in the code

This task asks you to set the correct response back ("Everything okay") based on whether there's not enough disk usage or CPU usage.

That is, the function check_cpu_usage should return true if the CPU usage is less than 75%, but in this case, it returns false.

Solution: Here's my solution:

nano health_checks.py

#!/usr/bin/env python3

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!")

else check_localhost() and check_connectivity():

    print("Everything ok")

Then check that it works by running the script

./health_checks.py

 

3. Create a new Python module

The goal of this task is to ask you to write a Python module. A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

The module you are going to write will be used to test the network connections, where this network module will check whether the network is correctly configured on the computer, and it will refer to the previous "health_checks.py" module.

There are two files we will be updating.

 

Write a python module for “Working with Python Scripts” quest

sudo apt install python3-requests

cd ~/scripts 

Now, we will open and edit the network.py file

nano network.py

#!/usr/bin/env python3

import requests

import socket

localhost = socket.gethostbyname('localhost')

request = requests.get("http://www.google.com")

Use the Python module

Now, we are now going to re-edit the file health_checks.py to make it call the checks in the network module. For this, you will need to import the module into health_checks.py script.

Now, we will open and edit the health_checks.py file

nano health_checks.py

#!/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")

else:

    print("Network checks failed")

I have made a copy of the python code that I used available on my Github gist here.

I hope this helps you with your journey on learning more about how you can use Python to solve problems!

Topics: python, coding

Vu Long Tran

Written by Vu Long Tran

Solutions Engineer APAC. ex-@Forrester consultant. Writing on #cloud #howto guides and #tech tinkering!