summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-05-04 21:25:28 -0700
committeradamedx <adamed@opscode.com>2014-05-18 23:16:04 -0700
commita5e5b0ec9ac33b239ca30b4acedcdc4cdadb74ca (patch)
tree0c47fb1f71d48f3b4f69ae4bc2cb8fdbfc6a506f
parentd7c4987c33f6bc36b7bb7eb0b76b2a8fb4f9f42c (diff)
downloadohai-a5e5b0ec9ac33b239ca30b4acedcdc4cdadb74ca.tar.gz
WMI utility refactoring
-rw-r--r--lib/ohai/util/wmi.rb77
-rw-r--r--lib/ohai/util/wmi/wmi_instance.rb49
2 files changed, 126 insertions, 0 deletions
diff --git a/lib/ohai/util/wmi.rb b/lib/ohai/util/wmi.rb
new file mode 100644
index 00000000..03e6af0f
--- /dev/null
+++ b/lib/ohai/util/wmi.rb
@@ -0,0 +1,77 @@
+#
+# 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'
+require 'ohai/util/wmi/wmi_instance'
+
+module Ohai
+ module Util
+ class Wmi
+
+ def self.query(wql_query, namespace = nil)
+ results = start_query(namespace, wql_query)
+
+ result_set = []
+
+ results.each do | result |
+ result_set.push(Wmi::Instance.new(result))
+ end
+
+ result_set
+ end
+
+ def self.instances_of(wmi_class, namespace = nil)
+ query("select * from #{wmi_class}")
+ end
+
+ def self.first_of(wmi_class, namespace = nil)
+ query_result = start_query(namespace, "select * from #{wmi_class}")
+ first_result = nil
+ query_result.each do | record |
+ first_result = record
+ break
+ end
+ first_result.nil? ? nil : Wmi::Instance.new(first_result)
+ end
+
+ private
+
+ def self.start_query(namespace, wql_query)
+ connection = new_connection(namespace)
+ connection.ExecQuery(wql_query)
+ end
+
+ def self.new_connection(namespace)
+ locator = WIN32OLE.new("WbemScripting.SWbemLocator")
+ locator.ConnectServer('.', namespace.nil? ? 'root/cimv2' : namespace)
+ end
+
+ def self.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
+ end
+ end
+end
+
diff --git a/lib/ohai/util/wmi/wmi_instance.rb b/lib/ohai/util/wmi/wmi_instance.rb
new file mode 100644
index 00000000..6b46a957
--- /dev/null
+++ b/lib/ohai/util/wmi/wmi_instance.rb
@@ -0,0 +1,49 @@
+#
+# 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.
+#
+module Ohai
+ module Util
+ class Wmi
+ class Instance
+
+ attr_reader :wmi_ole_object
+
+ def initialize(wmi_ole_object)
+ @wmi_ole_object = wmi_ole_object
+ @property_map = wmi_ole_object_to_hash(wmi_ole_object)
+ end
+
+ def [](key)
+ @property_map[key]
+ end
+
+ private
+
+ def wmi_ole_object_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.freeze
+ end
+ end
+ end
+ end
+end