Last active
May 11, 2017 16:04
-
-
Save SegFaultAX/d13679387c71b3ef26f8ca76e870e8d1 to your computer and use it in GitHub Desktop.
Parse diskstats (/proc/diskstats) [Ruby]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EXAMPLE = <<-EOE | |
1 0 ram0 0 0 0 0 0 0 0 0 0 0 0 | |
1 1 ram1 0 0 0 0 0 0 0 0 0 0 0 | |
1 2 ram2 0 0 0 0 0 0 0 0 0 0 0 | |
1 3 ram3 0 0 0 0 0 0 0 0 0 0 0 | |
1 4 ram4 0 0 0 0 0 0 0 0 0 0 0 | |
1 5 ram5 0 0 0 0 0 0 0 0 0 0 0 | |
1 6 ram6 0 0 0 0 0 0 0 0 0 0 0 | |
1 7 ram7 0 0 0 0 0 0 0 0 0 0 0 | |
1 8 ram8 0 0 0 0 0 0 0 0 0 0 0 | |
1 9 ram9 0 0 0 0 0 0 0 0 0 0 0 | |
1 10 ram10 0 0 0 0 0 0 0 0 0 0 0 | |
1 11 ram11 0 0 0 0 0 0 0 0 0 0 0 | |
1 12 ram12 0 0 0 0 0 0 0 0 0 0 0 | |
1 13 ram13 0 0 0 0 0 0 0 0 0 0 0 | |
1 14 ram14 0 0 0 0 0 0 0 0 0 0 0 | |
1 15 ram15 0 0 0 0 0 0 0 0 0 0 0 | |
7 0 loop0 0 0 0 0 0 0 0 0 0 0 0 | |
7 1 loop1 0 0 0 0 0 0 0 0 0 0 0 | |
7 2 loop2 0 0 0 0 0 0 0 0 0 0 0 | |
7 3 loop3 0 0 0 0 0 0 0 0 0 0 0 | |
7 4 loop4 0 0 0 0 0 0 0 0 0 0 0 | |
7 5 loop5 0 0 0 0 0 0 0 0 0 0 0 | |
7 6 loop6 0 0 0 0 0 0 0 0 0 0 0 | |
7 7 loop7 0 0 0 0 0 0 0 0 0 0 0 | |
11 0 sr0 0 0 0 0 0 0 0 0 0 0 0 | |
2 0 fd0 0 0 0 0 0 0 0 0 0 0 0 | |
8 16 sdb 89058036 3036 2038861338 27561368 177255631 250783894 114648810520 2204478124 0 55806380 2232046128 | |
8 0 sda 5943640 4656869 132155940 1335728 16665008 20784171 491671830 4545148 0 2107472 5873036 | |
8 1 sda1 324397 254 780862 23896 155 10246 80646 796 0 23844 24604 | |
8 2 sda2 2 0 4 0 0 0 0 0 0 0 0 | |
8 5 sda5 5554333 4656615 130855810 1297336 16664853 20773925 491591184 4544352 0 2069228 5833996 | |
252 0 dm-0 88209700 0 2031956178 27528932 428058600 0 114648810520 2374520096 0 56357652 2403020116 | |
252 1 dm-1 1891510 0 64292354 620312 29186278 0 425470448 4285744 0 1015860 4906752 | |
252 2 dm-2 7662265 0 61298120 1729304 8265092 0 66120736 34455500 0 1021008 36186260 | |
EOE | |
require 'ostruct' | |
class Diskstats | |
COLUMNS = %i( | |
major | |
minor | |
device_name | |
reads_success | |
reads_merged | |
sectors_read | |
read_time_ms | |
writes_success | |
writes_merged | |
sectors_written | |
write_time_ms | |
current_ios | |
io_time_ms | |
weightd_io_time_ms | |
) | |
attr_reader :raw_stats | |
def initialize(stats) | |
@raw_stats = stats | |
end | |
def rows | |
@rows ||= raw_stats.lines.map do |line| | |
OpenStruct.new(Hash[COLUMNS.zip(line.strip.split)]) | |
end | |
end | |
def self.from_proc | |
new(File.read('/proc/diskstats')) | |
end | |
end | |
puts Diskstats.new(EXAMPLE).rows.map(&:device_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment