Class: Vmstator::Parser

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

Instance Method Summary collapse

Instance Method Details

#active(flag = "") ⇒ Object

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



69
70
71
72
73
74
75
76
# File 'lib/vmstator.rb', line 69

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



60
61
62
63
64
65
66
# File 'lib/vmstator.rb', line 60

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.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vmstator.rb', line 109

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



93
94
95
96
97
98
99
100
101
# File 'lib/vmstator.rb', line 93

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)
  keys   = 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



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vmstator.rb', line 79

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.



104
105
106
# File 'lib/vmstator.rb', line 104

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vmstator.rb', line 23

def parse(flags=false)
  if flags 
    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
      # parse instances of the typical, average things
      return average(flags)
    end
  else
    return average
  end
end

#slab_info(flags = "") ⇒ Object

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



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

def slab_info(flags=false)
  raise VmstatError("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



55
56
57
# File 'lib/vmstator.rb', line 55

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