Module: Vmstat::ProcFS
- Defined in:
- lib/vmstat/procfs.rb
Overview
Implementation of performance metrics gathering for linux and other os with the proc file system.
Constant Summary collapse
- CPU_DATA =
Grep from the man procfs about cpu data in stat file:
/cpu(\d)#{'\s+(\d+)' * 4}/.freeze
- NET_DATA =
Grep the network stats from the procfs.
/(\w+):#{'\s*(\d+)' * 16}/
Instance Method Summary collapse
-
#boot_time ⇒ Time
Fetches the boot time of the system.
-
#cpu ⇒ Array<Vmstat::Cpu>
Fetches the cpu statistics (usage counter for user, nice, system and idle).
-
#memory ⇒ Vmstat::Memory
Fetches the memory usage information.
-
#network_interfaces ⇒ Array<Vmstat::NetworkInterface>
Fetches the information for all available network devices.
-
#procfs_file(*names) {|file| ... } ⇒ Object
private
Opens a proc file system file handle and returns the handle in the passed block.
-
#procfs_path ⇒ String
private
The path to the proc file system.
-
#task ⇒ Vmstat::Task
Fetches the current process cpu and memory data.
Instance Method Details
#boot_time ⇒ Time
Fetches the boot time of the system.
119 120 121 122 |
# File 'lib/vmstat/procfs.rb', line 119 def boot_time raw = procfs_file("uptime") { |file| file.read } Time.now - raw.split(/\s/).first.to_f end |
#cpu ⇒ Array<Vmstat::Cpu>
Fetches the cpu statistics (usage counter for user, nice, system and idle)
32 33 34 35 36 37 38 39 40 |
# File 'lib/vmstat/procfs.rb', line 32 def cpu cpus = [] procfs_file("stat") do |file| file.read.scan(CPU_DATA) do |i, user, nice, system, idle| cpus << Cpu.new(i.to_i, user.to_i, system.to_i, nice.to_i, idle.to_i) end end cpus end |
#memory ⇒ Vmstat::Memory
Fetches the memory usage information.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/vmstat/procfs.rb', line 46 def memory @pagesize ||= Vmstat.pagesize total = free = active = inactive = pageins = pageouts = 0 procfs_file("meminfo") do |file| content = file.read(2048) # the requested information is in the first bytes content.scan(/(\w+):\s+(\d+) kB/) do |name, kbytes| pages = (kbytes.to_i * 1024) / @pagesize case name when "MemTotal" then total = pages when "MemFree" then free = pages when "Active" then active = pages when "Inactive" then inactive = pages end end end procfs_file("vmstat") do |file| content = file.read if content =~ /pgpgin\s+(\d+)/ pageins = $1.to_i end if content =~ /pgpgout\s+(\d+)/ pageouts = $1.to_i end end Memory.new @pagesize, total-free-active-inactive, active, inactive, free, pageins, pageouts end |
#network_interfaces ⇒ Array<Vmstat::NetworkInterface>
Fetches the information for all available network devices.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vmstat/procfs.rb', line 85 def network_interfaces netifcs = [] procfs_file("net", "dev") do |file| file.read.scan(NET_DATA) do |columns| type = case columns[0] when /^eth/ then NetworkInterface::ETHERNET_TYPE when /^lo/ then NetworkInterface::LOOPBACK_TYPE end netifcs << NetworkInterface.new(columns[0].to_sym, columns[1].to_i, columns[3].to_i, columns[4].to_i, columns[9].to_i, columns[11].to_i, type) end end netifcs end |
#procfs_file(*names) {|file| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Opens a proc file system file handle and returns the handle in the passed block. Closes the file handle.
141 142 143 144 |
# File 'lib/vmstat/procfs.rb', line 141 def procfs_file(*names, &block) path = File.join(procfs_path, *names) File.open(path, "r", &block) end |
#procfs_path ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the path to the proc file system.
128 129 130 |
# File 'lib/vmstat/procfs.rb', line 128 def procfs_path "/proc".freeze end |
#task ⇒ Vmstat::Task
Fetches the current process cpu and memory data.
105 106 107 108 109 110 111 112 113 |
# File 'lib/vmstat/procfs.rb', line 105 def task @pagesize ||= Vmstat.pagesize procfs_file("self", "stat") do |file| data = file.read.split(/ /) Task.new(data[22].to_i / @pagesize, data[23].to_i, data[13].to_i * 1000, data[14].to_i * 1000) end end |