vmstat 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ module Vmstat
2
+ # Since on linux the type and mount information are note available in the
3
+ # statfs library, we have to use constants to find out the file system type.
4
+ # The mount point and the path will allways be the same, because we don't have
5
+ # the mount information. But one can still either use the device or mountpoint
6
+ # to get the information.
7
+ class LinuxDisk < Disk
8
+ # Mapping of file system type codes to file system names.
9
+ FS_CODES = {
10
+ 44533=>:adfs, 44543=>:affs, 1111905073=>:befs, 464386766=>:bfs,
11
+ 4283649346=>:cifs_number, 1937076805=>:coda, 19920823=>:coh,
12
+ 684539205=>:cramfs, 4979=>:devfs, 4278867=>:efs, 4989=>:ext,
13
+ 61265=>:ext2_old, 61267=>:ext4, 16964=>:hfs, 4187351113=>:hpfs,
14
+ 2508478710=>:hugetlbfs, 38496=>:isofs, 29366=>:jffs2,
15
+ 827541066=>:jfs, 4991=>:minix, 9320=>:minix2, 9336=>:minix22,
16
+ 19780=>:msdos, 22092=>:ncp, 26985=>:nfs, 1397118030=>:ntfs_sb,
17
+ 40865=>:openprom, 40864=>:proc, 47=>:qnx4, 1382369651=>:reiserfs,
18
+ 29301=>:romfs, 20859=>:smb, 19920822=>:sysv2, 19920821=>:sysv4,
19
+ 16914836=>:tmpfs, 352400198=>:udf, 72020=>:ufs, 7377 => :devpts,
20
+ 40866=>:usbdevice, 2768370933=>:vxfs, 19920820=>:xenix, 1481003842=>:xfs,
21
+ 19911021=>:xiafs, 1448756819=>:reiserfs, 1650812274 => :sysfs
22
+ }.freeze
23
+
24
+ # Mainly a wrapper for the {Vmstat::Disk} class constructor. This constructor
25
+ # handles the file system type mapping.
26
+ def initialize(fs = nil, path = nil, block_size = nil,
27
+ free_blocks = nil, available_blocks = nil, total_blocks = nil)
28
+ @fs = fs
29
+ super FS_CODES[@fs], path, path, block_size,
30
+ free_blocks, available_blocks, total_blocks
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,126 @@
1
+ module Vmstat
2
+ module ProcFS
3
+ # Grep from the man procfs about cpu data in stat file:
4
+ # @example Format
5
+ # (num) user nice system idle iowait irq softirq steal
6
+ # @example manpage
7
+ # iowait - time waiting for I/O to complete (since 2.5.41)
8
+ # irq - time servicing interrupts (since 2.6.0-test4)
9
+ # softirq - time servicing softirqs (since 2.6.0-test4)
10
+ # Since Linux 2.6.11:
11
+ # steal - stolen time, which is the time spent in other operating
12
+ # systems when running in a virtualized environment
13
+ # Since Linux 2.6.24:
14
+ # guest - which is the time spent running a virtual CPU for guest
15
+ # operating systems under the control of the Linux kernel.
16
+ CPU_DATA = /cpu(\d)#{'\s+(\d+)' * 4}/.freeze
17
+
18
+ # Grep the network stats from the procfs.
19
+ # @example Format (from /proc/net/dev)
20
+ # Inter-| Receive | Transmit
21
+ # face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
22
+ # @example Data
23
+ # eth0: 33660 227 0 0 0 0 0 0 36584 167 0 0 0 0 0 0
24
+ NET_DATA = /(\w+):#{'\s*(\d+)' * 16}/
25
+
26
+ # Fetches the cpu statistics (usage counter for user, nice, system and idle)
27
+ # @return [Array<Vmstat::Cpu>] the array of cpu counter
28
+ # @example
29
+ # Vmstat.cpu # => [#<struct Vmstat::Cpu ...>, #<struct Vmstat::Cpu ...>]
30
+ def cpu
31
+ cpus = []
32
+ procfs_file("stat") do |file|
33
+ file.read.scan(CPU_DATA) do |i, user, nice, system, idle|
34
+ cpus << Cpu.new(i.to_i, user.to_i, system.to_i, nice.to_i, idle.to_i)
35
+ end
36
+ end
37
+ cpus
38
+ end
39
+
40
+ # Fetches the memory usage information.
41
+ # @return [Vmstat::Memory] the memory data like free, used und total.
42
+ # @example
43
+ # Vmstat.memory # => #<struct Vmstat::Memory ...>
44
+ def memory
45
+ @pagesize ||= Vmstat.pagesize
46
+
47
+ total = free = active = inactive = pageins = pageouts = 0
48
+ procfs_file("meminfo") do |file|
49
+ content = file.read(2048) # the requested information is in the first bytes
50
+
51
+ content.scan(/(\w+):\s+(\d+) kB/) do |name, kbytes|
52
+ pages = (kbytes.to_i * 1024) / @pagesize
53
+
54
+ case name
55
+ when "MemTotal" then total = pages
56
+ when "MemFree" then free = pages
57
+ when "Active" then active = pages
58
+ when "Inactive" then inactive = pages
59
+ end
60
+ end
61
+ end
62
+
63
+ procfs_file("vmstat") do |file|
64
+ content = file.read
65
+
66
+ if content =~ /pgpgin\s+(\d+)/
67
+ pageins = $1.to_i
68
+ end
69
+
70
+ if content =~ /pgpgout\s+(\d+)/
71
+ pageouts = $1.to_i
72
+ end
73
+ end
74
+
75
+ Memory.new @pagesize, total-free-active-inactive, active, inactive, free,
76
+ pageins, pageouts
77
+ end
78
+
79
+ # Fetches the information for all available network devices.
80
+ # @return [Array<Vmstat::NetworkInterface>] the network device information
81
+ # @example
82
+ # Vmstat.network_interfaces # => [#<struct Vmstat::NetworkInterface ...>, ...]
83
+ def network_interfaces
84
+ netifcs = []
85
+ procfs_file("net", "dev") do |file|
86
+ file.read.scan(NET_DATA) do |columns|
87
+ netifcs << NetworkInterface.new(columns[0].to_sym, columns[1].to_i,
88
+ columns[3].to_i, columns[4].to_i,
89
+ columns[9].to_i, columns[11].to_i)
90
+ end
91
+ end
92
+ netifcs
93
+ end
94
+
95
+ # Fetches the boot time of the system.
96
+ # @return [Time] the boot time as regular time object.
97
+ # @example
98
+ # Vmstat.boot_time # => 2012-10-09 18:42:37 +0200
99
+ def boot_time
100
+ raw = procfs_file("uptime") { |file| file.read }
101
+ Time.now - raw.split(/\s/).first.to_f
102
+ end
103
+
104
+ # @return [String] the path to the proc file system
105
+ # @example
106
+ # procfs_path # => "/proc"
107
+ # @api private
108
+ def procfs_path
109
+ "/proc".freeze
110
+ end
111
+
112
+ # Opens a proc file system file handle and returns the handle in the
113
+ # passed block. Closes the file handle.
114
+ # @see File#open
115
+ # @param [Array<String>] names parts of the path to the procfs file
116
+ # @example
117
+ # procfs_file("net", "dev") { |file| }
118
+ # procfs_file("stat") { |file| }
119
+ # @yieldparam [IO] file the file handle
120
+ # @api private
121
+ def procfs_file(*names, &block)
122
+ path = File.join(procfs_path, *names)
123
+ File.open(path, "r", &block)
124
+ end
125
+ end
126
+ end
@@ -11,7 +11,7 @@ module Vmstat
11
11
  @load_average = Vmstat.load_average
