Class: OsCtl::Lib::LoadAvg

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/loadavg.rb

Overview

Read system load average from /proc/loadavg, see man proc(5)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: '/proc/loadavg') ⇒ LoadAvg

Returns a new instance of LoadAvg.

Parameters:

  • path (String) (defaults to: '/proc/loadavg')

    path to /proc/loadavg



17
18
19
# File 'lib/libosctl/loadavg.rb', line 17

def initialize(path: '/proc/loadavg')
  parse(path)
end

Instance Attribute Details

#avgHash<1, 5, 15> (readonly)

Returns load averages.

Returns:

  • (Hash<1, 5, 15>)

    load averages



5
6
7
# File 'lib/libosctl/loadavg.rb', line 5

def avg
  @avg
end

#last_pidInteger (readonly)

Returns:

  • (Integer)


14
15
16
# File 'lib/libosctl/loadavg.rb', line 14

def last_pid
  @last_pid
end

#runnableInteger (readonly)

Returns:

  • (Integer)


8
9
10
# File 'lib/libosctl/loadavg.rb', line 8

def runnable
  @runnable
end

#totalInteger (readonly)

Returns:

  • (Integer)


11
12
13
# File 'lib/libosctl/loadavg.rb', line 11

def total
  @total
end

Instance Method Details

#parse(path) ⇒ Object (protected)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/libosctl/loadavg.rb', line 31

def parse(path)
  parsed = File.read(path).strip.split

  @avg = {
    1 => parsed[0].to_f,
    5 => parsed[1].to_f,
    15 => parsed[2].to_f
  }

  runnable, total = parsed[3].split('/')
  @runnable = runnable.to_i
  @total = total.to_i

  @last_pid = parsed[4].to_i
end

#to_aObject



21
22
23
24
25
26
27
# File 'lib/libosctl/loadavg.rb', line 21

def to_a
  [
    @avg[1],
    @avg[5],
    @avg[15]
  ]
end