diff --git a/install.sh b/install.sh index 422f501fdf..14261e391e 100755 --- a/install.sh +++ b/install.sh @@ -1226,6 +1226,7 @@ IM_PROBES_LIB_FILES="\ src/im_mad/remotes/lib/vcenter_monitor.rb \ src/im_mad/remotes/lib/vcenter_cluster.rb \ src/im_mad/remotes/lib/monitord_client.rb \ + src/im_mad/remotes/lib/vcenter_monitor_vms.rb \ src/im_mad/remotes/lib/domain.rb \ src/im_mad/remotes/lib/process_list.rb" diff --git a/share/linters/.rubocop.yml b/share/linters/.rubocop.yml index fb42f111f8..0811bfc3b2 100644 --- a/share/linters/.rubocop.yml +++ b/share/linters/.rubocop.yml @@ -87,6 +87,7 @@ AllCops: - src/im_mad/remotes/one.d/poll - src/im_mad/remotes/az.d/poll - src/im_mad/remotes/lib/vcenter_cluster.rb + - src/im_mad/remotes/lib/vcenter_monitor_vms.rb - src/vnm_mad/remotes/ovswitch/post - src/vnm_mad/remotes/ovswitch/clean - src/vnm_mad/remotes/ovswitch/pre diff --git a/src/im_mad/remotes/lib/vcenter_cluster.rb b/src/im_mad/remotes/lib/vcenter_cluster.rb index 66d1043ff3..add5ad2297 100644 --- a/src/im_mad/remotes/lib/vcenter_cluster.rb +++ b/src/im_mad/remotes/lib/vcenter_cluster.rb @@ -17,6 +17,7 @@ require 'opennebula' require 'vcenter_driver' require 'logger' +require 'open3' def unindent(s) m = s.match(/^(\s*)/) @@ -636,7 +637,8 @@ class Cluster # 'ones' : VMs in OpenNebula #--------------------------------------------------------------------------- def vms_info(vm_type) - str_info , _ltime = @cluster.monitor_vms(@host.id, vm_type) + cmd = "#{File.dirname(__FILE__)}/vcenter_monitor_vms.rb #{@host.id} #{vm_type}" + str_info, _stderr, _status = Open3.capture3(cmd) return str_info end diff --git a/src/im_mad/remotes/lib/vcenter_monitor_vms.rb b/src/im_mad/remotes/lib/vcenter_monitor_vms.rb new file mode 100644 index 0000000000..482e33b9c6 --- /dev/null +++ b/src/im_mad/remotes/lib/vcenter_monitor_vms.rb @@ -0,0 +1,64 @@ +#!/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. # +# ---------------------------------------------------------------------------- # + +ONE_LOCATION ||= ENV['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) + $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ } + require 'rubygems' + Gem.use_paths(File.realpath(GEMS_LOCATION)) +end + +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) + +require 'vcenter_driver' + +CONFIG = VCenterConf.new + +host_id = ARGV[0] +vm_type = ARGV[1] + +begin + vi_client = VCenterDriver::VIClient.new_from_host(host_id) + + cluster = Cluster.new(host_id, vi_client) + + str_info , _ltime = cluster.monitor_vms(@host.id, vm_type) + + puts str_info +rescue StandardError => e + message = "Monitoring of VM #{vm_id} on vCenter cluster #{cluster_name} " \ + " failed due to \"#{e.message}\"." + OpenNebula.log_error(message) + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + + exit(-1) +ensure + vi_client.close_connection if vi_client +end