LoginSignup
2
3

More than 5 years have passed since last update.

vmstatをdaemon化して継続的なログを記録する

Posted at

サーバーのリソース状態を記録しておくための方法。

shファイルを作成

仕様:60秒ごとにログを取得。結果を日時とともにファイル(/tmp/vmstat.log)に書き込む。

/tmp/resource_log.sh
#!/bin/bash
exec /usr/bin/vmstat 60 | awk '{print strftime("%y/%m/%d %H:%M:%S"), $0} {fflush() }' >> /tmp/vmstat.log

shファイルは/tmpディレクトリに置いてありますが、どこでもよいです。

サービスファイル作成

実行用のサービスファイルを作ります。(/etc/systemd/system/配下)
ExecStartのところで、先ほど作ったshファイルを指定します。

/etc/systemd/system/resource_log.service
[Unit]
Description=VMSTAT as a Service

[Service]
Restart=on-failure
RestartSec=10
ExecStart=/tmp/rec_resource.sh

[Install]
WantedBy=basic.target

起動

以下のコマンドを実行。(systemctlに変更を通知 → 実行)

$ sudo systemctl daemon-reload
$ sudo systemctl start resource_log.service

確認

$ cat /tmp/vmstat.log

18/09/19 16:35:22 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
18/09/19 16:35:22  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
......

こんな感じで記録されていればOK。

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