Skip to content

Commit

Permalink
Added simple reader metrics (#1)
Browse files Browse the repository at this point in the history
* Added simple reader metrics

* Added ability to get metric from vCenter
  • Loading branch information
Yuri Zubov authored and majormoses committed Feb 27, 2018
1 parent 8e466d1 commit f5e96ba
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .gitignore
@@ -0,0 +1,24 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log
.vagrant/*
.DS_Store
.idea/*
*.gem
.ruby-version


# test-kitchen
.kitchen/
.kitchen.list.yml
168 changes: 168 additions & 0 deletions bin/metrics-vsphere.rb
@@ -0,0 +1,168 @@
#! /usr/bin/env ruby
#
# metrics-vsphere
#
# DESCRIPTION:
#
# OUTPUT:
# plain text, metric data, etc
#
# PLATFORMS:
# Linux, Windows, BSD, Solaris, etc
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: rbvmomi
#
# USAGE:
#
# NOTES:
#
# LICENSE:
# Yuri Zubov <yury.zubau@gmail.com>
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#

require 'sensu-plugin/metric/cli'
require 'rbvmomi'

#
# VSphere Graphite
#
class VsphereGraphite < Sensu::Plugin::Metric::CLI::Graphite
option :host,
description: 'ESX or ESXi hostname',
short: '-H HOST',
long: '--host HOST'

option :user,
description: 'Username to connect with',
short: '-u USER',
long: '--user USER'

option :password,
description: 'Password to use with the username.',
short: '-p PASSWORD',
long: '--password PASSWORD'

option :vm_name,
description: 'Virtual machine name.',
short: '-N VM_NAME',
long: '--name VM_NAME'

option :host_name,
description: 'Host name.',
short: '-h HOST_NAME',
long: '--host_name HOST_NAME'

option :data_center,
description: 'Data center name.',
short: '-D DATA_CENTER_NAME',
long: '--data_center_name DATA_CENTER_NAME'

option :compute_resource,
description: 'Compute resource name.',
short: '-c COMPUTE_RESOURCE_NAME',
long: '--compute_resource COMPUTE_RESOURCE_NAME'

option :command_type,
description: 'Specify command type (CPU, MEM, NET, IO, VMFS, RUNTIME, ...)',
short: '-l COMMAND_TYPE',
long: '--command_type COMMAND_TYPE'

option :insecure,
description: 'Use insecure connection',
short: '-i',
long: '--insecure',
default: false

option :period,
description: ' Sampling Period in seconds. Basic historic intervals: 300, 1800, 7200 or 86400. See config for any changes.',
short: '-p',
long: '--period',
proc: proc(&:to_i),
default: 300

option :scheme,
description: 'Metric naming scheme, text to prepend to metric',
short: '-S SCHEME',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}.vsphere"

def vim
@vim ||= RbVmomi::VIM.connect(
host: config[:host],
user: config[:user],
password: config[:password],
insecure: config[:insecure]
)
end

def find_or_take_first(resources, resource_name)
result = nil
if resource_name
result = resources.find { |x| x.name == resource_name }
unless result
unknown "#{resources.first.class.to_s.gsub(/^.*::/, '')}(#{resource_name}) wasn't found. Available(#{resources.map(&:name)})"
end
else
if resources.length == 1
result = resources.first
else
if resources.first.is_a?(RbVmomi::VIM::ComputeResource)
unknown "please use --compute_resource (#{resources.map(&:name)})"
end
if resources.first.is_a?(RbVmomi::VIM::HostSystem)
unknown "please use --host_name (#{resources.map(&:name)})"
end
if resources.first.is_a?(RbVmomi::VIM::Datacenter)
unknown "please use --data_center_name (#{resources.map(&:name)})"
end
end
end
result
end

def run
data_centers = vim.serviceInstance.content.rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter)
dc = find_or_take_first(data_centers, config[:data_center])

pm = vim.serviceInstance.content.perfManager

compute_resources = dc.hostFolder.children.grep(RbVmomi::VIM::ComputeResource)
if config[:vm_name] || config[:host_name] || config[:compute_resource]
compute_resource = find_or_take_first(compute_resources, config[:compute_resource])
end
host = find_or_take_first(compute_resource.host, config[:host_name]) if config[:vm_name] || config[:host_name]

if config[:vm_name]
vms = host.vm.grep(RbVmomi::VIM::VirtualMachine)
resource = find_or_take_first(vms, config[:vm_name])
elsif config[:host_name]
resource = host
elsif config[:compute_resource] && compute_resource.is_a?(RbVmomi::VIM::ClusterComputeResource)
resource = compute_resource
elsif
resource = dc
end

regexp = Regexp.new("^#{config[:command_type]}")
metrics = pm.retrieve_stats([resource],
[],
{ multi_instance: true, interval: config[:period],
start_time: (Time.now - config[:period]) }
)

if metrics
filtered_metrics = metrics[resource][:metrics].select{ |(metric, _), _| metric.to_s.match(regexp) && metric }
filtered_metrics.each do |(metric, instance), value|
output "#{config[:scheme]}.#{[instance, metric].flatten.select{|e| e != ''}.join('.')}", value.first
end

ok
else
warning
end
end
end
5 changes: 3 additions & 2 deletions sensu-plugins-vsphere.gemspec
Expand Up @@ -35,17 +35,18 @@ Gem::Specification.new do |s|
s.signing_key = File.expand_path(pvt_key) if $PROGRAM_NAME =~ /gem\z/
s.summary = 'Sensu plugins for mailer'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.version = SensuPluginsvsphere::Version::VER_STRING
s.version = SensuPluginsVsphere::Version::VER_STRING

s.add_runtime_dependency 'sensu-plugin', '~> 1.2.0'
s.add_runtime_dependency 'rbvmomi', '~> 1.11.6'

s.add_development_dependency 'bundler', '~> 1.7'
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
s.add_development_dependency 'github-markup', '~> 1.3'
s.add_development_dependency 'pry', '~> 0.10'
s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'redcarpet', '~> 3.2'
s.add_development_dependency 'rubocop', '0.32.1'
s.add_development_dependency 'rubocop', '>= 0.32.1'
s.add_development_dependency 'rspec', '~> 3.1'
s.add_development_dependency 'yard', '~> 0.8'
end

0 comments on commit f5e96ba

Please sign in to comment.