summaryrefslogtreecommitdiff
path: root/lib/ohai/plugins/linux/mdadm.rb
blob: 407e1dd91017250736154dbbac4d98af73329c44 (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
#
# Author:: Tim Smith <tsmith@limelight.com>
# Author:: Phil Dibowitz <phild@ipomc.com>
# Copyright:: Copyright (c) 2013-2014, Limelight Networks, Inc.
# Copyright:: Copyright (c) 2017 Facebook, Inc.
# Plugin:: mdadm
#
# 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(:Mdadm) do
  provides "mdadm"

  def create_raid_device_mash(stdout)
    device_mash = Mash.new
    device_mash[:device_counts] = Mash.new
    stdout.lines.each do |line|
      case line
      when /Version\s+: ([0-9.]+)/
        device_mash[:version] = Regexp.last_match[1].to_f
      when /Raid Level\s+: raid([0-9]+)/
        device_mash[:level] = Regexp.last_match[1].to_i
      when /Array Size.*\(([0-9.]+)/
        device_mash[:size] = Regexp.last_match[1].to_f
      when /State\s+: ([a-z]+)/
        device_mash[:state] = Regexp.last_match[1]
      when /Total Devices\s+: ([0-9]+)/
        device_mash[:device_counts][:total] = Regexp.last_match[1].to_i
      when /Raid Devices\s+: ([0-9]+)/
        device_mash[:device_counts][:raid] = Regexp.last_match[1].to_i
      when /Working Devices\s+: ([0-9]+)/
        device_mash[:device_counts][:working] = Regexp.last_match[1].to_i
      when /Failed Devices\s+: ([0-9]+)/
        device_mash[:device_counts][:failed] = Regexp.last_match[1].to_i
      when /Active Devices\s+: ([0-9]+)/
        device_mash[:device_counts][:active] = Regexp.last_match[1].to_i
      when /Spare Devices\s+: ([0-9]+)/
        device_mash[:device_counts][:spare] = Regexp.last_match[1].to_i
      end
    end
    device_mash
  end

  collect_data(:linux) do
    # gather a list of all raid arrays
    if File.exist?("/proc/mdstat")
      devices = {}
      File.open("/proc/mdstat").each do |line|
        if line =~ /(md[0-9]+)/
          device = Regexp.last_match[1]
          pieces = line.split(/\s+/)
          # there are variable numbers of fields until you hit the raid level
          # everything after that is members...
          # unless the array is inactive, in which case you don't get a raid
          # level.
          members = pieces.drop_while { |x| !x.start_with?("raid", "inactive") }
          # and drop that too
          members.shift unless members.empty?
          devices[device] = {
            "active" => [],
            "spare" => [],
            "journal" => nil,
          }
          members.each do |member|
            # We want to match the device, and optionally the type
            # most entries will look like:
            #   sdc1[5]
            # but some will look like:
            #   sdc1[5](J)
            # where the format is:
            #   <device>[<number in array>](<type>)
            # Type can be things like "J" for a journal device, or "S" for
            # a spare device.
            m = member.match(/(.+)\[\d+\](?:\((\w)\))?/)
            member_device = m[1]
            member_type = m[2]
            case member_type
            when "J"
              devices[device]["journal"] = member_device
            when "S"
              devices[device]["spare"] << member_device
            else
              devices[device]["active"] << member_device
            end
          end
        end
      end

      # create the mdadm mash and gather individual information if devices are present
      unless devices.empty?
        mdadm Mash.new
        devices.keys.sort.each do |device|
          mdadm[device] = Mash.new

          # gather detailed information on the array
          so = shell_out("mdadm --detail /dev/#{device}")

          # if the mdadm command was sucessful pass so.stdout to create_raid_device_mash to grab the tidbits we want
          mdadm[device] = create_raid_device_mash(so.stdout) if so.stdout
          mdadm[device]["members"] = devices[device]["active"]
          mdadm[device]["spares"] = devices[device]["spare"]
          mdadm[device]["journal"] = devices[device]["journal"]
        end
      end
    end
  end
end