Class: OsCtl::Lib::Zfs::ZpoolStatus

Inherits:
Object
  • Object
show all
Includes:
Utils::Log, Utils::System
Defined in:
lib/libosctl/zfs/zpool_status.rb

Overview

Read interface to zpool status

Defined Under Namespace

Classes: ConfigParser, Pool, VirtualDevice

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::System

#repeat_on_failure, #syscmd, #zfs

Methods included from Utils::Log

included

Constructor Details

#initialize(pools: [], status_string: nil) ⇒ ZpoolStatus

Returns a new instance of ZpoolStatus.

Parameters:

  • pools (Array<String>) (defaults to: [])

    pools to query, all pools are queried if empty

  • status_string (String) (defaults to: nil)

    optional output from zpool status to parse



59
60
61
# File 'lib/libosctl/zfs/zpool_status.rb', line 59

def initialize(pools: [], status_string: nil)
  @pools, @index = read(pools, status_string)
end

Instance Attribute Details

#poolsArray<Pool> (readonly)

Returns:



55
56
57
# File 'lib/libosctl/zfs/zpool_status.rb', line 55

def pools
  @pools
end

Instance Method Details

#[](name) ⇒ Pool?

Parameters:

  • name (String)

    pool name

Returns:



65
66
67
# File 'lib/libosctl/zfs/zpool_status.rb', line 65

def [](name)
  @index[name]
end

#read(pools, status_string) ⇒ Object (protected)



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
# File 'lib/libosctl/zfs/zpool_status.rb', line 71

def read(pools, status_string)
  list = []
  index = {}

  cur_pool = nil
  in_config = false
  config_parser = nil

  status_string ||= syscmd("zpool status -LP #{pools.join(' ')}").output

  status_string.strip.each_line do |line|
    stripped = line.strip

    if stripped.start_with?('pool:')
      in_config = false

      if cur_pool
        list << cur_pool
        index[cur_pool.name] = cur_pool
      end

      cur_pool = Pool.new(
        name: stripped[5..].strip,
        state: :unknown,
        scan: :none,
        scan_percent: nil,
        virtual_devices: []
      )

      config_parser = nil

    elsif cur_pool && in_config
      config_parser.parse_line(line, stripped)

    elsif cur_pool && stripped.start_with?('state:')
      cur_pool.state = stripped[6..].strip.downcase.to_sym

    elsif cur_pool && stripped.start_with?('scan:')
      scan = stripped[5..].strip

      cur_pool.scan =
        if scan.start_with?('resilver in progress')
          :resilver
        elsif scan.start_with?('scrub in progress')
          :scrub
        else
          :none
        end

    elsif cur_pool && stripped.start_with?('config:')
      in_config = true
      config_parser = ConfigParser.new(cur_pool)

    elsif cur_pool && cur_pool.scan != :none && /, (\d+\.\d+)% done,/ =~ stripped
      cur_pool.scan_percent = ::Regexp.last_match(1).to_f
    end
  end

  if cur_pool
    list << cur_pool
    index[cur_pool.name] = cur_pool
  end

  [list, index]
end