disk-guardian
S.M.A.R.T. drive health scanner for macOS and Linux.
Scripts
All scripts are in scripts/. Run them in sequence or pipe them together:
CODEBLOCK0
Data is stored in ~/.openclaw/workspace/disk-guardian/ by default. Use --data-dir to override on any script.
Workflow
- 1. Run
scan_drives.py — detects drives and runs smartctl -a on each. Outputs JSON. - Pipe to
parse_smart.py — extracts health attributes, assesses status (Good/Warning/Critical). - Pipe to
report.py — generates markdown report with risk rankings and recommendations. - Optionally pipe to
history.py --record to log the scan and get trend alerts.
Prerequisites
smartctl must be installed:
- - macOS: INLINECODE8
- Linux:
sudo apt install smartmontools or INLINECODE10
If smartctl is missing, scan_drives.py will output the install command and exit gracefully.
Script Details
scan_drives.py
- - macOS: uses
diskutil list to find whole disks, then INLINECODE13 - Linux: uses
lsblk to find block devices, then INLINECODE15 - INLINECODE16 : prepend sudo to smartctl commands (needed on Linux without udev rules)
- Outputs JSON: INLINECODE17
parse_smart.py
- - Detects drive type: NVMe, SSD, or HDD
- SATA/SSD: parses attribute table, extracts Reallocated Sector Count, Current Pending Sector, Uncorrectable Errors, Temperature, Power-On Hours, Read Error Rate, Seek Error Rate, and more
- NVMe: parses health log for Available Spare, Percentage Used, Media Errors, Unsafe Shutdowns, Critical Warning
- Assigns health_status: Good / Warning / Critical
- INLINECODE18 : read from file instead of stdin
- INLINECODE19 : parse a single raw smartctl text dump
history.py
- -
--record: append current scan to INLINECODE21 - INLINECODE22 : analyze stored history for concerning patterns (rising reallocated sectors, climbing temps, declining NVMe spare)
- INLINECODE23 : show last N entries per device
- INLINECODE24 : use alternate storage location
- INLINECODE25 : filter output to one device
report.py
- - Generates markdown with drive inventory table, per-drive status, risk ranking with scores, human-readable flag explanations, trend alerts, and recommendations
- Risk ranking accounts for health status, flag severity, trend alerts, and drive age (power-on hours)
- INLINECODE26 : save report to file instead of printing
- INLINECODE27 : load pre-computed trend JSON
SMART Attribute Reference
See references/smart-attributes.md for plain-English explanations of the 10 most critical SMART attributes and NVMe health indicators. Read it when explaining specific attributes to the user or when triaging a flagged drive.
Edge Cases
- - smartctl not installed:
scan_drives.py prints install instructions and exits with code 2 - Permission denied: Output includes hint to use
--sudo; on Linux, add user to disk group as permanent fix - Virtual drives (VMware, loop devices): smartctl will return errors; these are captured and included in error entries
- NVMe vs SATA: drive type auto-detected; NVMe uses health log fields instead of attribute table
- Corrupt history:
history.py backs up the file and starts fresh automatically
disk-guardian
适用于macOS和Linux的S.M.A.R.T.硬盘健康扫描工具。
脚本
所有脚本位于scripts/目录下。按顺序运行或通过管道串联:
bash
cd ~/.openclaw/workspace/skills/disk-guardian/scripts
完整流程(Linux上可能需要sudo权限):
python3 scan
drives.py | python3 parsesmart.py | python3 report.py
使用sudo(许多Linux系统需要):
python3 scan
drives.py --sudo | python3 parsesmart.py | python3 report.py
保存报告到文件:
python3 scan
drives.py | python3 parsesmart.py | python3 report.py --output ~/disk-report.md
记录扫描历史并获取趋势分析:
python3 scan
drives.py | python3 parsesmart.py | python3 history.py --record
数据默认存储在~/.openclaw/workspace/disk-guardian/目录下。可在任意脚本中使用--data-dir参数覆盖默认路径。
工作流程
- 1. 运行scandrives.py — 检测硬盘并对每个硬盘执行smartctl -a命令,输出JSON格式数据。
- 通过管道传递给parsesmart.py — 提取健康属性,评估状态(良好/警告/严重)。
- 通过管道传递给report.py — 生成包含风险评级和建议的Markdown报告。
- 可选:通过管道传递给history.py --record记录扫描结果并获取趋势告警。
前置条件
必须安装smartctl:
- - macOS:brew install smartmontools
- Linux:sudo apt install smartmontools 或 sudo dnf install smartmontools
如果缺少smartctl,scan_drives.py会输出安装命令并优雅退出。
脚本详情
scan_drives.py
- - macOS:使用diskutil list查找完整磁盘,然后执行smartctl -a /dev/diskN
- Linux:使用lsblk查找块设备,然后执行smartctl -a /dev/sdX
- --sudo:在smartctl命令前添加sudo(Linux上无udev规则时需要)
- 输出JSON:{os, smartctlpath, drivesfound, scanresults: [{device, smartctloutput|error}]}
parse_smart.py
- - 检测硬盘类型:NVMe、SSD或HDD
- SATA/SSD:解析属性表,提取重映射扇区计数、当前待处理扇区、不可纠正错误、温度、通电时间、读取错误率、寻道错误率等
- NVMe:解析健康日志,获取可用备用空间、使用百分比、介质错误、不安全关机次数、严重警告
- 分配健康状态:良好/警告/严重
- --input FILE:从文件读取而非标准输入
- --raw FILE --device NAME:解析单个原始smartctl文本转储
history.py
- - --record:将当前扫描结果追加到~/.openclaw/workspace/disk-guardian/history.json
- --trends:分析存储的历史数据,发现异常模式(重映射扇区增加、温度升高、NVMe备用空间减少)
- --list [--limit N]:显示每个设备最近N条记录
- --data-dir PATH:使用替代存储位置
- --device /dev/diskN:过滤输出到指定设备
report.py
- - 生成包含硬盘清单表、各硬盘状态、带评分的风险排名、可读标志说明、趋势告警和建议的Markdown报告
- 风险排名考虑健康状态、标志严重性、趋势告警和硬盘年龄(通电时间)
- --output FILE:将报告保存到文件而非打印
- --trends FILE:加载预计算的趋势JSON数据
SMART属性参考
参见references/smart-attributes.md,了解10个最关键SMART属性和NVMe健康指标的通俗解释。在向用户解释特定属性或排查标记硬盘时阅读此文档。
边界情况
- - 未安装smartctl:scan_drives.py打印安装说明并以代码2退出
- 权限被拒绝:输出包含使用--sudo的提示;在Linux上,将用户添加到disk组作为永久解决方案
- 虚拟硬盘(VMware、loop设备):smartctl将返回错误;这些错误会被捕获并包含在错误条目中
- NVMe vs SATA:自动检测硬盘类型;NVMe使用健康日志字段而非属性表
- 历史数据损坏:history.py自动备份文件并重新开始