Class: Ifconf::Snapshot
- Inherits:
-
Object
- Object
- Ifconf::Snapshot
- Includes:
- Enumerable
- Defined in:
- lib/ifconf/snapshot.rb
Overview
Immutable collection of parsed NetworkInterface objects and any parse errors from a single ifconfig run.
Instance Attribute Summary collapse
-
#captured_at ⇒ Object
readonly
Returns the value of attribute captured_at.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#interfaces ⇒ Object
readonly
Returns the value of attribute interfaces.
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #all_ipv4_addresses ⇒ Object
- #all_ipv6_addresses ⇒ Object
- #all_mac_addresses ⇒ Object
- #count ⇒ Object
- #deconstruct_keys(keys) ⇒ Object
- #down ⇒ Object
- #each(&block) ⇒ Object
- #error_count ⇒ Object
- #errors? ⇒ Boolean
- #ethernet_interfaces ⇒ Object
- #find_by_ip(ip) ⇒ Object
- #find_by_mac(mac) ⇒ Object
- #find_by_name(name) ⇒ Object (also: #[])
- #hard_errors ⇒ Object
-
#initialize(interfaces, errors, captured_at) ⇒ Snapshot
constructor
A new instance of Snapshot.
- #loopback ⇒ Object
- #names ⇒ Object
- #operational ⇒ Object
- #total_rx_bytes ⇒ Object
- #total_tx_bytes ⇒ Object
- #unhealthy_interfaces ⇒ Object
- #up ⇒ Object
- #warnings ⇒ Object
- #with_ip ⇒ Object
- #with_ipv4 ⇒ Object
- #with_ipv6 ⇒ Object
Constructor Details
#initialize(interfaces, errors, captured_at) ⇒ Snapshot
Returns a new instance of Snapshot.
19 20 21 22 23 24 |
# File 'lib/ifconf/snapshot.rb', line 19 def initialize(interfaces, errors, captured_at) @interfaces = interfaces.freeze @errors = errors.freeze @captured_at = captured_at freeze end |
Instance Attribute Details
#captured_at ⇒ Object (readonly)
Returns the value of attribute captured_at.
6 7 8 |
# File 'lib/ifconf/snapshot.rb', line 6 def captured_at @captured_at end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/ifconf/snapshot.rb', line 6 def errors @errors end |
#interfaces ⇒ Object (readonly)
Returns the value of attribute interfaces.
6 7 8 |
# File 'lib/ifconf/snapshot.rb', line 6 def interfaces @interfaces end |
Class Method Details
.build(interfaces:, errors:) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/ifconf/snapshot.rb', line 8 def self.build(interfaces:, errors:) iface_hash = {} interfaces.each do |iface| raise Ifconf::Error, "duplicate interface name: #{iface.name}" if iface_hash.key?(iface.name) iface_hash[iface.name] = iface end new(iface_hash, errors, Time.now) end |
Instance Method Details
#all ⇒ Object
57 58 59 |
# File 'lib/ifconf/snapshot.rb', line 57 def all @interfaces.values end |
#all_ipv4_addresses ⇒ Object
101 102 103 |
# File 'lib/ifconf/snapshot.rb', line 101 def all_ipv4_addresses all.filter_map { |i| i.ipv4_config.address }.uniq end |
#all_ipv6_addresses ⇒ Object
105 106 107 |
# File 'lib/ifconf/snapshot.rb', line 105 def all_ipv6_addresses all.flat_map(&:ipv6_addresses).uniq end |
#all_mac_addresses ⇒ Object
109 110 111 |
# File 'lib/ifconf/snapshot.rb', line 109 def all_mac_addresses all.filter_map(&:mac_address).uniq end |
#count ⇒ Object
65 66 67 |
# File 'lib/ifconf/snapshot.rb', line 65 def count @interfaces.size end |
#deconstruct_keys(keys) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ifconf/snapshot.rb', line 36 def deconstruct_keys(keys) if keys keys.each_with_object({}) do |key, hash| name = key.to_s hash[key] = @interfaces[name] if @interfaces.key?(name) end else @interfaces.transform_keys(&:to_sym) end end |
#down ⇒ Object
97 98 99 |
# File 'lib/ifconf/snapshot.rb', line 97 def down all.reject(&:up?) end |
#each(&block) ⇒ Object
26 27 28 |
# File 'lib/ifconf/snapshot.rb', line 26 def each(&block) @interfaces.each_value(&block) end |
#error_count ⇒ Object
129 130 131 |
# File 'lib/ifconf/snapshot.rb', line 129 def error_count @errors.length end |
#errors? ⇒ Boolean
125 126 127 |
# File 'lib/ifconf/snapshot.rb', line 125 def errors? @errors.any? end |
#ethernet_interfaces ⇒ Object
85 86 87 |
# File 'lib/ifconf/snapshot.rb', line 85 def ethernet_interfaces all.select { |i| i.encapsulation == :ethernet } end |
#find_by_ip(ip) ⇒ Object
47 48 49 50 |
# File 'lib/ifconf/snapshot.rb', line 47 def find_by_ip(ip) ip_str = ip.to_s find { |iface| iface.owns_ip?(ip_str) } end |
#find_by_mac(mac) ⇒ Object
52 53 54 55 |
# File 'lib/ifconf/snapshot.rb', line 52 def find_by_mac(mac) mac_obj = MacAddress.parse(mac.to_s) find { |iface| iface.mac_address == mac_obj } end |
#find_by_name(name) ⇒ Object Also known as: []
30 31 32 |
# File 'lib/ifconf/snapshot.rb', line 30 def find_by_name(name) @interfaces[name.to_s] end |
#hard_errors ⇒ Object
137 138 139 |
# File 'lib/ifconf/snapshot.rb', line 137 def hard_errors @errors.select(&:error?) end |
#loopback ⇒ Object
89 90 91 |
# File 'lib/ifconf/snapshot.rb', line 89 def loopback all.find(&:loopback?) end |
#names ⇒ Object
61 62 63 |
# File 'lib/ifconf/snapshot.rb', line 61 def names @interfaces.keys end |
#operational ⇒ Object
69 70 71 |
# File 'lib/ifconf/snapshot.rb', line 69 def operational all.select(&:operational?) end |
#total_rx_bytes ⇒ Object
113 114 115 |
# File 'lib/ifconf/snapshot.rb', line 113 def total_rx_bytes all.sum { |i| i.statistics.rx_bytes } end |
#total_tx_bytes ⇒ Object
117 118 119 |
# File 'lib/ifconf/snapshot.rb', line 117 def total_tx_bytes all.sum { |i| i.statistics.tx_bytes } end |
#unhealthy_interfaces ⇒ Object
121 122 123 |
# File 'lib/ifconf/snapshot.rb', line 121 def unhealthy_interfaces all.reject(&:healthy?) end |
#up ⇒ Object
93 94 95 |
# File 'lib/ifconf/snapshot.rb', line 93 def up all.select(&:up?) end |
#warnings ⇒ Object
133 134 135 |
# File 'lib/ifconf/snapshot.rb', line 133 def warnings @errors.select(&:warning?) end |
#with_ip ⇒ Object
73 74 75 |
# File 'lib/ifconf/snapshot.rb', line 73 def with_ip all.select { |i| i.ipv4_config.present? || i.ipv6_configs.any? } end |
#with_ipv4 ⇒ Object
77 78 79 |
# File 'lib/ifconf/snapshot.rb', line 77 def with_ipv4 all.select { |i| i.ipv4_config.present? } end |
#with_ipv6 ⇒ Object
81 82 83 |
# File 'lib/ifconf/snapshot.rb', line 81 def with_ipv6 all.select { |i| i.ipv6_configs.any? } end |