lib/knj/kvm.rb in knjrbfw-0.0.30 vs lib/knj/kvm.rb in knjrbfw-0.0.31
- old
+ new
@@ -1,6 +1,12 @@
+#This class and subclasses holds various functionality to view status for Kvm-instances.
class Knj::Kvm
+ #Lists all running Kvm-instances on this machine.
+ #===Examples
+ # Knj::Kvm.list do |kvm|
+ # print kvm.pid
+ # end
def self.list
list = []
Knj::Unix_proc.list("grep" => "kvm") do |proc_obj|
next if !proc_obj["cmd"].match(/^\/usr\/bin\/kvm\s+/)
@@ -33,28 +39,36 @@
return list
end
end
end
+#Describes each Kvm-instance.
class Knj::Kvm::Machine
+ #Sets the data called from Knj::Kvm.list.
def initialize(args)
@args = args
end
+ #Returns the PID of the Kvm-instance.
def pid
return @args[:pid]
end
+ #Returns the name from the Kvm-instance.
def name
return @args[:name]
end
+ #Returns the MAC from a network interfaces on the Kvm-instance.
def mac
raise "No MAC-address has been registered for this machine." if !@args.key?(:mac)
return @args[:mac]
end
+ #Returns what virtual interface the Kvm is using.
+ #===Examples
+ # kvm.iface #=> "vnet12"
def iface
if !@iface
res = Knj::Os.shellcmd("ifconfig | grep \"#{self.mac[3, self.mac.length]}\"")
if net_match = res.match(/^vnet(\d+)/)
@@ -65,10 +79,13 @@
end
return @iface
end
+ #Returns various data about the networking (how much have been sent and recieved).
+ #===Examples
+ # kvm.net_status #=> {:tx => 1024, :rx => 2048}
def net_status
res = Knj::Os.shellcmd("ifconfig \"#{self.iface}\"")
ret = {}
@@ -81,9 +98,12 @@
end
return ret
end
+ #Returns various data about how much disk IO the Kvm-instance have been using.
+ #===Examples
+ # kvm.io_status #=> {:read_bytes => 1024, :write_bytes => 2048}
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}'."
\ No newline at end of file