class Knj::Kvm::Machine

Describes each Kvm-instance.

Public Class Methods

new(args) click to toggle source

Sets the data called from Knj::Kvm.list.

# File lib/knj/kvm.rb, line 47
def initialize(args)
  @args = args
end

Public Instance Methods

iface() click to toggle source

Returns what virtual interface the Kvm is using.

Examples

kvm.iface #=> "vnet12"
# File lib/knj/kvm.rb, line 70
def iface
  if !@iface
    res = Knj::Os.shellcmd("ifconfig | grep \"#{self.mac[3, self.mac.length]}\"")
    
    if net_match = res.match(/^vnet(\d+)/)
      @iface = net_match[0]
    else
      raise "Could not figure out iface from '#{res}' for '#{self.name}'."
    end
  end
  
  return @iface
end
io_status() click to toggle source

Returns various data about how much disk IO the Kvm-instance have been using.

Examples

kvm.io_status #=> {:read_bytes => 1024, :write_bytes => 2048}
# File lib/knj/kvm.rb, line 106
def io_status
  io_status = File.read("/proc/#{self.pid}/io")
  
  if !matches = io_status.scan(/^(.+): (\d+)$/)
    raise "Could not match IO-status from: '#{io_status}'."
  end
  
  ret = {}
  matches.each do |match|
    ret[match[0].to_sym] = match[1].to_i
  end
  
  return ret
end
mac() click to toggle source

Returns the MAC from a network interfaces on the Kvm-instance.

# File lib/knj/kvm.rb, line 62
def mac
  raise "No MAC-address has been registered for this machine." if !@args.key?(:mac)
  return @args[:mac]
end
name() click to toggle source

Returns the name from the Kvm-instance.

# File lib/knj/kvm.rb, line 57
def name
  return @args[:name]
end
net_status() click to toggle source

Returns various data about the networking (how much have been sent and recieved).

Examples

kvm.net_status #=> {:tx => 1024, :rx => 2048}
# File lib/knj/kvm.rb, line 87
def net_status
  res = Knj::Os.shellcmd("ifconfig \"#{self.iface}\"")
  
  ret = {}
  
  if tx_bytes_match = res.match(/TX\s*bytes:\s*(\d+)/)
    ret[:tx] = tx_bytes_match[1].to_i
  end
  
  if rx_bytes_match = res.match(/RX\s*bytes:\s*(\d+)/)
    ret[:rx] = rx_bytes_match[1].to_i
  end
  
  return ret
end
pid() click to toggle source

Returns the PID of the Kvm-instance.

# File lib/knj/kvm.rb, line 52
def pid
  return @args[:pid]
end