Skip to content
Permalink
Browse files

push first production version (1.0.5)

  • Loading branch information...
jjmartres committed Sep 7, 2013
1 parent b2563ec commit a543588b3027dc99a18a676442854c2fbb5eabef
Showing with 4,050 additions and 1 deletion.
  1. +5 −0 .gitignore
  2. +14 −0 Gemfile
  3. +1 −0 Procfile
  4. +322 −1 README.md
  5. +1 −0 VERSION
  6. +3 −0 config.ru
  7. +12 −0 config/vimbix.yml
  8. +13 −0 config/viservers.yml
  9. +9 −0 lib/vimbix.rb
  10. +23 −0 lib/vimbix/base.rb
  11. +55 −0 lib/vimbix/config.rb
  12. +253 −0 lib/vimbix/connector.rb
  13. +133 −0 lib/vimbix/routing.rb
  14. +10 −0 lib/vimbix/version.rb
  15. +84 −0 lib/vimbix/words.rb
  16. +111 −0 rakefile
  17. +80 −0 scripts/vimbix-client
  18. +2,921 −0 zbx-vimbix.xml
@@ -0,0 +1,5 @@

Gemfile.lock
var/
data/
test/
14 Gemfile
@@ -0,0 +1,14 @@
source "https://rubygems.org"

gem "sinatra"
gem "sinatra-partial"
gem "sinatra-logger"
gem "sinatra-contrib"
gem "json"
gem "nokogiri", "= 1.5.10"
gem "rbvmomi", "= 1.6.0"
gem "rufus-scheduler"
gem "thin"
gem "rake"
gem "thread"
gem "logger"
@@ -0,0 +1 @@
web: bundle exec rackup config.ru -p $PORT
323 README.md

Large diffs are not rendered by default.

@@ -0,0 +1 @@
1.0.5
@@ -0,0 +1,3 @@
require './lib/vimbix'

run VIMbix.new
@@ -0,0 +1,12 @@
---
environment: production
rackup: config.ru
address: 0.0.0.0
port: 9090
pid: var/run/vimbix.pid
log: var/log/vimbix.log
max_conns: 1024
timeout: 30
server: 1
max_persistent_conns: 512
daemonize: true
@@ -0,0 +1,13 @@
# list all your VIServer (vCenter and/or ESX(i) hosts)
1:
hostname: 10.0.0.1 # VIServer IP address or fqdn
username: my-username # VIServer username
password: 'my-password' # VIServer password
frequency: 300s # VIServer check frequency. Supported format: 300s, 5m
timeout: 280s # VIServer check frequency. Supported format: 280s, 4m
2:
hostname: 10.2.7.123 # VIServer IP address or fqdn
username: my-username # VIServer username
password: 'my-password' # VIServer password
frequency: 300s # VIServer check frequency. Supported format: 300s, 5m
timeout: 280s # VIServer check frequency. Supported format: 280s, 4m
@@ -0,0 +1,9 @@
require "rubygems"
require "bundler"
Bundler.require :default

class VIMbix < Sinatra::Base
set :root, Dir[File.dirname(__FILE__) + "/.." ]
end

Dir[File.dirname(__FILE__) + "/vimbix/*.rb"].each { |file| require file }
@@ -0,0 +1,23 @@
class VIMbix < Sinatra::Base
register Sinatra::Partial

#set :root, Dir[File.dirname(__FILE__) + "/../.."]
#set :root, APP_ROOT

# read datadump file
def get_dump(viserver)
@logger = settings.logger

begin
f = File.open(File.join("#{settings.root}/data/datadump_#{viserver}"), "r")
data = f.readlines
return JSON.parse(data[0])
rescue Exception => fault
@logger.error(fault.message)
ensure
f.close unless f.nil?
end

end

end
@@ -0,0 +1,55 @@
class VIMbix < Sinatra::Base

configure do

