Class: Knj::Kvm::Machine

Inherits:
Object show all
Defined in:
lib/knj/kvm.rb

Overview

Describes each Kvm-instance.

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Machine

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



47
48
49
# File 'lib/knj/kvm.rb', line 47

def initialize(args)
  @args = args
end

Instance Method Details

#ifaceObject

Returns what virtual interface the Kvm is using.

Examples

kvm.iface #=> “vnet12”



70
71
72
73
74
75
76
77
78
79
80
81
82
# 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_statusObject

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

Examples

kvm.io_status #=> => 1024, :write_bytes => 2048



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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

#macObject

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



62
63
64
65
# 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

#nameObject

Returns the name from the Kvm-instance.



57
58
59
# File 'lib/knj/kvm.rb', line 57

def name
  return @args[:name]
end

#net_statusObject

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

Examples

kvm.net_status #=> => 1024, :rx => 2048



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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

#pidObject

Returns the PID of the Kvm-instance.



52
53
54
# File 'lib/knj/kvm.rb', line 52

def pid
  return @args[:pid]
end