Class: Ifconf::Snapshot

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_atObject (readonly)

Returns the value of attribute captured_at.



6
7
8
# File 'lib/ifconf/snapshot.rb', line 6

def captured_at
  @captured_at
end

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/ifconf/snapshot.rb', line 6

def errors
  @errors
end

#interfacesObject (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

#allObject



57
58
59
# File 'lib/ifconf/snapshot.rb', line 57

def all
  @interfaces.values
end

#all_ipv4_addressesObject



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_addressesObject



105
106
107
# File 'lib/ifconf/snapshot.rb', line 105

def all_ipv6_addresses
  all.flat_map(&:ipv6_addresses).uniq
end

#all_mac_addressesObject



109
110
111
# File 'lib/ifconf/snapshot.rb', line 109

def all_mac_addresses
  all.filter_map(&:mac_address).uniq
end

#countObject



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

#downObject



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_countObject



129
130
131
# File 'lib/ifconf/snapshot.rb', line 129

def error_count
  @errors.length
end

#errors?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/ifconf/snapshot.rb', line 125

def errors?
  @errors.any?
end

#ethernet_interfacesObject



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_errorsObject



137
138
139
# File 'lib/ifconf/snapshot.rb', line 137

def hard_errors
  @errors.select(&:error?)
end

#loopbackObject



89
90
91
# File 'lib/ifconf/snapshot.rb', line 89

def loopback
  all.find(&:loopback?)
end

#namesObject



61
62
63
# File 'lib/ifconf/snapshot.rb', line 61

def names
  @interfaces.keys
end

#operationalObject



69
70
71
# File 'lib/ifconf/snapshot.rb', line 69

def operational
  all.select(&:operational?)
end

#total_rx_bytesObject



113
114
115
# File 'lib/ifconf/snapshot.rb', line 113

def total_rx_bytes
  all.sum { |i| i.statistics.rx_bytes }
end

#total_tx_bytesObject



117
118
119
# File 'lib/ifconf/snapshot.rb', line 117

def total_tx_bytes
  all.sum { |i| i.statistics.tx_bytes }
end

#unhealthy_interfacesObject



121
122
123
# File 'lib/ifconf/snapshot.rb', line 121

def unhealthy_interfaces
  all.reject(&:healthy?)
end

#upObject



93
94
95
# File 'lib/ifconf/snapshot.rb', line 93

def up
  all.select(&:up?)
end

#warningsObject



133
134
135
# File 'lib/ifconf/snapshot.rb', line 133

def warnings
  @errors.select(&:warning?)
end

#with_ipObject



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_ipv4Object



77
78
79
# File 'lib/ifconf/snapshot.rb', line 77

def with_ipv4
  all.select { |i| i.ipv4_config.present? }
end

#with_ipv6Object



81
82
83
# File 'lib/ifconf/snapshot.rb', line 81

def with_ipv6
  all.select { |i| i.ipv6_configs.any? }
end