ELBをCloudwatch経由で自作Muninプラグインで監視モニタリングする

こちらの記事でRDSのMuininプラグイン作ったので流用してELBも作りました。RequestCount何かは5分間の合計になってしまい大ざっぱなグラフになってしまうのが少し微妙ですが。

yomon.hatenablog.com

前提

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/elb.yml')
graphs = config['graphs']

if m = (File.basename(__FILE__)).to_s.match(/elb_(.*)_cloudwatch$/)
    elb_name = m[1]
else
    exit 1
end

if ARGV.shift == 'config'
puts "host_name #{elb_name}"
  puts ""
  graphs.each do |graph|
    puts "multigraph elb_#{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']}_#{metric['statistics']}.label #{metric['label']}"
      puts "#{metric['metric_name']}_#{metric['statistics']}.min 0"
      puts "#{metric['metric_name']}_#{metric['statistics']}.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 elb_#{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: 'LoadBalancerName',
        value: elb_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']}_#{metric['statistics']}.value #{value}"
  end
  puts ""
end

Pluginインストール

スクリプトインストール

この手順では「elb_cloudwatch.rb」という名前でスクリプトを保存しました。この名前は何でも問題無いです。(拡張子外してもOK)

スクリプトを配置して、実行権限を付けます。

# cp elb_cloudwatch.rb /usr/share/munin/plugins/
# chmod +x /usr/share/munin/plugins/elb_cloudwatch.rb 

設定ファイル

以下のような設定ファイルを「rds.yml」という名前で用意します。

# AWS Settings
common:
  access_key: your_access_key
  secret_key: your_secret_key 
  region: ap-northeast-1
  category: elb
  namespace: AWS/ELB
  timespan: 300
  period: 300
# Graph definition ref:
graphs:
- title: RequestCount
  vlabel: Count
  base: 1000
  metrics:
    - metric_name: RequestCount
      statistics: Sum
      label: Sum
- title: Latency
  vlabel: Seconds
  base: 1000
  metrics:
    - metric_name: Latency
      statistics: Average
      label: Average
    - metric_name: Latency
      statistics: Maximum
      label: Max
- title: BackendConnectionErrors
  vlabel: Count
  base: 1000
  metrics:  
    - metric_name: BackendConnectionErrors
      statistics: Sum
      label: Sum
- title: HTTPCode_Backend
  vlabel: Count
  base: 1000
  metrics:  
    - metric_name: HTTPCode_Backend_2XX 
      statistics: Sum
      label: 2XX
    - metric_name: HTTPCode_Backend_3XX 
      statistics: Sum
      label: 3XX
    - metric_name: HTTPCode_Backend_4XX 
      statistics: Sum
      label: 4XX
    - metric_name: HTTPCode_Backend_5XX 
      statistics: Sum
      label: 5XX
- title: HTTPCode_ELB_5XX
  vlabel: Count
  base: 1000
  metrics:  
    - metric_name: HTTPCode_ELB_5XX
      statistics: Sum
      label: Sum
- title: HealthyHostCount
  vlabel: Count
  base: 1000
  metrics:  
    - metric_name: HealthyHostCount
      statistics: Average
      label: Average
- title: SurgeQueueLength
  vlabel: Count
  base: 1000
  metrics:  
    - metric_name: BackendConnectionErrors
      statistics: Sum
      label: Sum
- title: UnHealthyHostCount
  vlabel: Count
  base: 1000
  metrics:  
    - metric_name: BackendConnectionErrors
      statistics: Sum
      label: Sum

AWSのアクセスキーとシークレットキーを修正したら、以下のフォルダ作成して、設定ファイルを配置します。

# mkdir /etc/munin/plugin-conf.d/aws
# cp elb.yml /etc/munin/plugin-conf.d/aws/ 

シンボリックリンク作成

シンボリックリンクを張るのですが、ここのリンク名は重要です。AWS Management Console上のロードバランサー名の項目を組み込みます。

elb_[ロードバランサー名]_cloudwatch

このようにリンクを作成します。

# ln -s /usr/share/munin/plugins/elb_cloudwatch.rb /etc/munin/plugins/elb_myelb01_cloudwatch
# ln -s /usr/share/munin/plugins/elb_cloudwatch.rb /etc/munin/plugins/elb_myelb02_cloudwatch

munin-nodeを再起動します。

# systemctl restart munin-node

これでプラグインの導入完了です。

挙動確認等は以下の記事で書いたRDSのものと同じです。