diff options
author | Adam Edwards <adamed@opscode.com> | 2014-05-13 15:01:30 -0700 |
---|---|---|
committer | adamedx <adamed@opscode.com> | 2014-05-18 23:16:05 -0700 |
commit | dd1f12c74bec1327d5c788ad32c2b8b1ff13bf3e (patch) | |
tree | 80ea108343c3954cb360120b6856f7857cfbe2db | |
parent | 485375c5c2e0ecd214491cd8a6af0fc58bad89af (diff) | |
download | ohai-dd1f12c74bec1327d5c788ad32c2b8b1ff13bf3e.tar.gz |
Use wmi-lite gem dependency for wmi access
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | lib/ohai/plugins/hostname.rb | 4 | ||||
-rw-r--r-- | lib/ohai/plugins/kernel.rb | 4 | ||||
-rw-r--r-- | lib/ohai/plugins/uptime.rb | 4 | ||||
-rw-r--r-- | lib/ohai/plugins/windows/cpu.rb | 4 | ||||
-rw-r--r-- | lib/ohai/plugins/windows/filesystem.rb | 4 | ||||
-rw-r--r-- | lib/ohai/plugins/windows/network.rb | 4 | ||||
-rw-r--r-- | lib/ohai/wmi.rb | 80 | ||||
-rw-r--r-- | spec/unit/plugins/kernel_spec.rb | 8 |
9 files changed, 20 insertions, 94 deletions
@@ -2,6 +2,8 @@ source "https://rubygems.org" gemspec +gem 'wmi-lite', :git => 'https://github.com/opscode/wmi-lite.git', :branch => 'master' + group :development do gem "sigar", :platform => "ruby" diff --git a/lib/ohai/plugins/hostname.rb b/lib/ohai/plugins/hostname.rb index eec975e4..303880ed 100644 --- a/lib/ohai/plugins/hostname.rb +++ b/lib/ohai/plugins/hostname.rb @@ -145,10 +145,10 @@ Ohai.plugin(:Hostname) do end collect_data(:windows) do - require 'ohai/wmi' + require 'wmi-lite/wmi' require 'socket' - wmi = WmiRepository.new + wmi = WmiLite::Wmi.new host = wmi.first_of('Win32_ComputerSystem') hostname "#{host['name']}" diff --git a/lib/ohai/plugins/kernel.rb b/lib/ohai/plugins/kernel.rb index 702b94b8..5c3957ce 100644 --- a/lib/ohai/plugins/kernel.rb +++ b/lib/ohai/plugins/kernel.rb @@ -156,11 +156,11 @@ Ohai.plugin(:Kernel) do collect_data(:windows) do require 'win32ole' - require 'ohai/wmi' + require 'wmi-lite/wmi' WIN32OLE.codepage = WIN32OLE::CP_UTF8 - wmi = WmiRepository.new + wmi = WmiLite::Wmi.new kernel Mash.new diff --git a/lib/ohai/plugins/uptime.rb b/lib/ohai/plugins/uptime.rb index 57455194..8f879d35 100644 --- a/lib/ohai/plugins/uptime.rb +++ b/lib/ohai/plugins/uptime.rb @@ -94,8 +94,8 @@ Ohai.plugin(:Uptime) do end collect_data(:windows) do - require 'ohai/wmi' - wmi = WmiRepository.new + require 'wmi-lite/wmi' + wmi = WmiLite::Wmi.new uptime_seconds wmi.first_of('Win32_PerfFormattedData_PerfOS_System')['systemuptime'].to_i uptime seconds_to_human(uptime_seconds) end diff --git a/lib/ohai/plugins/windows/cpu.rb b/lib/ohai/plugins/windows/cpu.rb index 2dc7ed48..8f05732f 100644 --- a/lib/ohai/plugins/windows/cpu.rb +++ b/lib/ohai/plugins/windows/cpu.rb @@ -20,13 +20,13 @@ Ohai.plugin(:CPU) do provides "cpu" collect_data(:windows) do - require 'ohai/wmi' + require 'wmi-lite/wmi' cpuinfo = Mash.new cpu_number = 0 index = 0 - wmi = WmiRepository.new + wmi = WmiLite::Wmi.new processors = wmi.instances_of('Win32_Processor') processors.find(:all).each do |processor| diff --git a/lib/ohai/plugins/windows/filesystem.rb b/lib/ohai/plugins/windows/filesystem.rb index 09d59dff..c1d8d43c 100644 --- a/lib/ohai/plugins/windows/filesystem.rb +++ b/lib/ohai/plugins/windows/filesystem.rb @@ -21,10 +21,12 @@ Ohai.plugin(:Filesystem) do collect_data(:windows) do + require 'wmi-lite/wmi' + fs = Mash.new ld_info = Mash.new - wmi = WmiRepository.new + wmi = WmiLite::Wmi.new # Grab filesystem data from WMI # Note: we should really be parsing Win32_Volume and Win32_Mapped drive diff --git a/lib/ohai/plugins/windows/network.rb b/lib/ohai/plugins/windows/network.rb index 45fce9b0..aa7316f6 100644 --- a/lib/ohai/plugins/windows/network.rb +++ b/lib/ohai/plugins/windows/network.rb @@ -27,6 +27,8 @@ Ohai.plugin(:Network) do collect_data(:windows) do + require 'wmi-lite/wmi' + iface = Mash.new iface_config = Mash.new iface_instance = Mash.new @@ -36,7 +38,7 @@ Ohai.plugin(:Network) do counters[:network] = Mash.new unless counters[:network] # http://msdn.microsoft.com/en-us/library/windows/desktop/aa394217%28v=vs.85%29.aspx - wmi = WmiRepository.new + wmi = WmiLite::Wmi.new adapters = wmi.instances_of('Win32_NetworkAdapterConfiguration') diff --git a/lib/ohai/wmi.rb b/lib/ohai/wmi.rb deleted file mode 100644 index 0cc4c24f..00000000 --- a/lib/ohai/wmi.rb +++ /dev/null @@ -1,80 +0,0 @@ -# -# Author:: Adam Edwards (<adamed@getchef.com>) -# Copyright:: Copyright 2014 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. -# - -require 'win32ole' - -class WmiRepository - def initialize(namespace = nil, results_as_mash = false) - @connection = new_connection(namespace.nil? ? 'root/cimv2' : namespace) - @results_as_mash = results_as_mash - end - - def query(wql_query) - results = start_query(wql_query) - - result_set = [] - - results.each do | result | - result_set.push(wmi_result_to_snapshot(result)) - end - - result_set - end - - def instances_of(wmi_class) - query("select * from #{wmi_class}") - end - - def first_of(wmi_class) - query_result = start_query("select * from #{wmi_class}") - first_result = nil - query_result.each do | record | - first_result = record - break - end - first_result.nil? ? nil : wmi_result_to_snapshot(first_result) - end - - private - - def start_query(wql_query) - @connection.ExecQuery(wql_query) - end - - def new_connection(namespace) - locator = WIN32OLE.new("WbemScripting.SWbemLocator") - locator.ConnectServer('.', namespace) - end - - def wmi_result_to_hash(wmi_object) - property_map = {} - wmi_object.properties_.each do |property| - property_map[property.name.downcase] = wmi_object.invoke(property.name) - end - - property_map[:wmi_object] = wmi_object - - property_map - end - - def wmi_result_to_snapshot(wmi_object) - snapshot = wmi_result_to_hash(wmi_object) - @results_as_mash ? Mash.new(snapshot) : snapshot - end -end - diff --git a/spec/unit/plugins/kernel_spec.rb b/spec/unit/plugins/kernel_spec.rb index 49c2489f..74bc8c23 100644 --- a/spec/unit/plugins/kernel_spec.rb +++ b/spec/unit/plugins/kernel_spec.rb @@ -37,7 +37,7 @@ describe Ohai::System, "plugin kernel" do describe "when running on windows", :windows_only do before do - require 'lib/ohai/wmi' + require 'wmi-lite/wmi' @ohai_system = Ohai::System.new @plugin = get_plugin("kernel", @ohai_system) @@ -65,7 +65,7 @@ describe Ohai::System, "plugin kernel" do 'ostype' => os.OsType, :wmi_object => os } - WmiRepository.any_instance.should_receive(:first_of).with('Win32_OperatingSystem').and_return(os_wmi) + WmiLite::Wmi.any_instance.should_receive(:first_of).with('Win32_OperatingSystem').and_return(os_wmi) cs = double("WIN32OLE", :properties_ => [ double("WIN32OLE", :name => "SystemType") ], @@ -76,9 +76,9 @@ describe Ohai::System, "plugin kernel" do cs_wmi = { 'systemtype' => cs.SystemType, :wmi_object => cs } - WmiRepository.any_instance.should_receive(:first_of).with('Win32_ComputerSystem').and_return(cs_wmi) + WmiLite::Wmi.any_instance.should_receive(:first_of).with('Win32_ComputerSystem').and_return(cs_wmi) - WmiRepository.any_instance.should_receive(:instances_of).with('Win32_PnPSignedDriver').and_return([]) + WmiLite::Wmi.any_instance.should_receive(:instances_of).with('Win32_PnPSignedDriver').and_return([]) @plugin.run end |