12
12
  @memory = Vmstat.memory
13
13
  @network_interfaces = Vmstat.network_interfaces
14
- @task = Vmstat.task
14
+ @task = Vmstat.task if Vmstat.respond_to? :task
15
15
  end
16
16
  end
17
17
  end
@@ -0,0 +1,69 @@
1
+ module Vmstat
2
+ module Stub
3
+ # Fetches the boot time of the system.
4
+ # @return [Time] the boot time as regular time object.
5
+ # @example
6
+ # Vmstat.boot_time # => 2012-10-09 18:42:37 +0200
7
+ def self.boot_time
8
+ nil
9
+ end
10
+
11
+ # Fetches the cpu statistics (usage counter for user, nice, system and idle)
12
+ # @return [Array<Vmstat::Cpu>] the array of cpu counter
13
+ # @example
14
+ # Vmstat.cpu # => [#<struct Vmstat::Cpu ...>, #<struct Vmstat::Cpu ...>]
15
+ def self.cpu
16
+ []
17
+ end
18
+
19
+ # Fetches the usage data and other useful disk information for the given path.
20
+ # @param [String] path the path (mount point or device path) to the disk
21
+ # @return [Vmstat::Disk] the disk information
22
+ # @example
23
+ # Vmstat.disk("/") # => #<struct Vmstat::Disk type=:hfs, ...>
24
+ def self.disk(path)
25
+ nil
26
+ end
27
+
28
+ # Fetches the load average for the current system.
29
+ # @return [Vmstat::LoadAverage] the load average data
30
+ # @example
31
+ # Vmstat.load_average # => #<struct Vmstat::LoadAverage one_minute=...>
32
+ def self.load_average
33
+ nil
34
+ end
35
+
36
+ # Fetches the memory usage information.
37
+ # @return [Vmstat::Memory] the memory data like free, used und total.
38
+ # @example
39
+ # Vmstat.memory # => #<struct Vmstat::Memory ...>
40
+ def self.memory
41
+ nil
42
+ end
43
+
44
+ # Fetches the information for all available network devices.
45
+ # @return [Array<Vmstat::NetworkInterface>] the network device information
46
+ # @example
47
+ # Vmstat.network_interfaces # => [#<struct Vmstat::NetworkInterface ...>, ...]
48
+ def self.network_interfaces
49
+ []
50
+ end
51
+
52
+ # Fetches pagesize of the current system.
53
+ # @return [Fixnum] the pagesize of the current system in bytes.
54
+ # @example
55
+ # Vmstat.pagesize # => 4096
56
+ def self.pagesize
57
+ 4096
58
+ end
59
+
60
+ # Fetches time and memory usage for the current process.
61
+ # @note Currently only on Mac OS X
62
+ # @return [Array<Vmstat::Task>] the network device information
63
+ # @example
64
+ # Vmstat.task # => #<struct Vmstat::Task ...>
65
+ def self.task
66
+ nil
67
+ end
68
+ end
69
+ end
@@ -1,3 +1,3 @@
1
1
  module Vmstat
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1 @@
1
+ 0.24 0.32 0.21 1/79 2621
@@ -0,0 +1,46 @@
1
+ MemTotal: 507344 kB
2
+ MemFree: 428068 kB
3
+ Buffers: 13104 kB
4
+ Cached: 40296 kB
5
+ SwapCached: 0 kB
6
+ Active: 26032 kB
7
+ Inactive: 33620 kB
8
+ Active(anon): 6276 kB
9
+ Inactive(anon): 240 kB
10
+ Active(file): 19756 kB
11
+ Inactive(file): 33380 kB
12
+ Unevictable: 0 kB
13
+ Mlocked: 0 kB
14
+ HighTotal: 0 kB
15
+ HighFree: 0 kB
16
+ LowTotal: 507344 kB
17
+ LowFree: 428068 kB
18
+ SwapTotal: 521212 kB
19
+ SwapFree: 521212 kB
20
+ Dirty: 8 kB
21
+ Writeback: 0 kB
22
+ AnonPages: 6240 kB
23
+ Mapped: 5844 kB
24
+ Shmem: 268 kB
25
+ Slab: 14012 kB
26
+ SReclaimable: 6476 kB
27
+ SUnreclaim: 7536 kB
28
+ KernelStack: 688 kB
29
+ PageTables: 468 kB
30
+ NFS_Unstable: 0 kB
31
+ Bounce: 0 kB
32
+ WritebackTmp: 0 kB
33
+ CommitLimit: 774884 kB
34
+ Committed_AS: 48400 kB
35
+ VmallocTotal: 512056 kB
36
+ VmallocUsed: 2228 kB
37
+ VmallocChunk: 509236 kB
38
+ HardwareCorrupted: 0 kB
39
+ AnonHugePages: 0 kB
40
+ HugePages_Total: 0
41
+ HugePages_Free: 0
42
+ HugePages_Rsvd: 0
43
+ HugePages_Surp: 0
44
+ Hugepagesize: 2048 kB
45
+ DirectMap4k: 34752 kB
46
+ DirectMap2M: 489472 kB
@@ -0,0 +1,5 @@
1
+ Inter-| Receive | Transmit
2
+ face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
3
+ lo: 3224 40 0 0 0 0 0 0 3224 40 0 0 0 0 0 0
4
+ eth1: 0 0 1 2 0 0 0 0 0 0 3 4 0 0 0 0
5
+ eth0: 33660 227 0 0 0 0 0 0 36584 167 0 0 0 0 0 0
data/spec/procfs/stat ADDED
@@ -0,0 +1,12 @@
1
+ cpu 1312 0 3760 108331 788 0 169 0 0 0
2
+ cpu0 311 0 966 26788 520 0 109 0 0 0
3
+ cpu1 351 0 862 27263 162 0 15 0 0 0
4
+ cpu2 324 0 1092 26698 74 0 32 0 0 0
5
+ cpu3 326 0 838 27581 31 0 12 0 0 0
6
+ intr 39520 61 9 0 0 0 0 0 0 0 0 0 0 133 0 0 352 0 0 0 394 0 1864 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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
+ softirq 52656 0 26117 54 429 2037 0 3 6623 75 17318
@@ -0,0 +1 @@
1
+ 355.63 1302.86
@@ -0,0 +1,89 @@
1
+ nr_free_pages 107062
2
+ nr_inactive_anon 60
3
+ nr_active_anon 1557
4
+ nr_inactive_file 8341
5
+ nr_active_file 4939
6
+ nr_unevictable 0
7
+ nr_mlock 0
8
+ nr_anon_pages 1579
9
+ nr_mapped 1461
10
+ nr_file_pages 13346
11
+ nr_dirty 1
12
+ nr_writeback 0
13
+ nr_slab_reclaimable 1628
14
+ nr_slab_unreclaimable 1885
15
+ nr_page_table_pages 101
16
+ nr_kernel_stack 88
17
+ nr_unstable 0
18
+ nr_bounce 0
19
+ nr_vmscan_write 0
20
+ nr_vmscan_immediate_reclaim 0
21
+ nr_writeback_temp 0
22
+ nr_isolated_anon 0
23
+ nr_isolated_file 0
24
+ nr_shmem 67
25
+ nr_dirtied 185
26
+ nr_written 182
27
+ nr_anon_transparent_hugepages 0
28
+ nr_dirty_threshold 24392
29
+ nr_dirty_background_threshold 12196
30
+ pgpgin 64599
31
+ pgpgout 1104
32
+ pswpin 0
33
+ pswpout 0
34
+ pgalloc_dma 2
35
+ pgalloc_normal 190973
36
+ pgalloc_high 0
37
+ pgalloc_movable 0
38
+ pgfree 298439
39
+ pgactivate 5802
40
+ pgdeactivate 0
41
+ pgfault 581461
42
+ pgmajfault 42
43
+ pgrefill_dma 0
44
+ pgrefill_normal 0
45
+ pgrefill_high 0
46
+ pgrefill_movable 0
47
+ pgsteal_dma 0
48
+ pgsteal_normal 0
49
+ pgsteal_high 0
50
+ pgsteal_movable 0
51
+ pgscan_kswapd_dma 0
52
+ pgscan_kswapd_normal 0
53
+ pgscan_kswapd_high 0
54
+ pgscan_kswapd_movable 0
55
+ pgscan_direct_dma 0
56
+ pgscan_direct_normal 0
57
+ pgscan_direct_high 0
58
+ pgscan_direct_movable 0
59
+ pginodesteal 0
60
+ slabs_scanned 0
61
+ kswapd_steal 0
62
+ kswapd_inodesteal 0
63
+ kswapd_low_wmark_hit_quickly 0
64
+ kswapd_high_wmark_hit_quickly 0
65
+ kswapd_skip_congestion_wait 0
66
+ pageoutrun 1
67
+ allocstall 0
68
+ pgrotated 0
69
+ compact_blocks_moved 0
70
+ compact_pages_moved 0
71
+ compact_pagemigrate_failed 0
72
+ compact_stall 0
73
+ compact_fail 0
74
+ compact_success 0
75
+ htlb_buddy_alloc_success 0
76
+ htlb_buddy_alloc_fail 0
77
+ unevictable_pgs_culled 0
78
+ unevictable_pgs_scanned 0
79
+ unevictable_pgs_rescued 0
80
+ unevictable_pgs_mlocked 0
81
+ unevictable_pgs_munlocked 0
82
+ unevictable_pgs_cleared 0
83
+ unevictable_pgs_stranded 0
84
+ unevictable_pgs_mlockfreed 0
85
+ thp_fault_alloc 0
86
+ thp_fault_fallback 0
87
+ thp_collapse_alloc 0
88
+ thp_collapse_alloc_failed 0
89
+ thp_split 0
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,7 @@
7
7
  $:.unshift(File.expand_path("../lib", __FILE__))
