なんちゃってビジュアルリアルタイム監視


単に、gnuplot のグラフをターミナルで動的に閲覧するだけの古いやり方。

  • gnuplot
  • topless

今回は sar 1で例を書く。

  • sar

バックグラウンドジョブ

cron なんかで、数値のログを取るジョブを走らせる。
sar が単純なので。

$ pwd 
/PATH/TO/FOO
$ LANG=C sar 1 > sar.txt &

グラフ作成スクリプト

gnuplot でターミナルにグラフ出力する。

$ti とかは、必要あれば正しい日時に2

tail -200 sar.txt | while read -a ARR ; do
    ti="2018-01-01 ${ARR[0]}"
    d=$(( $(date +%s -d"$ti" ) -  946652400)) #
    echo $d ${ARR[2]}
done  > /tmp/tmp.txt
gnuplot <<'EOF'
#!/usr/bin/env gnuplot
set terminal dumb
set title 'CPU'
set xdata time
set timefmt "%s"
set format x "%M:%S"
plot '/tmp/tmp.txt'  u ($1):($2) notitle w l
EOF
rm /tmp/tmp.txt

これを実行すれば、

$ bash ./graph.sh
gnuplot> #!/usr/bin/env gnuplot
gnuplot> set terminal dumb
Terminal type set to 'dumb'
Options are 'feed  79 24'
gnuplot> set title 'CPU'
gnuplot> set xdata time
gnuplot> set timefmt "%s"
gnuplot> set format x "%M:%S"
gnuplot> plot 'tmp.txt'  u ($1):($2) notitle w l
                                         CPU

    2.5 ++-+--+--+-+--+--+--+--+--+-+--+--+--+--+--+-+--+--+--+--+--+-+--+-++
        +        +       +        +       +        +       + *      +       +
        |                                                    *              |
        |                                                    *              |
      2 ++                                       *           *             ++
        |                                        *           *              |
        |                                        *           *              |
        |                                        *           *              |
    1.5 ++                                   *   *           *             ++
        |   *                                *   *           *              |
        |   *                                *   *           *   *          |
      1 ++  *                                *   **   *     **   *  *      ++
        |   *                                *   **   *     **   *  *       |
        |   *                                *   ** * *   * **   *  *       |
        |    *                           *  **   ** * *   * **   *  *       |
    0.5 ++   *                           *  **   ** * **  * **   *  *      ++
        |    *                           *  **   ** * **  * **   *  *       |
        |    *                           *  **   ** * **  * **   *  *       |
        +    *   +       +        +     **+ **   **+****  *+**   *  *       +
      0 ++-+-*********************************************************+--+-++
      44:30    45:00   45:30    46:00   46:30    47:00   47:30    48:00   48:30

gnuplot>

ビジュアルリアルタイム監視

あとは topless で監視するだけ。

$ topless -s 10 'bash ./graph.sh | grep "^ "'

  1. CentOS6.8 で。 OS X の sar は別挙動。 

  2. 一応、gnuplot のタイムスタンプに変換はしてる。最近のgnuplotだとUNIXタイムと同じになってるから注意