class Vnstat::InterfaceCollection
A class encapsulating traffic information for all known network interfaces.
Public Class Methods
Retrieves the raw XML data for all interfaces.
@return [String]
# File lib/vnstat/interface_collection.rb, line 13 def self.load_data Utils.call_executable('--xml') end
Public Instance Methods
Returns traffic information for a certain interface.
@param [String] id The name of the interface. @raise [UnknownInterface] An error that is raised if the
specified interface is not tracked.
@return [Interface]
# File lib/vnstat/interface_collection.rb, line 50 def [](id) interfaces_hash.fetch(id.to_s) do raise UnknownInterface.new(id.to_s), "Unknown interface: #{id}" end end
Creates the traffic database for the given interface.
@param [String] id The network interface identifier @return [Interface, nil] The interface that has justed been added to
tracking.
# File lib/vnstat/interface_collection.rb, line 71 def create(id) success = Utils.call_executable_returning_status('--create', '-i', id) return nil unless success reload self[id] end
Sets the raw XML data for the {InterfaceCollection}.
@param [String] data A string representing the document.
Vnstat::Document#data=
# File lib/vnstat/interface_collection.rb, line 21 def data=(data) super each { |interface| interface.data = data } end
Iterates over each interface.
@yieldparam [Interface] interface
# File lib/vnstat/interface_collection.rb, line 61 def each(&block) interfaces_hash.each_value(&block) end
Returns the names of known interfaces.
@return [Array<String>]
# File lib/vnstat/interface_collection.rb, line 39 def ids interfaces_hash.keys end
A human readable representation of the {InterfaceCollection}.
@return [String]
# File lib/vnstat/interface_collection.rb, line 93 def inspect "#<#{self.class.name} ids: #{ids.inspect}>" end
Reset the total traffic counters and recount those using recorded months.
@return [InterfaceCollection]
# File lib/vnstat/interface_collection.rb, line 83 def rebuild success = Utils.call_executable_returning_status('--rebuildtotal') reload if success self end
Refreshes data cached in the current instance.
@return [InterfaceCollection]
# File lib/vnstat/interface_collection.rb, line 30 def reload self.data = self.class.load_data self end
Private Instance Methods
# File lib/vnstat/interface_collection.rb, line 99 def interfaces_hash @interfaces_hash ||= begin id_attr = xml_version == '2' ? :name : :id elements = data.xpath('//interface') elements.each_with_object({}) do |node, hash| id = node[id_attr] hash[id] = Interface.new(id, data) end end end