LoginSignup
9
8

More than 5 years have passed since last update.

バッチごとのプロセスメトリクス (CPU, Memory など) を可視化してみた所感

Last updated at Posted at 2016-07-31

モチベーションとしては、複数のバッチが並行して実行されているサーバーで、それぞれのバッチごとの CPU 使用率やメモリ使用量を可視化したかった

imageimage

可視化して良かった点

  • バッチサーバーの CPU 使用率 or メモリ使用量を減らしたいときに、どのバッチを改修すればいいのかすぐに決めやすくなった
  • その後、バッチの改修によって CPU 使用率 or メモリ使用量が減ったかどうかがひと目でわかるようになった

既存のプラグインなどで適うものがなかったので自作した

実装方法

https://github.com/yoheimuta/Linux-GetPidstat
https://metacpan.org/pod/Linux::GetPidstat

  • Perl 製
    • linux-get-pidstat というコマンドラインツールが付属する
    • 名前の通り Linux 向け
  • pidfile をもとに、プロセスメトリクスを収集して Mackerel に送信するコマンドラインツール
    • 書き出す先は Mackerel ではなくファイルでもいい

特徴

  • pidstat(1) の出力結果を Mackerel に送る
    • たとえば、60 秒間、毎秒 pidstat を実行した結果の平均値を出力するなど
    • 子プロセスや孫プロセスの結果を加算することができる
  • 適当なフォルダに置いてある pidfile を対象プロセスとして読み取る

準備: pidfile の作成

  • 対象のバッチプロセスが起動時に pidfile を作成して、終了時に削除するようにした

準備: linux-get-pidstat のインストール

cpanm Linux::GetPidstat

linux-get-pidstat の定期実行

  • crontab で毎分実行する
* * * * * user linux-get-pidstat  --no-dry_run --pid_dir=/var/tmp --mackerel_api_key=your_api_key --mackerel_service_name=your_service 2>&1 >> /tmp/linux-get-pidstat.log
  • コマンドオプションはつぎの通り
$ carton exec -- linux-get-pidstat --help
Usage:
        linux-get-pidstat - command description
          Usage: command [options]
          Options:
            --pid_dir               A directory path for pid files
            --res_file              A file path to be stored results
            --interval              Interval second to be given as a pidstat argument (default:1)
            --count                 Count number to be given as a pidstat argument (default:60)
            --dry_run               Dry run mode. not run the side-effects operation (default:1) (--no-dry_run is also supported)
            --datetime              Datetime (ex. '2016-06-10 00:00:00') to be recorded
            --include_child         Flag to be enabled to include child process metrics (default:1) (--no-include_child is also suppoted)
            --max_child_limit       Number to be used for limiting pidstat multi processes (default:30) (skip this limit if 0 is specified)
            --mackerel_api_key      An api key to be used for posting to mackerel
            --mackerel_service_name An mackerel service name
          Requirement Programs: pidstat and pstree commands
9
8
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
9
8