# init directory if not exists
["/var", "/var/log", "/var/run", "/data"].each do |subdir|
Dir.mkdir(File.join(settings.root, subdir)) unless File.exists?(File.join(settings.root, subdir))
end

# init logger
@logger = Logger.new(File.join("#{settings.root}/var/log/vimbix.log"),'weekly')
set :logger, @logger

# load viservers configuration file
begin
viservers_yml = File.join("#{settings.root}/config/viservers.yml")
rescue Exception => fault
@logger.error(fault.message)
exit(-1)
end

@viservers = YAML::load(File.open(viservers_yml))

# init scheduler
scheduler = Rufus::Scheduler.start_new
set :scheduler, scheduler

@viservers.each do |key,value|
scheduler.every value["frequency"], :timeout => value["timeout"], :first_at => Time.now do

beginning_time = Time.now
begin
viserver = Connector.new(value["hostname"], value["username"], value["password"], @logger)
data = viserver.collect
begin
filename = "datadump_#{value["hostname"]}"
f = File.open(File.join("#{settings.root}/data/#{filename}"), "w")
f << data.to_json
rescue Exception => fault
@logger.error(fault.message)
ensure
f.close unless f.nil?
end
@logger.info("File #{filename} saved")
rescue
@logger.error("Execution timed out (#{(Time.now - beginning_time).to_i} seconds)")
end

end

end

end

end
@@ -0,0 +1,253 @@
class Connector < RbVmomi::VIM
def initialize(host, user, pass, logger)
@host = host
@user = user
@pass = pass
@logger = logger
end

def collect

beginning_time = Time.now

container = Hash.new
[ "viserver", "hosts", "datastores", "virtualmachines"].each do |a|
container[a] = {}
end

# by default, we consider that check has failed
container["viserver"]["status"] = "FAILED"
container["viserver"]["timestampcheck"] = "#{Time.now.utc.to_i}"

# check if vCenter port (TCP 443) is reachable
begin
Timeout::timeout(30) do
begin
TCPSocket.new(@host, 443).close
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::TIMEOUT => fault
return container
@logger.error("#{@host}: #{fault.message}")
#exit
end
end
rescue
message = "#{@host}: connection timeout"
return container
@logger.error(message)
end

# initiate conection to the vCenter
begin
@vim = RbVmomi::VIM.connect :host => @host, :user => @user, :password => @pass, :insecure => true
@logger.info("#{@host}: connected")
rescue RbVmomi::Fault => fault
@logger.error("#{@host}: unable to connect. #{fault.message}")
end

# log that data collection is started
@logger.info("#{@host}: data collection started")

# get the vCenter rootFolder
begin
@rootFolder = @vim.serviceInstance.content.rootFolder
rescue RbVmomi::Fault => fault
@logger.error("#{@host}: unable to get the vCenter root folder. #{fault.message}")
end

# get all datacenters
begin
@datacenters = @rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter)
rescue RbVmomi::Fault => fault
@logger.error("#{@host}: unable to get all Datacenters. #{fault.message}")
end

# get serviceContent view
begin
@serviceContent = @vim.serviceContent.viewManager
rescue RbVmomi::Fault => fault
@logger.error("#{@host}: unable to get Service Content View. #{fault.message}")
end

# collect hosts informations
@datacenters.each do |datacenter|
# get all computerRessources on all datacenters
begin
@computerRessources = datacenter.hostFolder.childEntity
rescue RbVmomi::Fault => fault
@logger.error("#{@host}: unable to get all Computer Ressources. #{fault.message}")
end
pathStateActive = 0
pathStateDead = 0
pathStateDisabled = 0
pathStateStandby = 0
pathStateUnknown = 0
if @computerRessources.size > 1
i = 0
while i < (@computerRessources.size) do
datacenter.hostFolder.childEntity[i].host.grep(RbVmomi::VIM::HostSystem).each do |host|
name = host.name.gsub(/:/,"-")
host.config.multipathState.path.each do |path|
case path[:pathState]

