feature: 合并系统命令

This commit is contained in:
2026-11-03 20:15:27 +08:00
parent aeeef892ba
commit 0bd7b3accd
7 changed files with 128 additions and 174 deletions
+1 -2
View File
@@ -154,7 +154,7 @@ kopsc 的命令按用途分组,每组的完整说明(命令表、选项、
| **file** | 文件查找:按大小、修改时间、名称查找文件、查找空文件/空目录 | [README.file.md](README.file.md) | | **file** | 文件查找:按大小、修改时间、名称查找文件、查找空文件/空目录 | [README.file.md](README.file.md) |
| **svc** | 服务管理:查看状态、启用/禁用、查看日志 | [README.svc.md](README.svc.md) | | **svc** | 服务管理:查看状态、启用/禁用、查看日志 | [README.svc.md](README.svc.md) |
| **archive** | 压缩文件:自动识别格式并解压 | [README.archive.md](README.archive.md) | | **archive** | 压缩文件:自动识别格式并解压 | [README.archive.md](README.archive.md) |
| **sys** | 系统信息:OS、CPU、内存、磁盘一览、运行时间 | [README.sys.md](README.sys.md) | | **sys** | 系统信息:OS、运行时间、CPU、内存、磁盘一览 | [README.sys.md](README.sys.md) |
| **log** | 系统日志:搜索关键字、查看最近日志 | [README.log.md](README.log.md) | | **log** | 系统日志:搜索关键字、查看最近日志 | [README.log.md](README.log.md) |
| **fw** | 防火墙:查看状态、开放/关闭端口 | [README.fw.md](README.fw.md) | | **fw** | 防火墙:查看状态、开放/关闭端口 | [README.fw.md](README.fw.md) |
@@ -205,7 +205,6 @@ kopsc 的命令按用途分组,每组的完整说明(命令表、选项、
| 看最近日志 | `kopsc log last 100` | | 看最近日志 | `kopsc log last 100` |
| 看防火墙状态 | `kopsc fw status` | | 看防火墙状态 | `kopsc fw status` |
| 开放/关闭端口 | `kopsc fw allow 8080/tcp --yes` / `kopsc fw deny 8080/tcp --yes` | | 开放/关闭端口 | `kopsc fw allow 8080/tcp --yes` / `kopsc fw deny 8080/tcp --yes` |
| 看系统运行时间 | `kopsc sys uptime` |
| 看网卡和 IP | `kopsc net iface` | | 看网卡和 IP | `kopsc net iface` |
| 看 TCP 连接统计 | `kopsc net conn` | | 看 TCP 连接统计 | `kopsc net conn` |
| 测网速 | `kopsc net speed` | | 测网速 | `kopsc net speed` |
+9 -20
View File
@@ -1,18 +1,15 @@
# sys - 系统信息 # sys - 系统信息
一次性查看 OS、CPU、内存、磁盘概况,不用分别跑 `uname`、`lscpu`、`free`、`df`。 一次性查看 OS、运行时间、CPU、内存、磁盘概况,不用分别跑 `uname`、`lscpu`、`free`、`df`、`uptime`。
## 命令 ## 命令
| 命令 | 作用 | | 命令 | 作用 |
| --- | --- | | --- | --- |
| `kopsc sysinfo` | 显示系统信息一览 | | `kopsc sysinfo` | 显示系统信息一览(OS、运行时间、CPU、内存、磁盘) |
| `kopsc sys uptime` | 查看系统运行时间和启动时间 |
## 示例 ## 示例
### sysinfo
```bash ```bash
kopsc sysinfo kopsc sysinfo
``` ```
@@ -27,6 +24,12 @@ kopsc sysinfo
主机名: web-01 主机名: web-01
内核: 5.15.0-91-generic 内核: 5.15.0-91-generic
=== 运行时间 ===
运行时间: 15 天 3 小时 42 分钟
启动时间: 2026-10-19 08:30:15
负载均值: 0.15 0.22 0.18 (1/5/15分钟)
=== CPU === === CPU ===
逻辑核心: 4 逻辑核心: 4
@@ -44,26 +47,12 @@ Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 22G 28G 44% / /dev/vda1 50G 22G 28G 44% /
``` ```
### sys uptime
```bash
kopsc sys uptime
```
输出示例:
```
运行时间: 15 天 3 小时 42 分钟
启动时间: 2026-10-19 08:30:15
负载均值: 0.15 0.22 0.18 (1/5/15分钟)
```
## 平台实现 ## 平台实现
| 信息 | Linux | macOS | Windows | | 信息 | Linux | macOS | Windows |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| OS | `/etc/os-release` | runtime.GOOS | runtime.GOOS | | OS | `/etc/os-release` | runtime.GOOS | runtime.GOOS |
| 运行时间 | `/proc/uptime` + `/proc/loadavg` | `sysctl kern.boottime` | `wmic os get LastBootUpTime` |
| CPU | `/proc/cpuinfo` | `sysctl` | runtime | | CPU | `/proc/cpuinfo` | `sysctl` | runtime |
| 内存 | `/proc/meminfo` + cgroup | `sysctl hw.memsize` | `wmic` | | 内存 | `/proc/meminfo` + cgroup | `sysctl hw.memsize` | `wmic` |
| 磁盘 | `df -h` | `df -h` | `wmic logicaldisk` | | 磁盘 | `df -h` | `df -h` | `wmic logicaldisk` |
| 运行时间 | `/proc/uptime` | `sysctl kern.boottime` | `wmic os get LastBootUpTime` |
-1
View File
@@ -79,7 +79,6 @@ var builtins = map[string]Builtin{
"svc.enable": runSvcEnable, "svc.enable": runSvcEnable,
"svc.logs": runSvcLogs, "svc.logs": runSvcLogs,
"svc.status": runSvcStatus, "svc.status": runSvcStatus,
"sys.uptime": runSysUptime,
"sysinfo": runSysInfo, "sysinfo": runSysInfo,
"user.grant": runUserGrant, "user.grant": runUserGrant,
"user.info": runUserInfo, "user.info": runUserInfo,
+113 -1
View File
@@ -8,11 +8,12 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"time"
"gitea.odboy.cn/odboy/kenaito-kopsc/internal/platform" "gitea.odboy.cn/odboy/kenaito-kopsc/internal/platform"
) )
// runSysInfo implements `kopsc sysinfo`: show OS, CPU, memory, disk overview. // runSysInfo implements `kopsc sysinfo`: show OS, CPU, memory, disk, uptime overview.
func runSysInfo(ctx context.Context, req BuiltinRequest) error { func runSysInfo(ctx context.Context, req BuiltinRequest) error {
w := req.Stdout w := req.Stdout
res := platform.DetectResources() res := platform.DetectResources()
@@ -38,6 +39,10 @@ func runSysInfo(ctx context.Context, req BuiltinRequest) error {
} }
} }
// 运行时间
fmt.Fprintf(w, "\n=== 运行时间 ===\n\n")
printUptime(ctx, w)
fmt.Fprintf(w, "\n=== CPU ===\n\n") fmt.Fprintf(w, "\n=== CPU ===\n\n")
fmt.Fprintf(w, "逻辑核心: %d\n", res.CPUs) fmt.Fprintf(w, "逻辑核心: %d\n", res.CPUs)
if runtime.GOOS == "linux" { if runtime.GOOS == "linux" {
@@ -67,6 +72,113 @@ func runSysInfo(ctx context.Context, req BuiltinRequest) error {
return nil return nil
} }
// printUptime 打印系统运行时间信息
func printUptime(ctx context.Context, w interface{ Write([]byte) (int, error) }) {
switch runtime.GOOS {
case "linux":
printUptimeLinux(w)
case "darwin":
printUptimeDarwin(ctx, w)
case "windows":
printUptimeWindows(ctx, w)
}
}
func printUptimeLinux(w interface{ Write([]byte) (int, error) }) {
data, err := os.ReadFile("/proc/uptime")
if err != nil {
return
}
fields := strings.Fields(string(data))
if len(fields) < 1 {
return
}
seconds, err := strconv.ParseFloat(fields[0], 64)
if err != nil {
return
}
bootTime := time.Now().Add(-time.Duration(seconds) * time.Second)
fmt.Fprintf(w, "运行时间: %s\n", formatDuration(time.Duration(seconds)*time.Second))
fmt.Fprintf(w, "启动时间: %s\n", bootTime.Format("2006-01-02 15:04:05"))
// Load average from /proc/loadavg
if loadavg, err := os.ReadFile("/proc/loadavg"); err == nil {
parts := strings.Fields(string(loadavg))
if len(parts) >= 3 {
fmt.Fprintf(w, "负载均值: %s %s %s (1/5/15分钟)\n", parts[0], parts[1], parts[2])
}
}
}
func printUptimeDarwin(ctx context.Context, w interface{ Write([]byte) (int, error) }) {
out, err := exec.CommandContext(ctx, "sysctl", "-n", "kern.boottime").Output()
if err != nil {
return
}
// Output: { sec = 1234567890, usec = 123456 } Mon Jan 01 00:00:00 2024
line := string(out)
secStart := strings.Index(line, "sec = ")
if secStart < 0 {
return
}
secStr := strings.TrimSpace(line[secStart+6:])
if idx := strings.Index(secStr, ","); idx >= 0 {
secStr = secStr[:idx]
}
sec, err := strconv.ParseInt(secStr, 10, 64)
if err != nil {
return
}
bootTime := time.Unix(sec, 0)
uptime := time.Since(bootTime)
fmt.Fprintf(w, "运行时间: %s\n", formatDuration(uptime))
fmt.Fprintf(w, "启动时间: %s\n", bootTime.Format("2006-01-02 15:04:05"))
// Load average
if out, err := exec.CommandContext(ctx, "sysctl", "-n", "vm.loadavg").Output(); err == nil {
fmt.Fprintf(w, "负载均值: %s\n", strings.TrimSpace(string(out)))
}
}
func printUptimeWindows(ctx context.Context, w interface{ Write([]byte) (int, error) }) {
out, err := exec.CommandContext(ctx, "wmic", "os", "get", "LastBootUpTime", "/value").Output()
if err != nil {
return
}
line := strings.TrimSpace(string(out))
if !strings.HasPrefix(line, "LastBootUpTime=") {
return
}
timeStr := strings.TrimPrefix(line, "LastBootUpTime=")
if len(timeStr) < 14 {
return
}
bootTime, err := time.Parse("20060102150405", timeStr[:14])
if err != nil {
return
}
uptime := time.Since(bootTime)
fmt.Fprintf(w, "运行时间: %s\n", formatDuration(uptime))
fmt.Fprintf(w, "启动时间: %s\n", bootTime.Format("2006-01-02 15:04:05"))
}
// formatDuration formats a duration into a human-readable string.
func formatDuration(d time.Duration) string {
days := int(d.Hours()) / 24
hours := int(d.Hours()) % 24
minutes := int(d.Minutes()) % 60
if days > 0 {
return fmt.Sprintf("%d 天 %d 小时 %d 分钟", days, hours, minutes)
}
if hours > 0 {
return fmt.Sprintf("%d 小时 %d 分钟", hours, minutes)
}
return fmt.Sprintf("%d 分钟", minutes)
}
func cpuModelLinux() string { func cpuModelLinux() string {
data, err := os.ReadFile("/proc/cpuinfo") data, err := os.ReadFile("/proc/cpuinfo")
if err != nil { if err != nil {
-133
View File
@@ -1,133 +0,0 @@
package executor
import (
"context"
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
)
// runSysUptime implements `kopsc sys uptime`: show system uptime.
func runSysUptime(ctx context.Context, req BuiltinRequest) error {
switch runtime.GOOS {
case "linux":
return sysUptimeLinux(req)
case "darwin":
return sysUptimeDarwin(ctx, req)
case "windows":
return sysUptimeWindows(ctx, req)
default:
return fmt.Errorf("不支持的操作系统: %s", runtime.GOOS)
}
}
func sysUptimeLinux(req BuiltinRequest) error {
data, err := os.ReadFile("/proc/uptime")
if err != nil {
return fmt.Errorf("读取 /proc/uptime 失败: %w", err)
}
fields := strings.Fields(string(data))
if len(fields) < 1 {
return fmt.Errorf("/proc/uptime 格式异常")
}
seconds, err := strconv.ParseFloat(fields[0], 64)
if err != nil {
return fmt.Errorf("解析 uptime 失败: %w", err)
}
bootTime := time.Now().Add(-time.Duration(seconds) * time.Second)
fmt.Fprintf(req.Stdout, "运行时间: %s\n", formatDuration(time.Duration(seconds)*time.Second))
fmt.Fprintf(req.Stdout, "启动时间: %s\n", bootTime.Format("2006-01-02 15:04:05"))
// Load average
if len(fields) >= 2 {
idle, _ := strconv.ParseFloat(fields[1], 64)
fmt.Fprintf(req.Stdout, "空闲时间: %s\n", formatDuration(time.Duration(idle)*time.Second))
}
// Load average from /proc/loadavg
if loadavg, err := os.ReadFile("/proc/loadavg"); err == nil {
parts := strings.Fields(string(loadavg))
if len(parts) >= 3 {
fmt.Fprintf(req.Stdout, "负载均值: %s %s %s (1/5/15分钟)\n", parts[0], parts[1], parts[2])
}
}
return nil
}
func sysUptimeDarwin(ctx context.Context, req BuiltinRequest) error {
out, err := exec.CommandContext(ctx, "sysctl", "-n", "kern.boottime").Output()
if err != nil {
return fmt.Errorf("获取启动时间失败: %w", err)
}
// Output: { sec = 1234567890, usec = 123456 } Mon Jan 01 00:00:00 2024
line := string(out)
secStart := strings.Index(line, "sec = ")
if secStart < 0 {
return fmt.Errorf("无法解析 kern.boottime: %s", line)
}
secStr := strings.TrimSpace(line[secStart+6:])
if idx := strings.Index(secStr, ","); idx >= 0 {
secStr = secStr[:idx]
}
sec, err := strconv.ParseInt(secStr, 10, 64)
if err != nil {
return fmt.Errorf("解析启动时间失败: %w", err)
}
bootTime := time.Unix(sec, 0)
uptime := time.Since(bootTime)
fmt.Fprintf(req.Stdout, "运行时间: %s\n", formatDuration(uptime))
fmt.Fprintf(req.Stdout, "启动时间: %s\n", bootTime.Format("2006-01-02 15:04:05"))
// Load average
if out, err := exec.CommandContext(ctx, "sysctl", "-n", "vm.loadavg").Output(); err == nil {
fmt.Fprintf(req.Stdout, "负载均值: %s\n", strings.TrimSpace(string(out)))
}
return nil
}
func sysUptimeWindows(ctx context.Context, req BuiltinRequest) error {
// Use wmic to get boot time
out, err := exec.CommandContext(ctx, "wmic", "os", "get", "LastBootUpTime", "/value").Output()
if err != nil {
return fmt.Errorf("获取启动时间失败: %w", err)
}
line := strings.TrimSpace(string(out))
// LastBootUpTime=20240101000000.000000+480
if !strings.HasPrefix(line, "LastBootUpTime=") {
return fmt.Errorf("无法解析 wmic 输出: %s", line)
}
timeStr := strings.TrimPrefix(line, "LastBootUpTime=")
// Parse: 20240101000000.000000+480
if len(timeStr) < 14 {
return fmt.Errorf("时间格式异常: %s", timeStr)
}
bootTime, err := time.Parse("20060102150405", timeStr[:14])
if err != nil {
return fmt.Errorf("解析时间失败: %w", err)
}
uptime := time.Since(bootTime)
fmt.Fprintf(req.Stdout, "运行时间: %s\n", formatDuration(uptime))
fmt.Fprintf(req.Stdout, "启动时间: %s\n", bootTime.Format("2006-01-02 15:04:05"))
return nil
}
// formatDuration formats a duration into a human-readable string.
func formatDuration(d time.Duration) string {
days := int(d.Hours()) / 24
hours := int(d.Hours()) % 24
minutes := int(d.Minutes()) % 60
if days > 0 {
return fmt.Sprintf("%d 天 %d 小时 %d 分钟", days, hours, minutes)
}
if hours > 0 {
return fmt.Sprintf("%d 小时 %d 分钟", hours, minutes)
}
return fmt.Sprintf("%d 分钟", minutes)
}
+2 -13
View File
@@ -704,8 +704,8 @@ func Builtin() *Registry {
{ {
ID: "sysinfo", ID: "sysinfo",
Group: "sys", Group: "sys",
Summary: "显示系统信息一览(OS、CPU、内存、磁盘)", Summary: "显示系统信息一览(OS、运行时间、CPU、内存、磁盘)",
Action: "一次性输出操作系统、CPU 型号与核心数、内存总量与可用量、磁盘使用情况。" + Action: "一次性输出操作系统、运行时间、CPU 型号与核心数、内存总量与可用量、磁盘使用情况。" +
"Linux 读取 /proc 与 cgroup,macOS 使用 sysctl,Windows 使用 wmic", "Linux 读取 /proc 与 cgroup,macOS 使用 sysctl,Windows 使用 wmic",
Variants: threePlatformBuiltins("sysinfo"), Variants: threePlatformBuiltins("sysinfo"),
}, },
@@ -837,17 +837,6 @@ func Builtin() *Registry {
Variants: threePlatformBuiltins("fw.deny"), Variants: threePlatformBuiltins("fw.deny"),
}, },
// ----- sys additions -----
{
ID: "sys.uptime",
Group: "sys",
Summary: "查看系统运行时间和启动时间",
Action: "显示系统已运行多长时间、启动时间、负载均值。" +
"Linux: /proc/uptime + /proc/loadavg;macOS: sysctl kern.boottime;" +
"Windows: wmic os get LastBootUpTime",
Variants: threePlatformBuiltins("sys.uptime"),
},
// ----- net additions ----- // ----- net additions -----
{ {
ID: "net.iface", ID: "net.iface",
-1
View File
@@ -35,7 +35,6 @@ func TestBuiltinIDsStable(t *testing.T) {
"net.ping", "net.trace", "net.dns", "net.curl", "net.ping", "net.trace", "net.dns", "net.curl",
"log.search", "log.last", "log.search", "log.last",
"fw.status", "fw.allow", "fw.deny", "fw.status", "fw.allow", "fw.deny",
"sys.uptime",
"net.iface", "net.conn", "net.speed", "net.iface", "net.conn", "net.speed",
"proc.tree", "proc.tree",
"file.empty", "file.empty",