Skip to content

Instantly share code, notes, and snippets.

@btm
Forked from tas50/gist:3fe17a6d9fbede0a7669
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btm/5fb6b9011f7a3eb3a7b2 to your computer and use it in GitHub Desktop.
Save btm/5fb6b9011f7a3eb3a7b2 to your computer and use it in GitHub Desktop.
Ohai.plugin(:Mdadm) do
provides 'mdadm'
def create_raid_device_mash(stdout)
device_mash = Mash.new
device_mash[:device_counts] = Mash.new
stdout.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.exists?('/proc/mdstat')
devices = []
File.open('/proc/mdstat').each do |line|
devices << Regexp.last_match[1] if line =~ /(md[0-9]+)/
end
# create the mdadm mash and gather individual information if devices are present
unless devices.empty?
mdadm Mash.new
devices.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 stdout to create_raid_device_mash to grab the tidbits we want
mdadm[device] = create_raid_device_mash(so.stdout.lines) if so.exitstatus == 0
end
end
end
end
end
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper.rb')
describe Ohai::System, "Linux Mdadm Plugin" do
before(:each) do
@md0 = <<-MD
/dev/md0:
Version : 1.2
Creation Time : Thu Jan 30 03:11:40 2014
Raid Level : raid10
Array Size : 2929893888 (2794.16 GiB 3000.21 GB)
Used Dev Size : 976631296 (931.39 GiB 1000.07 GB)
Raid Devices : 6
Total Devices : 6
Persistence : Superblock is persistent
Update Time : Tue May 6 23:30:32 2014
State : clean
Active Devices : 6
Working Devices : 6
Failed Devices : 0
Spare Devices : 0
Layout : near=2
Chunk Size : 256K
Name : host.therealtimsmith.com:3 (local to host host.therealtimsmith.com)
UUID : 5ed74d5b:70bfe21d:8cd57792:c1e13d65
Events : 155
Number Major Minor RaidDevice State
0 8 32 0 active sync /dev/sdc
1 8 48 1 active sync /dev/sdd
2 8 64 2 active sync /dev/sde
3 8 80 3 active sync /dev/sdf
4 8 96 4 active sync /dev/sdg
5 8 112 5 active sync /dev/sdh
MD
@plugin = get_plugin("linux/mdadm")
@plugin.stub(:collect_os).and_return(:linux)
@double_file = double("/proc/mdstat")
@double_file.stub(:each).
and_yield("Personalities : [raid1] [raid6] [raid5] [raid4] [linear] [multipath] [raid0] [raid10]").
and_yield("md0 : active raid10 sdh[5] sdg[4] sdf[3] sde[2] sdd[1] sdc[0]").
and_yield(" 2929893888 blocks super 1.2 256K chunks 2 near-copies [6/6] [UUUUUU]")
File.stub(:open).with("/proc/mdstat").and_return(@double_file)
File.stub(:exists?).with("/proc/mdstat").and_return(true)
@plugin.stub(:shell_out).with("mdadm --detail /dev/md0").and_return(mock_shell_out(0, @md0, ""))
end
describe "gathering Mdadm information via /proc/mdstat and mdadm --detail" do
it "should not raise an error" do
lambda { @plugin.run }.should_not raise_error
end
it "should have a device count of 6 on md0" do
@plugin.run
@plugin[:mdadm][:md0][:device_counts][:raid].should be 6
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment