3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShell で簡易的なパフォーマンスログを取得する

Last updated at Posted at 2016-10-23

ブログからの転載

経緯

PowerShell 楽しいし、あまり深く考えずに「パフォーマンスモニターを PowerShell でやってみよう」という感じです。

気力が続けば、ここから Excel 出力してグラフ化(Excel 苦手だけど)まで行きたいと思います。

概要設計

  • CPU
  • Memory
  • Disk

上記 3 つの情報を定期的にログに落とす

ココではまずはテキストに TAB 区切りでログを落とすことにする。TAB 区切りは Excel に噛ますのが楽なので採用した。

CPU 情報の設計と取得

CPU 情報の条件としては次の内容で検討

  • CPU 全体、コアごと等の情報を取得できる
  • 使用率は 42.2% のように小数点以下も取得できる

上記の事を踏まえてデータレイアウトは次の通りとした

name(一意の名前) <TAB> persent(使用率) <TAB> get_datetime(取得日時)

$data = Get-Counter -Counter "\Processor Information(*)\% Processor Time"
$datetime = $data.Timestamp
foreach($item in $data.CounterSamples) {
    Write-Output(@($item.InstanceName.ToString(), $item.CookedValue, $datetime) -join "`t")
}

_total  3.80632670560463        2016/10/04 0:12:10
0,_total        3.80632670560463        2016/10/04 0:12:10
0,3     1.49767854653915        2016/10/04 0:12:10
0,2     1.49767854653915        2016/10/04 0:12:10
0,1     7.65407363738045        2016/10/04 0:12:10
0,0     4.5758760919598 2016/10/04 0:12:10

Memory の設計と取得

Memory 情報の条件としては次の内容で検討

  • 物理メモリ、仮想メモリ(swap)の情報を取得できる
  • 全体の容量と使用している容量(又は空き容量)を取得できる

上記の事を踏まえてデータレイアウトは次の通りとした

name(Physical or Virtual) <TAB> free(空き容量) <TAB> total(全体容量) <TAB> get_datetime(取得日時)

$data = Get-WmiObject Win32_OperatingSystem
$datetime = Get-Date

Write-Output(@('PhysicalMemory', $data.FreePhysicalMemory, $data.TotalVisibleMemorySize, $datetime) -join "`t")
Write-Output(@('VirtualMemory', $data.FreeVirtualMemory, $data.TotalVirtualMemorySize, $datetime) -join "`t")
PhysicalMemory  9727032 16676748        2016/10/04 0:34:53
VirtualMemory   9497996 19167116        2016/10/04 0:34:53

Disk の設計と取得

Disk 情報の条件としては次の内容で検討

  • 接続されている物理ディスクの情報を取得できる
  • 全体の容量と使用している容量(又は空き容量)を取得できる

上記の事を踏まえてデータレイアウトは次の通りとした

name(Device ID) <TAB> free(空き容量) <TAB> total(全体容量) <TAB> get_datetime(取得日時)

$data = Get-WmiObject -Class win32_logicaldisk
$datetime = Get-Date
foreach($item in $data) {
    Write-Output(@($item.DeviceID, $item.FreeSpace, $item.Size, $datetime) -join "`t")
}
C:      117137235968    254993756160    2016/10/04 0:52:09
D:      120767868928    259676696576    2016/10/04 0:52:09

考察

仕様とデータの設計、データの取得までをサーッと作りました。

このままでも Windows タスクスケジューラーに設定してテキストファイルを吐き続けられますが、それでは面白みに欠けるので次回は Excel ファイルに吐くところを作ってみたいと思います。

3
5
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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?