vmstat 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +100 -110
- data/ext/vmstat/vmstat.c +133 -140
- data/ext/vmstat/vmstat.h +11 -18
- data/lib/vmstat.rb +79 -37
- data/lib/vmstat/cpu.rb +4 -0
- data/lib/vmstat/disk.rb +20 -0
- data/lib/vmstat/load_average.rb +4 -0
- data/lib/vmstat/memory.rb +24 -0
- data/lib/vmstat/network_interface.rb +19 -0
- data/lib/vmstat/snapshot.rb +17 -0
- data/lib/vmstat/task.rb +6 -0
- data/lib/vmstat/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/vmstat/cpu_spec.rb +34 -0
- data/spec/vmstat/disk_spec.rb +32 -0
- data/spec/vmstat/load_average_spec.rb +23 -0
- data/spec/vmstat/memory_spec.rb +44 -0
- data/spec/vmstat/network_spec.rb +38 -0
- data/spec/vmstat/snapshot_spec.rb +49 -0
- data/spec/vmstat/task_spec.rb +28 -0
- data/spec/vmstat_spec.rb +20 -161
- metadata +25 -4
data/ext/vmstat/vmstat.h
CHANGED
@@ -1,22 +1,15 @@
|
|
1
1
|
#include <ruby.h>
|
2
2
|
|
3
|
-
|
4
|
-
VALUE vmstat_hardware = Qnil;
|
5
|
-
VALUE method_network(VALUE self);
|
6
|
-
VALUE method_cpu(VALUE self);
|
7
|
-
VALUE method_memory(VALUE self);
|
8
|
-
VALUE method_disk(VALUE self, VALUE path);
|
9
|
-
VALUE method_load_avg(VALUE self);
|
10
|
-
VALUE method_boot_time(VALUE self);
|
3
|
+
#define AVGCOUNT 3
|
11
4
|
|
12
|
-
|
13
|
-
|
5
|
+
VALUE vmstat = Qnil;
|
6
|
+
VALUE vmstat_network_interfaces(VALUE self);
|
7
|
+
VALUE vmstat_cpu(VALUE self);
|
8
|
+
VALUE vmstat_memory(VALUE self);
|
9
|
+
VALUE vmstat_disk(VALUE self, VALUE path);
|
10
|
+
VALUE vmstat_load_average(VALUE self);
|
11
|
+
VALUE vmstat_boot_time(VALUE self);
|
14
12
|
|
15
|
-
|
16
|
-
VALUE
|
17
|
-
|
18
|
-
VALUE SYM_ORIGIN, SYM_MOUNT, SYM_AVAILABLE_BYTES, SYM_TOTAL_BYTES, SYM_USED_BYTES;
|
19
|
-
VALUE SYM_PAGESIZE, SYM_WIRED, SYM_ACTIVE, SYM_INACTIVE, SYM_ZERO_FILLED,
|
20
|
-
SYM_REACTIVATED, SYM_PURGEABLE, SYM_PURGED, SYM_PAGEINS, SYM_PAGEOUTS,
|
21
|
-
SYM_FAULTS, SYM_COW_FAULTS, SYM_LOOKUPS, SYM_HITS, SYM_WIRED_BYTES,
|
22
|
-
SYM_ACTIVE_BYTES, SYM_INACTIVE_BYTES;
|
13
|
+
#if defined(__APPLE__)
|
14
|
+
VALUE vmstat_task(VALUE self);
|
15
|
+
#endif
|
data/lib/vmstat.rb
CHANGED
@@ -1,50 +1,92 @@
|
|
1
1
|
require "vmstat/version"
|
2
|
-
require "vmstat/vmstat" # native lib
|
3
2
|
|
4
3
|
module Vmstat
|
5
|
-
|
6
|
-
|
4
|
+
autoload :Cpu, "vmstat/cpu"
|
5
|
+
autoload :NetworkInterface, "vmstat/network_interface"
|
6
|
+
autoload :Disk, "vmstat/disk"
|
7
|
+
autoload :Memory, "vmstat/memory"
|
8
|
+
autoload :Task, "vmstat/task"
|
9
|
+
autoload :LoadAverage, "vmstat/load_average"
|
10
|
+
autoload :Snapshot, "vmstat/snapshot"
|
11
|
+
|
12
|
+
# Creates a full snapshot of the systems hardware statistics.
|
13
|
+
# @param [Array<String>] the paths to the disks to snapshot.
|
14
|
+
# @return [Vmstat::Snapshot] a snapshot of all statistics.
|
15
|
+
# @example
|
16
|
+
# Vmstat.snapshot # => #<struct Vmstat::Snapshot ...>
|
17
|
+
def self.snapshot(paths = ["/"])
|
18
|
+
Snapshot.new(paths)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Fetches the boot time of the system.
|
22
|
+
# @return [Time] the boot time as regular time object.
|
23
|
+
# @example
|
24
|
+
# Vmstat.boot_time # => 2012-10-09 18:42:37 +0200
|
25
|
+
def self.boot_time
|
26
|
+
# implemented in native extension ...
|
27
|
+
end
|
28
|
+
|
29
|
+
# Fetches the cpu statistics (usage counter for user, nice, system and idle)
|
30
|
+
# @return [Array<Vmstat::Cpu>] the array of cpu counter
|
31
|
+
# @example
|
32
|
+
# Vmstat.cpu # => [#<struct Vmstat::Cpu ...>, #<struct Vmstat::Cpu ...>]
|
33
|
+
def self.cpu
|
34
|
+
# implemented in native extension ...
|
35
|
+
end
|
36
|
+
|
37
|
+
# Fetches the usage data and other useful disk information for the given path.
|
38
|
+
# @param [String] path the path (mount point or device path) to the disk
|
39
|
+
# @return [Vmstat::Disk] the disk information
|
40
|
+
# @example
|
41
|
+
# Vmstat.disk("/") # => #<struct Vmstat::Disk type=:hfs, ...>
|
42
|
+
def self.disk(path)
|
43
|
+
# implemented in native extension ...
|
44
|
+
end
|
45
|
+
|
46
|
+
# Fetches the load average for the current system.
|
47
|
+
# @return [Vmstat::LoadAverage] the load average data
|
48
|
+
# @example
|
49
|
+
# Vmstat.load_average # => #<struct Vmstat::LoadAverage one_minute=...>
|
50
|
+
def self.load_average
|
51
|
+
# implemented in native extension ...
|
52
|
+
end
|
53
|
+
|
54
|
+
# Fetches the memory usage information.
|
55
|
+
# @return [Vmstat::Memory] the memory data like free, used und total.
|
56
|
+
# @example
|
57
|
+
# Vmstat.memory # => #<struct Vmstat::Memory ...>
|
58
|
+
def self.memory
|
59
|
+
# implemented in native extension ...
|
60
|
+
end
|
61
|
+
|
62
|
+
# Fetches the information for all available network devices.
|
63
|
+
# @return [Array<Vmstat::NetworkInterface>] the network device information
|
64
|
+
# @example
|
65
|
+
# Vmstat.network_interfaces # => [#<struct Vmstat::NetworkInterface ...>, ...]
|
66
|
+
def self.network_interfaces
|
67
|
+
# implemented in native extension ...
|
68
|
+
end
|
7
69
|
|
8
|
-
#
|
9
|
-
|
70
|
+
# Fetches time and memory usage for the current process.
|
71
|
+
# @note Currently only on Mac OS X
|
72
|
+
# @return [Array<Vmstat::Task>] the network device information
|
73
|
+
# @example
|
74
|
+
# Vmstat.task # => #<struct Vmstat::Task ...>
|
75
|
+
def self.task
|
76
|
+
# implemented in native extension ...
|
77
|
+
end
|
10
78
|
|
11
79
|
# Filters all available ethernet devices.
|
12
|
-
# @return [
|
13
|
-
# @example
|
14
|
-
# Vmstat.ethernet_devices # => { :en0 => {
|
15
|
-
# # :in_bytes=>7104874723,
|
16
|
-
# # :in_errors=>0,
|
17
|
-
# # :in_drops=>0,
|
18
|
-
# # :out_bytes=>478849502,
|
19
|
-
# # :out_errors=>0,
|
20
|
-
# # :type=>6},
|
21
|
-
# # :p2p0=> ...
|
80
|
+
# @return [Array<NetworkInterface>] the ethernet devices
|
22
81
|
def self.ethernet_devices
|
23
|
-
|
82
|
+
network_interfaces.select(&:ethernet?)
|
24
83
|
end
|
25
84
|
|
26
85
|
# Filters all available loopback devices.
|
27
|
-
# @return [
|
28
|
-
# @example
|
29
|
-
# Vmstat.loopback_devices # => { :lo0 => {
|
30
|
-
# # :in_bytes=>6935997,
|
31
|
-
# # :in_errors=>0,
|
32
|
-
# # :in_drops=>0,
|
33
|
-
# # :out_bytes=>6935997,
|
34
|
-
# # :out_errors=>0,
|
35
|
-
# # :type=>24
|
36
|
-
# # }}
|
86
|
+
# @return [Array<NetworkInterface>] the loopback devices
|
37
87
|
def self.loopback_devices
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
# A method to filter the devices.
|
42
|
-
# @param [Fixnum] type the type to filter for
|
43
|
-
# @return [Hash<Symbol, Hash>] the filtered device name and values
|
44
|
-
# @api private
|
45
|
-
def self.filter_devices(type)
|
46
|
-
network.select do |name, attrbutes|
|
47
|
-
attrbutes[:type] == type
|
48
|
-
end
|
88
|
+
network_interfaces.select(&:loopback?)
|
49
89
|
end
|
50
90
|
end
|
91
|
+
|
92
|
+
require "vmstat/vmstat" # native lib
|
data/lib/vmstat/cpu.rb
ADDED
data/lib/vmstat/disk.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Vmstat
|
2
|
+
class Disk < Struct.new(:type, :origin, :mount, :block_size,
|
3
|
+
:free_blocks, :available_blocks, :total_blocks)
|
4
|
+
def free_bytes
|
5
|
+
free_blocks * block_size
|
6
|
+
end
|
7
|
+
|
8
|
+
def available_bytes
|
9
|
+
available_blocks * block_size
|
10
|
+
end
|
11
|
+
|
12
|
+
def used_bytes
|
13
|
+
(total_blocks - free_blocks) * block_size
|
14
|
+
end
|
15
|
+
|
16
|
+
def total_bytes
|
17
|
+
total_blocks * block_size
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Vmstat
|
2
|
+
class Memory < Struct.new(:pagesize,
|
3
|
+
:wired, :active, :inactive, :free,
|
4
|
+
:pageins, :pageouts,
|
5
|
+
:zero_filled, :reactivated, :purgeable,
|
6
|
+
:purged, :faults,
|
7
|
+
:copy_on_write_faults, :lookups, :hits)
|
8
|
+
def wired_bytes
|
9
|
+
wired * pagesize
|
10
|
+
end
|
11
|
+
|
12
|
+
def active_bytes
|
13
|
+
active * pagesize
|
14
|
+
end
|
15
|
+
|
16
|
+
def inactive_bytes
|
17
|
+
inactive * pagesize
|
18
|
+
end
|
19
|
+
|
20
|
+
def free_bytes
|
21
|
+
free * pagesize
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Vmstat
|
2
|
+
class NetworkInterface < Struct.new(:name, :in_bytes, :in_errors, :in_drops,
|
3
|
+
:out_bytes, :out_errors, :type)
|
4
|
+
|
5
|
+
# The type of ethernet devices on freebsd/mac os x
|
6
|
+
ETHERNET_TYPE = 0x06
|
7
|
+
|
8
|
+
# The type of loopback devices on freebsd/mac os x
|
9
|
+
LOOPBACK_TYPE = 0x18
|
10
|
+
|
11
|
+
def loopback?
|
12
|
+
type == LOOPBACK_TYPE
|
13
|
+
end
|
14
|
+
|
15
|
+
def ethernet?
|
16
|
+
type == ETHERNET_TYPE
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Vmstat
|
2
|
+
class Snapshot
|
3
|
+
attr_reader :at, :boot_time, :cpu, :disks, :load_average,
|
4
|
+
:memory, :network_interfaces, :task
|
5
|
+
|
6
|
+
def initialize(paths = [])
|
7
|
+
@at = Time.now
|
8
|
+
@boot_time = Vmstat.boot_time
|
9
|
+
@cpu = Vmstat.cpu
|
10
|
+
@disks = paths.map { |path| Vmstat.disk(path) }
|
11
|
+
@load_average = Vmstat.load_average
|
12
|
+
@memory = Vmstat.memory
|
13
|
+
@network_interfaces = Vmstat.network_interfaces
|
14
|
+
@task = Vmstat.task
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/vmstat/task.rb
ADDED
data/lib/vmstat/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vmstat::Cpu do
|
4
|
+
context "Vmstat#cpu" do
|
5
|
+
let(:cpu) { Vmstat.cpu }
|
6
|
+
|
7
|
+
it "should return an array of ethernet information" do
|
8
|
+
cpu.should be_a(Array)
|
9
|
+
end
|
10
|
+
|
11
|
+
context "first cpu" do
|
12
|
+
let(:first_cpu) { cpu.first }
|
13
|
+
subject { first_cpu }
|
14
|
+
|
15
|
+
it "should return a vmstat cpu object" do
|
16
|
+
should be_a(Vmstat::Cpu)
|
17
|
+
end
|
18
|
+
|
19
|
+
context "methods" do
|
20
|
+
it { should respond_to(:user) }
|
21
|
+
it { should respond_to(:system) }
|
22
|
+
it { should respond_to(:nice) }
|
23
|
+
it { should respond_to(:idle) }
|
24
|
+
end
|
25
|
+
|
26
|
+
context "content" do
|
27
|
+
its(:user) { should be_a_kind_of(Numeric) }
|
28
|
+
its(:system) { should be_a_kind_of(Numeric) }
|
29
|
+
its(:nice) { should be_a_kind_of(Numeric) }
|
30
|
+
its(:idle) { should be_a_kind_of(Numeric) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vmstat::Disk do
|
4
|
+
context "Vmstat#disk" do
|
5
|
+
let(:disk) { Vmstat.disk("/") }
|
6
|
+
subject { disk }
|
7
|
+
|
8
|
+
it "should be a vmstat disk object" do
|
9
|
+
should be_a(Vmstat::Disk)
|
10
|
+
end
|
11
|
+
|
12
|
+
context "methods" do
|
13
|
+
it { should respond_to(:type) }
|
14
|
+
it { should respond_to(:origin) }
|
15
|
+
it { should respond_to(:mount) }
|
16
|
+
it { should respond_to(:free_bytes) }
|
17
|
+
it { should respond_to(:available_bytes) }
|
18
|
+
it { should respond_to(:used_bytes) }
|
19
|
+
it { should respond_to(:total_bytes) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "content" do
|
23
|
+
its(:type) { should be_a(Symbol) }
|
24
|
+
its(:origin) { should be_a(String) }
|
25
|
+
its(:mount) { should be_a(String) }
|
26
|
+
its(:free_bytes) { should be_a_kind_of(Numeric) }
|
27
|
+
its(:available_bytes) { should be_a_kind_of(Numeric) }
|
28
|
+
its(:used_bytes) { should be_a_kind_of(Numeric) }
|
29
|
+
its(:total_bytes) { should be_a_kind_of(Numeric) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vmstat::LoadAverage do
|
4
|
+
context "Vmstat#load_average" do
|
5
|
+
subject { Vmstat.load_average }
|
6
|
+
|
7
|
+
it "should be an vmstat load average object" do
|
8
|
+
should be_a(Vmstat::LoadAverage)
|
9
|
+
end
|
10
|
+
|
11
|
+
context "methods" do
|
12
|
+
it { should respond_to(:one_minute) }
|
13
|
+
it { should respond_to(:five_minutes) }
|
14
|
+
it { should respond_to(:fifteen_minutes) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context "content" do
|
18
|
+
its(:one_minute) { should be_a(Float) }
|
19
|
+
its(:five_minutes) { should be_a(Float) }
|
20
|
+
its(:fifteen_minutes) { should be_a(Float) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vmstat::Memory do
|
4
|
+
context "Vmstat#memory" do
|
5
|
+
let(:memory) { Vmstat.memory }
|
6
|
+
subject { memory }
|
7
|
+
|
8
|
+
it "should be a vmstat memory object" do
|
9
|
+
should be_a(Vmstat::Memory)
|
10
|
+
end
|
11
|
+
|
12
|
+
context "methods" do
|
13
|
+
it { should respond_to(:pagesize) }
|
14
|
+
it { should respond_to(:wired) }
|
15
|
+
it { should respond_to(:active) }
|
16
|
+
it { should respond_to(:inactive) }
|
17
|
+
it { should respond_to(:free) }
|
18
|
+
it { should respond_to(:wired_bytes) }
|
19
|
+
it { should respond_to(:active_bytes) }
|
20
|
+
it { should respond_to(:inactive_bytes) }
|
21
|
+
it { should respond_to(:free_bytes) }
|
22
|
+
it { should respond_to(:zero_filled) }
|
23
|
+
it { should respond_to(:reactivated) }
|
24
|
+
it { should respond_to(:faults) }
|
25
|
+
it { should respond_to(:copy_on_write_faults) }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "content" do
|
29
|
+
its(:pagesize) { should be_a_kind_of(Numeric) }
|
30
|
+
its(:wired) { should be_a_kind_of(Numeric) }
|
31
|
+
its(:active) { should be_a_kind_of(Numeric) }
|
32
|
+
its(:inactive) { should be_a_kind_of(Numeric) }
|
33
|
+
its(:free) { should be_a_kind_of(Numeric) }
|
34
|
+
its(:wired_bytes) { should be_a_kind_of(Numeric) }
|
35
|
+
its(:active_bytes) { should be_a_kind_of(Numeric) }
|
36
|
+
its(:inactive_bytes) { should be_a_kind_of(Numeric) }
|
37
|
+
its(:free_bytes) { should be_a_kind_of(Numeric) }
|
38
|
+
its(:zero_filled) { should be_a_kind_of(Numeric) }
|
39
|
+
its(:reactivated) { should be_a_kind_of(Numeric) }
|
40
|
+
its(:faults) { should be_a_kind_of(Numeric) }
|
41
|
+
its(:copy_on_write_faults) { should be_a_kind_of(Numeric) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vmstat::NetworkInterface do
|
4
|
+
context "Vmstat#network" do
|
5
|
+
let(:network) { Vmstat.network_interfaces }
|
6
|
+
|
7
|
+
it "should return the enthernet and loopback network data as an array" do
|
8
|
+
network.should be_a(Array)
|
9
|
+
end
|
10
|
+
|
11
|
+
context "loopback device" do
|
12
|
+
let(:loopback) { network.find { |interface| interface.loopback? } }
|
13
|
+
subject { loopback }
|
14
|
+
|
15
|
+
it "should be a vmstat network interface object" do
|
16
|
+
should be_a(Vmstat::NetworkInterface)
|
17
|
+
end
|
18
|
+
|
19
|
+
context "methods" do
|
20
|
+
it { should respond_to(:in_bytes) }
|
21
|
+
it { should respond_to(:out_bytes) }
|
22
|
+
it { should respond_to(:in_errors) }
|
23
|
+
it { should respond_to(:out_errors) }
|
24
|
+
it { should respond_to(:in_drops) }
|
25
|
+
it { should respond_to(:type) }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "content" do
|
29
|
+
its(:in_bytes) { should be_a_kind_of(Numeric) }
|
30
|
+
its(:out_bytes) { should be_a_kind_of(Numeric) }
|
31
|
+
its(:in_errors) { should be_a_kind_of(Numeric) }
|
32
|
+
its(:out_errors) { should be_a_kind_of(Numeric) }
|
33
|
+
its(:in_drops) { should be_a_kind_of(Numeric) }
|
34
|
+
its(:type) { should be_a_kind_of(Numeric) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|