Class: OsCtl::Lib::LoadAvgReader

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

Overview

Reads and parses per-container load averages from `/var/lib/lxcfs/proc/.loadavgs`

Defined Under Namespace

Classes: LoadAvg

Constant Summary collapse

FILE =
'/var/lib/lxcfs/proc/.loadavgs'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoadAvgReader

Returns a new instance of LoadAvgReader



60
61
62
# File 'lib/libosctl/loadavg_reader.rb', line 60

def initialize
  @fh = File.open(FILE, 'r')
end

Class Method Details

.read_allArray<LoadAvg>

Read loadavgs for all containers

Returns:



19
20
21
22
23
24
# File 'lib/libosctl/loadavg_reader.rb', line 19

def self.read_all
  reader = new
  ret = reader.read.to_a
  reader.close
  ret
end

.read_all_hashHash<String, LoadAvg>

Read loadavgs for all containers and return as a hash

Returns:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/libosctl/loadavg_reader.rb', line 28

def self.read_all_hash
  reader = new
  ret = {}

  reader.read.each do |lavg|
    ret[ lavg.ident ] = lavg
  end

  reader.close
  ret
end

.read_for(ctids) ⇒ Array<LoadAvg>

Read loadavgs for selected containers

Parameters:

  • ctids (Array<String>)

    container ids with pool, i.e. `<pool>:<ctid>`

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/libosctl/loadavg_reader.rb', line 43

def self.read_for(ctids)
  reader = new
  ret = []

  reader.read.each do |lavg|
    if ctids.include?(lavg.ident)
      ret << lavg
      ctids.delete(lavg.ident)
    end

    break if ctids.empty?
  end

  reader.close
  ret
end

Instance Method Details

#closeObject



69
70
71
# File 'lib/libosctl/loadavg_reader.rb', line 69

def close
  @fh.close
end

#parse(line) ⇒ Object (protected)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/libosctl/loadavg_reader.rb', line 74

def parse(line)
  # <cgroup> <avg1> <avg5> <avg15> <runnable>/<total> <last_pid>
  cols = line.strip.split
  return if cols.size != 6

  pool, ctid = parse_ct(cols[0])
  return if pool.nil?

  runnable, total = cols[4].split('/').map(&:to_i)

  LoadAvg.new(
    pool,
    ctid,
    {1 => cols[1].to_f, 5 => cols[2].to_f, 15 => cols[3].to_f},
    runnable,
    total,
    cols[5].to_i
  )
end

#parse_ct(cgroup) ⇒ Object (protected)



94
95
96
97
98
99
100
101
102
# File 'lib/libosctl/loadavg_reader.rb', line 94

def parse_ct(cgroup)
  return if /^\/osctl\/pool\.([^\/]+)/ !~ cgroup
  pool = $1

  return if /ct\.([^\/]+)\/user\-owned\// !~ cgroup
  ct = $1

  [pool, ct]
end

#readEnumerator

Returns:

  • (Enumerator)


65
66
67
# File 'lib/libosctl/loadavg_reader.rb', line 65

def read
  @fh.each_line.lazy.map { |line| parse(line) }.reject { |v| v.nil? }
end