VMWare ESXiのリソースをMuninで監視するためのプラグイン(Ruby)作ってみた

ESXiのMunin監視を行うためのプラグインを作ってみました。

監視項目

  • CPU利用量
  • Memory利用量
  • Datastore利用量

画面イメージ

GEMのインストール

こちらのRuby Gemを利用します。

gem install rbvmomi

github.com

プラグインコード

#!/var/lib/munin/.rbenv/shims/ruby
require 'rbvmomi'

# ログオンパラメータ
esxi_host = 'esxi_hostname'
esxi_user = 'esxi_username'
esxi_pass = 'esxi_userpassword'

# Munin グラフ定義
if ARGV.shift == 'config'
    puts "host_name #{esxi_host}"
    puts ""
    #CPU Graph Definition
    puts "multigraph esxi_cpu_usage"
    puts "graph_title ESXi CPU Usage"
    puts "graph_args --base 1000 --lower-limit 0"
    puts "graph_scale no"
    puts "graph_vlabel Mhz"
    puts "graph_category esxi"
    puts "CPUUsageMhz.label CPU Usage (Mhz)"
    puts "CPUUsageMhz.min 0"
    puts "CPUUsageMhz.draw AREASTACK"
    puts "CPUUsageMhz.type GAUGE"
    puts "CPUFreeMhz.label CPU Free (Mhz)"
    puts "CPUFreeMhz.min 0"
    puts "CPUFreeMhz.draw AREASTACK"
    puts "CPUFreeMhz.type GAUGE"
    puts " "
    #Memory Graph Definition
    puts "multigraph esxi_mem_usage"
    puts "graph_title ESXi Memory Usage"
    puts "graph_args --base 1024 --lower-limit 0"
    puts "graph_scale no"
    puts "graph_vlabel MB"
    puts "graph_category esxi"
    puts "MEMUsageGB.label Memory Usage (GB)"
    puts "MEMUsageGB.min 0"
    puts "MEMUsageGB.draw AREASTACK"
    puts "MEMUsageGB.type GAUGE"
    puts "MEMFreeGB.label Memory Free (GB)"
    puts "MEMFreeGB.min 0"
    puts "MEMFreeGB.draw AREASTACK"
    puts "MEMFreeGB.type GAUGE"
    puts " "
    #Datastore Graph Definition
    puts "multigraph esxi_disk_usage"
    puts "graph_title ESXi Datastore Usage"
    puts "graph_args --base 1024 --lower-limit 0"
    puts "graph_scale no"
    puts "graph_vlabel GB"
    puts "graph_category esxi"
    puts "DiskUsageGB.label Datastore Usage (GB)"
    puts "DiskUsageGB.min 0"
    puts "DiskUsageGB.draw AREASTACK"
    puts "DiskUsageGB.type GAUGE"
    puts "DiskFreeGB.label Datastore Free (GB)"
    puts "DiskFreeGB.min 0"
    puts "DiskFreeGB.draw AREASTACK"
    puts "DiskFreeGB.type GAUGE"
    exit 0
end

vim = RbVmomi::VIM.connect(
    host: esxi_host,
    user: esxi_user,
    password: esxi_pass,
    insecure: true)
host_system = vim.root.childEntity[0].hostFolder.childEntity[0].host[0]

#CPU
core_mhz = host_system.summary.hardware.cpuMhz
core_num = host_system.summary.hardware.numCpuCores
cpu_capacity_mhz = core_mhz * core_num
cpu_usage_mhz = host_system.summary.quickStats.overallCpuUsage
cpu_free_mhz = cpu_capacity_mhz - cpu_usage_mhz
puts "multigraph esxi_cpu_usage"
puts "CPUUsageMhz.value #{cpu_usage_mhz}"
puts "CPUFreeMhz.value #{cpu_free_mhz}"
puts " "

#Memory
mem_usage_gb = host_system.summary.quickStats.overallMemoryUsage/1024.to_f
mem_capacity_gb = host_system.summary.hardware.memorySize/1024/1024/1024.to_f
mem_free_gb = mem_capacity_gb - mem_usage_gb
puts "multigraph esxi_mem_usage"
puts "MEMUsageGB.value #{mem_usage_gb.round(2)}"
puts "MEMFreeGB.value #{mem_free_gb.round(2)}"
puts " "

#Datastore
datastore = vim.root.childEntity[0].hostFolder.childEntity[0].datastore[0]
datastore_capacity_byte = datastore.info.vmfs.capacity
datastore_capacity_gb = datastore_capacity_byte/1024/1024/1024.to_f

datastore_disk_free_byte = datastore.info.freeSpace
datastore_usage_byte = datastore_capacity_byte - datastore_disk_free_byte
datastore_usage_gb = datastore_usage_byte/1024/1024/1024.to_f

datastore_free_gb = datastore_capacity_gb - datastore_usage_gb
puts "multigraph esxi_disk_usage"
puts "DiskUsageGB.value #{datastore_usage_gb.round(2)}"
puts "DiskFreeGB.value #{datastore_free_gb.round(2)}"
puts " "

munin.confの設定

Muninサーバ自身に配置したスクリプトを動かすので munin.conf の設定は以下のようになります。

[esxi;esxi_hostname]
    address 127.0.0.1
    use_node_name no

1つのプラグインファイルで複数ホストを監視したい場合

シンボリックリンクを使って、シンボリックリンクのファイル名からESXiのホスト名を取得する方法があります。

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

このあたりと同じ実装です。

yomon.hatenablog.com