Module: DeadBro::Collectors::Network
- Defined in:
- lib/dead_bro/collectors/network.rb
Overview
Network collector exposes best-effort rx/tx byte counters and per-interval rates for Linux systems via /proc/net/dev.
Constant Summary collapse
- SAMPLE_KEY =
"network"
Class Method Summary collapse
- .build_interface_stats(prev, current, now) ⇒ Object
- .collect ⇒ Object
- .current_time ⇒ Object
- .default_ignore ⇒ Object
- .ignore_interfaces ⇒ Object
- .linux? ⇒ Boolean
- .macos? ⇒ Boolean
- .read_interfaces_linux ⇒ Object
- .read_interfaces_macos ⇒ Object
Class Method Details
.build_interface_stats(prev, current, now) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/dead_bro/collectors/network.rb', line 224 def build_interface_stats(prev, current, now) prev_ts = prev && prev["timestamp"] elapsed = prev_ts ? (now - prev_ts.to_f) : nil current.map do |name, data| prev_data = prev && prev["interfaces"] && prev["interfaces"][name] rx_rate = tx_rate = nil if elapsed && elapsed > 0 && prev_data rx_delta = data["rx_bytes"] - prev_data["rx_bytes"].to_i tx_delta = data["tx_bytes"] - prev_data["tx_bytes"].to_i rx_rate = (rx_delta / elapsed.to_f).round(2) if rx_delta >= 0 tx_rate = (tx_delta / elapsed.to_f).round(2) if tx_delta >= 0 end { name: name, rx_bytes: data["rx_bytes"], tx_bytes: data["tx_bytes"], rx_bytes_per_s: rx_rate, tx_bytes_per_s: tx_rate } end rescue [] end |
.collect ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/dead_bro/collectors/network.rb', line 15 def collect if linux? && File.readable?("/proc/net/dev") current = read_interfaces_linux elsif macos? current = read_interfaces_macos else return {available: false} end return {available: false} if current.empty? now = current_time prev = SampleStore.load(SAMPLE_KEY) SampleStore.save(SAMPLE_KEY, {"timestamp" => now, "interfaces" => current}) # Filter to keep only the top interface by total activity (rx + tx) top_interface = current.max_by do |_, data| (data["rx_bytes"] || 0) + (data["tx_bytes"] || 0) end # current is a Hash: { "eth0" => { ... }, ... } # top_interface is an Array: ["eth0", { ... }] or nil filtered_current = {} filtered_current[top_interface[0]] = top_interface[1] if top_interface # Save *all* current interfaces to store for continuity, # but only report the top one to the backend. # Actually, if we switch top interface, we need history for the new one. # So we should save all, but only return one. { available: true, interfaces: build_interface_stats(prev, filtered_current, now) } rescue => e { error_class: e.class.name, error_message: e..to_s[0, 500] } end |
.current_time ⇒ Object
71 72 73 74 75 |
# File 'lib/dead_bro/collectors/network.rb', line 71 def current_time Process.clock_gettime(Process::CLOCK_MONOTONIC) rescue Time.now.to_f end |
.default_ignore ⇒ Object
87 88 89 |
# File 'lib/dead_bro/collectors/network.rb', line 87 def default_ignore %w[lo lo0 docker0] end |
.ignore_interfaces ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/dead_bro/collectors/network.rb', line 77 def ignore_interfaces if DeadBro.configuration.respond_to?(:interfaces_ignore) DeadBro.configuration.interfaces_ignore || default_ignore else default_ignore end rescue default_ignore end |
.linux? ⇒ Boolean
57 58 59 60 61 62 |
# File 'lib/dead_bro/collectors/network.rb', line 57 def linux? host_os = RbConfig::CONFIG["host_os"].to_s.downcase host_os.include?("linux") rescue false end |
.macos? ⇒ Boolean
64 65 66 67 68 69 |
# File 'lib/dead_bro/collectors/network.rb', line 64 def macos? host_os = RbConfig::CONFIG["host_os"].to_s.downcase host_os.include?("darwin") rescue false end |
.read_interfaces_linux ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dead_bro/collectors/network.rb', line 91 def read_interfaces_linux ignored = ignore_interfaces interfaces = {} File.foreach("/proc/net/dev") do |line| next unless line.include?(":") name, data = line.split(":", 2) name = name.strip next if ignored.include?(name) fields = data.split # /proc/net/dev format: # Inter-| Receive | Transmit # face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed rx_bytes = begin Integer(fields[0]) rescue nil end tx_bytes = begin Integer(fields[8]) rescue nil end next unless rx_bytes && tx_bytes interfaces[name] = { "rx_bytes" => rx_bytes, "tx_bytes" => tx_bytes } end interfaces rescue {} end |
.read_interfaces_macos ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/dead_bro/collectors/network.rb', line 129 def read_interfaces_macos ignored = ignore_interfaces interfaces = {} # netstat -ib output format (simplified): # Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll # lo0 16384 <Link#1> 309756 0 49057632 309756 0 49057632 0 # en0 1500 <Link#4> 88:66:5a:00:22:11 2685232 0 3123456789 1501234 0 234567890 0 output = `netstat -ib` output.each_line do |line| fields = line.split next if fields.size < 10 # heuristic check for header or malformed line name = fields[0] fields[2] network = fields[2] # We only care about lines with <Link#...> which contain the byte counters next unless network && network.start_with?("<Link#") next if ignored.include?(name) # Header columns: Name(0) Mtu(1) Network(2) Address(3) Ipkts(4) Ierrs(5) Ibytes(6) Opkts(7) Oerrs(8) Obytes(9) Coll(10) # Note: Address column might be missing if no MAC address (like lo0), checking field alignment # netstat -ib alignment is tricky, sometimes space separated. # Assuming standard output where <Link#..> is present: # For <Link#...> lines: # Name Mtu Network Address Ipkts Ierrs Ibytes ... # en0 1500 <Link#4> 88:66:5a:00:22:11 ... ... bytes(6) ... bytes(9) # lo0 16384 <Link#1> ... ... bytes(5?) -> No address column for lo0 link row? # Let's re-verify netstat -ib output. # Actually "Address" column exists for links, usually MAC address for en0, empty/implied for lo0? # Wait, regex is safer. # Try to identify based on identifying the Network column being <Link...> # Usually: # fields[0] = Name # ... # fields[2] = <Link#...> # ... # We need to find Ibytes and Obytes. # If Address is present (MAC), Ibytes is at index 6, Obytes at 9. # If Address is NOT present (?), indices shift? # Actually netstat -ib usually aligns content. # Let's count from the end? # Typical line: en0 1500 <Link#4> 88:66:5a:... 2685232 0 3123456789 1501234 0 234567890 0 # fields: [en0, 1500, <Link#4>, MAC, Ipkts, Ierrs, Ibytes, Opkts, Oerrs, Obytes, Coll] -> 11 fields # Ibytes = 6, Obytes = 9 # lo0 line: lo0 16384 <Link#1> 309756 0 49057632 309756 0 49057632 0 # fields: [lo0, 16384, <Link#1>, Ipkts, Ierrs, Ibytes, Opkts, Oerrs, Obytes, Coll] -> 10 fields? Address missing? # Yes, lo0 often has no address in Link row. # Ibytes = 5, Obytes = 8 # Logic: # if fields[3] looks like a MAC address, use 6 and 9. # else (assuming it's Ipkts), use 5 and 8. # Actually, Ipkts is always an integer. MAC is xx:xx:xx... idx_ibytes = 6 idx_obytes = 9 if /^\d+$/.match?(fields[3]) # Field 3 is Ipkts (integer) -> Address column missing idx_ibytes = 5 idx_obytes = 8 end rx_bytes = begin Integer(fields[idx_ibytes]) rescue nil end tx_bytes = begin Integer(fields[idx_obytes]) rescue nil end next unless rx_bytes && tx_bytes interfaces[name] = { "rx_bytes" => rx_bytes, "tx_bytes" => tx_bytes } end interfaces rescue {} end |