Skip to content

Commit

Permalink
Unit tests for freebsd network plugin; plus fixes
Browse files Browse the repository at this point in the history
This adds unittests for the FreeBSD network plugin. It also fixes
various issues:

* It now running on modern freebsd which has extra columns in netstat output.
* It now properly reports encapuslation, MTU, and metric

It also includes a test case with `::1` on `lo1` to start building out
support for sorting out chef#1838

Signed-off-by: Phil Dibowitz <[email protected]>
  • Loading branch information
jaymzh committed Oct 22, 2024
1 parent 88f6936 commit 0d76bd7
Show file tree
Hide file tree
Showing 2 changed files with 388 additions and 9 deletions.
43 changes: 34 additions & 9 deletions lib/ohai/plugins/freebsd/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@
flags = $1.split(",")
iface[cint][:flags] = flags if flags.length > 0
end
if line =~ /metric: (\d+) mtu: (\d+)/
if line =~ /metric (\d+) mtu (\d+)/
iface[cint][:metric] = $1
iface[cint][:mtu] = $2
end
if line =~ /media: (\w+)/
iface[cint][:encapsulation] = $1
end
end

so = shell_out("arp -an")
Expand All @@ -104,22 +107,44 @@
# which have been auto-configured (interfaces statically configured
# into a system, but not located at boot time are not shown).
so = shell_out("netstat -ibdn")
so.stdout.lines do |line|
head = so.stdout.lines[0]
have_drop = false
if head =~ /Idrop/
have_drop = true
# Name Mtu Network Address Ipkts Ierrs Idrop Ibytes Opkts Oerrs Obytes Coll Drop
# vtnet0 1500 <Link#1> fa:16:3e:ba:3e:25 579 0 0 46746 210 0 26242 0 0
# $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11
regex = /^([\w\.\*]+)\s+\d+\s+<Link#\d+>\s+([\w\d:]*)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
else
# Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Drop
# ed0 1500 <Link#1> 54:52:00:68:92:85 333604 26 151905886 175472 0 24897542 0 905
# $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
if line =~ /^([\w\.\*]+)\s+\d+\s+<Link#\d+>\s+([\w:]*)\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
regex = /^([\w\.\*]+)\s+\d+\s+<Link#\d+>\s+([\w\d:]*)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
end
so.stdout.lines do |line|
if line =~ regex
net_counters[$1] ||= Mash.new
net_counters[$1]["rx"] ||= Mash.new
net_counters[$1]["tx"] ||= Mash.new
net_counters[$1]["rx"]["packets"] = $3
net_counters[$1]["rx"]["errors"] = $4
net_counters[$1]["rx"]["bytes"] = $5
net_counters[$1]["tx"]["packets"] = $6
net_counters[$1]["tx"]["errors"] = $7
net_counters[$1]["tx"]["bytes"] = $8
net_counters[$1]["tx"]["collisions"] = $9
net_counters[$1]["tx"]["dropped"] = $10
if have_drop
net_counters[$1]["rx"]["dropped"] = $5
net_counters[$1]["rx"]["bytes"] = $6
net_counters[$1]["tx"]["packets"] = $7
net_counters[$1]["tx"]["errors"] = $8
net_counters[$1]["tx"]["bytes"] = $9
net_counters[$1]["tx"]["collisions"] = $10
net_counters[$1]["tx"]["dropped"] = $11
else
net_counters[$1]["rx"]["bytes"] = $5
net_counters[$1]["tx"]["packets"] = $6
net_counters[$1]["tx"]["errors"] = $7
net_counters[$1]["tx"]["bytes"] = $8
net_counters[$1]["tx"]["collisions"] = $9
net_counters[$1]["tx"]["dropped"] = $10
end

end
end

Expand Down
Loading

0 comments on commit 0d76bd7

Please sign in to comment.