server_stats 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 82f55e59413dce591aca23b83badccecacf2d8b718c4a75f8223d057d0f18b6e
4
+ data.tar.gz: b09417be6fc6eebcc9751d85040e021c01c1a42ca1f9fa0046598294792c9883
5
+ SHA512:
6
+ metadata.gz: 9ce29a90992cd55290271c6e0ddcca0b8738d336bc2ae76ae5d28eeafea11bfcc6f35c3d240903f96a910cda1f30aad2e710010c72f85149556ffa3b0430413f
7
+ data.tar.gz: e0b19c9a9ac548072c9e5e62d73c830bf20f318c1434ea4127fc2f2c67acbb288fa514e3f01f475dccb982c16fbc9f8d76676126c8365c209a89640cc5edf00a
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tam Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,397 @@
1
+ # server_stats
2
+
3
+ [Tiếng Việt](README.md) | [English](README_EN.md)
4
+
5
+ Thư viện Ruby gem nhẹ, **không phụ thuộc gem ngoài (zero-dependency)** để thu thập thông số tài nguyên và trạng thái server trên **Ubuntu (Linux)** và **macOS**.
6
+
7
+ Hỗ trợ cả **Giao diện dòng lệnh (CLI)** và **Ruby API** để tích hợp trực tiếp vào ứng dụng Rails, Sinatra, script giám sát, Sidekiq background jobs, health check endpoints,...
8
+
9
+ ---
10
+
11
+ ## 🚀 Các tính năng chính
12
+
13
+ - **CPU**:
14
+ - Tổng số core CPU (Logical cores & Physical cores).
15
+ - % Sử dụng CPU (`usage_percent`).
16
+ - CPU model / kiến trúc chip.
17
+ - Tải trung bình hệ thống (Load Average: 1m, 5m, 15m).
18
+ - **RAM / Bộ nhớ**:
19
+ - Tổng dung lượng RAM (`total_bytes` & `total_human`).
20
+ - Dung lượng RAM còn trống thực tế (`free_bytes` & `free_human`).
21
+ - Dung lượng RAM khả dụng (`available_bytes` & `available_human`).
22
+ - Dung lượng RAM đã dùng (`used_bytes` & `used_human`).
23
+ - % Sử dụng RAM (`usage_percent`).
24
+ - Chi tiết chuyên sâu: Buffer, Cached, Swap (Linux) / Wired, Active, Inactive, Compressed (macOS).
25
+ - **Ổ cứng (Disk)**:
26
+ - Tổng dung lượng ổ cứng (`total_bytes` & `total_human`).
27
+ - Dung lượng còn trống / khả dụng (`available_bytes` & `available_human`).
28
+ - Dung lượng đã dùng (`used_bytes` & `used_human`).
29
+ - % Sử dụng ổ cứng (`usage_percent`).
30
+ - Filesystem và mount path (tuỳ biến kiểm tra bất kỳ đường dẫn mount nào, mặc định `/`).
31
+ - **Hệ điều hành & Hệ thống**:
32
+ - Tên và phiên bản OS (Ubuntu 20.04/22.04/24.04, macOS Sonoma/Sequoia,...).
33
+ - Hostname máy chủ.
34
+ - Kiến trúc CPU (x86_64, arm64, aarch64,...).
35
+ - Kernel release.
36
+ - Thời gian uptime (`uptime_seconds` & `uptime_human`).
37
+ - **Zero Dependencies**: Sử dụng hoàn toàn Ruby Core & Standard Library (`optparse`, `json`, `etc`, `socket`), không cần biên dịch C extension hay cài thêm gem phụ thuộc.
38
+
39
+ ---
40
+
41
+ ## 📋 Yêu cầu môi trường
42
+
43
+ - Ruby `>= 2.5.0` (Tương thích tốt với Ruby 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4+).
44
+ - Hệ điều hành:
45
+ - **Ubuntu / Debian / Linux** (hỗ trợ kiến trúc x86_64, aarch64).
46
+ - **macOS** (hỗ trợ cả Apple Silicon M1/M2/M3/M4 và Intel).
47
+
48
+ ---
49
+
50
+ ## 📦 Cài đặt
51
+
52
+ ### Cách 1: Cài đặt trực tiếp qua `gem`
53
+ ```bash
54
+ gem install server_stats
55
+ ```
56
+
57
+ ### Cách 2: Thêm vào `Gemfile` của dự án
58
+ Thêm dòng sau vào `Gemfile`:
59
+ ```ruby
60
+ gem 'server_stats'
61
+ ```
62
+ Sau đó chạy:
63
+ ```bash
64
+ bundle install
65
+ ```
66
+
67
+ ### Cách 3: Cài đặt từ mã nguồn (Build từ source)
68
+ ```bash
69
+ git clone https://github.com/ntamvl/server_stats.git
70
+ cd server_stats
71
+ gem build server_stats.gemspec
72
+ gem install server_stats-0.1.0.gem
73
+ ```
74
+
75
+ ---
76
+
77
+ ## 🖥️ Hướng dẫn sử dụng CLI (Command Line Interface)
78
+
79
+ Sau khi cài đặt, bạn có thể gọi lệnh `server_stats` (hoặc alias `server-stats`).
80
+
81
+ ### 1. Xem toàn bộ thông số máy chủ (Mặc định)
82
+ ```bash
83
+ server_stats
84
+ ```
85
+ **Kết quả mẫu:**
86
+ ```text
87
+ ============================================================
88
+ SERVER STATS REPORT
89
+ ============================================================
90
+ OS: macOS 26.7.1 (arm64)
91
+ Hostname: Tams-MacBook-Pro-M3-Max.local
92
+ Uptime: 2h 49m 36s (Kernel: 25.6.0)
93
+ ------------------------------------------------------------
94
+ CPU Cores: 16 cores (Physical: 16)
95
+ CPU Model: Apple M3 Max
96
+ CPU Usage: 13.00%
97
+ Load Avg: 2.89 (1m), 3.35 (5m), 3.43 (15m)
98
+ ------------------------------------------------------------
99
+ RAM Total: 48.00 GB
100
+ RAM Used: 32.08 GB (66.83%)
101
+ RAM Available: 15.92 GB
102
+ RAM Free: 822.83 MB
103
+ ------------------------------------------------------------
104
+ Disk Total: 926.35 GB (Mount: /)
105
+ Disk Used: 11.78 GB (1.27%)
106
+ Disk Available: 50.84 GB
107
+ Filesystem: /dev/disk3s1s1
108
+ ============================================================
109
+ ```
110
+
111
+ ### 2. Chỉ xem thông số CPU
112
+ ```bash
113
+ server_stats --cpu
114
+ ```
115
+ **Kết quả mẫu:**
116
+ ```text
117
+ CPU Cores: 16 (Physical: 16)
118
+ CPU Model: Apple M3 Max
119
+ CPU Usage: 14.00%
120
+ Load Average: 2.97 (1m), 3.33 (5m), 3.42 (15m)
121
+ ```
122
+
123
+ ### 3. Chỉ xem thông số RAM / Bộ nhớ
124
+ ```bash
125
+ server_stats --memory
126
+ ```
127
+ **Kết quả mẫu:**
128
+ ```text
129
+ RAM Total: 48.00 GB
130
+ RAM Used: 32.42 GB (67.54%)
131
+ RAM Available: 15.58 GB
132
+ RAM Free: 509.45 MB
133
+ ```
134
+
135
+ Nếu muốn xem dạng byte thô (raw bytes):
136
+ ```bash
137
+ server_stats --memory --raw
138
+ ```
139
+ ```text
140
+ RAM Total: 51539607552 bytes
141
+ RAM Used: 34326265856 bytes (66.60%)
142
+ RAM Available: 17213341696 bytes
143
+ RAM Free: 602013696 bytes
144
+ ```
145
+
146
+ ### 4. Chỉ xem thông số Ổ cứng (Disk)
147
+ Kiểm tra thư mục gốc `/`:
148
+ ```bash
149
+ server_stats --disk
150
+ ```
151
+ Kiểm tra phân vùng mount cụ thể (ví dụ `/data` hoặc `/var`):
152
+ ```bash
153
+ server_stats --disk /data
154
+ ```
155
+ **Kết quả mẫu:**
156
+ ```text
157
+ Mount Path: /data
158
+ Filesystem: /dev/sdb1
159
+ Disk Total: 500.00 GB
160
+ Disk Used: 120.50 GB (24.10%)
161
+ Disk Available: 379.50 GB
162
+ Disk Free: 379.50 GB
163
+ ```
164
+
165
+ ### 5. Chỉ xem thông tin Hệ điều hành & Phần cứng
166
+ ```bash
167
+ server_stats --system
168
+ ```
169
+ **Kết quả mẫu:**
170
+ ```text
171
+ OS: macOS 26.7.1
172
+ Hostname: Tams-MacBook-Pro-M3-Max.local
173
+ Architecture: arm64
174
+ Kernel: 25.6.0
175
+ Uptime: 2h 50m 3s (10203s)
176
+ ```
177
+
178
+ ### 6. Xuất định dạng JSON (Dành cho script giám sát, Prometheus agent, Datadog, Zabbix,...)
179
+ Xuất toàn bộ hệ thống dưới dạng JSON có format (pretty JSON):
180
+ ```bash
181
+ server_stats --json
182
+ ```
183
+ **Kết quả JSON mẫu:**
184
+ ```json
185
+ {
186
+ "timestamp": "2026-10-05T18:06:48Z",
187
+ "system": {
188
+ "os": "macOS",
189
+ "os_family": "macos",
190
+ "os_version": "26.7.1",
191
+ "hostname": "Tams-MacBook-Pro-M3-Max.local",
192
+ "architecture": "arm64",
193
+ "kernel": "25.6.0",
194
+ "uptime_seconds": 10196,
195
+ "uptime_human": "2h 49m 56s"
196
+ },
197
+ "cpu": {
198
+ "cores": 16,
199
+ "physical_cores": 16,
200
+ "usage_percent": 14.0,
201
+ "load_average": {
202
+ "one_minute": 3.14,
203
+ "five_minutes": 3.37,
204
+ "fifteen_minutes": 3.43
205
+ },
206
+ "model": "Apple M3 Max"
207
+ },
208
+ "memory": {
209
+ "total_bytes": 51539607552,
210
+ "free_bytes": 638238720,
211
+ "available_bytes": 17006346240,
212
+ "used_bytes": 34533261312,
213
+ "usage_percent": 67.0,
214
+ "total_human": "48.00 GB",
215
+ "free_human": "608.67 MB",
216
+ "available_human": "15.84 GB",
217
+ "used_human": "32.16 GB"
218
+ },
219
+ "disk": {
220
+ "path": "/",
221
+ "filesystem": "/dev/disk3s1s1",
222
+ "total_bytes": 994662584320,
223
+ "used_bytes": 12647350272,
224
+ "free_bytes": 54586494976,
225
+ "available_bytes": 54586494976,
226
+ "usage_percent": 1.27,
227
+ "total_human": "926.35 GB",
228
+ "used_human": "11.78 GB",
229
+ "free_human": "50.84 GB",
230
+ "available_human": "50.84 GB"
231
+ }
232
+ }
233
+ ```
234
+
235
+ Kết hợp với lọc thành phần:
236
+ ```bash
237
+ server_stats --cpu --json
238
+ server_stats --memory --json
239
+ server_stats --disk / --json
240
+ ```
241
+
242
+ Xuất JSON trên 1 dòng duy nhất (compact):
243
+ ```bash
244
+ server_stats --json --compact
245
+ ```
246
+
247
+ ### 7. Điều chỉnh khoảng thời gian lấy mẫu CPU
248
+ Lấy mẫu CPU trong 1.0 giây để độ chính xác cao hơn:
249
+ ```bash
250
+ server_stats --cpu --interval 1.0
251
+ ```
252
+
253
+ ### 8. Bảng tổng hợp tham số dòng lệnh
254
+
255
+ | Tham số | Viết tắt | Ý nghĩa | Mặc định |
256
+ |---|---|---|---|
257
+ | `--all` | `-a` | Hiển thị tất cả thông số hệ thống | Mặc định |
258
+ | `--cpu` | `-c` | Chỉ hiển thị thông số CPU | - |
259
+ | `--memory`, `--ram` | `-m` | Chỉ hiển thị thông số RAM | - |
260
+ | `--disk [PATH]` | `-d` | Chỉ hiển thị thông số ổ cứng tại đường dẫn | `/` |
261
+ | `--system` | `-s` | Chỉ hiển thị thông tin hệ điều hành | - |
262
+ | `--json` | `-j` | Xuất định dạng JSON | `false` |
263
+ | `--compact` | | Xuất JSON không thụt dòng (khi kèm `-j`) | `false` |
264
+ | `--raw` | `-r` | Hiển thị byte thô thay vì GB/MB | `false` |
265
+ | `--interval SEC` | `-i` | Thời gian lấy mẫu CPU tính bằng giây | `0.5` |
266
+ | `--version` | `-v` | Xem phiên bản gem | - |
267
+ | `--help` | `-h` | Xem hướng dẫn trợ giúp | - |
268
+
269
+ ---
270
+
271
+ ## 🛠️ Hướng dẫn sử dụng trong ứng dụng Ruby (Ruby API)
272
+
273
+ ### 1. Sử dụng cơ bản
274
+ ```ruby
275
+ require 'server_stats'
276
+
277
+ # Lấy thông số CPU
278
+ cpu = ServerStats.cpu
279
+ puts "Số core CPU: #{cpu.cores}"
280
+ puts "% CPU sử dụng: #{cpu.usage_percent}%"
281
+ puts "Load 1m: #{cpu.load_average[:one_minute]}"
282
+
283
+ # Lấy thông số RAM
284
+ mem = ServerStats.memory # hoặc ServerStats.ram
285
+ puts "RAM Tổng: #{mem.total_human} (#{mem.total_bytes} bytes)"
286
+ puts "RAM Khả dụng: #{mem.available_human}"
287
+ puts "RAM Trống: #{mem.free_human}"
288
+ puts "RAM Đã dùng: #{mem.used_human} (#{mem.usage_percent}%)"
289
+
290
+ # Lấy thông số Ổ cứng
291
+ disk = ServerStats.disk('/')
292
+ puts "Ổ cứng Tổng: #{disk.total_human}"
293
+ puts "Ổ cứng Còn trống: #{disk.available_human}"
294
+ puts "Ổ cứng Đã dùng: #{disk.used_human} (#{disk.usage_percent}%)"
295
+
296
+ # Lấy thông tin Hệ điều hành
297
+ sys = ServerStats.system
298
+ puts "OS: #{sys.os} #{sys.os_version}"
299
+ puts "Hostname: #{sys.hostname}"
300
+ puts "Uptime: #{sys.uptime_human}"
301
+ ```
302
+
303
+ ### 2. Lấy dữ liệu dạng Hash hoặc JSON
304
+ ```ruby
305
+ require 'server_stats'
306
+
307
+ # Chuyển đổi toàn bộ snapshot thành Hash
308
+ stats_hash = ServerStats.to_h
309
+
310
+ # Chuyển đổi thành chuỗi JSON
311
+ json_string = ServerStats.to_json
312
+
313
+ # Pretty JSON
314
+ pretty_json = ServerStats.to_json(pretty: true)
315
+
316
+ # Lấy báo cáo định dạng bảng chuỗi
317
+ puts ServerStats.summary
318
+ ```
319
+
320
+ ### 3. Ví dụ: Endpoint Health Check trong Rails
321
+ ```ruby
322
+ # app/controllers/health_controller.rb
323
+ class HealthController < ApplicationController
324
+ def show
325
+ stats = ServerStats.all
326
+
327
+ # Cảnh báo nếu RAM hoặc Disk vượt ngưỡng 90%
328
+ status = if stats.memory.usage_percent > 90.0 || stats.disk.usage_percent > 90.0
329
+ :service_unavailable
330
+ else
331
+ :ok
332
+ end
333
+
334
+ render json: {
335
+ status: status == :ok ? "healthy" : "warning",
336
+ stats: stats.to_h
337
+ }, status: status
338
+ end
339
+ end
340
+ ```
341
+
342
+ ### 4. Ví dụ: Script cảnh báo RAM/CPU gửi Telegram / Slack
343
+ ```ruby
344
+ require 'server_stats'
345
+ require 'net/http'
346
+ require 'json'
347
+
348
+ cpu = ServerStats.cpu(interval: 1.0)
349
+ mem = ServerStats.memory
350
+ disk = ServerStats.disk('/')
351
+
352
+ if cpu.usage_percent > 85.0 || mem.usage_percent > 85.0
353
+ message = <<~MSG
354
+ ⚠️ CẢNH BÁO TÀI NGUYÊN MÁY CHỦ: #{ServerStats.system.hostname}
355
+ - CPU: #{cpu.usage_percent}% (#{cpu.cores} cores)
356
+ - RAM: #{mem.used_human} / #{mem.total_human} (#{mem.usage_percent}%)
357
+ - Disk: #{disk.used_human} / #{disk.total_human} (#{disk.usage_percent}%)
358
+ MSG
359
+ puts message
360
+ # Gửi webhook Slack/Telegram tại đây...
361
+ end
362
+ ```
363
+
364
+ ---
365
+
366
+ ## 🔬 Cơ chế thu thập dữ liệu kỹ thuật
367
+
368
+ | Thành phần | Ubuntu / Linux | macOS |
369
+ |---|---|---|
370
+ | **RAM Total & Free** | Đọc `/proc/meminfo` trực tiếp (`MemTotal`, `MemAvailable`, `MemFree`, `Buffers`, `Cached`). | `sysctl -n hw.memsize` kết hợp phân tích các page từ `vm_stat` (`Pages free`, `inactive`, `speculative`, `purgeable`). |
371
+ | **CPU Cores** | `Etc.nprocessors` kết hợp `/proc/cpuinfo` cho physical cores. | `sysctl -n hw.ncpu` và `sysctl -n hw.physicalcpu`. |
372
+ | **CPU Usage %** | Lấy mẫu 2 lần liên tiếp từ `/proc/stat` tính vi sai `delta_idle` và `delta_total`. Không phụ thuộc lệnh ngoài. | Lấy mẫu từ `iostat -c 2` (hoặc snapshot `top`), an toàn trong sandbox. |
373
+ | **Disk Space** | Tiêu chuẩn POSIX: `df -P -k <path>`, parse blocks 1024-byte chính xác. | Tiêu chuẩn POSIX: `df -P -k <path>`. |
374
+ | **Uptime** | Đọc trực tiếp từ `/proc/uptime`. | Tính từ thời điểm boot hệ thống `sysctl -n kern.boottime`. |
375
+ | **OS Info** | Phân tích file `/etc/os-release` chuẩn Linux. | Sử dụng `sw_vers` và `uname`. |
376
+
377
+ ---
378
+
379
+ ## 🧪 Chạy kiểm thử (Testing)
380
+
381
+ Dự án đi kèm bộ test suite độc lập 100% không cần gem test bên ngoài:
382
+
383
+ ```bash
384
+ # Chạy bộ test suite
385
+ ruby test/run_all.rb
386
+ ```
387
+
388
+ Hoặc qua Rake:
389
+ ```bash
390
+ rake test
391
+ ```
392
+
393
+ ---
394
+
395
+ ## 📄 Bản quyền (License)
396
+
397
+ Mã nguồn được phân phối dưới giấy phép [MIT License](LICENSE).