Skip to content

Commit 2cd35d2

Browse files
committed
df
1 parent f791088 commit 2cd35d2

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Machine/LinuxStats.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,32 @@ public struct NetworkIoInfoAll: Identifiable {
227227
}
228228

229229
public struct Threads: Identifiable {
230+
// 线程的唯一标识符,使用pid作为id
230231
public var id: String {
231232
pid
232233
}
233234

235+
// 线程的进程ID
234236
public let pid: String
237+
// 线程的ID
235238
public let tid: String
239+
// 线程占用的CPU百分比
236240
public let cpu: Double
241+
// 线程占用的内存百分比
237242
public let mem: Double
243+
// 线程的用户
238244
public let user: String
245+
// 线程的命令名称
239246
public let comm: String
247+
// 线程的命令行参数
240248
public let args: String
241249
}
250+
251+
public struct DiskInfo: Identifiable {
252+
// 磁盘信息的唯一标识符,使用UUID生成
253+
public let id = UUID()
254+
// 磁盘已使用的空间大小
255+
public var used: Int64 = 0
256+
// 磁盘可用的空间大小
257+
public var avail: Int64 = 0
258+
}

src/Machine/Machine.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ public extension SSH {
462462
return await getTemp()?.max()
463463
}
464464

465+
/// 获取当前系统中的线程信息
466+
/// - Returns: 返回一个包含线程信息的数组,如果获取失败则返回nil
465467
func getThreads() async -> [Threads]? {
466468
let (text, _) = await exec(command: "ps -eo pid,tid,%cpu,%mem,user,comm,args --sort=-%cpu | awk 'NR>1 {print $1\",\"$2\",\"$3\",\"$4\",\"$5\",\"$6\",\"$7}'")
467469
guard let text = text?.trim(),!text.isEmpty else {
@@ -478,4 +480,24 @@ public extension SSH {
478480
}
479481
return threads
480482
}
483+
484+
/// 获取磁盘使用信息
485+
/// - Returns: 返回一个DiskInfo对象,包含总的已用空间和可用空间,如果获取失败则返回nil
486+
func getDiskInfo() async -> DiskInfo? {
487+
let (text, _) = await exec(command: "df | awk 'NR>1 {print $3\",\"$4}'")
488+
guard let text = text?.trim(),!text.isEmpty else {
489+
return nil
490+
}
491+
var info = DiskInfo()
492+
let lines = text.components(separatedBy: .newlines)
493+
for line in lines {
494+
let df = line.trim().components(separatedBy: ",").map { Int64($0) ?? 0 }
495+
guard df.count == 2 else {
496+
continue
497+
}
498+
info.used += df[0]
499+
info.avail += df[1]
500+
}
501+
return info
502+
}
481503
}

0 commit comments

Comments
 (0)