when "active"
pathStateActive += 1
when "dead"
pathStateDead += 1
when "disabled"
pathStateDisabled += 1
when "standby"
pathStateStandby += 1
when "unknown"
pathStateUnknown += 1
end
end
data = {
"hostname" => host.name,
"product" => host.summary.config.product.fullName,
"hardwaremodel" => host.summary.hardware.model,
"cpumodel" => host.summary.hardware.cpuModel,
"cpumhz" => host.summary.hardware.cpuMhz*1000000,
"cpucore" => host.summary.hardware.numCpuCores,
"cpuusage" => host.summary.quickStats.overallCpuUsage*1000000,
"cpuusagepercent" => (host.summary.quickStats.overallCpuUsage.to_f/(host.summary.hardware.cpuMhz.to_f*host.summary.hardware.numCpuCores.to_f)),
"totalcpusize" => host.summary.hardware.numCpuCores*host.summary.hardware.cpuMhz*1000000,
"totalmemorysize" => host.summary.hardware.memorySize,
"memoryusage" => host.summary.quickStats.overallMemoryUsage*1024*1024,
"memoryusagepercent" => (host.summary.quickStats.overallMemoryUsage*1024*1024).to_f/host.summary.hardware.memorySize.to_f,
"powerstate" => host.summary.runtime.powerState,
"maintenancemode" => host.summary.runtime.inMaintenanceMode,
"uptime" => host.summary.quickStats.uptime,
"overallstatus" => host.summary.overallStatus,
"pathstateactive" => pathStateActive,
"pathstatedead" => pathStateDead,
"pathstatedisabled" => pathStateDisabled,
"pathstatestandby" => pathStateStandby,
"pathstateunknown" => pathStateUnknown
}
container["hosts"][name]=data
end
i += 1
end
else
datacenter.hostFolder.childEntity[0].host.grep(RbVmomi::VIM::HostSystem).each do |host|
name = host.name.gsub(/:/,"-")
host.config.multipathState.path.each do |path|
case path[:pathState]

when "active"
pathStateActive += 1
when "dead"
pathStateDead += 1
when "disabled"
pathStateDisabled += 1
when "standby"
pathStateStandby += 1
when "unknown"
pathStateUnknown += 1
end
end
data = {
"hostname" => host.name,
"product" => host.summary.config.product.fullName,
"hardwaremodel" => host.summary.hardware.model,
"cpumodel" => host.summary.hardware.cpuModel,
"cpumhz" => host.summary.hardware.cpuMhz*1000000,
"cpucore" => host.summary.hardware.numCpuCores,
"cpuusage" => host.summary.quickStats.overallCpuUsage*1000000,
"cpuusagepercent" => (host.summary.quickStats.overallCpuUsage.to_f/(host.summary.hardware.cpuMhz.to_f*host.summary.hardware.numCpuCores.to_f)),
"totalcpusize" => host.summary.hardware.numCpuCores*host.summary.hardware.cpuMhz*1000000,
"totalmemorysize" => host.summary.hardware.memorySize,
"memoryusage" => host.summary.quickStats.overallMemoryUsage*1024*1024,
"memoryusagepercent" => (host.summary.quickStats.overallMemoryUsage*1024*1024).to_f/host.summary.hardware.memorySize.to_f,
"powerstate" => host.summary.runtime.powerState,
"maintenancemode" => host.summary.runtime.inMaintenanceMode,
"uptime" => host.summary.quickStats.uptime,
"overallstatus" => host.summary.overallStatus,
"pathstateactive" => pathStateActive,
"pathstatedead" => pathStateDead,
"pathstatedisabled" => pathStateDisabled,
"pathstatestandby" => pathStateStandby,
"pathstateunknown" => pathStateUnknown
}
container["hosts"][name]=data
end
end
end

