vmstator 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/examples/basic_example.rb +28 -2
- data/lib/vmstator/active.rb +8 -0
- data/lib/vmstator/average.rb +8 -0
- data/lib/vmstator/cache.rb +31 -0
- data/lib/vmstator/disk.rb +35 -0
- data/lib/vmstator/disk_statistics.rb +25 -0
- data/lib/vmstator/disk_summary.rb +45 -0
- data/lib/vmstator/event_counter_statistics.rb +73 -0
- data/lib/vmstator/forks.rb +9 -0
- data/lib/vmstator/memory.rb +53 -0
- data/lib/vmstator/parser.rb +121 -0
- data/lib/vmstator/slab_info.rb +25 -0
- data/lib/vmstator/stats.rb +12 -0
- data/lib/vmstator/version.rb +1 -1
- data/lib/vmstator.rb +113 -164
- data/pkg/vmstator-1.0.1.gem +0 -0
- metadata +15 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9f01ed118a36cdc3d161ab2b7cf1c30693661da
|
4
|
+
data.tar.gz: c4a608ff0ec342043e2ce7afde48b5a87d797f28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c6ad37064f53314407b1372695dc4fd00b648bd27c93037b0bed4ee11878324703432ca06c53abec5ac2e9defb5898dc48b17adab05d384deb8e6822d7fadc1
|
7
|
+
data.tar.gz: 1f5526ebed499e79496a8de503753f53619b7ee44f59dd876621e20df97fdc064ea9a04d519acde1cf01844811d96264c94dfb158ef92f8256627217f4da8391
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
data/examples/basic_example.rb
CHANGED
@@ -1,6 +1,32 @@
|
|
1
1
|
# Path setting slight of hand:
|
2
2
|
$: << File.expand_path("../../lib", __FILE__)
|
3
3
|
require 'vmstator'
|
4
|
-
require 'pry'
|
5
4
|
|
6
|
-
|
5
|
+
parser = Vmstator::Parser.new
|
6
|
+
|
7
|
+
parser.active
|
8
|
+
# => Vmstator::DiskSummary
|
9
|
+
|
10
|
+
parser.average
|
11
|
+
# => Vmstator::DiskSummary
|
12
|
+
|
13
|
+
parser.disk_statistics
|
14
|
+
# => Vmstator::DiskStatistics
|
15
|
+
|
16
|
+
parser.disk_summary
|
17
|
+
# => Vmstator::DiskSummary
|
18
|
+
|
19
|
+
parser.event_counter_statistics
|
20
|
+
# => Vmstator::EventCounterStatistics
|
21
|
+
|
22
|
+
parser.slab_info
|
23
|
+
# => Vmstator::SlabInfo
|
24
|
+
|
25
|
+
parser.forks
|
26
|
+
# => Integer
|
27
|
+
|
28
|
+
parser.parse("-a -S m")
|
29
|
+
# => Whatever the flags were will determine output.
|
30
|
+
|
31
|
+
parser.version
|
32
|
+
# => String containing vmstat's version number.
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Vmstator
|
2
|
+
|
3
|
+
class Cache
|
4
|
+
attr_reader :num
|
5
|
+
attr_reader :name
|
6
|
+
attr_reader :size
|
7
|
+
attr_reader :total
|
8
|
+
attr_reader :pages
|
9
|
+
|
10
|
+
def initialize(data=false)
|
11
|
+
if data
|
12
|
+
update(data)
|
13
|
+
end
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def update(data)
|
18
|
+
if data
|
19
|
+
@num = data[:num].to_i
|
20
|
+
@name = data[:name]
|
21
|
+
@sec = data[:size].to_i
|
22
|
+
@total = data[:total].to_i
|
23
|
+
@pages = data[:pages].to_i
|
24
|
+
else
|
25
|
+
return false
|
26
|
+
end
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Vmstator
|
2
|
+
|
3
|
+
class Disk
|
4
|
+
attr_reader :ms
|
5
|
+
attr_reader :cur
|
6
|
+
attr_reader :sec
|
7
|
+
attr_reader :name
|
8
|
+
attr_reader :total
|
9
|
+
attr_reader :merged
|
10
|
+
attr_reader :sectors
|
11
|
+
|
12
|
+
def initialize(data=false)
|
13
|
+
if data
|
14
|
+
update(data)
|
15
|
+
end
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
def update(data)
|
20
|
+
if data
|
21
|
+
@ms = data[:ms] if data[:ms]
|
22
|
+
@cur = data[:cur] if data[:cur]
|
23
|
+
@sec = data[:sec] if data[:sec]
|
24
|
+
@name = data[:name] if data[:name]
|
25
|
+
@total = data[:total] if data[:total]
|
26
|
+
@merged = data[:merged] if data[:merged]
|
27
|
+
@sectors = data[:sectors] if data[:sectors]
|
28
|
+
else
|
29
|
+
return false
|
30
|
+
end
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Vmstator
|
2
|
+
|
3
|
+
class DiskStatistics
|
4
|
+
# attrs for each attribute
|
5
|
+
attr_reader :disks
|
6
|
+
|
7
|
+
def initialize(data=false)
|
8
|
+
@disks = []
|
9
|
+
if data
|
10
|
+
update(data)
|
11
|
+
end
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
def update(data)
|
16
|
+
if data
|
17
|
+
@disks << Disk.new(data)
|
18
|
+
else
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Vmstator
|
2
|
+
|
3
|
+
class DiskSummary
|
4
|
+
attr_reader :disks
|
5
|
+
attr_reader :partitions
|
6
|
+
attr_reader :total_reads
|
7
|
+
attr_reader :merged_reads
|
8
|
+
attr_reader :read_sectors
|
9
|
+
attr_reader :milli_reading
|
10
|
+
attr_reader :writes
|
11
|
+
attr_reader :merged_writes
|
12
|
+
attr_reader :written_sectors
|
13
|
+
attr_reader :milli_writing
|
14
|
+
attr_reader :inprogress_io
|
15
|
+
attr_reader :milli_spent_io
|
16
|
+
|
17
|
+
def initialize(data=false)
|
18
|
+
if data
|
19
|
+
update(data)
|
20
|
+
end
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
def update(data)
|
25
|
+
if data
|
26
|
+
@disks = data[:disks]
|
27
|
+
@partitions = data[:partitions]
|
28
|
+
@total_reads = data[:total_reads]
|
29
|
+
@merged_reads = data[:merged_reads]
|
30
|
+
@read_sectors = data[:read_sectors]
|
31
|
+
@milli_reading = data[:milli_reading]
|
32
|
+
@writes = data[:writes]
|
33
|
+
@merged_writes = data[:merged_writes]
|
34
|
+
@written_sectors = data[:written_sectors]
|
35
|
+
@milli_writing = data[:milli_writing]
|
36
|
+
@inprogress_io = data[:inprogress_io]
|
37
|
+
@milli_spent_io = data[:milli_spent_io]
|
38
|
+
else
|
39
|
+
return false
|
40
|
+
end
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Vmstator
|
2
|
+
|
3
|
+
class EventCounterStatistics
|
4
|
+
attr_reader :total_memory
|
5
|
+
attr_reader :used_memory
|
6
|
+
attr_reader :active_memory
|
7
|
+
attr_reader :inactive_memory
|
8
|
+
attr_reader :free_memory
|
9
|
+
attr_reader :buffer_memory
|
10
|
+
attr_reader :swap_cache
|
11
|
+
attr_reader :total_swap
|
12
|
+
attr_reader :used_swap
|
13
|
+
attr_reader :free_swap
|
14
|
+
attr_reader :non_nice_user_cpu_ticks
|
15
|
+
attr_reader :nice_user_cpu_ticks
|
16
|
+
attr_reader :system_cpu_ticks
|
17
|
+
attr_reader :idle_cpu_ticks
|
18
|
+
attr_reader :io_wait_cpu_ticks
|
19
|
+
attr_reader :irq_cpu_ticks
|
20
|
+
attr_reader :softirq_cpu_ticks
|
21
|
+
attr_reader :stolen_cpu_ticks
|
22
|
+
attr_reader :pages_paged_in
|
23
|
+
attr_reader :pages_paged_out
|
24
|
+
attr_reader :pages_swapped_in
|
25
|
+
attr_reader :pages_swapped_out
|
26
|
+
attr_reader :interrupts
|
27
|
+
attr_reader :cpu_context_switches
|
28
|
+
attr_reader :boot_time
|
29
|
+
attr_reader :forks
|
30
|
+
|
31
|
+
def initialize(data=false)
|
32
|
+
if data
|
33
|
+
update(data)
|
34
|
+
end
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
def update(data)
|
39
|
+
if data
|
40
|
+
@total_memory = data[:total_memory]
|
41
|
+
@used_memory = data[:used_memory]
|
42
|
+
@active_memory = data[:active_memory]
|
43
|
+
@inactive_memory = data[:inactive_memory]
|
44
|
+
@free_memory = data[:free_memory]
|
45
|
+
@buffer_memory = data[:buffer_memory]
|
46
|
+
@swap_cache = data[:swap_cache]
|
47
|
+
@total_swap = data[:total_swap]
|
48
|
+
@used_swap = data[:used_swap]
|
49
|
+
@free_swap = data[:free_swap]
|
50
|
+
@non_nice_user_cpu_ticks = data[:non_nice_user_cpu_ticks]
|
51
|
+
@nice_user_cpu_ticks = data[:nice_user_cpu_ticks]
|
52
|
+
@system_cpu_ticks = data[:system_cpu_ticks]
|
53
|
+
@idle_cpu_ticks = data[:idle_cpu_ticks]
|
54
|
+
@io_wait_cpu_ticks = data[:io_wait_cpu_ticks]
|
55
|
+
@irq_cpu_ticks = data[:irq_cpu_ticks]
|
56
|
+
@softirq_cpu_ticks = data[:softirq_cpu_ticks]
|
57
|
+
@stolen_cpu_ticks = data[:stolen_cpu_ticks]
|
58
|
+
@pages_paged_in = data[:pages_paged_in]
|
59
|
+
@pages_paged_out = data[:pages_paged_out]
|
60
|
+
@pages_swapped_in = data[:pages_swapped_in]
|
61
|
+
@pages_swapped_out = data[:pages_swapped_out]
|
62
|
+
@interrupts = data[:interrupts]
|
63
|
+
@cpu_context_switches = data[:cpu_context_switches]
|
64
|
+
@boot_time = data[:boot_time]
|
65
|
+
@forks = data[:forks]
|
66
|
+
else
|
67
|
+
return false
|
68
|
+
end
|
69
|
+
true
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Vmstator
|
2
|
+
|
3
|
+
class Memory < Stats
|
4
|
+
attr_reader :runnable # number of runnable processes
|
5
|
+
attr_reader :uninter # number of processes in uninterruptible sleep
|
6
|
+
attr_reader :used # amount of virtual memory used
|
7
|
+
attr_reader :free # amount of idle memory
|
8
|
+
attr_reader :buffer # amount of memory used as buffers
|
9
|
+
attr_reader :cache # amount of memory used as cache
|
10
|
+
attr_reader :swapped_in # amount of memory swapped in from disk (/s)
|
11
|
+
attr_reader :swapped_to # amount memory swapped to disk (/s)
|
12
|
+
attr_reader :blocks_recv # blocks received from a device (blocks/s)
|
13
|
+
attr_reader :blocks_sent # blocks sent to a block device (blocks/s)
|
14
|
+
attr_reader :interrupts # number of interrupts (/s)
|
15
|
+
attr_reader :cntxt_swtchs # number of context switches (/s)
|
16
|
+
attr_reader :non_kernel # time spent running non-kernel code
|
17
|
+
attr_reader :kernel # time spent running kernel code
|
18
|
+
attr_reader :idle_time # time spent idle
|
19
|
+
attr_reader :waiting # time spent waiting for IO
|
20
|
+
attr_reader :stolen # time stolen from a virtual machine
|
21
|
+
|
22
|
+
def update(data)
|
23
|
+
if data
|
24
|
+
@runnable = data[:r]
|
25
|
+
@uninter = data[:b]
|
26
|
+
@swapped_in = data[:si]
|
27
|
+
@swapped_to = data[:so]
|
28
|
+
@blocks_recv = data[:bi]
|
29
|
+
@blocks_sent = data[:bo]
|
30
|
+
@interrupts = data[:in]
|
31
|
+
@cntxt_swtchs = data[:cs]
|
32
|
+
@non_kernel = data[:us]
|
33
|
+
@kernel = data[:sy]
|
34
|
+
@idle_time = data[:id]
|
35
|
+
@waiting = data[:wa]
|
36
|
+
@stolen = data[:st]
|
37
|
+
@used = data[:swpd]
|
38
|
+
@free = data[:free]
|
39
|
+
if self.is_a? AverageMemory
|
40
|
+
@cache = data[:cache]
|
41
|
+
@buffer = data[:buff]
|
42
|
+
else
|
43
|
+
@cache = false
|
44
|
+
@buffer = false
|
45
|
+
end
|
46
|
+
else
|
47
|
+
return false
|
48
|
+
end
|
49
|
+
true
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module Vmstator
|
2
|
+
|
3
|
+
class Parser
|
4
|
+
# parse() parses the the vmstat command
|
5
|
+
# with optional vmstat command-line flags
|
6
|
+
# which is passed in as a string
|
7
|
+
def parse(flags=false)
|
8
|
+
if flags
|
9
|
+
if flags =~ /(-d|--disk)/
|
10
|
+
# parse instances of disk_statistics
|
11
|
+
return disk_statistics(flags)
|
12
|
+
elsif flags =~ /(-D|--disk-sum)/
|
13
|
+
# parse instances of disk summary
|
14
|
+
return disk_summary(flags)
|
15
|
+
elsif flags =~ /(-a|--active)/
|
16
|
+
# parse instances of active memory
|
17
|
+
return active(flags)
|
18
|
+
elsif flags =~ /(-m|--slabs)/
|
19
|
+
# parse instances of slab info
|
20
|
+
return slab_info(flags)
|
21
|
+
elsif flags =~ /(-f|--forks)/
|
22
|
+
# parse instances of forks
|
23
|
+
return forks
|
24
|
+
elsif flags =~ /(-s|--stats)/
|
25
|
+
# parse instances of event counter statistics
|
26
|
+
return event_counter_statistics(flags)
|
27
|
+
elsif flags =~ /(-V|--version)/
|
28
|
+
# parse instances of version
|
29
|
+
return version
|
30
|
+
else
|
31
|
+
# parse instances of the typical, average things
|
32
|
+
return average(flags)
|
33
|
+
end
|
34
|
+
else
|
35
|
+
return average
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def version
|
40
|
+
`vmstat -V`.split.last
|
41
|
+
end
|
42
|
+
|
43
|
+
# slab_info() will run the -m flag and return that data
|
44
|
+
def average(flags="")
|
45
|
+
output = `vmstat #{flags}`.split("\n")
|
46
|
+
labels = output[1]
|
47
|
+
stats = output[2]
|
48
|
+
data = Hash[labels.split.map(&:to_sym).zip stats.split]
|
49
|
+
Vmstator::AverageMemory.new(data)
|
50
|
+
end
|
51
|
+
|
52
|
+
# active() will run the -a flag and return the data
|
53
|
+
def active(flag="")
|
54
|
+
output = `vmstat #{flags}`.split("\n")
|
55
|
+
labels = output[1]
|
56
|
+
stats = output[2]
|
57
|
+
data = Hash[labels.split.map(&:to_sym).zip stats.split]
|
58
|
+
Vmstator::ActiveMemory.new(data)
|
59
|
+
end
|
60
|
+
|
61
|
+
# event_counter_statistics() will run the -s flag and return the data
|
62
|
+
def event_counter_statistics(flags="")
|
63
|
+
output = `vmstat #{flags}`
|
64
|
+
keys = output.split(/\d/).compact.join.split("\n").map(&:strip)
|
65
|
+
# this looks silly. but I kind'a like it
|
66
|
+
keys.map(&:downcase).map {|s|
|
67
|
+
s.gsub(" ", "_")}.map {|s|
|
68
|
+
s.gsub("-", "_")}.map {|s|
|
69
|
+
s.gsub(/\b(\w){1}_{1}/, "") }.map(&:to_sym)
|
70
|
+
values = output.split(/[A-z]/).compact.join.split("\n").map(&:strip)
|
71
|
+
data = Hash[keys.zip values]
|
72
|
+
Vmstator::DiskSummary.new(data)
|
73
|
+
end
|
74
|
+
|
75
|
+
# disk_summary() will run the -D flag and return the data
|
76
|
+
def disk_summary(flags="")
|
77
|
+
output = `vmstat #{flags}`
|
78
|
+
keys = output.split(/\d/).compact.join.split("\n").map(&:strip)
|
79
|
+
keys.map(&:downcase).map {|s|
|
80
|
+
s.gsub(" ", "_")}.map {|s|
|
81
|
+
s.gsub("-", "_")}.map {|s|}.map(&:to_sym)
|
82
|
+
end
|
83
|
+
|
84
|
+
# forks() will run the -f flag and return that data.
|
85
|
+
def forks
|
86
|
+
`vmstat -f`.split.first
|
87
|
+
end
|
88
|
+
|
89
|
+
# disk_statistics() will run the -d flag and return that data.
|
90
|
+
def disk_statistics(flags="")
|
91
|
+
disk_stats = Vmstator::DiskStatistics.new
|
92
|
+
output = `vmstat #{flags}`.split("\n")
|
93
|
+
# remove first line of the output
|
94
|
+
output.shift
|
95
|
+
output.shift
|
96
|
+
output.each do |line|
|
97
|
+
name, total, merged, sectors, ms, total, merged, sectors, ms, cur, sec = line.split
|
98
|
+
data = {:name => name, :totoal => total, :merged => merged, :sectors => sectors,
|
99
|
+
:ms => ms, :cur => cur, :sec => sec }
|
100
|
+
disk_stats.update(data)
|
101
|
+
end
|
102
|
+
disk_stats
|
103
|
+
end
|
104
|
+
|
105
|
+
# slab_info() will run the -m flag and return that data
|
106
|
+
def slab_info(flags="")
|
107
|
+
# TODO : may go back, make this an option to use sudo or not.
|
108
|
+
# You need sudo permission to run this flag.
|
109
|
+
slab_info = Vstator::SlabInfo.new
|
110
|
+
`sudo vmstat #{flags}`.split("\n").each do |info|
|
111
|
+
next if info == "Cache Num Total Size Pages"
|
112
|
+
name, num, total, size, pages = info.split
|
113
|
+
data = { :name => name, :num => num, :total => total,
|
114
|
+
:size => size, :pages => pages }
|
115
|
+
slab_info.update(data)
|
116
|
+
end
|
117
|
+
slab_info
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Vmstator
|
2
|
+
|
3
|
+
class SlabInfo
|
4
|
+
# attrs for each attribute
|
5
|
+
attr_reader :caches
|
6
|
+
|
7
|
+
def initialize(data=false)
|
8
|
+
@caches = []
|
9
|
+
if data
|
10
|
+
update(data)
|
11
|
+
end
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
def update(data)
|
16
|
+
if data
|
17
|
+
@caches << Cache.new(data)
|
18
|
+
else
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/vmstator/version.rb
CHANGED
data/lib/vmstator.rb
CHANGED
@@ -1,194 +1,143 @@
|
|
1
1
|
require "vmstator/errors"
|
2
2
|
require "vmstator/version"
|
3
|
+
require "vmstator/stats"
|
4
|
+
require "vmstator/memory"
|
5
|
+
require "vmstator/active"
|
6
|
+
require "vmstator/average"
|
7
|
+
require "vmstator/cache"
|
8
|
+
require "vmstator/disk"
|
9
|
+
require "vmstator/disk_statistics"
|
10
|
+
require "vmstator/disk_summary"
|
11
|
+
require "vmstator/errors"
|
12
|
+
require "vmstator/event_counter_statistics"
|
13
|
+
require "vmstator/forks"
|
14
|
+
require "vmstator/slab_info"
|
15
|
+
require "vmstator/version"
|
3
16
|
|
4
17
|
module Vmstator
|
5
18
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
#
|
40
|
-
# # Typical use case:
|
41
|
-
# vmstats = Vmstator::Stats.new(:flags => "-S m", :parse => true )
|
42
|
-
# # => true
|
43
|
-
#
|
44
|
-
def initialize(args=false)
|
45
|
-
unless vmstat_exists?
|
46
|
-
Vmstator::VmstatError.new("Vmstat was not found on this system!")
|
47
|
-
end
|
48
|
-
if args
|
49
|
-
process_flags(args[:flags]) if args[:flags]
|
50
|
-
parse if args[:parse]
|
19
|
+
class Parser
|
20
|
+
# parse() parses the the vmstat command
|
21
|
+
# with optional vmstat command-line flags
|
22
|
+
# which is passed in as a string
|
23
|
+
def parse(flags=false)
|
24
|
+
if flags
|
25
|
+
if flags =~ /(-d|--disk)/
|
26
|
+
# parse instances of disk_statistics
|
27
|
+
return disk_statistics(flags)
|
28
|
+
elsif flags =~ /(-D|--disk-sum)/
|
29
|
+
# parse instances of disk summary
|
30
|
+
return disk_summary(flags)
|
31
|
+
elsif flags =~ /(-a|--active)/
|
32
|
+
# parse instances of active memory
|
33
|
+
return active(flags)
|
34
|
+
elsif flags =~ /(-m|--slabs)/
|
35
|
+
# parse instances of slab info
|
36
|
+
return slab_info(flags)
|
37
|
+
elsif flags =~ /(-f|--forks)/
|
38
|
+
# parse instances of forks
|
39
|
+
return forks
|
40
|
+
elsif flags =~ /(-s|--stats)/
|
41
|
+
# parse instances of event counter statistics
|
42
|
+
return event_counter_statistics(flags)
|
43
|
+
elsif flags =~ /(-V|--version)/
|
44
|
+
# parse instances of version
|
45
|
+
return version
|
46
|
+
else
|
47
|
+
# parse instances of the typical, average things
|
48
|
+
return average(flags)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
return average
|
51
52
|
end
|
52
|
-
true
|
53
53
|
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
def dry_run
|
58
|
-
unless system("vmstat #{@flags} > /dev/null 2>&1")
|
59
|
-
raise Vmstator::VmstatError.new("Vmstat ran into a problem with the flag(s): #{@flags}")
|
60
|
-
end
|
55
|
+
def version
|
56
|
+
`vmstat -V`.split.last
|
61
57
|
end
|
62
58
|
|
63
|
-
#
|
64
|
-
def
|
65
|
-
`vmstat
|
59
|
+
# average() will run the command normally and return the data
|
60
|
+
def average(flags="")
|
61
|
+
output = `vmstat #{flags}`.split("\n")
|
62
|
+
labels = output[1]
|
63
|
+
stats = output[2]
|
64
|
+
data = Hash[labels.split.map(&:to_sym).zip stats.split.map(&:to_i)]
|
65
|
+
Vmstator::AverageMemory.new(data)
|
66
66
|
end
|
67
|
-
|
68
|
-
# active() will
|
69
|
-
|
70
|
-
|
71
|
-
|
67
|
+
|
68
|
+
# active() will run the -a flag and return the data
|
69
|
+
def active(flags=false)
|
70
|
+
flags = "-a" unless flags
|
71
|
+
output = `vmstat #{flags}`.split("\n")
|
72
|
+
labels = output[1]
|
73
|
+
stats = output[2]
|
74
|
+
data = Hash[labels.split.map(&:to_sym).zip stats.split.map(&:to_i)]
|
75
|
+
Vmstator::ActiveMemory.new(data)
|
72
76
|
end
|
73
77
|
|
74
|
-
#
|
75
|
-
|
76
|
-
|
77
|
-
`vmstat
|
78
|
+
# event_counter_statistics() will run the -s flag and return the data
|
79
|
+
def event_counter_statistics(flags=false)
|
80
|
+
flags = "-s" unless flags
|
81
|
+
output = `vmstat #{flags}`
|
82
|
+
values = output.split(/[A-z]/).compact.join.split("\n").map(&:strip).map(&:to_i)
|
83
|
+
keys = output.split(/\d/).compact.join.split("\n").map(&:strip)
|
84
|
+
keys = keys.map(&:downcase).map { |s|
|
85
|
+
s.gsub(" ", "_")}.map { |s|
|
86
|
+
s.gsub("-", "_")}.map { |s|
|
87
|
+
s.gsub(/\b(\w){1}_{1}/, "") }.map(&:to_sym)
|
88
|
+
data = Hash[keys.zip values]
|
89
|
+
Vmstator::EventCounterStatistics.new(data)
|
78
90
|
end
|
79
91
|
|
80
|
-
#
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
92
|
+
# disk_summary() will run the -D flag and return the data
|
93
|
+
def disk_summary(flags=false)
|
94
|
+
flags = "-D" unless flags
|
95
|
+
output = `vmstat #{flags}`
|
96
|
+
values = output.split(/[A-z]/).compact.join.split("\n").map(&:strip).map(&:to_i)
|
97
|
+
keys = output.split(/\d/).compact.join.split("\n").map(&:strip)
|
98
|
+
keys = keys.map(&:downcase).map {|s| s.gsub(" ", "_")}.map(&:to_sym)
|
99
|
+
data = Hash[keys.zip values]
|
100
|
+
Vmstator::DiskSummary.new(data)
|
101
|
+
end
|
102
|
+
|
103
|
+
# forks() will run the -f flag and return that data.
|
104
|
+
def forks
|
105
|
+
`vmstat -f`.split.first.to_i
|
106
|
+
end
|
107
|
+
|
108
|
+
# disk_statistics() will run the -d flag and return that data.
|
109
|
+
def disk_statistics(flags="")
|
110
|
+
flags = "-d" unless flags
|
111
|
+
disk_stats = Vmstator::DiskStatistics.new
|
112
|
+
output = `vmstat #{flags}`.split("\n")
|
113
|
+
# remove first line of the output
|
87
114
|
output.shift
|
88
115
|
output.shift
|
89
|
-
@disk_info[:disk_count] = output.count
|
90
116
|
output.each do |line|
|
91
|
-
|
92
|
-
|
93
|
-
|
117
|
+
name, total, merged, sectors, ms, total, merged, sectors, ms, cur, sec = line.split
|
118
|
+
data = {:name => name, :totoal => total, :merged => merged, :sectors => sectors,
|
119
|
+
:ms => ms, :cur => cur, :sec => sec }
|
120
|
+
disk_stats.update(data)
|
94
121
|
end
|
95
|
-
|
96
|
-
end
|
97
|
-
|
98
|
-
# event_counter_statistics() will return the event
|
99
|
-
# count statistics in the form of a hash
|
100
|
-
def event_counter_statistics(flags=@flags)
|
101
|
-
output = `vmstat #{flags}`
|
102
|
-
keys = output.split(/\d/).compact.join.split("\n").map(&:strip)
|
103
|
-
values = output.split(/[A-z]/).compact.join.split("\n").map(&:strip)
|
104
|
-
Hash[keys.zip values]
|
122
|
+
disk_stats
|
105
123
|
end
|
106
124
|
|
107
125
|
# slab_info() will run the -m flag and return that data
|
108
|
-
def slab_info
|
126
|
+
def slab_info(flags="")
|
127
|
+
flags = "-m" unless flags
|
109
128
|
# TODO : may go back, make this an option to use sudo or not.
|
110
129
|
# You need sudo permission to run this flag.
|
111
|
-
|
112
|
-
|
130
|
+
flags = "-m" if flags.empty?
|
131
|
+
slab_info = Vmstator::SlabInfo.new
|
132
|
+
`sudo vmstat #{flags}`.split("\n").each do |info|
|
113
133
|
next if info == "Cache Num Total Size Pages"
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
@slab_info[cache][:size] = size
|
119
|
-
@slab_info[cache][:pages] = pages
|
120
|
-
end
|
121
|
-
@slab_info
|
122
|
-
end
|
123
|
-
|
124
|
-
# parse() is probably the main work horse of this class. It parses the
|
125
|
-
# the vmstat command after a dry run is done to check the command. It
|
126
|
-
# does quite few things: including parsing all of the vmstat information
|
127
|
-
# into a hash stored in @data; and will then store the relevant information
|
128
|
-
# into the appropriate attr_reader
|
129
|
-
#
|
130
|
-
# == Example
|
131
|
-
#
|
132
|
-
# # Typical use case to parse a vmstat command.
|
133
|
-
# vmstats = Vmstator::Stats.new(:flags => "-S m")
|
134
|
-
# vmstats.parse
|
135
|
-
# # => true
|
136
|
-
#
|
137
|
-
def parse(flags=@flags)
|
138
|
-
if flags =~ /(-d|--disk)/
|
139
|
-
return disk_statistics
|
140
|
-
elsif flags =~ /(-f|--forks)/
|
141
|
-
return forks
|
142
|
-
elsif flags =~ /(-s|--stats)/
|
143
|
-
return event_counter_statistics
|
144
|
-
end
|
145
|
-
dry_run
|
146
|
-
@output = `vmstat #{flags}`.split("\n")
|
147
|
-
labels = @output[1]
|
148
|
-
stats = @output[2]
|
149
|
-
@data = Hash[labels.split.map(&:to_sym).zip stats.split]
|
150
|
-
@swpd, @used = @data[:swpd], @data[:swpd]
|
151
|
-
@buff, @buffer = @data[:buff], @data[:buff]
|
152
|
-
@r, @runnable = @data[:r], @data[:r]
|
153
|
-
@b, @uninter = @data[:b], @data[:b]
|
154
|
-
@si, @swapped_in = @data[:si], @data[:si]
|
155
|
-
@so, @swapped_to = @data[:so], @data[:so]
|
156
|
-
@bi, @blocs_recv = @data[:bi], @data[:bi]
|
157
|
-
@bo, @blocks_sent = @data[:bo], @data[:bo]
|
158
|
-
@in, @interrupts = @data[:in], @data[:in]
|
159
|
-
@cs, @cntxt_swtchs = @data[:cs], @data[:cs]
|
160
|
-
@us, @non_kernel = @data[:us], @data[:us]
|
161
|
-
@sy, @kernel = @data[:sy], @data[:sy]
|
162
|
-
@id, @idle_time = @data[:id], @data[:id]
|
163
|
-
@wa, @waiting = @data[:wa], @data[:wa]
|
164
|
-
@st, @stolen = @data[:st], @data[:st]
|
165
|
-
@cache = @data[:cache]
|
166
|
-
@free = @data[:free]
|
167
|
-
true
|
168
|
-
end
|
169
|
-
|
170
|
-
private
|
171
|
-
|
172
|
-
# vmstat_exists?() checks if vmstat exists
|
173
|
-
# or not returning true or false for the command.
|
174
|
-
def vmstat_exists?
|
175
|
-
system("which vmstat > /dev/null 2>&1")
|
176
|
-
end
|
177
|
-
|
178
|
-
# process_flags() allows the flags argument that is passed
|
179
|
-
# in to be either an array or a string. That way the flags
|
180
|
-
# argument can be processed intuitively as either a string
|
181
|
-
# of flags or simple an array of those flags.
|
182
|
-
def process_flags(flags)
|
183
|
-
if flags.is_a? String
|
184
|
-
@flags = flags
|
185
|
-
elsif flags.is_a? Array
|
186
|
-
@flags = flags.join(" ")
|
187
|
-
else
|
188
|
-
false
|
134
|
+
name, num, total, size, pages = info.split
|
135
|
+
data = { :name => name, :num => num, :total => total,
|
136
|
+
:size => size, :pages => pages }
|
137
|
+
slab_info.update(data)
|
189
138
|
end
|
139
|
+
slab_info
|
190
140
|
end
|
191
141
|
|
192
142
|
end
|
193
|
-
|
194
143
|
end
|
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vmstator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kent 'picat' Gruber
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,10 +71,23 @@ files:
|
|
71
71
|
- bin/setup
|
72
72
|
- examples/basic_example.rb
|
73
73
|
- lib/vmstator.rb
|
74
|
+
- lib/vmstator/active.rb
|
75
|
+
- lib/vmstator/average.rb
|
76
|
+
- lib/vmstator/cache.rb
|
77
|
+
- lib/vmstator/disk.rb
|
78
|
+
- lib/vmstator/disk_statistics.rb
|
79
|
+
- lib/vmstator/disk_summary.rb
|
74
80
|
- lib/vmstator/errors.rb
|
81
|
+
- lib/vmstator/event_counter_statistics.rb
|
82
|
+
- lib/vmstator/forks.rb
|
83
|
+
- lib/vmstator/memory.rb
|
84
|
+
- lib/vmstator/parser.rb
|
85
|
+
- lib/vmstator/slab_info.rb
|
86
|
+
- lib/vmstator/stats.rb
|
75
87
|
- lib/vmstator/version.rb
|
76
88
|
- pkg/vmstator-0.1.0.gem
|
77
89
|
- pkg/vmstator-1.0.0.gem
|
90
|
+
- pkg/vmstator-1.0.1.gem
|
78
91
|
- vmstator.gemspec
|
79
92
|
homepage: https://github.com/picatz/Vmstator
|
80
93
|
licenses:
|