Simulate Service Availability and Basic Monitoring
In this step, you will explore the principle of Availability, the third component of the CIA security triad. Availability ensures that systems and services are operational and accessible when needed. You will simulate a simple web service, learn how to check if it's running, simulate a service failure, and then write a basic monitoring script to automate the availability check.
First, ensure you are in the ~/project directory.
We will use Python's built-in web server to simulate a running service. This command will start a web server on port 8000 and serve the files in the current directory. The & at the end runs the process in the background, so you can continue using your terminal.
python3 -m http.server 8000 &
You will see a message indicating the process has started, along with its Process ID (PID).
[1] 12345
Press Enter to continue.
Your service is now running. A basic way to check if a service is running is to see if its process exists. We can use the pgrep command to find the PID of a process by name.
pgrep -f http.server
This should return the PID of the server process, confirming it's running in memory.
12345
However, a running process doesn't guarantee the service is working correctly. A better check is to try and connect to it, just like a user would. We'll use the curl command to send a request to our local server.
curl http://localhost:8000/confidential_data.txt
Since the service is available, it will respond with the contents of the file you created in the previous steps.
Top Secret Details
This is an unauthorized modification.
Now, let's simulate a service failure. We will use the kill command to terminate the web server process. You'll need the PID you found earlier.
## Replace 12345 with the actual PID from the pgrep command
kill 12345
After running kill, you might see a "Terminated" message for the background job. Now, let's try to access the service again with curl.
curl http://localhost:8000/confidential_data.txt
This time, the command will fail because the service is no longer running and cannot accept the connection. This demonstrates a lack of availability.
curl: (7) Failed to connect to localhost port 8000 after 0 ms: Connection refused
Finally, let's create a simple monitoring script to automate this check. Create a new file named monitor.sh using nano.
nano monitor.sh
Enter the following script into the editor. This script uses curl to check the service. If it gets a successful response (200 OK), it reports the service is "UP"; otherwise, it reports "DOWN".
#!/bin/bash
## Check if the service at localhost:8000 is responding
if curl -s --head http://localhost:8000 | grep "200 OK" > /dev/null; then
echo "Service Status: UP"
else
echo "Service Status: DOWN"
fi
Save the file and exit nano by pressing Ctrl+X, then Y, and Enter.
Make the script executable using chmod:
chmod +x monitor.sh
Now, run your monitoring script. Since the service is stopped, it should report "DOWN".
./monitor.sh
Service Status: DOWN
You have now learned how to start a service, check its availability, simulate a failure, and create a basic script to monitor it.