Class: Ifconf::InterfaceStatistics
- Inherits:
-
Object
- Object
- Ifconf::InterfaceStatistics
- Defined in:
- lib/ifconf/interface_statistics.rb
Overview
Immutable value object holding RX/TX packet, byte, and error counters for an interface.
Constant Summary collapse
- BYTE_UNITS =
["B", "KiB", "MiB", "GiB", "TiB"].freeze
- COUNTER_KEYS =
[ :rx_packets, :rx_bytes, :rx_errors, :rx_dropped, :rx_overruns, :rx_frame, :tx_packets, :tx_bytes, :tx_errors, :tx_dropped, :tx_overruns, :tx_carrier, :collisions ].freeze
Instance Attribute Summary collapse
-
#collisions ⇒ Object
readonly
Returns the value of attribute collisions.
-
#rx_bytes ⇒ Object
readonly
Returns the value of attribute rx_bytes.
-
#rx_dropped ⇒ Object
readonly
Returns the value of attribute rx_dropped.
-
#rx_errors ⇒ Object
readonly
Returns the value of attribute rx_errors.
-
#rx_frame ⇒ Object
readonly
Returns the value of attribute rx_frame.
-
#rx_overruns ⇒ Object
readonly
Returns the value of attribute rx_overruns.
-
#rx_packets ⇒ Object
readonly
Returns the value of attribute rx_packets.
-
#tx_bytes ⇒ Object
readonly
Returns the value of attribute tx_bytes.
-
#tx_carrier ⇒ Object
readonly
Returns the value of attribute tx_carrier.
-
#tx_dropped ⇒ Object
readonly
Returns the value of attribute tx_dropped.
-
#tx_errors ⇒ Object
readonly
Returns the value of attribute tx_errors.
-
#tx_overruns ⇒ Object
readonly
Returns the value of attribute tx_overruns.
-
#tx_packets ⇒ Object
readonly
Returns the value of attribute tx_packets.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #drop_rate ⇒ Object
- #error_rate ⇒ Object
- #hash ⇒ Object
- #healthy? ⇒ Boolean
-
#initialize(**counters) ⇒ InterfaceStatistics
constructor
A new instance of InterfaceStatistics.
- #rx_bytes_human ⇒ Object
- #to_h ⇒ Object
- #total_bytes ⇒ Object
- #total_dropped ⇒ Object
- #total_errors ⇒ Object
- #total_packets ⇒ Object
- #tx_bytes_human ⇒ Object
Constructor Details
#initialize(**counters) ⇒ InterfaceStatistics
Returns a new instance of InterfaceStatistics.
38 39 40 41 42 |
# File 'lib/ifconf/interface_statistics.rb', line 38 def initialize(**counters) validate_counters(counters) assign_counters(counters) freeze end |
Instance Attribute Details
#collisions ⇒ Object (readonly)
Returns the value of attribute collisions.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def collisions @collisions end |
#rx_bytes ⇒ Object (readonly)
Returns the value of attribute rx_bytes.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def rx_bytes @rx_bytes end |
#rx_dropped ⇒ Object (readonly)
Returns the value of attribute rx_dropped.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def rx_dropped @rx_dropped end |
#rx_errors ⇒ Object (readonly)
Returns the value of attribute rx_errors.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def rx_errors @rx_errors end |
#rx_frame ⇒ Object (readonly)
Returns the value of attribute rx_frame.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def rx_frame @rx_frame end |
#rx_overruns ⇒ Object (readonly)
Returns the value of attribute rx_overruns.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def rx_overruns @rx_overruns end |
#rx_packets ⇒ Object (readonly)
Returns the value of attribute rx_packets.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def rx_packets @rx_packets end |
#tx_bytes ⇒ Object (readonly)
Returns the value of attribute tx_bytes.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def tx_bytes @tx_bytes end |
#tx_carrier ⇒ Object (readonly)
Returns the value of attribute tx_carrier.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def tx_carrier @tx_carrier end |
#tx_dropped ⇒ Object (readonly)
Returns the value of attribute tx_dropped.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def tx_dropped @tx_dropped end |
#tx_errors ⇒ Object (readonly)
Returns the value of attribute tx_errors.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def tx_errors @tx_errors end |
#tx_overruns ⇒ Object (readonly)
Returns the value of attribute tx_overruns.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def tx_overruns @tx_overruns end |
#tx_packets ⇒ Object (readonly)
Returns the value of attribute tx_packets.
6 7 8 |
# File 'lib/ifconf/interface_statistics.rb', line 6 def tx_packets @tx_packets end |
Class Method Details
.build(rx: {}, tx: {}, collisions: 0) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ifconf/interface_statistics.rb', line 16 def self.build(rx: {}, tx: {}, collisions: 0) new( rx_packets: rx.fetch(:packets, 0), rx_bytes: rx.fetch(:bytes, 0), rx_errors: rx.fetch(:errors, 0), rx_dropped: rx.fetch(:dropped, 0), rx_overruns: rx.fetch(:overruns, 0), rx_frame: rx.fetch(:frame, 0), tx_packets: tx.fetch(:packets, 0), tx_bytes: tx.fetch(:bytes, 0), tx_errors: tx.fetch(:errors, 0), tx_dropped: tx.fetch(:dropped, 0), tx_overruns: tx.fetch(:overruns, 0), tx_carrier: tx.fetch(:carrier, 0), collisions: collisions, ) end |
.zero ⇒ Object
34 35 36 |
# File 'lib/ifconf/interface_statistics.rb', line 34 def self.zero build end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
96 97 98 |
# File 'lib/ifconf/interface_statistics.rb', line 96 def ==(other) other.is_a?(InterfaceStatistics) && to_h == other.to_h end |
#drop_rate ⇒ Object
66 67 68 69 70 |
# File 'lib/ifconf/interface_statistics.rb', line 66 def drop_rate return 0.0 if total_packets.zero? total_dropped.to_f / total_packets end |
#error_rate ⇒ Object
60 61 62 63 64 |
# File 'lib/ifconf/interface_statistics.rb', line 60 def error_rate return 0.0 if total_packets.zero? total_errors.to_f / total_packets end |
#hash ⇒ Object
102 103 104 |
# File 'lib/ifconf/interface_statistics.rb', line 102 def hash to_h.hash end |
#healthy? ⇒ Boolean
72 73 74 |
# File 'lib/ifconf/interface_statistics.rb', line 72 def healthy? @rx_errors.zero? && @tx_errors.zero? end |
#rx_bytes_human ⇒ Object
76 77 78 |
# File 'lib/ifconf/interface_statistics.rb', line 76 def rx_bytes_human humanize_bytes(@rx_bytes) end |
#to_h ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ifconf/interface_statistics.rb', line 84 def to_h { rx_packets: @rx_packets, rx_bytes: @rx_bytes, rx_errors: @rx_errors, rx_dropped: @rx_dropped, rx_overruns: @rx_overruns, rx_frame: @rx_frame, tx_packets: @tx_packets, tx_bytes: @tx_bytes, tx_errors: @tx_errors, tx_dropped: @tx_dropped, tx_overruns: @tx_overruns, tx_carrier: @tx_carrier, collisions: @collisions } end |
#total_bytes ⇒ Object
48 49 50 |
# File 'lib/ifconf/interface_statistics.rb', line 48 def total_bytes @rx_bytes + @tx_bytes end |
#total_dropped ⇒ Object
56 57 58 |
# File 'lib/ifconf/interface_statistics.rb', line 56 def total_dropped @rx_dropped + @tx_dropped end |
#total_errors ⇒ Object
52 53 54 |
# File 'lib/ifconf/interface_statistics.rb', line 52 def total_errors @rx_errors + @tx_errors end |
#total_packets ⇒ Object
44 45 46 |
# File 'lib/ifconf/interface_statistics.rb', line 44 def total_packets @rx_packets + @tx_packets end |
#tx_bytes_human ⇒ Object
80 81 82 |
# File 'lib/ifconf/interface_statistics.rb', line 80 def tx_bytes_human humanize_bytes(@tx_bytes) end |