0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MrtgログでGnuplotグラフを作る(Linux)

Last updated at Posted at 2024-06-07

Linux シェルスクリプト編

はじめに

サーバ-監視ツールの中で監視対象ホストのCPU、Memory、Diskを統合したGnuplotグラフを作る際、すでに作成しているMrtgグラフのログを使うことにしたので考え方を書き置きます。

作成手順

  1. Mrtgで作成されたCPU、Memory、Diskのログを用意します

  2. 各ログを上から任意の行数(注1)を読み、unixtimeを時間に変更した統合ログを時間に変換し古い順(正順)に最適行(注2)作成します

  3. グラフ作成テンプレートへ渡す変数を組み込んだhost.exeファイルを作成します

  4. host.exeファイルを実行し、svgイメージファイルを作成します

    (注1)gnuplotグラフのX軸を24時間とした場合の経験値は400行、(注2)は28行ですが、適宜調整します

Mrtgログと統合ログ

  mrtgログドキュメント


* Mrtgログフォーマット 
host.cpu.log                host.ram.log                 host.disk.log
1716375662 6 100            1716375662 31 100            1716375662 47 100
1716375662 6 100 6 100      1716375662 31 100 31 100     1716375662 47 100 47 100
1716375062 2 100 2 100      1716375062 26 100 26 100     1716375662 47 100 47 100
1716375000 2 100 2 100      1716375000 26 100 26 100     1716375000 47 100 47 100
1716374700 4 100 14 100     1716374700 30 100 42 100     1716374700 47 100 47 100 
各ログの1列目:unixtime, 2列目:CPU使用率、Memory使用率、Disk使用率

* 統合ログフォーマット(1列目時間の正順)
1列目:時間、2列目:CPU、3列目:Memory、4列目:Disk
host.log
18 4 31 47
19 2 26 47
20 6 30 47

グラフテンプレートとexeファイル

* サンプル画像用テンプレート mkplot.plt
 set title ghost." Load Average"
 set xlabel "Time Hour"
 set ylabel "Load %"
 set yrange [0:100]
 set size 1 , 0.9
 set terminal svg
 set output path."/".ghost.".svg"
 plot path."/".ghost.".ok" using 2:xticlabels(1) with lines title "CPU", path."/".ghost.".ok" using 3 with lines title "Memory", path."/".ghost.".ok" using 4 with lines title "Disk"
 unset output
 quit
* 実行ファイル host.exe
/usr/bin/gnuplot -e 'path="plot"; ghost="host"' bin/mkplot.plt

変数pathはCPU,Memory,Diskの統合ログのパス、変数ghostはグラフを描くホスト名
* 実行
$ host.exe

  • サンプル画像
    gnuplotsample.png

ソースとサンプルデータ

ここのgraphsample/linuxにあります。ソースはシェルスクリプトで書いており、mrtgログが複数のホストにより任意の間隔で採られることから、これに合わせたタイミングでグラフを作成するようcrontab等で自動作成することが出来ます。

おわりに

mrtgグラフを作成していたので利用することにしました。このグラフはサーバ監視システムの機能の一部として動作させています。興味ある方はここからどうぞ。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?