《Openclaw技能编写 - macOS系统资源获取技能》预览
文档样式:
SKILL.md
name: macos-system-stats
description: "Get macOS system resource utilization including CPU, memory, disk, and network usage in real-time or summary format."
homepage: https://support.apple.com/guide/terminal/welcome/mac
metadata: {
"openclaw": {
"emoji": "📊",
"requires": {
"bins": ["top", "vm_stat", "df", "iostat", "netstat", "ps"]
}
}
}
macOS System Stats Skill
Monitor and report macOS system resource utilization including CPU, memory, disk I/O, and network statistics.
When to Use
✅ USE this skill when:
- "What's my CPU usage?"
- "How much memory is available?"
- "Check disk space on my Mac"
- "Monitor system performance"
- "Get a system health overview"
❌ DON'T use this skill when:
- Windows/Linux systems → use platform-specific tools
- Detailed process analysis → use Activity Monitor directly
- Historical trends → use system monitoring tools
- Remote server monitoring → use appropriate remote tools
Commands
Quick System Overview
./system-stats.sh --overview
Specific Resource Checks
# CPU usage only
./system-stats.sh --cpu
# Memory usage only
./system-stats.sh --memory
# Disk usage only
./system-stats.sh --disk
# Network stats only
./system-stats.sh --network
# Top processes by CPU
./system-stats.sh --top-cpu
# Top processes by memory
./system-stats.sh --top-mem
Real-time Monitoring
# Live monitoring (5 second updates)
./system-stats.sh --live --duration=30
# Custom interval
./system-stats.sh --live --interval=2 --duration=60
Output Formats
# JSON output for parsing
./system-stats.sh --overview --format=json
# Simple text (default)
./system-stats.sh --overview --format=text
Notes
- All commands work on macOS without additional dependencies
- Real-time monitoring requires appropriate permissions
- JSON output is machine-readable for automation
- Duration values are in seconds
- Default live monitoring interval is 5 seconds
命令脚本
system-stats.sh
#!/bin/bash
# macOS System Stats - 获取系统资源利用率
# 支持 JSON 和文本格式输出
set -euo pipefail
# 默认参数
FORMAT="text"
HELP=false
# 解析命令行参数
while [[ $# -gt 0 ]]; do
case $1 in
--format|-f)
FORMAT="$2"
shift 2
;;
--help|-h)
HELP=true
shift
;;
*)
echo "未知参数: $1" >&2
exit 1
;;
esac
done
if [ "$HELP" = true ]; then
cat << EOF
用法: $0 [--format text|json]
选项:
--format, -f 输出格式 (text 或 json)
--help, -h 显示帮助信息
示例:
$0 # 默认文本格式
$0 --format json # JSON 格式输出
EOF
exit 0
fi
# 获取 CPU 使用率 (使用 top 命令)
get_cpu_usage() {
# 使用 top 获取 CPU 使用率,排除空闲时间
local cpu_idle=$(top -l 1 | grep "CPU usage" | awk '{print $7}' | sed 's/%//')
local cpu_used=$(echo "100 - $cpu_idle" | bc -l)
printf "%.1f" $cpu_used
}
# 获取内存使用情况
get_memory_info() {
local memory_info=$(vm_stat)
local page_size=$(echo "$memory_info" | grep "page size" | awk '{print $8}')
# 提取各种内存数据(单位:页)
local pages_free=$(echo "$memory_info" | grep "Pages free" | awk '{print $3}' | sed 's/\.//')
local pages_active=$(echo "$memory_info" | grep "Pages active" | awk '{print $3}' | sed 's/\.//')
local pages_inactive=$(echo "$memory_info" | grep "Pages inactive" | awk '{print $3}' | sed 's/\.//')
local pages_wired=$(echo "$memory_info" | grep "Pages wired down" | awk '{print $4}' | sed 's/\.//')
# 转换为字节
local free_bytes=$((pages_free * page_size))
local active_bytes=$((pages_active * page_size))
local inactive_bytes=$((pages_inactive * page_size))
local wired_bytes=$((pages_wired * page_size))
# 总内存使用量(活跃 + 非活跃 + 有线)
local used_bytes=$((active_bytes + inactive_bytes + wired_bytes))
# 获取总物理内存
local total_bytes=$(sysctl hw.memsize | awk '{print $2}')
# 计算使用百分比
local mem_percent=$(echo "scale=1; $used_bytes * 100 / $total_bytes" | bc)
if [ "$FORMAT" = "json" ]; then
echo "\"memory_total_bytes\": $total_bytes,"
echo "\"memory_used_bytes\": $used_bytes,"
echo "\"memory_percent\": $mem_percent"
else
# 转换为 GB 显示
local total_gb=$(echo "scale=1; $total_bytes / 1024 / 1024 / 1024" | bc)
local used_gb=$(echo "scale=1; $used_bytes / 1024 / 1024 / 1024" | bc)
echo "Memory: ${used_gb}GB / ${total_gb}GB (${mem_percent}%)"
fi
}
# 获取磁盘使用情况
get_disk_info() {
if [ "$FORMAT" = "json" ]; then
local disk_info=$(df -k / | tail -1 | awk '{
total = $2 * 1024;
used = $3 * 1024;
percent = $5;
gsub(/%/, "", percent);
printf "\"disk_total_bytes\": %d,\n\"disk_used_bytes\": %d,\n\"disk_percent\": %.1f", total, used, percent
}')
echo "$disk_info"
else
df -h / | tail -1 | awk '{printf "Disk: %s / %s (%s)\n", $3, $2, $5}'
fi
}
# 主函数
main() {
local cpu_usage=$(get_cpu_usage)
if [ "$FORMAT" = "json" ]; then
echo "{"
echo "\"cpu_percent\": $cpu_usage,"
get_memory_info
echo ","
get_disk_info
echo "}"
else
echo "=== macOS 系统资源利用率 ==="
echo "CPU: ${cpu_usage}%"
get_memory_info
get_disk_info
echo ""
echo "数据更新时间: $(date '+%Y-%m-%d %H:%M:%S')"
fi
}
main