summaryrefslogtreecommitdiff
path: root/lib/ohai/plugins/windows/filesystem.rb
blob: e637cf54e9d1287dbe263613106903446b82a9b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#
# Author:: James Gartrell (<jgartrel@gmail.com>)
# 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(:Filesystem) do
  provides "filesystem"

  # Volume encryption or decryption status
  #
  # @see https://docs.microsoft.com/en-us/windows/desktop/SecProv/getconversionstatus-win32-encryptablevolume#parameters
  #
  CONVERSION_STATUS ||= %w{FullyDecrypted FullyEncrypted EncryptionInProgress
                         DecryptionInProgress EncryptionPaused DecryptionPaused}.freeze

  # Returns a Mash loaded with logical details
  #
  # Uses Win32_LogicalDisk and logical_properties to return encryption details of volumes.
  #
  # Returns an empty Mash in case of any WMI exception.
  #
  # @see https://docs.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-logicaldisk
  #
  # @return [Mash]
  #
  def logical_info
    wmi = WmiLite::Wmi.new("Root\\CIMV2")

    # Note: we should really be parsing Win32_Volume and Win32_Mapped drive.
    disks = wmi.instances_of("Win32_LogicalDisk")
    logical_properties(disks)
  rescue WmiLite::WmiException
    Ohai::Log.debug("Unable to access Win32_LogicalDisk. Skipping logical details")
    Mash.new
  end

  # Returns a Mash loaded with encryption details
  #
  # Uses Win32_EncryptableVolume and encryption_properties to return encryption details of volumes.
  #
  # Returns an empty Mash in case of any WMI exception.
  #
  # @note We are fetching Encryption Status only as of now
  #
  # @see https://msdn.microsoft.com/en-us/library/windows/desktop/aa376483(v=vs.85).aspx
  #
  # @return [Mash]
  #
  def encryptable_info
    wmi = WmiLite::Wmi.new("Root\\CIMV2\\Security\\MicrosoftVolumeEncryption")
    disks = wmi.instances_of("Win32_EncryptableVolume")
    encryption_properties(disks)
  rescue WmiLite::WmiException
    Ohai::Log.debug("Unable to access Win32_EncryptableVolume. Skipping encryptable details")
    Mash.new
  end

  # Refines and calculates logical properties out of given instances
  #
  # @param [WmiLite::Wmi::Instance] disks
  #
  # @return [Mash] Each drive containing following properties:
  #
  #  * :kb_size (Integer)
  #  * :kb_available (Integer)
  #  * :kb_used (Integer)
  #  * :percent_used (Integer)
  #  * :mount (String)
  #  * :fs_type (String)
  #  * :volume_name (String)
  #
  def logical_properties(disks)
    properties = Mash.new
    disks.each do |disk|
      property = Mash.new
      drive = disk["deviceid"]
      property[:kb_size] = disk["size"].to_i / 1000
      property[:kb_available] = disk["freespace"].to_i / 1000
      property[:kb_used] = property[:kb_size] - property[:kb_available]
      property[:percent_used] = (property[:kb_size] == 0 ? 0 : (property[:kb_used] * 100 / property[:kb_size]))
      property[:mount] = disk["name"]
      property[:fs_type] = disk["filesystem"].to_s.downcase
      property[:volume_name] = disk["volumename"].to_s.downcase
      properties[drive] = property
    end
    properties
  end

  # Refines and calculates encryption properties out of given instances
  #
  # @param [WmiLite::Wmi::Instance] disks
  #
  # @return [Mash] Each drive containing following properties:
  #
  #  * :encryption_status (String)
  #
  def encryption_properties(disks)
    properties = Mash.new
    disks.each do |disk|
      drive = disk["driveletter"]
      property = Mash.new
      property[:encryption_status] = disk["conversionstatus"] ? CONVERSION_STATUS[disk["conversionstatus"]] : ""
      properties[drive] = property
    end
    properties
  end

  # Merges all the various properties of filesystems
  #
  # @param [Array<Mash>] disks_info
  #   Array of the Mashes containing disk properties
  #
  # @return [Mash]
  #
  def merge_info(disks_info)
    fs = Mash.new
    disks_info.each do |info|
      info.each do |disk, data|
        if fs[disk]
          fs[disk].merge!(data)
        else
          fs[disk] = data.dup
        end
      end
    end
    fs
  end

  collect_data(:windows) do
    require "wmi-lite/wmi"
    filesystem merge_info([logical_info,
                           encryptable_info])
  end
end