summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2021-12-09 16:22:42 -0500
committerGitHub <noreply@github.com>2021-12-09 16:22:42 -0500
commit481368e08b8c0d4058c890aaead584a726f0c441 (patch)
treee09e645be605f666d8e9b4a531fba2eb5d9a3fee
parenta2c2f9658402406dcbfb8e205dc6086ad38cb0d6 (diff)
parent1da5761c8954c1b3555dedaa09ee8788f25f7f7f (diff)
downloadohai-481368e08b8c0d4058c890aaead584a726f0c441.tar.gz
Merge pull request #1720 from tecracer-theinen/feature/vmware-windows-and-type
Add support for VMware detection on Windows and details
-rw-r--r--lib/ohai/plugins/vmware.rb24
-rw-r--r--spec/unit/plugins/vmware_spec.rb32
2 files changed, 43 insertions, 13 deletions
diff --git a/lib/ohai/plugins/vmware.rb b/lib/ohai/plugins/vmware.rb
index d8c89e66..250a9e24 100644
--- a/lib/ohai/plugins/vmware.rb
+++ b/lib/ohai/plugins/vmware.rb
@@ -45,12 +45,14 @@ Ohai.plugin(:VMware) do
logger.trace("Plugin VMware: #{vmtools_path} not found")
else
vmware Mash.new
+ vmware[:host] = Mash.new
+ vmware[:guest] = Mash.new
begin
# vmware-toolbox-cmd stat <param> commands
# Iterate through each parameter supported by the "vwware-toolbox-cmd stat" command, assign value
# to attribute "vmware[:<parameter>]"
%w{hosttime speed sessionid balloon swap memlimit memres cpures cpulimit}.each do |param|
- vmware[param] = from_cmd("#{vmtools_path} stat #{param}")
+ vmware[param] = from_cmd([vmtools_path, "stat", param])
if /UpdateInfo failed/.match?(vmware[param])
vmware[param] = nil
end
@@ -59,8 +61,23 @@ Ohai.plugin(:VMware) do
# Iterate through each parameter supported by the "vmware-toolbox-cmd status" command, assign value
# to attribute "vmware[:<parameter>]"
%w{upgrade timesync}.each do |param|
- vmware[param] = from_cmd("#{vmtools_path} #{param} status")
+ vmware[param] = from_cmd([vmtools_path, param, "status"])
end
+ # Distinguish hypervisors by presence of raw session data (vSphere only)
+ raw_session = from_cmd([vmtools_path, "stat", "raw", "json", "session"])
+ if raw_session.empty?
+ vmware[:host] = {
+ type: "vmware_desktop",
+ }
+ else
+ require "json" unless defined?(JSON)
+ session = JSON.parse(raw_session)
+ vmware[:host] = {
+ type: "vmware_vsphere",
+ version: session["version"],
+ }
+ end
+ vmware[:guest][:vmware_tools_version] = from_cmd([vmtools_path, "-v"]).split(" ").first
rescue
logger.trace("Plugin VMware: Error while collecting VMware guest attributes")
end
@@ -71,4 +88,7 @@ Ohai.plugin(:VMware) do
get_vm_attributes("/usr/bin/vmware-toolbox-cmd") if virtualization[:systems][:vmware]
end
+ collect_data(:windows) do
+ get_vm_attributes("C:/Program Files/VMWare/VMware Tools/VMwareToolboxCmd.exe") if virtualization[:systems][:vmware]
+ end
end
diff --git a/spec/unit/plugins/vmware_spec.rb b/spec/unit/plugins/vmware_spec.rb
index 86d13dc4..c61700f0 100644
--- a/spec/unit/plugins/vmware_spec.rb
+++ b/spec/unit/plugins/vmware_spec.rb
@@ -28,17 +28,19 @@ describe Ohai::System, "plugin vmware" do
context "when on vmware guest with toolbox installed" do
before do
allow(File).to receive(:exist?).with("/usr/bin/vmware-toolbox-cmd").and_return(true)
- allow(plugin).to receive(:shell_out).with("#{path} stat speed").and_return(mock_shell_out(0, "2000 MHz", nil))
- allow(plugin).to receive(:shell_out).with("#{path} stat hosttime").and_return(mock_shell_out(0, "04 Jun 2015 19:21:16", nil))
- allow(plugin).to receive(:shell_out).with("#{path} stat sessionid").and_return(mock_shell_out(0, "0x0000000000000000", nil))
- allow(plugin).to receive(:shell_out).with("#{path} stat balloon").and_return(mock_shell_out(0, "0 MB", nil))
- allow(plugin).to receive(:shell_out).with("#{path} stat swap").and_return(mock_shell_out(0, "0 MB", nil))
- allow(plugin).to receive(:shell_out).with("#{path} stat memlimit").and_return(mock_shell_out(0, "4000000000 MB", nil))
- allow(plugin).to receive(:shell_out).with("#{path} stat memres").and_return(mock_shell_out(0, "0 MB", nil))
- allow(plugin).to receive(:shell_out).with("#{path} stat cpures").and_return(mock_shell_out(0, "0 MHz", nil))
- allow(plugin).to receive(:shell_out).with("#{path} stat cpulimit").and_return(mock_shell_out(0, "4000000000 MB", nil))
- allow(plugin).to receive(:shell_out).with("#{path} upgrade status").and_return(mock_shell_out(0, "VMware Tools are up-to-date.", nil))
- allow(plugin).to receive(:shell_out).with("#{path} timesync status").and_return(mock_shell_out(0, "Disabled", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "speed"]).and_return(mock_shell_out(0, "2000 MHz", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "hosttime"]).and_return(mock_shell_out(0, "04 Jun 2015 19:21:16", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "sessionid"]).and_return(mock_shell_out(0, "0x0000000000000000", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "balloon"]).and_return(mock_shell_out(0, "0 MB", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "swap"]).and_return(mock_shell_out(0, "0 MB", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "memlimit"]).and_return(mock_shell_out(0, "4000000000 MB", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "memres"]).and_return(mock_shell_out(0, "0 MB", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "cpures"]).and_return(mock_shell_out(0, "0 MHz", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "cpulimit"]).and_return(mock_shell_out(0, "4000000000 MB", nil))
+ allow(plugin).to receive(:shell_out).with([path, "upgrade", "status"]).and_return(mock_shell_out(0, "VMware Tools are up-to-date.", nil))
+ allow(plugin).to receive(:shell_out).with([path, "timesync", "status"]).and_return(mock_shell_out(0, "Disabled", nil))
+ allow(plugin).to receive(:shell_out).with([path, "stat", "raw", "json", "session"]).and_return(mock_shell_out(0, '{ "version": "VMware ESX 7.0.0 build-15843807" }', nil))
+ allow(plugin).to receive(:shell_out).with([path, "-v"]).and_return(mock_shell_out(0, "10.3.0.5330", nil))
plugin[:virtualization] = Mash.new
plugin[:virtualization][:systems] = Mash.new
plugin[:virtualization][:systems][:vmware] = Mash.new
@@ -56,6 +58,14 @@ describe Ohai::System, "plugin vmware" do
it "gets tools update status" do
expect(plugin[:vmware][:upgrade]).to eq("VMware Tools are up-to-date.")
end
+
+ it "gets tools version" do
+ expect(plugin[:vmware][:guest][:vmware_tools_version]).to eq("10.3.0.5330")
+ end
+
+ it "gets vSphere version" do
+ expect(plugin[:vmware][:host][:version]).to eq("VMware ESX 7.0.0 build-15843807")
+ end
end
context "when on vmware guest without toolbox" do