Thứ Tư, 10 tháng 9, 2025

Bài 1: Python & Node.js cho DevOps

1. Vì sao DevOps cần Python & Node.js?

  • Python → viết script automation (backup log, kiểm tra hệ thống, triển khai hạ tầng).

  • Node.js → xây dựng API backend, microservices để sau này deploy lên Docker/K8s.

👉 Một DevOps Engineer giỏi thường dùng Python để tự động hóaNode.js để phục vụ dịch vụ backend.


2. Cài đặt Python

Ubuntu/Debian

sudo apt update && sudo apt install -y python3 python3-pip

RHEL/CentOS/Oracle Linux/Fedora

sudo dnf install -y python3 python3-pip

Kiểm tra

python3 --version pip3 --version

👉 Kết quả mẫu:

Python 3.10.12 pip 22.0.2

3. Ví dụ Python 1 – Giám sát CPU, RAM, Disk

Bước 1: Cài thư viện

pip3 install psutil

Bước 2: Tạo file monitor.py

import psutil print(f"CPU Usage: {psutil.cpu_percent()}%") print(f"Memory Usage: {psutil.virtual_memory().percent}%") print(f"Disk Usage: {psutil.disk_usage('/').percent}%")

Bước 3: Chạy

python3 monitor.py

👉 Kết quả mẫu:

CPU Usage: 7.2% Memory Usage: 63.5% Disk Usage: 41.8%

4. Ví dụ Python 2 – Backup file log

Bước 1: Tạo file backup_logs.py

import shutil import datetime src = "/var/log/syslog" # Ubuntu/Debian # src = "/var/log/messages" # CentOS/RHEL/Fedora/Oracle Linux dst = f"/tmp/syslog_backup_{datetime.date.today()}.log" shutil.copy(src, dst) print(f"Backup saved: {dst}")

Bước 2: Chạy

python3 backup_logs.py

👉 Kết quả mẫu:

Backup saved: /tmp/syslog_backup_2025-09-09.log

5. Cài đặt Node.js

Ubuntu/Debian

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install -y nodejs

RHEL/CentOS/Oracle Linux/Fedora

sudo dnf module install -y nodejs:18

Kiểm tra

node -v npm -v

👉 Kết quả mẫu:

v18.17.1 8.19.2

6. Ví dụ Node.js – Hello DevOps API

Bước 1: Tạo project

mkdir myapp && cd myapp npm init -y npm install express

Bước 2: Tạo file index.js

const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello DevOps!'); }); app.listen(port, () => { console.log(`App running at http://localhost:${port}`); });

Bước 3: Chạy server

node index.js

👉 Kết quả trong terminal:

App running at http://localhost:3000

Bước 4: Kiểm tra API

curl http://localhost:3000

👉 Kết quả:

Hello DevOps!

7. Bài tập thực hành + Lời giải


🔹 Bài tập 1: Python cảnh báo CPU

Yêu cầu: Nếu CPU > 80% thì in thêm "ALERT: High CPU!".

Code mẫu:

import psutil cpu = psutil.cpu_percent() print(f"CPU Usage: {cpu}%") if cpu > 80: print("ALERT: High CPU!")

👉 Kết quả khi CPU thấp:

CPU Usage: 12.5%

👉 Kết quả khi CPU cao:

CPU Usage: 85.2% ALERT: High CPU!

🔹 Bài tập 2: Backup log với timestamp

Yêu cầu: Backup log nhưng thêm cả giờ–phút–giây vào tên file.

Code mẫu:

import shutil import datetime src = "/var/log/syslog" now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") dst = f"/tmp/syslog_backup_{now}.log" shutil.copy(src, dst) print(f"Backup saved: {dst}")

👉 Kết quả:

Backup saved: /tmp/syslog_backup_20250909_113015.log

🔹 Bài tập 3: Node.js API /status

Yêu cầu: Tạo API /status trả về JSON gồm CPU, RAM, Disk.
(Mình dùng thư viện os của Node.js để đọc RAM/CPU cơ bản)

Cài thêm thư viện:

npm install os-utils

Sửa file index.js:

