The Python fundamentals you actually need for automation — variables, data types, conditionals, loops, and functions — used to build a real DevOps script: a system health checker that reads CPU, memory, and disk usage and compares them against thresholds.
- Variables, data types, operators, and type casting
if / elif / else,forandwhileloops- Writing functions
- Reading live system metrics with
psutil - Basic
try / excepterror handling
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtvariables.py— variables, data types, operatorscontrol_flow.py— if/else and loopsfunctions.py— functionssystem_health.py— the main one: CPU/memory/disk health report
python variables.py
python system_health.pysystem_health.py prints something like:
=== System Health Report ===
CPU : 12.4% -> Healthy
Memory : 63.1% -> Healthy
Disk : 78.0% -> WARNING (above 75%)
Overall: NEEDS ATTENTION
Write your own system_health.py from scratch:
- Ask the user for a CPU threshold with
input()and cast it to a number. - Read current CPU usage with
psutil.cpu_percent(interval=1). - Print whether the CPU is healthy or over the threshold.
- Bonus: also check memory (
psutil.virtual_memory().percent) and disk (psutil.disk_usage("/").percent). - Bonus: wrap the input in
try / except ValueErrorso bad input doesn't crash it.