Skip to content

Commit 43f15c0

Browse files
author
Vincent Landgraf
committed
#12 implements net/openbsd support using shellout
1 parent 2225888 commit 43f15c0

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

lib/vmstat.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,7 @@ def self.loopback_devices
9898

9999
if RUBY_PLATFORM =~ /linux/
100100
Vmstat.send(:extend, Vmstat::ProcFS)
101+
elsif RUBY_PLATFORM =~ /(net|open)bsd/
102+
# command based implementation of mem, net, cpu
103+
require "vmstat/netopenbsd"
101104
end

lib/vmstat/netopenbsd.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module Vmstat
2+
def self.cpu
3+
vmstat = `vmstat 1 1`.lines[2].chomp.split(/\s+/)
4+
[Cpu.new(0, vmstat[-3].to_i, vmstat[-2].to_i, 0, vmstat[-1].to_i)]
5+
end
6+
7+
def self.memory
8+
uvmexp = `vmstat -s`
9+
10+
Memory.new(
11+
extract_uvm_val(uvmexp, 'bytes per page'), # pagesize
12+
extract_uvm_val(uvmexp, 'pages managed'), # wired
13+
extract_uvm_val(uvmexp, 'pages active'), # active
14+
extract_uvm_val(uvmexp, 'pages inactive'), # inactive
15+
extract_uvm_val(uvmexp, 'pages free'), # free
16+
extract_uvm_val(uvmexp, 'pagein operations'), # pageins
17+
extract_uvm_val(uvmexp, 'pages being paged out') # pageouts
18+
)
19+
end
20+
21+
def self.network_interfaces
22+
bytes = `netstat -ibq`.lines.grep(/<Link>/) # bytes
23+
pkgs = `netstat -iqd`.lines.grep(/<Link>/) # packages
24+
25+
itf = Hash.new { |h, k| h[k] = NetworkInterface.new(k) }
26+
27+
bytes.each do |line|
28+
# Name Mtu Network Address Ibytes Obytes
29+
name, _, _, _, ibytes, obytes = line.split(/\s+/)
30+
itf[name].in_bytes = ibytes.to_i
31+
itf[name].out_bytes = obytes.to_i
32+
end
33+
34+
pkgs.each do |line|
35+
# Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Colls Drop
36+
name, _, _, _, _, ierrs, _, oerrs, _, drop = line.split(/\s+/)
37+
itf[name].in_errors = ierrs.to_i
38+
itf[name].in_drops = drop.to_i
39+
itf[name].out_errors = oerrs.to_i
40+
end
41+
42+
itf.each do |name, nic|
43+
if name =~ /lo\d+/
44+
nic.type = NetworkInterface::LOOPBACK_TYPE
45+
else
46+
nic.type = NetworkInterface::ETHERNET_TYPE
47+
end
48+
end
49+
50+
itf.values
51+
end
52+
53+
def self.extract_uvm_val(uvmexp, name)
54+
regexp = Regexp.new('(\d+)\s' + name)
55+
uvmexp.lines.grep(regexp) do |line|
56+
return $1.to_i
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)