Class: Vmstator::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/vmstator.rb,
lib/vmstator/linux/parser.rb

Instance Method Summary collapse

Instance Method Details

#active(flag = "") ⇒ Object

active() will run the -a flag and return the data



87
88
89
90
91
92
93
94
# File 'lib/vmstator.rb', line 87

def active(flags=false)
  flags  = "-a" unless flags
  output = `vmstat #{flags}`.split("\n")
  labels = output[1]
  stats  = output[2]
  data   = Hash[labels.split.map(&:to_sym).zip stats.split.map(&:to_i)]
  Vmstator::ActiveMemory.new(data)
end

#average(flags = "") ⇒ Object

slab_info() will run the -m flag and return that data



78
79
80
81
82
83
84
# File 'lib/vmstator.rb', line 78

def average(flags="")
  output = `vmstat #{flags}`.split("\n")
  labels = output[1]
  stats  = output[2]
  data   = Hash[labels.split.map(&:to_sym).zip stats.split.map(&:to_i)]
  Vmstator::AverageMemory.new(data)
end

#disk_statistics(flags = "") ⇒ Object

disk_statistics() will run the -d flag and return that data.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/vmstator.rb', line 127

def disk_statistics(flags=false)
  flags      = "-d" unless flags
  disk_stats = Vmstator::DiskStatistics.new
  output     = `vmstat #{flags}`.split("\n")
  # remove first line of the output
  output.shift
  output.shift
  output.each do |line|
    name, total, merged, sectors, ms, total, merged, sectors, ms, cur, sec = line.split
    data = { :name => name, 
             :total => total.to_i, 
             :merged => merged.to_i, 
             :sectors => sectors.to_i,
             :ms => ms.to_i,     
             :cur => cur.to_i,      
             :sec => sec.to_i }
    disk_stats.update(data)
  end
  disk_stats
end

#disk_summary(flags = "") ⇒ Object

disk_summary() will run the -D flag and return the data



111
112
113
114
115
116
117
118
119
# File 'lib/vmstator.rb', line 111

def disk_summary(flags=false)
  flags  = "-D" unless flags
  output = `vmstat #{flags}` 
  values = output.split(/[A-z]/).compact.join.split("\n").map(&:strip).map(&:to_i)
  kecys   = output.split(/\d/).compact.join.split("\n").map(&:strip) 
  keys   = keys.map(&:downcase).map {|s| s.gsub(" ", "_")}.map(&:to_sym)
  data   = Hash[keys.zip values]
  Vmstator::DiskSummary.new(data)
end

#event_counter_statistics(flags = "") ⇒ Object

event_counter_statistics() will run the -s flag and return the data



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vmstator.rb', line 97

def event_counter_statistics(flags=false)
  flags  = "-s" unless flags
  output = `vmstat #{flags}` 
  values = output.split(/[A-z]/).compact.join.split("\n").map(&:strip).map(&:to_i)
  keys   = output.split(/\d/).compact.join.split("\n").map(&:strip)
  keys   = keys.map(&:downcase).map { |s| 
              s.gsub(" ", "_")}.map { |s| 
              s.gsub("-", "_")}.map { |s| 
              s.gsub(/\b(\w){1}_{1}/, "") }.map(&:to_sym)
  data = Hash[keys.zip values]
  Vmstator::EventCounterStatistics.new(data)
end

#forksObject

forks() will run the -f flag and return that data.



122
123
124
# File 'lib/vmstator.rb', line 122

def forks
  `vmstat -f`.split.first.to_i
end

#parse(flags = false) ⇒ Object

parse() parses the the vmstat command with optional vmstat command-line flags which is passed in as a string



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vmstator.rb', line 22

def parse(flags=false)
  if RUBY_PLATFORM =~ /darwin/i
    return parse_macos
  elsif RUBY_PLATFORM =~ /linux/i
    return parse_linux(flags) 
  elsif RUBY_PLATFORM =~ /win32|win64|\.NET|windows|cygwin|mingw32/i
    raise Vmstator::VmstatError.new("This platform is not supported. Yet!?")
  else
    raise Vmstator::VmstatError.new("Unable to parse vmstat on this platform!")
  end
end

#parse_linux(flags = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vmstator.rb', line 34

def parse_linux(flags=false)
  if flags =~ /(-d|--disk)/
    # parse instances of disk_statistics
    return disk_statistics(flags)
  elsif flags =~ /(-D|--disk-sum)/
    # parse instances of disk summary
    return disk_summary(flags)
  elsif flags =~ /(-a|--active)/
    # parse instances of active memory
    return active(flags)
  elsif flags =~ /(-m|--slabs)/
    # parse instances of slab info
    return slab_info(flags)
  elsif flags =~ /(-f|--forks)/
    # parse instances of forks
    return forks
  elsif flags =~ /(-s|--stats)/
    # parse instances of event counter statistics
    return event_counter_statistics(flags)
  elsif flags =~ /(-V|--version)/
    # parse instances of version
    return version
  else
     flags ? average(flags) : average
  end
end

#parse_macosObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vmstator.rb', line 61

def parse_macos
  output = `vm_stat`.split("\n")
  output.shift
  data = {}
  output.each do |line|
    key, value = line.split(':').map(&:strip)  
    key        = key.downcase.gsub(" ", "_").gsub("-", "_").gsub('"', '').to_sym
    data[key]  = value.to_i
  end
  Vmstator::MacOSPages.new(data)
end

#slab_info(flags = "") ⇒ Object

slab_info() will run the -m flag and return that data



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vmstator.rb', line 149

def slab_info(flags=false)
  raise Vmstator::VmstatError.new("This must be run with root privileges!") unless Process.uid == 0
  flags     = "-m" unless flags
  slab_info = Vmstator::SlabInfo.new 
  `sudo vmstat #{flags}`.split("\n").each do |info|
    next if info == "Cache                       Num  Total   Size  Pages"
    name, num, total, size, pages = info.split
    data = { :name => name, 
             :num => num.to_i, 
             :total => total.to_i,
             :size => size.to_i,  
             :pages => pages.to_i }
    slab_info.update(data)
  end
  slab_info
end

#versionObject



73
74
75
# File 'lib/vmstator.rb', line 73

def version
  `vmstat -V`.split.last
end