8
8
  require 'vmstat'
9
9
  require 'ostruct'
10
+ require 'timecop'
10
11
 
11
12
  RSpec.configure do |config|
12
13
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vmstat::ProcFS do
4
+ let(:procfs) do
5
+ Class.new do
6
+ extend Vmstat::ProcFS
7
+
8
+ def self.procfs_path
9
+ File.expand_path("../../procfs", __FILE__)
10
+ end
11
+ end
12
+ end
13
+ subject { procfs }
14
+
15
+ context "#cpu" do
16
+ subject { procfs.cpu }
17
+
18
+ it { should be_a(Array)}
19
+ it do
20
+ should == [
21
+ Vmstat::Cpu.new(0, 311, 966, 0, 26788),
22
+ Vmstat::Cpu.new(1, 351, 862, 0, 27263),
23
+ Vmstat::Cpu.new(2, 324, 1092, 0, 26698),
24
+ Vmstat::Cpu.new(3, 326, 838, 0, 27581)
25
+ ]
26
+ end
27
+ end
28
+
29
+ context "#memory" do
30
+ subject { procfs.memory }
31
+
32
+ it { should be_a(Vmstat::Memory) }
33
+ it do
34
+ should == Vmstat::Memory.new(4096, 4906, 6508, 8405, 107017, 64599, 1104)
35
+ end
36
+
37
+ it "should have the right total" do
38
+ (subject.wired_bytes + subject.active_bytes +
39
+ subject.inactive_bytes + subject.free_bytes).should == 507344 * 1024
40
+ end
41
+ end
42
+
43
+ context "#boot_time" do
44
+ subject { procfs.boot_time }
45
+
46
+ it { should be_a(Time) }
47
+ it { Timecop.freeze(Time.now) { should == Time.now - 355.63 } }
48
+ end
49
+
50
+ context "#network_interfaces" do
51
+ subject { procfs.network_interfaces }
52
+
53
+ it { should be_a(Array) }
54
+ it do
55
+ should == [
56
+ Vmstat::NetworkInterface.new(:lo, 3224, 0, 0, 3224, 0),
57
+ Vmstat::NetworkInterface.new(:eth1, 0, 1, 2, 0, 3),
58
+ Vmstat::NetworkInterface.new(:eth0, 33660, 0, 0, 36584, 0)
59
+ ]
60
+ end
61
+ end
62
+ end