From b693539a55cd8bc4494f0f76ac9b08c8a15b48b8 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 20 Dec 2016 11:24:32 -0800 Subject: =?UTF-8?q?Move=20the=20=E2=80=9CVirtualization=E2=80=9D=20plugin?= =?UTF-8?q?=20into=20libvirt=20namespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This plugin isn’t actually a virtualization plugin. It’s actually a libvirt plugin. Move the data out of the Virtualization mash and stick it into its own location. Also rename the plugin LibVirt. I need to get a system setup with libvirt to test this and probably tweak it further / add tests. Signed-off-by: Tim Smith --- lib/ohai/plugins/libvirt.rb | 81 +++++++++++++++++++++++++++++++++++++ lib/ohai/plugins/virtualization.rb | 83 -------------------------------------- 2 files changed, 81 insertions(+), 83 deletions(-) create mode 100644 lib/ohai/plugins/libvirt.rb delete mode 100644 lib/ohai/plugins/virtualization.rb diff --git a/lib/ohai/plugins/libvirt.rb b/lib/ohai/plugins/libvirt.rb new file mode 100644 index 00000000..46587b01 --- /dev/null +++ b/lib/ohai/plugins/libvirt.rb @@ -0,0 +1,81 @@ +# +# Author:: Benjamin Black () +# Copyright:: Copyright (c) 2009-2016 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# + +Ohai.plugin(:LibVirt) do + %w{ uri capabilities nodeinfo domains networks storage }.each do |info| + provides "libvirt/#{info}" + end + + collect_data do + if virtualization[:role].eql?("host") + begin + require "libvirt" # this is the ruby-libvirt gem not the libvirt gem + + libvirt Mash.new + emu = (virtualization[:system].eql?("kvm") ? "qemu" : libvirt[:system]) + libvirt[:libvirt_version] = Libvirt.version(emu)[0].to_s + + virtconn = Libvirt.open_read_only("#{emu}:///system") + + libvirt[:uri] = virtconn.uri + libvirt[:capabilities] = Mash.new + + libvirt[:nodeinfo] = Mash.new + ni = virtconn.node_get_info + %w{cores cpus memory mhz model nodes sockets threads}.each { |a| libvirt[:nodeinfo][a] = ni.send(a) } + + libvirt[:domains] = Mash.new + virtconn.list_domains.each do |d| + dv = virtconn.lookup_domain_by_id d + libvirt[:domains][dv.name] = Mash.new + libvirt[:domains][dv.name][:id] = d + %w{os_type uuid}.each { |a| libvirt[:domains][dv.name][a] = dv.send(a) } + %w{cpu_time max_mem memory nr_virt_cpu state}.each { |a| libvirt[:domains][dv.name][a] = dv.info.send(a) } + + end + + libvirt[:networks] = Mash.new + virtconn.list_networks.each do |n| + nv = virtconn.lookup_network_by_name n + libvirt[:networks][n] = Mash.new + %w{bridge_name uuid}.each { |a| libvirt[:networks][n][a] = nv.send(a) } + end + + libvirt[:storage] = Mash.new + virtconn.list_storage_pools.each do |pool| + sp = virtconn.lookup_storage_pool_by_name pool + libvirt[:storage][pool] = Mash.new + %w{autostart uuid}.each { |a| libvirt[:storage][pool][a] = sp.send(a) } + %w{allocation available capacity state}.each { |a| libvirt[:storage][pool][a] = sp.info.send(a) } + + libvirt[:storage][pool][:volumes] = Mash.new + sp.list_volumes.each do |v| + libvirt[:storage][pool][:volumes][v] = Mash.new + sv = sp.lookup_volume_by_name v + %w{key name path}.each { |a| libvirt[:storage][pool][:volumes][v][a] = sv.send(a) } + %w{allocation capacity type}.each { |a| libvirt[:storage][pool][:volumes][v][a] = sv.info.send(a) } + end + end + + virtconn.close + rescue LoadError => e + Ohai::Log.debug("Plugin LibVirt: Can't load gem ruby-libvirt. Cannot continue.") + end + end + end +end diff --git a/lib/ohai/plugins/virtualization.rb b/lib/ohai/plugins/virtualization.rb deleted file mode 100644 index 7e199b93..00000000 --- a/lib/ohai/plugins/virtualization.rb +++ /dev/null @@ -1,83 +0,0 @@ -# -# Author:: Benjamin Black () -# Copyright:: Copyright (c) 2009-2016 Chef Software, Inc. -# License:: Apache License, Version 2.0 -# -# 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. -# - -# Note: despite the name this is really a libvirt plugin. -# perhaps we'd be better off renaming it? -tsmith - -Ohai.plugin(:VirtualizationInfo) do - %w{ uri capabilities nodeinfo domains networks storage }.each do |info| - provides "virtualization/#{info}" - end - - collect_data do - unless virtualization.nil? || !(virtualization[:role].eql?("host")) - begin - require "libvirt" # this is the ruby-libvirt gem not the libvirt gem - - emu = (virtualization[:system].eql?("kvm") ? "qemu" : virtualization[:system]) - virtualization[:libvirt_version] = Libvirt.version(emu)[0].to_s - - virtconn = Libvirt.open_read_only("#{emu}:///system") - - virtualization[:uri] = virtconn.uri - virtualization[:capabilities] = Mash.new - - virtualization[:nodeinfo] = Mash.new - ni = virtconn.node_get_info - %w{cores cpus memory mhz model nodes sockets threads}.each { |a| virtualization[:nodeinfo][a] = ni.send(a) } - - virtualization[:domains] = Mash.new - virtconn.list_domains.each do |d| - dv = virtconn.lookup_domain_by_id d - virtualization[:domains][dv.name] = Mash.new - virtualization[:domains][dv.name][:id] = d - %w{os_type uuid}.each { |a| virtualization[:domains][dv.name][a] = dv.send(a) } - %w{cpu_time max_mem memory nr_virt_cpu state}.each { |a| virtualization[:domains][dv.name][a] = dv.info.send(a) } - - end - - virtualization[:networks] = Mash.new - virtconn.list_networks.each do |n| - nv = virtconn.lookup_network_by_name n - virtualization[:networks][n] = Mash.new - %w{bridge_name uuid}.each { |a| virtualization[:networks][n][a] = nv.send(a) } - end - - virtualization[:storage] = Mash.new - virtconn.list_storage_pools.each do |pool| - sp = virtconn.lookup_storage_pool_by_name pool - virtualization[:storage][pool] = Mash.new - %w{autostart uuid}.each { |a| virtualization[:storage][pool][a] = sp.send(a) } - %w{allocation available capacity state}.each { |a| virtualization[:storage][pool][a] = sp.info.send(a) } - - virtualization[:storage][pool][:volumes] = Mash.new - sp.list_volumes.each do |v| - virtualization[:storage][pool][:volumes][v] = Mash.new - sv = sp.lookup_volume_by_name v - %w{key name path}.each { |a| virtualization[:storage][pool][:volumes][v][a] = sv.send(a) } - %w{allocation capacity type}.each { |a| virtualization[:storage][pool][:volumes][v][a] = sv.info.send(a) } - end - end - - virtconn.close - rescue LoadError => e - Ohai::Log.debug("Plugin Virtualization: Can't load gem: #{e}. Cannot continue.") - end - end - end -end -- cgit v1.2.1 From 52420f094ccffe3d227f54307285f33671d5a697 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 20 Dec 2016 17:19:22 -0800 Subject: Refactor the libvirt code a bit Everyone likes methods Signed-off-by: Tim Smith --- lib/ohai/plugins/libvirt.rb | 124 ++++++++++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 45 deletions(-) diff --git a/lib/ohai/plugins/libvirt.rb b/lib/ohai/plugins/libvirt.rb index 46587b01..0ccd8ba6 100644 --- a/lib/ohai/plugins/libvirt.rb +++ b/lib/ohai/plugins/libvirt.rb @@ -16,66 +16,100 @@ # limitations under the License. # +# Note: This plugin requires libvirt-bin/libvirt-dev as well as the ruby-libvirt +# gem to be installed before it will properly parse data + Ohai.plugin(:LibVirt) do %w{ uri capabilities nodeinfo domains networks storage }.each do |info| provides "libvirt/#{info}" + depends "virtualization" end - collect_data do - if virtualization[:role].eql?("host") - begin - require "libvirt" # this is the ruby-libvirt gem not the libvirt gem - - libvirt Mash.new - emu = (virtualization[:system].eql?("kvm") ? "qemu" : libvirt[:system]) - libvirt[:libvirt_version] = Libvirt.version(emu)[0].to_s - - virtconn = Libvirt.open_read_only("#{emu}:///system") + def emu + @emu ||= (virtualization[:system].eql?("kvm") ? "qemu" : virtualization[:system]) + end - libvirt[:uri] = virtconn.uri - libvirt[:capabilities] = Mash.new + def load_libvirt + begin + require "libvirt" # this is the ruby-libvirt gem not the libvirt gem + Ohai::Log.debug("Plugin LibVirt: Successfully loaded ruby-libvirt gem") + rescue LoadError + Ohai::Log.debug("Plugin LibVirt: Can't load gem ruby-libvirt.") + end + end - libvirt[:nodeinfo] = Mash.new - ni = virtconn.node_get_info - %w{cores cpus memory mhz model nodes sockets threads}.each { |a| libvirt[:nodeinfo][a] = ni.send(a) } + def virtconn + @virt_connect ||= Libvirt.open_read_only("#{emu}:///system") + end - libvirt[:domains] = Mash.new - virtconn.list_domains.each do |d| - dv = virtconn.lookup_domain_by_id d - libvirt[:domains][dv.name] = Mash.new - libvirt[:domains][dv.name][:id] = d - %w{os_type uuid}.each { |a| libvirt[:domains][dv.name][a] = dv.send(a) } - %w{cpu_time max_mem memory nr_virt_cpu state}.each { |a| libvirt[:domains][dv.name][a] = dv.info.send(a) } + def get_node_data + node_data = Mash.new + ni = virtconn.node_get_info + %w{cores cpus memory mhz model nodes sockets threads}.each { |a| node_data[a] = ni.send(a) } + node_data + end - end + def get_domain_data + domain_data = Mash.new + virtconn.list_domains.each do |d| + dv = virtconn.lookup_domain_by_id d + domain_data[dv.name] = Mash.new + domain_data[dv.name][:id] = d + %w{os_type uuid}.each { |a| domain_data[dv.name][a] = dv.send(a) } + %w{cpu_time max_mem memory nr_virt_cpu state}.each { |a| domain_data[dv.name][a] = dv.info.send(a) } + end + domain_data + end - libvirt[:networks] = Mash.new - virtconn.list_networks.each do |n| - nv = virtconn.lookup_network_by_name n - libvirt[:networks][n] = Mash.new - %w{bridge_name uuid}.each { |a| libvirt[:networks][n][a] = nv.send(a) } - end + def get_network_data + network_data = Mash.new + virtconn.list_networks.each do |n| + nv = virtconn.lookup_network_by_name n + network_data[n] = Mash.new + %w{bridge_name uuid}.each { |a| network_data[n][a] = nv.send(a) } + end + network_data + end - libvirt[:storage] = Mash.new - virtconn.list_storage_pools.each do |pool| - sp = virtconn.lookup_storage_pool_by_name pool - libvirt[:storage][pool] = Mash.new - %w{autostart uuid}.each { |a| libvirt[:storage][pool][a] = sp.send(a) } - %w{allocation available capacity state}.each { |a| libvirt[:storage][pool][a] = sp.info.send(a) } + def get_storage_data + storage_data = Mash.new + virtconn.list_storage_pools.each do |pool| + sp = virtconn.lookup_storage_pool_by_name pool + storage_data[pool] = Mash.new + %w{autostart uuid}.each { |a| storage_data[pool][a] = sp.send(a) } + %w{allocation available capacity state}.each { |a| storage_data[pool][a] = sp.info.send(a) } - libvirt[:storage][pool][:volumes] = Mash.new - sp.list_volumes.each do |v| - libvirt[:storage][pool][:volumes][v] = Mash.new - sv = sp.lookup_volume_by_name v - %w{key name path}.each { |a| libvirt[:storage][pool][:volumes][v][a] = sv.send(a) } - %w{allocation capacity type}.each { |a| libvirt[:storage][pool][:volumes][v][a] = sv.info.send(a) } - end - end + storage_data[pool][:volumes] = Mash.new + sp.list_volumes.each do |v| + storage_data[pool][:volumes][v] = Mash.new + sv = sp.lookup_volume_by_name v + %w{key name path}.each { |a| storage_data[pool][:volumes][v][a] = sv.send(a) } + %w{allocation capacity type}.each { |a| storage_data[pool][:volumes][v][a] = sv.info.send(a) } + end + end + storage_data + end + collect_data do + if virtualization[:role].eql?("host") + load_libvirt + begin + libvirt_data = Mash.new + libvirt_data[:uri] = virtconn.uri + libvirt_data[:libvirt_version] = Libvirt.version(emu)[0].to_s + libvirt_data[:nodeinfo] = get_node_data + libvirt_data[:domains] = get_domain_data + libvirt_data[:networks] = get_network_data + libvirt_data[:storage] = get_storage_data virtconn.close - rescue LoadError => e - Ohai::Log.debug("Plugin LibVirt: Can't load gem ruby-libvirt. Cannot continue.") + libvirt libvirt_data + rescue NameError + Ohai::Log.debug("Plugin LibVirt: Cannot load Libvirt. Skipping...") + rescue Libvirt::ConnectionError + Ohai::Log.debug("Plugin LibVirt: Failed to connect to #{emu}:///system. Skipping...") end + else + Ohai::Log.debug("Plugin LibVirt: Node is not a virtualization host. Skipping...") end end end -- cgit v1.2.1 From 1672a9a135738a9ccccb2daa09ccfd7000545beb Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 24 Jan 2017 14:55:14 -0800 Subject: Update the case of the plugin name Signed-off-by: Tim Smith --- lib/ohai/plugins/libvirt.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ohai/plugins/libvirt.rb b/lib/ohai/plugins/libvirt.rb index 0ccd8ba6..0faf3637 100644 --- a/lib/ohai/plugins/libvirt.rb +++ b/lib/ohai/plugins/libvirt.rb @@ -19,7 +19,7 @@ # Note: This plugin requires libvirt-bin/libvirt-dev as well as the ruby-libvirt # gem to be installed before it will properly parse data -Ohai.plugin(:LibVirt) do +Ohai.plugin(:Libvirt) do %w{ uri capabilities nodeinfo domains networks storage }.each do |info| provides "libvirt/#{info}" depends "virtualization" @@ -32,9 +32,9 @@ Ohai.plugin(:LibVirt) do def load_libvirt begin require "libvirt" # this is the ruby-libvirt gem not the libvirt gem - Ohai::Log.debug("Plugin LibVirt: Successfully loaded ruby-libvirt gem") + Ohai::Log.debug("Plugin Libvirt: Successfully loaded ruby-libvirt gem") rescue LoadError - Ohai::Log.debug("Plugin LibVirt: Can't load gem ruby-libvirt.") + Ohai::Log.debug("Plugin Libvirt: Can't load gem ruby-libvirt.") end end @@ -104,12 +104,12 @@ Ohai.plugin(:LibVirt) do virtconn.close libvirt libvirt_data rescue NameError - Ohai::Log.debug("Plugin LibVirt: Cannot load Libvirt. Skipping...") + Ohai::Log.debug("Plugin Libvirt: Cannot load Libvirt. Skipping...") rescue Libvirt::ConnectionError - Ohai::Log.debug("Plugin LibVirt: Failed to connect to #{emu}:///system. Skipping...") + Ohai::Log.debug("Plugin Libvirt: Failed to connect to #{emu}:///system. Skipping...") end else - Ohai::Log.debug("Plugin LibVirt: Node is not a virtualization host. Skipping...") + Ohai::Log.debug("Plugin Libvirt: Node is not a virtualization host. Skipping...") end end end -- cgit v1.2.1 From edc31b5005220f8624a51cf3f72e5e14b1dfdfdc Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Wed, 25 Jan 2017 13:57:37 -0800 Subject: Improve load failure warning Signed-off-by: Tim Smith --- lib/ohai/plugins/libvirt.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ohai/plugins/libvirt.rb b/lib/ohai/plugins/libvirt.rb index 0faf3637..95b2f1e1 100644 --- a/lib/ohai/plugins/libvirt.rb +++ b/lib/ohai/plugins/libvirt.rb @@ -104,7 +104,7 @@ Ohai.plugin(:Libvirt) do virtconn.close libvirt libvirt_data rescue NameError - Ohai::Log.debug("Plugin Libvirt: Cannot load Libvirt. Skipping...") + Ohai::Log.debug("Plugin Libvirt: Cannot load ruby-libvirt gem. Skipping...") rescue Libvirt::ConnectionError Ohai::Log.debug("Plugin Libvirt: Failed to connect to #{emu}:///system. Skipping...") end -- cgit v1.2.1