7
2

More than 3 years have passed since last update.

Swift:macOSのシステム情報を取得するフレームワーク作った

Last updated at Posted at 2020-02-01

概要

macOS向けのアクティビティインジケーターアプリのRunCatを開発するにあたり、CPU使用率、メモリ性能、ディスク容量、そしてネットワーク接続状況をそれぞれ取得する機能を実装したのですが、今回はそれらのシステム情報を取得する機能のみを抽出したフレームワークを作りました。

ActivityKit

Apache License, Version 2.0でGitHubにオープンソースとして公開してあり、CocoaPodsやCarthageでもインストール可能です。
GitHubのリンク:ActivityKit

使用例

CPU使用率、メモリ性能、ディスク容量、ネットワーク接続状況のすべてを5秒間隔で(文字列として)取得する

import ActivityKit

var observer = ActivityObserver(interval: 5.0)

Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) { (t) in
    print(observer.statistics)
}
出力例
CPU usage: 16.0%, system: 4.8%, user: 11.2%, idle: 84.0%
Memory performance: 79.5%, pressure: 35.1%, app: 7.1 GB, wired: 2.9 GB, compressed: 2.7 GB
Disk capacity: 42.6%, total: 920 GB, free: 528 GB, used: 392 GB
Network: Wi-Fi, Local IP: xx.x.x.xx, upload: 3.1 KB/s, download: 20.0 KB/s

CPU使用率のみ取得

CPU
print(observer.cpuDescription)
// => CPU usage: 16.0%, system: 4.8%, user: 11.2%, idle: 84.0%

// それぞれの数値を取得することも可能
let usage = observer.cpuUsage
usage.percentage // Double
usage.system // Double
usage.user // Double
usage.idle // Double

メモリ性能のみ取得

Memory
print(observer.memoryDescription)
// => Memory performance: 79.5%, pressure: 35.1%, app: 7.1 GB, wired: 2.9 GB, compressed: 2.7 GB

// それぞれの数値を取得することも可能
let performance = observer.memoryPerformance
performance.percentage // Double
performance.pressure // Double
performance.app // Double
performance.wired // Double
performance.compressed // Double

ディスク容量のみ取得

Disk
print(observer.diskDescription)
// => Disk capacity: 42.6%, total: 920 GB, free: 528 GB, used: 392 GB

// それぞれの数値を取得することも可能
let capacity = observer.diskCapacity
capacity.percentage // Double
capacity.total // Double
capacity.free // Double
capacity.used // Double

ネットワーク接続状況のみ取得

Network
print(observer.networkDescription)
// => Network: Wi-Fi, Local IP: xx.x.x.xx, upload: 3.1 KB/s, download: 20.0 KB/s

// それぞれの数値を取得することも可能
let connection = observer.networkConnection
connection.name // String
connection.localIP // String
connection.upload // Double
connection.download // Double

所感

RunCat開発1年間の集大成です。オリジナルのアクティビティモニタを作りたい場合には便利かも。

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2