Skip to content

Commit 1c37787

Browse files
authored
Merge pull request #17 from tainguyenbp/feat/learning-hacking-with-golang-20240706
test bash script
2 parents cfe4d62 + b3c85a6 commit 1c37787

File tree

7 files changed

+524
-0
lines changed

7 files changed

+524
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
function _gp
4+
check_cpu() {
5+
local threshold=80
6+
local count5min=0
7+
local countTotal=0
8+
local iterations=3 # 3x10 sec
9+
local max_count=$(bc <<< 300/10) # 5 minutes / 10 seconds interval
10+
11+
while true; do
12+
cpu_usage=$(top -bn2 | grep "Cpu(s)" | sed -n '2p' | awk '{print $2}' | cut -d'.' -f1)
13+
((countTotal++))
14+
if [ "$cpu_usage" -gt "$threshold" ]; then
15+
((count++))
16+
if [ "$count" -ge "$max_count" ]; then
17+
echo "CPU usage has been above $threshold% for more than 5 minutes" >&2
18+
break # Exit the loop after alerting once
19+
fi
20+
else
21+
count5min=0 # Reset counter because CPU usage dropped below threshold
22+
fi
23+
if [ "$countTotal" -eq "$iterations" ]; then
24+
break # Exit the loop after 5 minutes
25+
fi
26+
echo "CPU usage is $cpu_usage% - Threshold: $threshold% .. waiting.."
27+
sleep 10 # Check every 10 seconds
28+
done
29+
}
30+
31+
function _check_disk_space() {
32+
local threshold=10 # Set threshold to 10%
33+
local disk_usage=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
34+
35+
if [ "$disk_usage" -gt $((100 - threshold)) ]; then
36+
echo "Disk space on root partition is less than $threshold% available." &>2
37+
else
38+
echo "Disk space on root partition is sufficient."
39+
fi
40+
}
41+
42+
function _check_disk_space
43+
function _check_cpu
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# System monitoring Bash script
2+
Write a bash script that monitors system health and sends an alert if any of the following conditions are met:
3+
4+
The CPU usage exceeds 80% for more than 5 minutes.
5+
The available disk space on the root partition is less than 10%.
6+
If any condition is met, the script should output an appropriate message to the standard error (stderr) indicating the issue.
7+
You can use the following template to create your script:
8+
```
9+
check_cpu() {
10+
local threshold=80
11+
#... other variables
12+
13+
while true; do
14+
# Check CPU usage
15+
# Your CPU usage check logic here
16+
done
17+
}
18+
19+
check_disk_space() {
20+
local threshold=10
21+
#... other variables
22+
23+
# Check disk space
24+
# Your disk space check logic here
25+
}
26+
27+
check_disk_space
28+
check_cpu
29+
30+
```
31+
32+
# Solution
33+
Below is an example of a bash script that monitors CPU usage and disk space, sending an alert if the conditions are met.
34+
35+
Your solution may vary and be written differently, however if you are not familiar with bash scripting, we recommend you to learn the basics of bash scripting before attempting this question.
36+
```
37+
function _check_cpu() {
38+
local threshold=80
39+
local count5min=0
40+
local countTotal=0
41+
local iterations=3 # 3x10 sec
42+
local max_count=$(bc <<< 300/10) # 5 minutes / 10 seconds interval
43+
44+
while true; do
45+
cpu_usage=$(top -bn2 | grep "Cpu(s)" | sed -n '2p' | awk '{print $2}' | cut -d'.' -f1)
46+
((countTotal++))
47+
if [ "$cpu_usage" -gt "$threshold" ]; then
48+
((count++))
49+
if [ "$count" -ge "$max_count" ]; then
50+
echo "CPU usage has been above $threshold% for more than 5 minutes" >&2
51+
break # Exit the loop after alerting once
52+
fi
53+
else
54+
count5min=0 # Reset counter because CPU usage dropped below threshold
55+
fi
56+
if [ "$countTotal" -eq "$iterations" ]; then
57+
break # Exit the loop after 5 minutes
58+
fi
59+
echo "CPU usage is $cpu_usage% - Threshold: $threshold% .. waiting.."
60+
sleep 10 # Check every 10 seconds
61+
done
62+
}
63+
64+
function _check_disk_space() {
65+
local threshold=10 # Set threshold to 10%
66+
local disk_usage=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
67+
68+
if [ "$disk_usage" -gt $((100 - threshold)) ]; then
69+
echo "Disk space on root partition is less than $threshold% available." &>2
70+
else
71+
echo "Disk space on root partition is sufficient."
72+
fi
73+
}
74+
75+
_check_disk_space
76+
_check_cpu
77+
78+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/gorilla/csrf
2+
3+
go 1.22.3

python-basic/.DS_Store

6 KB
Binary file not shown.

python-basic/flask-app/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.9-slim
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Copy the current directory contents into the container at /app
8+
COPY . /app
9+
10+
# Install any needed packages specified in requirements.txt
11+
RUN pip install --no-cache-dir Flask requests
12+
13+
# Make port 3000 available to the world outside this container
14+
EXPOSE 3000
15+
16+
# Define environment variable
17+
ENV FLASK_APP=app.py
18+
19+
# Run the application
20+
CMD ["flask", "run", "--host=0.0.0.0", "--port=3000"]

python-basic/flask-app/app.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from flask import Flask, request, jsonify
2+
3+
import requests
4+
5+
app = Flask(__name__)
6+
7+
# Fetch data from JSONPlaceholder
8+
9+
API_URL = "https://jsonplaceholder.typicode.com/posts"
10+
11+
@app.route('/posts', methods=['GET'])
12+
13+
def get_posts():
14+
user_id = request.args.get('userId')
15+
response = requests.get(API_URL)
16+
posts = response.json()
17+
18+
# Filter posts by userId if provided
19+
if user_id:
20+
21+
posts = [post for post in posts if post['userId'] == int(user_id)]
22+
23+
return jsonify(posts)
24+
25+
if __name__ == '__main__':
26+
27+
app.run(host='0.0.0.0', port=5000)
28+

0 commit comments

Comments
 (0)