Skip to content

Instantly share code, notes, and snippets.

@peelman
Created November 8, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peelman/f92e5db676dd324dac878ec6d024b607 to your computer and use it in GitHub Desktop.
Save peelman/f92e5db676dd324dac878ec6d024b607 to your computer and use it in GitHub Desktop.
A very quick Nagios Plugin to check the overall status of an ESXi Host using rbvmomi.

Overview

This is a quick nagios plugin / script to query an ESXi server and grab the "overall status" parameter. This is a parameter each host should respond to and will reflect if any alarms are active on the system. See the official documentation for more info.

This should give notice in the event of a power supply failure, memory errors, loss of connectivity to datastore, etc.

The script only requires a Hostname, but allows for the providing of both a hostname and IP address. The hostname is required to make sure that the status being returned is for the correct host (I don't trust CIM to always return the hosts inside the "hostFolder" in the same order). It is actually used to query the API for the provided string, so it must be accurate and reflect what the system thinks its hostname is. Our environment and conditions mandated this. YMMV.

Parameters

Takes several parameters:

  • Hostname
  • IP Address
  • Port
  • CIM Username
  • CIM Password
  • Don't use SSL
  • Don't check certs

Run the script with -h to see the switches and what they do (or you can just look at the code).

Nagios

Object definitions for Nagios configs would look something like this:

define command {
  command_name    check_esxi_host_status
  command_line    $USER1$/contrib/check_esxi_status.rb -H $HOSTNAME$ -I $HOSTADDRESS$ -U $_HOSTCIM_USER$ -P '$_HOSTCIM_PASS$' -S
}

define service {
  use                             generic-service
  hostgroup_name                  esxi-hosts
  contacts                        everybody
  service_description             ESXi Host Health
  check_command                   check_esxi_host_status
  servicegroups                   vmware-health-checks
}

Script output will look something akin to this:

OK: host1.vcenter.domain.tld reports green; The host is healthy.
#!/usr/bin/ruby
######################################################################################################
#
# Description:
# Query an ESXi Server using rbvmomi to check the overallStatus parameter
#
######################################################################################################
#
# Author: Nick Peelman (2016-11-08)
#
######################################################################################################
#
# Usage:
# check_esxi_status.rb -H <hostname> -U <username> -P <password> --ignore-cert
#
######################################################################################################
#
# Requirements
# * gem install rbvmomi
#
######################################################################################################
require 'optparse'
require 'rbvmomi'
######################################################################################################
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.ManagedEntity.Status.html
HEALTH_STATUSES = {
gray: {
exit_code: 3,
status: "UNKNOWN",
message: "The host repots that its health is unknown.",
},
green: {
exit_code: 0,
status: "OK",
message: "The host is healthy.",
},
red: {
exit_code: 2,
status: "CRITICAL",
message: "The host is reporting a problem.",
},
yellow: {
exit_code: 1,
status: "WARNING",
message: "The host is reporting a possible problem.",
},
}
######################################################################################################
@hostname = nil
@ipaddress = nil
@port = 443
@username = nil
@password = nil
@use_ssl = true
@ignore_cert = false
def parse_args(args = {})
@opts = OptionParser.new
@opts.on('-H', "--host HOSTNAME", "Hostname of the ESXi Server") do |hostname|
@hostname = hostname
end
@opts.on('-I', "--ip-address ADDRESS", "IP Address of the ESXi Server") do |ipaddress|
@ipaddress = ipaddress
end
@opts.on("-U", "--username USERNAME", "Username") do |username|
@username = username
end
@opts.on("-P", "--password PASSWORD", "Password") do |password|
@password = password
end
@opts.on("-p", "--port", "TCP port to use") do |port|
@port = port
end
@opts.on("-s", "--no-ssl", "Don't use SSL") do
@use_ssl = false
end
@opts.on("-S", "--ignore-cert", "Don't validate certificate") do
@ignore_cert = true
end
@opts.on_tail("-h", "--help", "Show this message") do
puts @opts
exit
end
@opts.parse!
end
def get_host(vim)
vim.serviceInstance.find_datacenter.hostFolder.findByDnsName(@hostname, RbVmomi::VIM::HostSystem)
end
def get_status(host)
host.overallStatus
end
def get_host_name(vim)
get_host(vim).name
rescue NoMethodError
puts "SCRIPT_ERROR: Hostname not found; host at that IP might not match the hostname provided"
exit 3
end
def get_vim
connect_to = @ipaddress
connect_to ||= @hostname
vim = RbVmomi::VIM.connect({
host: connect_to,
port: @port,
user: @username,
password: @password,
ssl: @use_ssl,
insecure: @ignore_cert
})
rescue Errno::ECONNREFUSED
puts "UNKNOWN: Host is refusing to talk to us"
exit 3
end
parse_args()
vim = get_vim()
name = get_host_name(vim)
status_color = get_status(get_host(vim))
nagios_output = HEALTH_STATUSES[status_color.to_sym]
output = "#{nagios_output[:status]}: #{name} reports #{status_color}; #{nagios_output[:message]}"
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment