mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-23 13:15:14 +09:00
* Modification to state vms works * Merge ClusterMonitor, VirtualMachineMonitor and DatastoreMonitor into VcenterMonitor class * Change vcenter.d files to links * Add vcenter into LOCAL_HYPERVISORS in common file. * Fix typos * Move vm_to_one from vm_template to vcenter monitor lib
This commit is contained in:
1 parent
8cd4b04ff8
commit
72ce344399
7 files changed
+274
-539
No files matched your search
@@ -217,7 +217,7 @@ end
|
||||
#-------------------------------------------------------------------------------
|
||||
# Script helper functions and gLobals
|
||||
#-------------------------------------------------------------------------------
|
||||
LOCAL_HYPERVISOR = %w[az ec2 one packet].freeze
|
||||
LOCAL_HYPERVISOR = %w[az ec2 one packet vcenter].freeze
|
||||
|
||||
def local?(hypervisor)
|
||||
LOCAL_HYPERVISOR.include?(hypervisor)
|
||||
|
||||
@@ -37,13 +37,13 @@ require 'opennebula'
|
||||
require 'vcenter_driver'
|
||||
|
||||
# Gather vCenter cluster monitor info
|
||||
class ClusterMonitor
|
||||
class VcenterMonitor
|
||||
|
||||
POLL_ATTRIBUTE = OpenNebula::VirtualMachine::Driver::POLL_ATTRIBUTE
|
||||
VM_STATE = OpenNebula::VirtualMachine::Driver::VM_STATE
|
||||
|
||||
def initialize(vi_client, host_id)
|
||||
@vi_client = vi_client
|
||||
def initialize(host_id)
|
||||
@vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
||||
# Get CCR reference
|
||||
client = OpenNebula::Client.new
|
||||
host = OpenNebula::Host.new_with_id(host_id, client)
|
||||
@@ -60,7 +60,7 @@ class ClusterMonitor
|
||||
.new_from_ref(ccr_ref, @vi_client)
|
||||
end
|
||||
|
||||
def monitor_cluster
|
||||
def monitor_clusters
|
||||
total_cpu,
|
||||
num_cpu_cores,
|
||||
effective_cpu,
|
||||
@@ -489,7 +489,7 @@ class ClusterMonitor
|
||||
# if the machine does not exist in opennebula
|
||||
# it means that is a wild:
|
||||
unless @vm.one_exist?
|
||||
vm_template64 = Base64.encode64(@vm.vm_to_one(vm_name))
|
||||
vm_template64 = Base64.encode64(vm_to_one(vm_name))
|
||||
.gsub("\n", '')
|
||||
vm_info << 'VCENTER_TEMPLATE="YES",'
|
||||
vm_info << "IMPORT_TEMPLATE=\"#{vm_template64}\","
|
||||
@@ -801,6 +801,95 @@ class ClusterMonitor
|
||||
end
|
||||
end
|
||||
|
||||
def vm_to_one(vm_name)
|
||||
str = "NAME = \"#{vm_name}\"\n"\
|
||||
"CPU = \"#{@vm.vm_info["config.hardware.numCPU"]}\"\n"\
|
||||
"vCPU = \"#{@vm.vm_info["config.hardware.numCPU"]}\"\n"\
|
||||
"MEMORY = \"#{@vm.vm_info["config.hardware.memoryMB"]}\"\n"\
|
||||
"HYPERVISOR = \"vcenter\"\n"\
|
||||
"CONTEXT = [\n"\
|
||||
" NETWORK = \"YES\",\n"\
|
||||
" SSH_PUBLIC_KEY = \"$USER[SSH_PUBLIC_KEY]\"\n"\
|
||||
"]\n"\
|
||||
"VCENTER_INSTANCE_ID =\"#{@vm.vm_info[:vc_uuid]}\"\n"\
|
||||
"VCENTER_CCR_REF =\"#{@vm.vm_info[:cluster_ref]}\"\n"
|
||||
|
||||
str << "IMPORT_VM_ID =\"#{@vm["_ref"]}\"\n"
|
||||
str << "DEPLOY_ID =\"#{@vm["_ref"]}\"\n"
|
||||
# @state = 'POWEROFF' if @state == 'd'
|
||||
@state = vm_state(@vm['summary.runtime.powerState'])
|
||||
str << "IMPORT_STATE =\"#{@state}\"\n"
|
||||
|
||||
# Get DS information
|
||||
if !@vm.vm_info["datastore"].nil?
|
||||
!@vm.vm_info["datastore"].last.nil? &&
|
||||
!@vm.vm_info["datastore"].last._ref.nil?
|
||||
ds_ref = @vm.vm_template_ds_ref
|
||||
str << "VCENTER_DS_REF = \"#{ds_ref}\"\n"
|
||||
end
|
||||
|
||||
vnc_port = nil
|
||||
keymap = VCenterDriver::VIHelper.get_default("VM/TEMPLATE/GRAPHICS/KEYMAP")
|
||||
|
||||
@vm.vm_info["config.extraConfig"].select do |xtra|
|
||||
if xtra[:key].downcase=="remotedisplay.vnc.port"
|
||||
vnc_port = xtra[:value]
|
||||
end
|
||||
|
||||
if xtra[:key].downcase=="remotedisplay.vnc.keymap"
|
||||
keymap = xtra[:value]
|
||||
end
|
||||
end
|
||||
|
||||
if !@vm.vm_info["config.extraConfig"].empty?
|
||||
str << "GRAPHICS = [\n"\
|
||||
" TYPE =\"vnc\",\n"
|
||||
str << " PORT =\"#{vnc_port}\",\n" if vnc_port
|
||||
str << " KEYMAP =\"#{keymap}\",\n" if keymap
|
||||
str << " LISTEN =\"0.0.0.0\"\n"
|
||||
str << "]\n"
|
||||
end
|
||||
|
||||
if !@vm.vm_info["config.annotation"] || @vm.vm_info["config.annotation"].empty?
|
||||
str << "DESCRIPTION = \"vCenter Template imported by OpenNebula" \
|
||||
" from Cluster #{@vm.vm_info["cluster_name"]}\"\n"
|
||||
else
|
||||
notes = @vm.vm_info["config.annotation"].gsub("\\", "\\\\").gsub("\"", "\\\"")
|
||||
str << "DESCRIPTION = \"#{notes}\"\n"
|
||||
end
|
||||
|
||||
case @vm.vm_info["guest.guestFullName"]
|
||||
when /CentOS/i
|
||||
str << "LOGO=images/logos/centos.png\n"
|
||||
when /Debian/i
|
||||
str << "LOGO=images/logos/debian.png\n"
|
||||
when /Red Hat/i
|
||||
str << "LOGO=images/logos/redhat.png\n"
|
||||
when /Ubuntu/i
|
||||
str << "LOGO=images/logos/ubuntu.png\n"
|
||||
when /Windows XP/i
|
||||
str << "LOGO=images/logos/windowsxp.png\n"
|
||||
when /Windows/i
|
||||
str << "LOGO=images/logos/windows8.png\n"
|
||||
when /Linux/i
|
||||
str << "LOGO=images/logos/linux.png\n"
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
# Return Datastores information
|
||||
def monitor_datastores
|
||||
dc = @cluster.get_dc
|
||||
ds_folder = dc.datastore_folder
|
||||
items = ds_folder.fetch!
|
||||
monitor = ''
|
||||
items.each do |ref, ds|
|
||||
monitor << "VCENTER_DS_REF=\"#{ref}\"\n"
|
||||
end
|
||||
monitor
|
||||
end
|
||||
|
||||
# Get NSX Manager info detected from vCenter side.
|
||||
def nsx_info
|
||||
nsx_info = ''
|
||||
@@ -838,20 +927,22 @@ class ClusterMonitor
|
||||
nsx_info
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Gather VMs monitor info
|
||||
class VirtualMachineMonitor
|
||||
|
||||
POLL_ATTRIBUTE = OpenNebula::VirtualMachine::Driver::POLL_ATTRIBUTE
|
||||
VM_STATE = OpenNebula::VirtualMachine::Driver::VM_STATE
|
||||
|
||||
def initialize(vi_client, vm)
|
||||
@vi_client = vi_client
|
||||
host_id = vm['HISTORY_RECORDS/HISTORY[last()]/HID']
|
||||
@one_vm = vm
|
||||
|
||||
@vm = VCenterDriver::VirtualMachine.new_from_ref(@vi_client, vm['DEPLOY_ID'], vm['NAME'])
|
||||
# Converts the VI string state to OpenNebula state convention
|
||||
# Guest states are:
|
||||
# - poweredOff The virtual machine is currently powered off.
|
||||
# - poweredOn The virtual machine is currently powered on.
|
||||
# - suspended The virtual machine is currently suspended.
|
||||
def vm_state(state)
|
||||
case state
|
||||
when 'poweredOn'
|
||||
'RUNNING'
|
||||
when 'suspended'
|
||||
'POWEROFF'
|
||||
when 'poweredOff'
|
||||
'POWEROFF'
|
||||
else
|
||||
'UNKNOWN'
|
||||
end
|
||||
end
|
||||
|
||||
# monitor function used when VMM poll action is called
|
||||
@@ -862,7 +953,7 @@ class VirtualMachineMonitor
|
||||
|
||||
@state = state_to_c(@vm['summary.runtime.powerState'])
|
||||
|
||||
if @state != OpenNebula::VirtualMachine::Driver::VM_STATE[:active]
|
||||
if @state != VM_STATE[:active]
|
||||
reset_monitor
|
||||
return
|
||||
end
|
||||
@@ -1041,41 +1132,120 @@ class VirtualMachineMonitor
|
||||
(write_kbpersec * 1024 * refresh_rate).to_i
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Gather datastore info
|
||||
class DatastoreMonitor
|
||||
|
||||
def initialize(vi_client, host_id)
|
||||
@vi_client = vi_client
|
||||
def vms(host_id)
|
||||
vc_uuid = @vi_client.vim.serviceContent.about.instanceUuid
|
||||
# Get CCR reference
|
||||
client = OpenNebula::Client.new
|
||||
host = OpenNebula::Host.new_with_id(host_id, client)
|
||||
rc = host.info
|
||||
if OpenNebula::is_error? rc
|
||||
if OpenNebula.is_error? rc
|
||||
STDERR.puts rc.message
|
||||
exit 1
|
||||
end
|
||||
|
||||
ccr_ref = host['TEMPLATE/VCENTER_CCR_REF']
|
||||
|
||||
vm_pool = VCenterDriver::VIHelper
|
||||
.one_pool(OpenNebula::VirtualMachinePool)
|
||||
|
||||
# opts common to all vms
|
||||
opts = {
|
||||
pool: vm_pool,
|
||||
vc_uuid: vc_uuid
|
||||
}
|
||||
|
||||
# Get vCenter Cluster
|
||||
@cluster = VCenterDriver::ClusterComputeResource
|
||||
.new_from_ref(ccr_ref, @vi_client)
|
||||
.new_from_ref(ccr_ref, @vi_client)
|
||||
|
||||
# Print Datastore information
|
||||
dc = @cluster.get_dc
|
||||
ds_folder = dc.datastore_folder
|
||||
@items = ds_folder.fetch!
|
||||
puts monitor
|
||||
end
|
||||
@monitored_vms = Set.new
|
||||
|
||||
def monitor
|
||||
monitor = ''
|
||||
@items.each do |ref, ds|
|
||||
monitor << "VCENTER_DS_REF=\"#{ref}\"\n"
|
||||
# View for VMs inside this cluster
|
||||
view = @vi_client.vim.serviceContent.viewManager
|
||||
.CreateContainerView({ container: @cluster.item,
|
||||
type: ['VirtualMachine'],
|
||||
recursive: true })
|
||||
|
||||
pc = @vi_client.vim.serviceContent.propertyCollector
|
||||
|
||||
monitored_properties = [
|
||||
'name', # VM name
|
||||
'summary.runtime.powerState', # VM power state
|
||||
'config.extraConfig' # VM extraconfig info.. opennebula.vm.running
|
||||
]
|
||||
|
||||
filter_spec = RbVmomi::VIM.PropertyFilterSpec(
|
||||
:objectSet => [
|
||||
:obj => view,
|
||||
:skip => true,
|
||||
:selectSet => [
|
||||
RbVmomi::VIM.TraversalSpec(
|
||||
:name => 'traverseEntities',
|
||||
:type => 'ContainerView',
|
||||
:path => 'view',
|
||||
:skip => false
|
||||
)
|
||||
]
|
||||
],
|
||||
:propSet => [
|
||||
{ :type => 'VirtualMachine', :pathSet => monitored_properties }
|
||||
]
|
||||
)
|
||||
|
||||
result = pc.RetrieveProperties(:specSet => [filter_spec])
|
||||
|
||||
vms = []
|
||||
result.each do |r|
|
||||
# uuid
|
||||
uuid = r.obj._ref + vc_uuid
|
||||
# Next if it's not a VirtualMachine
|
||||
next unless r.obj.is_a?(RbVmomi::VIM::VirtualMachine)
|
||||
|
||||
# Next if it's a template
|
||||
next if r.obj.config.template
|
||||
|
||||
one_vm = VCenterDriver::VirtualMachine.new_from_ref(@vi_client,
|
||||
r.obj._ref,
|
||||
r.obj.name,
|
||||
opts = {})
|
||||
|
||||
vm = {
|
||||
:uuid => uuid,
|
||||
:id => one_vm.vm_id || -1,
|
||||
:name => r.obj._ref,
|
||||
:type => 'vcenter',
|
||||
:state => vm_state(r.obj.summary.runtime.powerState)
|
||||
}
|
||||
|
||||
vms << vm
|
||||
end
|
||||
monitor
|
||||
|
||||
view.DestroyView # Destroy the view
|
||||
vms
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
############################################################################
|
||||
# Module Interface
|
||||
# Interface for probe_db - VirtualMachineDB
|
||||
############################################################################
|
||||
module DomainList
|
||||
|
||||
def self.state_info(name, id)
|
||||
vmm = VcenterMonitor.new(id)
|
||||
|
||||
vms = vmm.vms(id)
|
||||
info = {}
|
||||
vms.each do |vm|
|
||||
info[vm[:uuid]] = { :id => vm[:id],
|
||||
:uuid => vm[:uuid],
|
||||
:name => vm[:name],
|
||||
:state => vm[:state],
|
||||
:hyperv => 'vcenter' }
|
||||
end
|
||||
info
|
||||
end
|
||||
|
||||
end
|
||||
@@ -16,21 +16,36 @@
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
ONE_LOCATION = ENV['ONE_LOCATION'] unless defined? ONE_LOCATION
|
||||
|
||||
if !ONE_LOCATION
|
||||
RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
|
||||
GEMS_LOCATION ||= '/usr/share/one/gems'
|
||||
else
|
||||
RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
|
||||
GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
|
||||
end
|
||||
|
||||
if File.directory?(GEMS_LOCATION)
|
||||
Gem.use_paths(GEMS_LOCATION)
|
||||
$LOAD_PATH.reject! {|l| l =~ /(vendor|site)_ruby/ }
|
||||
end
|
||||
|
||||
$LOAD_PATH << RUBY_LIB_LOCATION
|
||||
|
||||
require_relative '../../../lib/vcenter.rb'
|
||||
|
||||
host = ARGV[-1]
|
||||
host_id = ARGV[-2]
|
||||
|
||||
begin
|
||||
# Vcenter connection
|
||||
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
||||
# Cluster Monitoring
|
||||
cm = ClusterMonitor.new(vi_client, host_id)
|
||||
puts cm.monitor_cluster
|
||||
puts cm.monitor_host_systems
|
||||
# VCenter Monitoring object
|
||||
vcm = VcenterMonitor.new(host_id)
|
||||
puts vcm.monitor_clusters
|
||||
puts vcm.monitor_host_systems
|
||||
# Retrieve customizations
|
||||
begin
|
||||
puts cm.monitor_customizations
|
||||
puts vcm.monitor_customizations
|
||||
rescue StandardError
|
||||
# Do not break monitoring on customization error
|
||||
puts 'ERROR="Customizations could not be retrieved,' \
|
||||
@@ -38,10 +53,10 @@ begin
|
||||
end
|
||||
|
||||
# Get NSX info detected from vCenter Server
|
||||
puts cm.nsx_info
|
||||
puts vcm.nsx_info
|
||||
|
||||
# Print VM monitor info
|
||||
vm_monitor_info, last_perf_poll = cm.monitor_vms(host_id)
|
||||
# VM monitor info
|
||||
vm_monitor_info, last_perf_poll = vcm.monitor_vms(host_id)
|
||||
if !vm_monitor_info.empty?
|
||||
puts "VM_POLL=YES"
|
||||
puts vm_monitor_info
|
||||
@@ -51,7 +66,7 @@ begin
|
||||
puts "VCENTER_LAST_PERF_POLL=" << last_perf_poll << "\n" if last_perf_poll
|
||||
|
||||
# Datastore Monitoring
|
||||
dm = DatastoreMonitor.new(vi_client, host_id)
|
||||
puts vcm.monitor_datastores
|
||||
|
||||
rescue StandardError => e
|
||||
STDERR.puts "IM poll for vcenter cluster #{host_id} failed due to "\
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2019, OpenNebula Project, OpenNebula Systems #
|
||||
# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems #
|
||||
# #
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
||||
# not use this file except in compliance with the License. You may obtain #
|
||||
@@ -14,4 +15,36 @@
|
||||
# See the License for the specific language governing permissions and #
|
||||
# limitations under the License. #
|
||||
# -------------------------------------------------------------------------- #
|
||||
exit 0
|
||||
|
||||
ONE_LOCATION = ENV['ONE_LOCATION'] unless defined? ONE_LOCATION
|
||||
|
||||
if !ONE_LOCATION
|
||||
RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
|
||||
GEMS_LOCATION ||= '/usr/share/one/gems'
|
||||
else
|
||||
RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
|
||||
GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
|
||||
end
|
||||
|
||||
if File.directory?(GEMS_LOCATION)
|
||||
Gem.use_paths(GEMS_LOCATION)
|
||||
$LOAD_PATH.reject! {|l| l =~ /(vendor|site)_ruby/ }
|
||||
end
|
||||
|
||||
$LOAD_PATH << RUBY_LIB_LOCATION
|
||||
|
||||
require_relative '../../../lib/vcenter'
|
||||
require_relative '../../../lib/probe_db'
|
||||
|
||||
host = ARGV[-1]
|
||||
host_id = ARGV[-2]
|
||||
|
||||
begin
|
||||
vmdb = VirtualMachineDB.new('vcenter',
|
||||
:missing_state => 'UNKNOWN',
|
||||
:sync => 180)
|
||||
vmdb.purge
|
||||
puts vmdb.to_status(host, host_id)
|
||||
rescue StandardError => e
|
||||
OpenNebula.handle_driver_exception('im probe_vm_status', e, host)
|
||||
end
|
||||
@@ -1,332 +0,0 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems #
|
||||
# #
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
||||
# not use this file except in compliance with the License. You may obtain #
|
||||
# a copy of the License at #
|
||||
# #
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 #
|
||||
# #
|
||||
# Unless required by applicable law or agreed to in writing, software #
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, #
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
|
||||
# See the License for the specific language governing permissions and #
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
require 'socket'
|
||||
require 'base64'
|
||||
require 'resolv'
|
||||
require 'ipaddr'
|
||||
require 'zlib'
|
||||
require 'yaml'
|
||||
require 'open3'
|
||||
require 'openssl'
|
||||
|
||||
require 'rexml/document'
|
||||
|
||||
require_relative '../lib/probe_db'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# This class represents a monitord client. It handles udp and tcp connections
|
||||
# and send update messages to monitord
|
||||
#-------------------------------------------------------------------------------
|
||||
class MonitorClient
|
||||
|
||||
# Defined in src/monitor/include/MonitorDriverMessages.h
|
||||
MESSAGE_TYPES = %w[MONITOR_VM MONITOR_HOST SYSTEM_HOST BEACON_HOST STATE_VM
|
||||
START_MONITOR STOP_MONITOR].freeze
|
||||
|
||||
MESSAGE_STATUS = { true =>'SUCCESS', false => 'FAILURE' }.freeze
|
||||
|
||||
MESSAGE_TYPES.each do |mt|
|
||||
define_method("#{mt}_udp".downcase.to_sym) do |rc, payload|
|
||||
msg = "#{mt} #{MESSAGE_STATUS[rc]} #{@hostid} #{pack(payload)}"
|
||||
@socket_udp.send(msg, 0)
|
||||
end
|
||||
end
|
||||
|
||||
MESSAGE_TYPES.each do |mt|
|
||||
define_method("#{mt}_tcp".downcase.to_sym) do |rc, payload|
|
||||
msg = "#{mt} #{MESSAGE_STATUS[rc]} #{@hostid} #{pack(payload)}"
|
||||
|
||||
socket_tcp = TCPSocket.new(@host, @port)
|
||||
socket_tcp.send(msg, 0)
|
||||
socket_tcp.close
|
||||
end
|
||||
end
|
||||
|
||||
# Options to create a monitord client
|
||||
# :host [:String] to send the messages to
|
||||
# :port [:String] of monitord server
|
||||
# :hostid [:String] OpenNebula ID of this host
|
||||
# :pubkey [:String] public key to encrypt messages
|
||||
def initialize(server, port, id, opt = {})
|
||||
@opts = {
|
||||
:pubkey => ''
|
||||
}.merge opt
|
||||
|
||||
addr = Socket.getaddrinfo(server, port)[0]
|
||||
|
||||
@family = addr[0]
|
||||
@host = addr[3]
|
||||
@port = addr[1]
|
||||
|
||||
@socket_udp = UDPSocket.new(@family)
|
||||
@socket_udp.connect(@host, @port)
|
||||
|
||||
@pubkey = @opts[:pubkey]
|
||||
|
||||
@hostid = id
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Formats message payload to send over the wire
|
||||
def pack(data)
|
||||
if @pubkey
|
||||
block_size = @pubkey.n.num_bytes - 11
|
||||
|
||||
edata = ''
|
||||
index = 0
|
||||
|
||||
loop do
|
||||
break if index >= data.length
|
||||
|
||||
edata << @pubkey.public_encrypt(data[index, block_size])
|
||||
|
||||
index += block_size
|
||||
end
|
||||
|
||||
data = edata
|
||||
end
|
||||
|
||||
zdata = Zlib::Deflate.deflate(data, Zlib::BEST_COMPRESSION)
|
||||
data64 = Base64.strict_encode64(zdata)
|
||||
|
||||
data64
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# This class wraps the execution of a probe directory and sends data to
|
||||
# monitord (optionally)
|
||||
#-------------------------------------------------------------------------------
|
||||
class ProbeRunner
|
||||
|
||||
def initialize(hyperv, path, stdin)
|
||||
@path = File.join(File.dirname(__FILE__), '..', "#{hyperv}-probes.d",
|
||||
path)
|
||||
@stdin = stdin
|
||||
end
|
||||
|
||||
# Run the probes once
|
||||
# @return[Array] rc, data. rc 0 for success and data is the output of
|
||||
# probes. If rc is -1 it signal failure and data is the error message of
|
||||
# the failing probe
|
||||
#
|
||||
# rubocop:disable Lint/SuppressedException
|
||||
def run_probes
|
||||
data = ''
|
||||
dpro = Dir.new(@path)
|
||||
|
||||
dpro.each do |probe|
|
||||
probe_path = File.join(@path, probe)
|
||||
|
||||
next unless File.file?(probe_path)
|
||||
next unless File.executable?(probe_path)
|
||||
|
||||
cmd = "#{probe_path} #{ARGV.join(' ')}"
|
||||
|
||||
o_, e_, s_ = Open3.popen3(cmd) do |i, o, e, t|
|
||||
out_reader = Thread.new { o.read }
|
||||
err_reader = Thread.new { e.read }
|
||||
|
||||
begin
|
||||
i.write @stdin
|
||||
rescue Errno::EPIPE
|
||||
end
|
||||
|
||||
i.close
|
||||
|
||||
out = out_reader.value
|
||||
err = err_reader.value
|
||||
rc = t.value
|
||||
|
||||
begin
|
||||
Process.waitpid(rc.pid)
|
||||
rescue Errno::ECHILD
|
||||
end
|
||||
|
||||
[out, err, rc]
|
||||
end
|
||||
|
||||
data += o_
|
||||
|
||||
return [-1, "Error executing #{probe}: #{e_}"] if s_.exitstatus != 0
|
||||
end
|
||||
|
||||
[0, data]
|
||||
end
|
||||
# rubocop:enable Lint/SuppressedException
|
||||
|
||||
# Singleton call for run_probes method
|
||||
def self.run_once(hyperv, path, stdin)
|
||||
runner = ProbeRunner.new(hyperv, path, stdin)
|
||||
runner.run_probes
|
||||
end
|
||||
|
||||
# Executes the probes in the directory in a loop. The block is called after
|
||||
# each execution to optionally send the data to monitord
|
||||
def self.monitor_loop(hyperv, path, period, stdin, &block)
|
||||
# Failure retries, simple exponential backoff
|
||||
sfail = [1, 1, 1, 2, 4, 8, 8, 16, 32, 64]
|
||||
nfail = 0
|
||||
|
||||
runner = ProbeRunner.new(hyperv, path, stdin)
|
||||
|
||||
loop do
|
||||
sleep_time = 0
|
||||
|
||||
ts = Time.now
|
||||
|
||||
rc, data = runner.run_probes
|
||||
|
||||
begin
|
||||
block.call(rc, data)
|
||||
|
||||
run_time = (Time.now - ts).to_i
|
||||
sleep_time = (period.to_i - run_time) if period.to_i > run_time
|
||||
|
||||
nfail = 0
|
||||
rescue StandardError
|
||||
sleep_time = sfail[nfail]
|
||||
|
||||
nfail += 1 if nfail < sfail.length - 1
|
||||
end
|
||||
|
||||
sleep(sleep_time) if sleep_time > 0
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Script helper functions and gLobals
|
||||
#-------------------------------------------------------------------------------
|
||||
LOCAL_HYPERVISOR = %w[az ec2 one packet vcenter].freeze
|
||||
|
||||
def local?(hypervisor)
|
||||
LOCAL_HYPERVISOR.include?(hypervisor)
|
||||
end
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Configuration (from monitord)
|
||||
#-------------------------------------------------------------------------------
|
||||
xml_txt = STDIN.read
|
||||
|
||||
begin
|
||||
hyperv = ARGV[0].split(' ')[0]
|
||||
|
||||
xml_txt = Base64.decode64(xml_txt) if local? hyperv
|
||||
config = REXML::Document.new(xml_txt).root
|
||||
|
||||
host = config.elements['UDP_LISTENER/MONITOR_ADDRESS'].text.to_s
|
||||
port = config.elements['UDP_LISTENER/PORT'].text.to_s
|
||||
pubkey = config.elements['UDP_LISTENER/PUBKEY'].text.to_s
|
||||
hostid = config.elements['HOST_ID'].text.to_s
|
||||
|
||||
probes = {
|
||||
:system_host_udp => {
|
||||
:period => config.elements['PROBES_PERIOD/SYSTEM_HOST'].text.to_s,
|
||||
:path => 'host/system'
|
||||
},
|
||||
|
||||
:monitor_host_udp => {
|
||||
:period => config.elements['PROBES_PERIOD/MONITOR_HOST'].text.to_s,
|
||||
:path => 'host/monitor'
|
||||
},
|
||||
|
||||
:state_vm_tcp => {
|
||||
:period => config.elements['PROBES_PERIOD/STATE_VM'].text.to_s,
|
||||
:path => 'vm/status'
|
||||
},
|
||||
|
||||
:monitor_vm_udp => {
|
||||
:period => config.elements['PROBES_PERIOD/MONITOR_VM'].text.to_s,
|
||||
:path => 'vm/monitor'
|
||||
},
|
||||
|
||||
:beacon_host_udp => {
|
||||
:period => config.elements['PROBES_PERIOD/BEACON_HOST'].text.to_s,
|
||||
:path => 'host/beacon'
|
||||
}
|
||||
}
|
||||
|
||||
if !pubkey.empty?
|
||||
exp = /(-+BEGIN RSA PUBLIC KEY-+)([^-]*)(-+END RSA PUBLIC KEY-+)/
|
||||
m = pubkey.match(exp)
|
||||
|
||||
if !m
|
||||
puts 'Public key not in PEM format'
|
||||
exit(-1)
|
||||
end
|
||||
|
||||
pktxt = m[2].strip.tr(' ', "\n")
|
||||
|
||||
pubkey = OpenSSL::PKey::RSA.new "-----BEGIN RSA PUBLIC KEY-----\n" \
|
||||
"#{pktxt}\n-----END RSA PUBLIC KEY-----"
|
||||
else
|
||||
pubkey = nil
|
||||
end
|
||||
rescue StandardError => e
|
||||
puts e.inspect
|
||||
exit(-1)
|
||||
end
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Run configuration probes and send information to monitord
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
client = MonitorClient.new(host, port, hostid, :pubkey => pubkey)
|
||||
|
||||
rc, dt = ProbeRunner.run_once(hyperv, probes[:system_host_udp][:path], xml_txt)
|
||||
|
||||
puts dt
|
||||
|
||||
STDOUT.flush
|
||||
|
||||
exit(-1) if rc == -1
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Start monitor threads and shepherd
|
||||
#-------------------------------------------------------------------------------
|
||||
Process.setsid
|
||||
|
||||
STDIN.close
|
||||
|
||||
_rd, wr = IO.pipe
|
||||
|
||||
STDOUT.reopen(wr)
|
||||
STDERR.reopen(wr)
|
||||
|
||||
threads = []
|
||||
|
||||
probes.each do |msg_type, conf|
|
||||
threads << Thread.new do
|
||||
ProbeRunner.monitor_loop(hyperv,
|
||||
conf[:path],
|
||||
conf[:period],
|
||||
xml_txt) do |result, da|
|
||||
da.strip!
|
||||
next if da.empty?
|
||||
|
||||
client.send(msg_type, result == 0, da)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
threads.each {|thr| thr.join }
|
||||
@@ -0,0 +1 @@
|
||||
../common.d/monitord-client.rb
|
||||
@@ -1,77 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems #
|
||||
# #
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
||||
# not use this file except in compliance with the License. You may obtain #
|
||||
# a copy of the License at #
|
||||
# #
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 #
|
||||
# #
|
||||
# Unless required by applicable law or agreed to in writing, software #
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, #
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
|
||||
# See the License for the specific language governing permissions and #
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
#--------------------------------------------------------------------------- #
|
||||
# Process Arguments
|
||||
#--------------------------------------------------------------------------- #
|
||||
ACTION="start"
|
||||
|
||||
if [ "$1" = "stop" ]; then
|
||||
shift
|
||||
ACTION="stop"
|
||||
fi
|
||||
|
||||
ARGV=$*
|
||||
|
||||
HID=$2
|
||||
|
||||
STDIN=`cat -`
|
||||
|
||||
# Directory that contains this file
|
||||
DIR=$(pwd)
|
||||
|
||||
# Basename
|
||||
BASENAME=$(basename $0 _control.sh)
|
||||
|
||||
# Collectd client (Ruby)
|
||||
CLIENT=$DIR/${BASENAME}.rb
|
||||
|
||||
# Collectd client PID
|
||||
CLIENT_PID_FILE=/tmp/one-monitord-$HID.pid
|
||||
|
||||
# Launch the client
|
||||
function start_client() {
|
||||
rm $CLIENT_PID_FILE >/dev/null 2>&1
|
||||
|
||||
echo "$STDIN" | /usr/bin/env ruby $CLIENT $ARGV &
|
||||
|
||||
echo $! > $CLIENT_PID_FILE
|
||||
}
|
||||
|
||||
# Stop the client
|
||||
function stop_client() {
|
||||
local pids=$(ps axuww | grep "$CLIENT $ARGV" | grep -v grep | awk '{print $2}')
|
||||
|
||||
if [ -n "$pids" ]; then
|
||||
kill -9 $pids
|
||||
sleep 5
|
||||
fi
|
||||
|
||||
rm -f $CLIENT_PID_FILE
|
||||
}
|
||||
|
||||
case $ACTION in
|
||||
start)
|
||||
stop_client
|
||||
start_client
|
||||
;;
|
||||
|
||||
stop)
|
||||
stop_client
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1 @@
|
||||
../common.d/monitord-client_control.sh
|
||||
@@ -949,82 +949,6 @@ class Template
|
||||
self['runtime.host.name']
|
||||
end
|
||||
|
||||
def vm_to_one(vm_name)
|
||||
str = "NAME = \"#{vm_name}\"\n"\
|
||||
"CPU = \"#{@vm_info["config.hardware.numCPU"]}\"\n"\
|
||||
"vCPU = \"#{@vm_info["config.hardware.numCPU"]}\"\n"\
|
||||
"MEMORY = \"#{@vm_info["config.hardware.memoryMB"]}\"\n"\
|
||||
"HYPERVISOR = \"vcenter\"\n"\
|
||||
"CONTEXT = [\n"\
|
||||
" NETWORK = \"YES\",\n"\
|
||||
" SSH_PUBLIC_KEY = \"$USER[SSH_PUBLIC_KEY]\"\n"\
|
||||
"]\n"\
|
||||
"VCENTER_INSTANCE_ID =\"#{@vm_info[:vc_uuid]}\"\n"\
|
||||
"VCENTER_CCR_REF =\"#{@vm_info[:cluster_ref]}\"\n"
|
||||
|
||||
str << "IMPORT_VM_ID =\"#{self["_ref"]}\"\n"
|
||||
str << "DEPLOY_ID =\"#{self["_ref"]}\"\n"
|
||||
@state = 'POWEROFF' if @state == 'd'
|
||||
str << "IMPORT_STATE =\"#{@state}\"\n"
|
||||
|
||||
# Get DS information
|
||||
if !@vm_info["datastore"].nil?
|
||||
!@vm_info["datastore"].last.nil? &&
|
||||
!@vm_info["datastore"].last._ref.nil?
|
||||
ds_ref = vm_template_ds_ref
|
||||
str << "VCENTER_DS_REF = \"#{ds_ref}\"\n"
|
||||
end
|
||||
|
||||
vnc_port = nil
|
||||
keymap = VCenterDriver::VIHelper.get_default("VM/TEMPLATE/GRAPHICS/KEYMAP")
|
||||
|
||||
@vm_info["config.extraConfig"].select do |xtra|
|
||||
if xtra[:key].downcase=="remotedisplay.vnc.port"
|
||||
vnc_port = xtra[:value]
|
||||
end
|
||||
|
||||
if xtra[:key].downcase=="remotedisplay.vnc.keymap"
|
||||
keymap = xtra[:value]
|
||||
end
|
||||
end
|
||||
|
||||
if !@vm_info["config.extraConfig"].empty?
|
||||
str << "GRAPHICS = [\n"\
|
||||
" TYPE =\"vnc\",\n"
|
||||
str << " PORT =\"#{vnc_port}\",\n" if vnc_port
|
||||
str << " KEYMAP =\"#{keymap}\",\n" if keymap
|
||||
str << " LISTEN =\"0.0.0.0\"\n"
|
||||
str << "]\n"
|
||||
end
|
||||
|
||||
if !@vm_info["config.annotation"] || @vm_info["config.annotation"].empty?
|
||||
str << "DESCRIPTION = \"vCenter Template imported by OpenNebula" \
|
||||
" from Cluster #{@vm_info["cluster_name"]}\"\n"
|
||||
else
|
||||
notes = @vm_info["config.annotation"].gsub("\\", "\\\\").gsub("\"", "\\\"")
|
||||
str << "DESCRIPTION = \"#{notes}\"\n"
|
||||
end
|
||||
|
||||
case @vm_info["guest.guestFullName"]
|
||||
when /CentOS/i
|
||||
str << "LOGO=images/logos/centos.png\n"
|
||||
when /Debian/i
|
||||
str << "LOGO=images/logos/debian.png\n"
|
||||
when /Red Hat/i
|
||||
str << "LOGO=images/logos/redhat.png\n"
|
||||
when /Ubuntu/i
|
||||
str << "LOGO=images/logos/ubuntu.png\n"
|
||||
when /Windows XP/i
|
||||
str << "LOGO=images/logos/windowsxp.png\n"
|
||||
when /Windows/i
|
||||
str << "LOGO=images/logos/windows8.png\n"
|
||||
when /Linux/i
|
||||
str << "LOGO=images/logos/linux.png\n"
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
#Gets MOREF from Datastore used by the VM. It validates
|
||||
#the selected DS is not only used to host swap.
|
||||
def vm_template_ds_ref
|
||||
|
||||
Reference in new issue
Block a user