Class: Elesai::Megacli::Megacli

Inherits:
Object
  • Object
show all
Includes:
Workflow
Defined in:
lib/elesai/megacli/megacli.rb

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Instance Method Details

#adapter_line(adapter, key, value) ⇒ Object

Adapter



115
116
117
118
119
# File 'lib/elesai/megacli/megacli.rb', line 115

def adapter_line(adapter,key,value)
  @log.debug "  [#{current_state}] event adapter_line: new #{adapter.inspect}"
  adapter[key.to_sym] = value.to_i
  @lsi.add(adapter)
end

#adapter_match(k, match) ⇒ Object

Adapter



80
81
82
83
84
85
# File 'lib/elesai/megacli/megacli.rb', line 80

def adapter_match(k,match)
  @log.debug "ADAPTER! #{match.string}"
  key = 'id'
  value = match[:value]
  adapter_line!(LSI::Adapter.new,key,value)
end

#attribute_line(key, value) ⇒ Object

Attribute



135
136
137
# File 'lib/elesai/megacli/megacli.rb', line 135

def attribute_line(key,value)
  @log.debug "  [#{current_state}] event: attribute_line: #{key} => #{value}"
end

#attribute_match(k, match) ⇒ Object

Attribute



89
90
91
92
93
94
95
# File 'lib/elesai/megacli/megacli.rb', line 89

def attribute_match(k,match)
  @log.debug "ATTRIBUTE! #{match.string}"
  key = match[:key].gsub(/\s+/,"").downcase
  value_tmp = match[:value]
  value = value_tmp.nil? ? nil : value_tmp.strip
  attribute_line!(key,value)
end

#exit_lineObject

Exit



182
183
184
# File 'lib/elesai/megacli/megacli.rb', line 182

def exit_line
  @log.debug "  [#{current_state}] event: exit_line"
end

#exit_match(k, match) ⇒ Object

Exit



99
100
101
102
# File 'lib/elesai/megacli/megacli.rb', line 99

def exit_match(k,match)
  @log.debug "EXIT! #{match.string}"
  exit_line!
end

#on_adapter_entry(old_state, event, *args) ⇒ Object



121
122
123
124
125
126
# File 'lib/elesai/megacli/megacli.rb', line 121

def on_adapter_entry(old_state, event, *args)
  @log.debug "        [#{current_state}] on_entry: leaving #{old_state}; args: #{args}"

  @context.close unless @context.current.nil? or Elesai::LSI::Adapter === @context.current
  @context.open args[0]
end

#on_adapter_exit(new_state, event, *args) ⇒ Object



128
129
130
131
# File 'lib/elesai/megacli/megacli.rb', line 128

def on_adapter_exit(new_state, event, *args)
  @log.debug "      [#{current_state}] on_exit: entering #{new_state}; args: #{args}"
  @context.flash!(new_state)
end

#on_attribute_entry(old_state, event, *args) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/elesai/megacli/megacli.rb', line 139

def on_attribute_entry(old_state, event, *args)
  @log.debug "        [#{current_state}] entry: leaving #{old_state}; args: #{args}; context: #{@context.current.class}"

  c = @context.current
  k = args[0].to_sym
  v = args[1]

  # Some attributes require special treatment for our purposes

  case k
    when :coercedsize, :noncoercedsize, :rawsize, :size
      m = /(?<number>[0-9\.]+)\s+(?<unit>[A-Z]+)/.match(v)
      v = LSI::PhysicalDrive::Size.new(m[:number],m[:unit])
    when :raidlevel
      m = /Primary-(?<primary>\d+),\s+Secondary-(?<secondary>\d+)/.match(v)
      v = LSI::VirtualDrive::RaidLevel.new(m[:primary],m[:secondary])
    when :firmwarestate
      st,sp = v.gsub(/\s/,'').split(/,/)
      state = st.gsub(/\s/,'_').downcase.to_sym
      spin = sp.gsub(/\s/,'_').downcase.to_sym unless sp.nil?
      v = LSI::PhysicalDrive::FirmwareState.new(state,spin)
    when :state
      v = v.gsub(/\s/,'_').downcase.to_sym
    when :mediatype
      v = v.scan(/[A-Z]/).join
    when :inquirydata
      v = v.gsub(/\s+/,' ')
    when :relativestateofcharge, :absolutestateofcharge, :remainingcapacity, :designcapacity # :remainingcapacityalarm is treated a plain string
      m = /(?<number>[0-9\.]+)\s+(?<unit>[A-Za-z%]+)/.match(v)
      v = LSI::BBU::NumberUnit.new(m[:number].to_f,m[:unit])
  end
  c[k] = v
end

#on_attribute_exit(new_state, event, *args) ⇒ Object



173
174
175
176
177
178
# File 'lib/elesai/megacli/megacli.rb', line 173

def on_attribute_exit(new_state, event, *args)
  @log.debug "      [#{current_state}] exit: entering #{new_state} throught event #{event}; args: #{args}"
  @context.close if Elesai::LSI::PhysicalDrive === @context.current and event != :attribute_line

  @context.flash!(new_state)
end

#on_exit_entry(new_state, event, *args) ⇒ Object



186
187
188
189
190
191
# File 'lib/elesai/megacli/megacli.rb', line 186

def on_exit_entry(new_state, event, *args)
  @log.debug "      [#{current_state}] exit: entering #{new_state} through event #{event}; args: #{args}"
  until @context.current.nil? do
    @context.close
  end
end

#on_start_exit(new_state, event, *args) ⇒ Object

Start



108
109
110
111
# File 'lib/elesai/megacli/megacli.rb', line 108

def on_start_exit(new_state, event, *args)
  @log.debug "      [#{current_state}]: on_exit : #{event} -> #{new_state}; args: #{args}"
  @context = Context.new(current_state,@lsi)
end

#parse!(lsi, opts) ⇒ Object

Parse!



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/elesai/megacli/megacli.rb', line 195

def parse!(lsi,opts)

  @lsi = lsi
  @log = Elesai::Logger.instance.log
  output = nil

  if STDIN.ready?
    output = $stdin.read
  else
    if opts[:fake].start_with? '-'
      megacli = opts[:megacli].nil? ? "Megacli" : opts[:megacli]
      command = "#{megacli} #{opts[:fake]} -nolog"
      command = Process.uid == 0 ? command : "sudo " << command
      output, stderr_str, status = Open3.capture3(command)
      raise RuntimeError, stderr_str unless status.exitstatus == 0
    else
      output = File.read(opts[:fake])
    end
  end

  output.each_line do |line|
    begin
      line.strip!
      line.gsub!(/^=+$/,'')
      next if line == ''

      match_flag = false
      @megacli.each do |k, v|
        if line =~ v[:re]
          v[:method].call(k,v[:re].match(line))
          match_flag = true
          break
        else
          match_flag = false
          next
        end
      end
      raise StandardError, "cannot parse '#{line}'" unless match_flag
    rescue ArgumentError # ignore lines with invalid byte sequence in UTF-8
      next
    end
  end
end