Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for freebsd network plugin; plus fixes #1841

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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