# collect datastores informations
@datacenters.each do |datacenter|
datacenter.datastore.grep(RbVmomi::VIM::Datastore).each do |datastore|
name = datastore.name.gsub(/:/,"-")
vmlist =[]
datastore.vm.grep(RbVmomi::VIM::VirtualMachine).each {|v| vmlist << v.name }
data = {
"name" => datastore.name,
"capacity" => datastore.summary.capacity,
"capacityfree" => datastore.summary.freeSpace,
"capacityused" => datastore.summary.capacity - datastore.summary.freeSpace,
"capacityusedpercent" => 1 - (datastore.summary.freeSpace.to_f / datastore.summary.capacity.to_f),
"accessible" => datastore.summary.accessible,
"maintenancemode" => datastore.summary.maintenanceMode,
"type" => datastore.summary.type,
"vmcount" => ((vmlist.join(', ')).split(",")).count,
"vmlist" => vmlist.join(', ')
}
container["datastores"][name]=data
end
end

# collect virtualmachines informations
@serviceContent.CreateContainerView({:container => @rootFolder ,:type => ['VirtualMachine'], :recursive => true}).view.each do |vm|
name = vm.name.gsub(/:/,"-")
maxcpuusage = vm.summary.runtime.maxCpuUsage*1000000 unless vm.summary.runtime.maxCpuUsage.nil?
percentcpuusage = vm.summary.quickStats.overallCpuUsage.to_f / vm.summary.runtime.maxCpuUsage.to_f unless vm.summary.runtime.maxCpuUsage.nil?
data = {
"name" => vm.name,
"runninghost" => vm.runtime.host.name,
"powerstate" => vm.summary.runtime.powerState,
"toolsinstallermounted" => vm.summary.runtime.toolsInstallerMounted,
"consolidationeeded" => vm.summary.runtime.consolidationNeeded,
"cleanpoweroff" => vm.summary.runtime.cleanPowerOff,
"boottime" => vm.summary.runtime.bootTime,
"guestfullname" => vm.summary.guest.guestFullName,
"hostname" => vm.summary.guest.hostName,
"ipaddress" => vm.summary.guest.ipAddress,
"vmwaretools" => vm.summary.guest.toolsVersionStatus2,
"maxcpuusage" => maxcpuusage,
"overallcpuusage" => vm.summary.quickStats.overallCpuUsage*1000000,
"percentcpuusage" => percentcpuusage,
"numcpu" => vm.summary.config.numCpu,
"memorysize" => vm.summary.config.memorySizeMB*1024*1024,
"hostmemoryusage" => vm.summary.quickStats.hostMemoryUsage*1024*1024,
"guestmemoryusage" => vm.summary.quickStats.guestMemoryUsage*1024*1024,
"balloonedmemory" => vm.summary.quickStats.balloonedMemory*1024*1024,
"percentmemoryusage" => vm.summary.quickStats.hostMemoryUsage.to_f / vm.summary.config.memorySizeMB.to_f,
"uncommittedstorage" => vm.summary.storage.uncommitted,
"usedstorage" => vm.summary.storage.committed,
"provisionedstorage" => vm.summary.storage.uncommitted + vm.summary.storage.committed,
"percentusedstorage" => vm.summary.storage.committed.to_f / (vm.summary.storage.uncommitted + vm.summary.storage.committed).to_f,
"unsharedstorage" => vm.summary.storage.unshared,
"storagelocation" => vm.summary.config.vmPathName,
"uptime" => vm.summary.quickStats.uptimeSeconds,
"overallstatus" => vm.summary.overallStatus
}
container["virtualmachines"][name]=data
end

# log that data collection is terminated
@logger.info("#{@host}: data collection ended (#{(Time.now - beginning_time).to_i} seconds)")

@vim.serviceContent.about.props.each do |key,value|
container["viserver"]["#{key.to_s.downcase}"] = value
end
# set api serial number
container["viserver"]["timestampcheck"] = "#{Time.now.utc.to_i}"

# set api status
container["viserver"]["status"] = "OK"

# return data
return container
end

end

0 comments on commit a543588

Please sign in to comment.
You can’t perform that action at this time.