Class: Knj::Iotop

Inherits:
Object show all
Defined in:
lib/knj/iotop.rb

Overview

Currently broken - cannot get iotop to return values when running through script :-(

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Iotop

Returns a new instance of Iotop.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'lib/knj/iotop.rb', line 3

def initialize(args)
  @data = {}
  @mutex = Monitor.new
  
  cmd = "iotop -bPk"
  
  if args[:pids]
    args[:pids].each do |pid|
      cmd << " --pid=#{pid.to_i}"
    end
  end
  
  @stdout = IO.popen(cmd)
  
  @thread = Knj::Thread.new do
    @stdout.each_line do |line_str|
      if line_str.match(/^Total\s+disk\s+read:\s+([\d\.]+)\s+(K\/s)\s+\|\s+Total\s+disk\s+write:\s+([\d\.]+)\s+(K\/s)\s*$/i)
        #ignore.
      elsif line_str.match(/  PID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN      IO    COMMAND/)
        #ignore.
      elsif match = line_str.match(/^\s*(\d+)\s+(.+?)\s+(.+?)\s+([\d\.]+)\s+(K\/s)\s+([\d\.]+)\s+(K\/s)\s+([\d\.]+)\s+(%)\s+([\d\.]+)\s+(%)\s+(.+)\s*$/)
        @mutex.synchronize do
          pid = match[1].to_i
          
          if match[12].index("bestseller") != nil
            print line_str + "\n"
            Php4r.print_r(match)
          end
          
          @data[pid] = {
            :pid => pid,
            :prio => match[2],
            :user => match[3],
            :disk_read => (match[4].to_f * 1024).to_i,
            :disk_write => (match[6].to_f * 1024).to_i,
            :spawpin => match[8].to_f,
            :io => match[10].to_f,
            :cmd => match[12]
          }
        end
      else
        raise "Could not parse line: '#{line_str}'."
      end
    end
  end
end

Instance Method Details

#[](pid) ⇒ Object

Returns information for the given PID.



51
52
53
54
55
56
57
# File 'lib/knj/iotop.rb', line 51

def [](pid)
  @mutex.synchronize do
    pid = pid.to_i
    raise "No such PID: '#{pid}'." if !@data.key?(pid)
    return @data[pid]
  end
end