Skip to content

Commit 5d29803

Browse files
committed
Unit tests for freebsd network plugin; plus fixes
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]>
1 parent 88f6936 commit 5d29803

File tree

2 files changed

+371
-9
lines changed

2 files changed

+371
-9
lines changed

lib/ohai/plugins/freebsd/network.rb

+34-9
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@
8080
flags = $1.split(",")
8181
iface[cint][:flags] = flags if flags.length > 0
8282
end
83-
if line =~ /metric: (\d+) mtu: (\d+)/
83+
if line =~ /metric (\d+) mtu (\d+)/
8484
iface[cint][:metric] = $1
8585
iface[cint][:mtu] = $2
8686
end
87+
if line =~ /media: (\w+)/
88+
iface[cint][:encapsulation] = $1
89+
end
8790
end
8891

8992
so = shell_out("arp -an")
@@ -104,22 +107,44 @@
104107
# which have been auto-configured (interfaces statically configured
105108
# into a system, but not located at boot time are not shown).
106109
so = shell_out("netstat -ibdn")
107-
so.stdout.lines do |line|
110+
head = so.stdout.lines[0]
111+
have_drop = false
112+
if head =~ /Idrop/
113+
have_drop = true
114+
# Name Mtu Network Address Ipkts Ierrs Idrop Ibytes Opkts Oerrs Obytes Coll Drop
115+
# vtnet0 1500 <Link#1> fa:16:3e:ba:3e:25 579 0 0 46746 210 0 26242 0 0
116+
# $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11
117+
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+)/
118+
else
108119
# Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Drop
109120
# ed0 1500 <Link#1> 54:52:00:68:92:85 333604 26 151905886 175472 0 24897542 0 905
110121
# $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
111-
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+)/
122+
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+)/
123+
end
124+
so.stdout.lines do |line|
125+
if line =~ regex
112126
net_counters[$1] ||= Mash.new
113127
net_counters[$1]["rx"] ||= Mash.new
114128
net_counters[$1]["tx"] ||= Mash.new
115129
net_counters[$1]["rx"]["packets"] = $3
116130
net_counters[$1]["rx"]["errors"] = $4
117-
net_counters[$1]["rx"]["bytes"] = $5
118-
net_counters[$1]["tx"]["packets"] = $6
119-
net_counters[$1]["tx"]["errors"] = $7
120-
net_counters[$1]["tx"]["bytes"] = $8
121-
net_counters[$1]["tx"]["collisions"] = $9
122-
net_counters[$1]["tx"]["dropped"] = $10
131+
if have_drop
132+
net_counters[$1]["rx"]["dropped"] = $5
133+
net_counters[$1]["rx"]["bytes"] = $6
134+
net_counters[$1]["tx"]["packets"] = $7
135+
net_counters[$1]["tx"]["errors"] = $8
136+
net_counters[$1]["tx"]["bytes"] = $9
137+
net_counters[$1]["tx"]["collisions"] = $10
138+
net_counters[$1]["tx"]["dropped"] = $11
139+
else
140+
net_counters[$1]["rx"]["bytes"] = $5
141+
net_counters[$1]["tx"]["packets"] = $6
142+
net_counters[$1]["tx"]["errors"] = $7
143+
net_counters[$1]["tx"]["bytes"] = $8
144+
net_counters[$1]["tx"]["collisions"] = $9
145+
net_counters[$1]["tx"]["dropped"] = $10
146+
end
147+
123148
end
124149
end
125150

0 commit comments

Comments
 (0)