const express = require('express'); const os = require('os-utils'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello DevOps!'); }); app.get('/status', (req, res) => { os.cpuUsage(function(v){ const status = { cpu: (v*100).toFixed(2) + "%", memory: ((1 - os.freememPercentage())*100).toFixed(2) + "%", disk: "Demo only - cần thư viện khác để lấy disk" }; res.json(status); }); }); app.listen(port, () => { console.log(`App running at http://localhost:${port}`); });

Chạy server:

node index.js

Kiểm tra API /status:

curl http://localhost:3000/status

👉 Kết quả mẫu:

{ "cpu": "14.23%", "memory": "61.75%", "disk": "Demo only - cần thư viện khác để lấy disk" }

✅ Sau bài này bạn đã biết:

  • Viết Python script để giám sát và backup.

  • Viết Node.js API để cung cấp dữ liệu hệ thống.

  • Cách chạy code chi tiết trên nhiều Linux distro.

=============================
Website không chứa bất kỳ quảng cáo nào, mọi đóng góp để duy trì phát triển cho website (donation) xin vui lòng gửi về STK 90.2142.8888 - Ngân hàng Vietcombank Thăng Long - TRAN VAN BINH
=============================
Nếu bạn không muốn bị AI thay thế và tiết kiệm 3-5 NĂM trên con đường trở thành DBA chuyên nghiệp hay làm chủ Database thì hãy đăng ký ngay KHOÁ HỌC ORACLE DATABASE A-Z ENTERPRISE, được Coaching trực tiếp từ tôi với toàn bộ bí kíp thực chiến, thủ tục, quy trình của gần 20 năm kinh nghiệm (mà bạn sẽ KHÔNG THỂ tìm kiếm trên Internet/Google) từ đó giúp bạn dễ dàng quản trị mọi hệ thống Core tại Việt Nam và trên thế giới, đỗ OCP.
- CÁCH ĐĂNG KÝ: Gõ (.) hoặc để lại số điện thoại hoặc inbox https://m.me/tranvanbinh.vn hoặc Hotline/Zalo 090.29.12.888
- Chi tiết tham khảo:
https://bit.ly/oaz_w
=============================
2 khóa học online qua video giúp bạn nhanh chóng có những kiến thức nền tảng về Linux, Oracle, học mọi nơi, chỉ cần có Internet/4G:
- Oracle cơ bản: https://bit.ly/admin_1200
- Linux: https://bit.ly/linux_1200
=============================
KẾT NỐI VỚI CHUYÊN GIA TRẦN VĂN BÌNH:
📧 Mail: binhoracle@gmail.com
☎️ Mobile/Zalo: 0902912888
👨 Facebook: https://www.facebook.com/BinhOracleMaster
👨 Inbox Messenger: https://m.me/101036604657441 (profile)
👨 Fanpage: https://www.facebook.com/tranvanbinh.vn
👨 Inbox Fanpage: https://m.me/tranvanbinh.vn
👨👩 Group FB: https://www.facebook.com/groups/DBAVietNam
👨 Website: https://www.tranvanbinh.vn
👨 Blogger: https://tranvanbinhmaster.blogspot.com
🎬 Youtube: https://www.youtube.com/@binhguru
👨 Tiktok: https://www.tiktok.com/@binhguru
👨 Linkin: https://www.linkedin.com/in/binhoracle
👨 Twitter: https://twitter.com/binhguru
👨 Podcast: https://www.podbean.com/pu/pbblog-eskre-5f82d6
👨 Địa chỉ: Tòa nhà Sun Square - 21 Lê Đức Thọ - Phường Mỹ Đình 1 - Quận Nam Từ Liêm - TP.Hà Nội

=============================
cơ sở dữ liệu, cơ sở dữ liệu quốc gia, database, AI, trí tuệ nhân tạo, artificial intelligence, machine learning, deep learning, LLM, ChatGPT, DeepSeek, Grok, oracle tutorial, học oracle database, Tự học Oracle, Tài liệu Oracle 12c tiếng Việt, Hướng dẫn sử dụng Oracle Database, Oracle SQL cơ bản, Oracle SQL là gì, Khóa học Oracle Hà Nội, Học chứng chỉ Oracle ở đầu, Khóa học Oracle online,sql tutorial, khóa học pl/sql tutorial, học dba, học dba ở việt nam, khóa học dba, khóa học dba sql, tài liệu học dba oracle, Khóa học Oracle online, học oracle sql, học oracle ở đâu tphcm, học oracle bắt đầu từ đâu, học oracle ở hà nội, oracle database tutorial, oracle database 12c, oracle database là gì, oracle database 11g, oracle download, oracle database 19c/21c/23c/23ai, oracle dba tutorial, oracle tunning, sql tunning , oracle 12c, oracle multitenant, Container Databases (CDB), Pluggable Databases (PDB), oracle cloud, oracle security, oracle fga, audit_trail,oracle RAC, ASM, oracle dataguard, oracle goldengate, mview, oracle exadata, oracle oca, oracle ocp, oracle ocm , oracle weblogic, postgresql tutorial, mysql tutorial, mariadb tutorial, ms sql server tutorial, nosql, mongodb tutorial, oci, cloud, middleware tutorial, docker, k8s, micro service, hoc solaris tutorial, hoc linux tutorial, hoc aix tutorial, unix tutorial, securecrt, xshell, mobaxterm, putty

ĐỌC NHIỀU

Trần Văn Bình - Oracle Database Master