Module: Rubyipmi

Defined in:
lib/rubyipmi.rb,
lib/rubyipmi/observablehash.rb,
lib/rubyipmi/freeipmi/connection.rb,
lib/rubyipmi/ipmitool/connection.rb,
lib/rubyipmi/ipmitool/errorcodes.rb,
lib/rubyipmi/commands/basecommand.rb

Defined Under Namespace

Modules: Freeipmi, Ipmitool Classes: BaseCommand, ObservableHash

Constant Summary collapse

PRIV_TYPES =
['CALLBACK', 'USER', 'OPERATOR', 'ADMINISTRATOR']

Class Method Summary collapse

Class Method Details

.connect(user, pass, host, provider = 'any', opts = {:driver => 'auto', :timeout => 'default', :debug => false}) ⇒ Object

The connect method will create a connection object based the provider type passed in If provider is left blank the function will use the first available provider



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
# File 'lib/rubyipmi.rb', line 34

def self.connect(user, pass, host, provider='any', opts={:driver => 'auto',
                                                         :timeout => 'default', :debug => false})

  # use this variable to reduce cmd calls
  installed = false

  if provider.is_a?(Hash)
    opts = provider
    provider = 'any'
  end

  # Verify options just in case user passed in a incomplete hash
  opts[:driver]  ||= 'auto'
  opts[:timeout] ||= 'default'
  opts[:debug]   = false if opts[:debug] != true

  if ! opts[:privilege].nil?
    if ! supported_privilege_type?(opts[:privilege])
      raise "Invalid privilege type :#{opts[:privilege]}, must be one of: #{PRIV_TYPES.join("\n")}"
    end
  end

  # use the first available provider
  if provider == 'any'
    if is_provider_installed?("freeipmi")
      provider = "freeipmi"
      installed = true
    elsif is_provider_installed?("ipmitool")
      provider = "ipmitool"
      installed = true
    else
      raise "No IPMI provider is installed, please install freeipmi or ipmitool"
    end
  end

  # Support multiple drivers
  # Note: these are just generic names of drivers that need to be specified for each provider
  unless valid_drivers.include?(opts[:driver])
    raise "You must specify a valid driver: #{valid_drivers.join(',')}"
  end

  # If the provider is available create a connection object
  if installed or is_provider_installed?(provider)
    if provider == "freeipmi"
      @conn = Rubyipmi::Freeipmi::Connection.new(user, pass, host, opts)
    elsif provider == "ipmitool"
      @conn = Rubyipmi::Ipmitool::Connection.new(user,pass,host, opts)
    else
      raise "Incorrect provider given, must use freeipmi or ipmitool"
    end
  else
    # Can't find the provider command line tool, maybe try other provider?
    raise "The IPMI provider: #{provider} is not installed"

  end
end

.connectionObject



96
97
98
99
# File 'lib/rubyipmi.rb', line 96

def self.connection
  return @conn if @conn
  raise "No Connection available, please use the connect method"
end

.get_diag(user, pass, host) ⇒ Object

gets data from the bmc device and puts in a hash for diagnostics



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rubyipmi.rb', line 144

def self.get_diag(user, pass, host)
  data = {}

  if Rubyipmi.is_provider_installed?('freeipmi')
    @freeconn = Rubyipmi::connect(user, pass, host, 'freeipmi')
    if @freeconn
      puts "Retrieving freeipmi data"
      data['freeipmi'] = @freeconn.get_diag
    end
  end
  if Rubyipmi.is_provider_installed?('ipmitool')
    @ipmiconn = Rubyipmi::connect(user, pass, host, 'ipmitool')
    if @ipmiconn
      puts "Retrieving ipmitool data"
      data['ipmitool'] = @ipmiconn.get_diag
    end
  end
  return data
end

.is_provider_installed?(provider) ⇒ Boolean

Return true or false if the provider is available

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rubyipmi.rb', line 111

def self.is_provider_installed?(provider)
  case provider
    when "freeipmi"
      cmdpath = locate_command('ipmipower')
    when "ipmitool"
      cmdpath = locate_command('ipmitool')
    else
      raise "Invalid BMC provider type"
  end
  # return false if command was not found
  return ! cmdpath.nil?
end

.locate_command(commandname) ⇒ Object

method used to find the command which also makes it easier to mock with



102
103
104
105
106
107
108
# File 'lib/rubyipmi.rb', line 102

def self.locate_command(commandname)
  location = `which #{commandname}`.strip
  if not $?.success?
    location = nil
  end
  return location
end

.provider_installed?Boolean

returns true if any of the providers are installed

Returns:

  • (Boolean)


129
130
131
# File 'lib/rubyipmi.rb', line 129

def self.provider_installed?
  providers_installed?.length > 0
end

.providersObject



124
125
126
# File 'lib/rubyipmi.rb', line 124

def self.providers
  ["freeipmi", "ipmitool"]
end

.providers_installed?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
# File 'lib/rubyipmi.rb', line 133

def self.providers_installed?
  available = []
  providers.each do |prov|
    if is_provider_installed?(prov)
      available << prov
    end
  end
  return available
end

.supported_privilege_type?(type) ⇒ Boolean

returns boolean true if privilege type is valid

Returns:

  • (Boolean)


92
93
94
# File 'lib/rubyipmi.rb', line 92

def self.supported_privilege_type?(type)
   PRIV_TYPES.include?(type)
end

.valid_driversObject



27
28
29
# File 'lib/rubyipmi.rb', line 27

def self.valid_drivers
  ['auto', "lan15", "lan20", "open"]
end