Skip to content

Commit c233b70

Browse files
author
Vincent Landgraf
committed
#10 create solaris module for testing
1 parent 378d178 commit c233b70

3 files changed

Lines changed: 344 additions & 48 deletions

File tree

lib/vmstat.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Vmstat
2020
autoload :ProcFS, "vmstat/procfs"
2121
autoload :Stub, "vmstat/stub"
2222
autoload :Snapshot, "vmstat/snapshot"
23+
autoload :Solaris, "vmstat/solaris"
2324
extend Stub # the default empty implementation
2425

2526
# @!method self.boot_time
@@ -103,5 +104,5 @@ def self.loopback_devices
103104
require "vmstat/netopenbsd"
104105
elsif RUBY_PLATFORM =~ /solaris|smartos/
105106
# command based implementation of mem, net, cpu
106-
require "vmstat/solaris"
107+
Vmstat.send(:include, Vmstat::Solaris)
107108
end

lib/vmstat/solaris.rb

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,75 @@
11
module Vmstat
2-
def self.cpu
3-
kstat = `kstat -p "cpu_stat:::/idle|kernel|user/"`
4-
cpus = Hash.new { |h, k| h[k] = Hash.new }
2+
module Solaris
3+
module ClassMethods
4+
def cpu
5+
kstat = `kstat -p "cpu_stat:::/idle|kernel|user/"`
6+
cpus = Hash.new { |h, k| h[k] = Hash.new }
57

6-
kstat.lines.each do |line|
7-
_, _, cpu, key, value = line.strip.split(/:|\s+/)
8-
values[cpu] = value
9-
end
8+
kstat.lines.each do |line|
9+
_, cpu, _, key, value = line.strip.split(/:|\s+/)
10+
cpus[cpu.to_i][key] = value
11+
end
1012

11-
cpus.map do |k, v|
12-
name = k.gsub(/cpu_stat/, "").to_i
13-
Cpu.new(name, v["user"].to_i, v["kernel"].to_i, 0, v["idle"].to_i)
14-
end
15-
end
13+
cpus.map do |num, v|
14+
Cpu.new(num, v["user"].to_i, v["kernel"].to_i, 0, v["idle"].to_i)
15+
end
16+
end
1617

17-
def self.boot_time
18-
Time.at(`kstat -p unix:::boot_time`.strip.split(/\s+/).last.to_i)
19-
end
18+
def boot_time
19+
Time.at(`kstat -p unix:::boot_time`.strip.split(/\s+/).last.to_i)
20+
end
2021

21-
def self.memory
22-
kstat = `kstat -p -n system_pages`
23-
values = Hash.new
22+
def memory
23+
kstat = `kstat -p -n system_pages`
24+
values = Hash.new
2425

25-
kstat.lines.each do |line|
26-
_, _, _, key, value = line.strip.split(/:|\s+/)
27-
values[key] = value
28-
end
26+
kstat.lines.each do |line|
27+
_, _, _, key, value = line.strip.split(/:|\s+/)
28+
values[key] = value
29+
end
2930

30-
total = values['pagestotal'].to_i
31-
free = values['pagesfree'].to_i
32-
locked = values['pageslocked'].to_i
33-
34-
Memory.new(pagesize,
35-
locked, # wired
36-
total - free - locked, # active
37-
0, # inactive
38-
free, # free
39-
0, #pageins
40-
0) #pageouts
41-
end
31+
total = values['pagestotal'].to_i
32+
free = values['pagesfree'].to_i
33+
locked = values['pageslocked'].to_i
34+
35+
Memory.new(pagesize,
36+
locked, # wired
37+
total - free - locked, # active
38+
0, # inactive
39+
free, # free
40+
0, #pageins
41+
0) #pageouts
42+
end
4243

43-
def self.network_interfaces
44-
kstat = `kstat -p link:::`
45-
itfs = Hash.new { |h, k| h[k] = Hash.new }
44+
def network_interfaces
45+
kstat = `kstat -p link:::`
46+
itfs = Hash.new { |h, k| h[k] = Hash.new }
4647

47-
kstat.lines.each do |line|
48-
_, _, name, key, value = line.strip.split(/:|\s+/)
49-
itfs[name][key] = value
48+
kstat.lines.each do |line|
49+
_, _, name, key, value = line.strip.split(/:|\s+/)
50+
itfs[name.to_sym][key] = value
51+
end
52+
53+
itfs.map do |k, v|
54+
NetworkInterface.new(k, v['rbytes64'].to_i,
55+
v['ierrors'].to_i,
56+
0,
57+
v['obytes64'].to_i,
58+
v['oerrors'].to_i,
59+
NetworkInterface::ETHERNET_TYPE)
60+
end
61+
end
5062
end
5163

52-
itfs.map do |k, v|
53-
NetworkInterface.new(k, v['rbytes64'].to_i,
54-
v['ierrors'].to_i,
55-
0,
56-
v['obytes64'].to_i,
57-
v['oerrors'].to_i,
58-
NetworkInterface::ETHERNET_TYPE)
64+
extend ClassMethods
65+
66+
def self.included base
67+
base.instance_eval do
68+
def cpu; Vmstat::Solaris.cpu end
69+
def boot_time; Vmstat::Solaris.boot_time end
70+
def memory; Vmstat::Solaris.memory end
71+
def network_interfaces; Vmstat::Solaris.network_interfaces end
72+
end
5973
end
6074
end
6175
end

0 commit comments

Comments
 (0)