CloudWatch経由でRDSのmuninでのモニタリングを実装しました。
ちなみに、標準プラグインを利用したい場合は以下の記事に書きました。
前提
rubyが入っているということと、以下のGemが必要です。
gem install aws-sdk
Rubyスクリプト
スクリプトは以下になります。
#! /usr/bin/env ruby require 'aws-sdk' require 'yaml' config = YAML.load_file('/etc/munin/plugin-conf.d/aws/rds.yml') graphs = config['graphs'] if m = (File.basename(__FILE__)).to_s.match(/rds_([^_]+)_cloudwatch$/) db_name = m[1] else exit 1 end if ARGV.shift == 'config' puts "host_name #{db_name}" puts "" graphs.each do |graph| puts "multigraph rds_#{graph['title'].delete(' ')}" puts "graph_title #{graph['title']}" puts "graph_args --base #{graph['base']}" puts "graph_vlabel #{graph['vlabel']}" puts "graph_category #{config['common']['category']}" graph['metrics'].each do |metric| puts "#{metric['metric_name']}.label #{metric['metric_name']}" puts "#{metric['metric_name']}.min 0" puts "#{metric['metric_name']}.type GAUGE" end puts "" end exit 0 end cred = Aws::Credentials.new( config['common']['access_key'], config['common']['secret_key'] ) cw = Aws::CloudWatch::Client.new(region: config['common']['region'], credentials: cred) graphs.each do |graph| puts "multigraph rds_#{graph['title'].delete(' ')}" graph['metrics'].each do |metric| data = cw.get_metric_statistics({ namespace: config['common']['namespace'], metric_name: metric['metric_name'], start_time: Time.now - config['common']['timespan'], dimensions:[{ name: 'DBInstanceIdentifier', value: db_name }], end_time: Time.now, period: config['common']['period'], statistics: [metric['statistics']] }).datapoints value = data.empty? ? 0 : data.first[metric['statistics'].downcase] puts "#{metric['metric_name']}.value #{value}" end puts "" end
Pluginインストール
スクリプトインストール
この手順では「rds_cloudwatch.rb」という名前でスクリプトを保存しました。この名前は何でも問題無いです。(拡張子外してもOK)
スクリプトを配置して、実行権限を付けます。
# cp rds_cloudwatch.rb /usr/share/munin/plugins/ # chmod +x /usr/share/munin/plugins/rds_cloudwatch.rb
設定ファイル
以下のような設定ファイルを「rds.yml」という名前で用意します。
--- # AWS Settings common: access_key: your_access_key secret_key: your_secret_key region: ap-northeast-1 category: rds namespace: AWS/RDS timespan: 300 period: 300 # Graph definition ref: graphs: - title: CPU Utilization vlabel: percent base: 1000 metrics: - metric_name: CPUUtilization statistics: Average - title: Latency vlabel: sec base: 1000 metrics: - metric_name: ReadLatency statistics: Average - metric_name: WriteLatency statistics: Average - title: Swap Usage vlabel: Bytes base: 1024 metrics: - metric_name: SwapUsage statistics: Average - title: Free Memory vlabel: Bytes base: 1024 metrics: - metric_name: FreeableMemory statistics: Average - title: IOPS vlabel: Count/Second base: 1000 metrics: - metric_name: WriteThroughput statistics: Average - metric_name: ReadThroughput statistics: Average - title: IO Throughput vlabel: Bytes/Second base: 1000 metrics: - metric_name: WriteThroughput statistics: Average - metric_name: ReadThroughput statistics: Average - title: FreeStorageSpace vlabel: Bytes base: 1024 metrics: - metric_name: FreeStorageSpace statistics: Average - title: Network Throughput vlabel: Bytes/Second base: 1024 metrics: - metric_name: NetworkReceiveThroughput statistics: Average - metric_name: NetworkTransmitThroughput statistics: Average - title: BinLog DiskUsage vlabel: Bytes base: 1024 metrics: - metric_name: BinLogDiskUsage statistics: Average - title: Database Connections vlabel: Count base: 1000 metrics: - metric_name: DatabaseConnections statistics: Average - title: Disk Queue Depth vlabel: Count base: 1000 metrics: - metric_name: DiskQueueDepth statistics: Average
AWSのアクセスキーとシークレットキーを修正したら、設定ファイルを新規にawsフォルダ作成して配置します。
# mkdir /etc/munin/plugin-conf.d/aws # cp rds.yml /etc/munin/plugin-conf.d/aws/
シンボリックリンク作成
シンボリックリンクを張るのですが、ここのリンク名は重要です。RDSのDBインスタンス名をリンク名に組み込みます。
rds_[DBインスタンス名]_cloudwatch
こんな感じに。
# ln -s /usr/share/munin/plugins/rds_cloudwatch.rb /etc/munin/plugins/rds_mydb01_cloudwatch # ln -s /usr/share/munin/plugins/rds_cloudwatch.rb /etc/munin/plugins/rds_mydb02_cloudwatch
munin-nodeを再起動します。
# systemctl restart munin-node
これで導入完了です。
挙動テスト
まずはconfigオプションを付けて実行してみます。host_nameの項目がファイル毎に異なっている(DBインスタンス名が表示される)ことを確認します。
# munin-run rds_mydb01_cloudwatch config host_name mydb01 multigraph rds_CPUUtilization graph_title CPU Utilization graph_args --base 1000 --省略 # munin-run rds_mydb01_cloudwatch config host_name mydb02 multigraph rds_CPUUtilization graph_title CPU Utilization graph_args --base 1000 graph_vlabel percent graph_category rds --省略
今度はconfigオプション外してCloudwatchのメトリクス情報が取れていることを確認します。
# munin-run rds_mydb02_cloudwatch multigraph rds_CPUUtilization CPUUtilization.value 0.88 multigraph rds_Latency ReadLatency.value 8.0e-05 multigraph rds_Latency WriteLatency.value 0.000804445945945946 --省略
Muninに仮想ノード設定
最後にmuninに設定します。
# vim /etc/munin/munin.conf
以下のように設定します。「use_node_name no」とすることで仮想ノードを設定できます。
### RDS [rds;mydb01] address 127.0.0.1 use_node_name no [rds;mydb02] address 127.0.0.1 use_node_name no
munin-node再起動して設定反映させておきます。
systemctl restart munin-node
後はcronの処理待てばグラフに反映されますが、すぐにグラフに反映させたければ以下の通り。
# su - munin --shell=/bin/sh
$ /usr/bin/munin-cron
画面の雰囲気
DBインスタンス毎にノード作ってみました。
沢山グラフ並んでいます。