Class: OsCtl::Lib::LoadAvgReader

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

Overview

Returns per-container load averages

Defined Under Namespace

Classes: LoadAvg

Constant Summary collapse

FILE =

This file contains a list of load averages from first-level cgroup namespaces

Available only on vpsAdminOS

'/proc/vpsadminos/loadavg'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.read_for(containers) ⇒ Hash<String, LoadAvg>

Read loadavgs for given containers and return them in a hash indexed by ctid

Parameters:

  • containers (Array<Hash>)

    list of containers as received from osctld

Returns:



22
23
24
25
26
27
28
29
30
31
# File 'lib/libosctl/loadavg_reader.rb', line 22

def self.read_for(containers)
  reader = new
  lavgs = {}

  reader.read(containers) do |lavg|
    lavgs[lavg.ident] = lavg
  end

  lavgs
end

Instance Method Details

#get_cgroup_ns_ids(containers) ⇒ Object (protected)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/libosctl/loadavg_reader.rb', line 55

def get_cgroup_ns_ids(containers)
  ret = {}

  containers.each do |ct|
    next if ct[:init_pid].nil?

    begin
      ptr = File.readlink(File.join('/proc', ct[:init_pid].to_s, 'ns/cgroup'))
    rescue Errno::ENOENT
      next
    end

    next if /^cgroup:\[(\d+)\]$/ !~ ptr

    cg_id = ::Regexp.last_match(1)
    ret[cg_id] = ct
  end

  ret
end

#parse(line, cgns_ids) ⇒ Object (protected)



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

def parse(line, cgns_ids)
  # <cgns id> <avg1> <avg5> <avg15> <runnable>/<total>
  cols = line.strip.split
  return if cols.size != 5

  ct = cgns_ids[cols[0]]
  return if ct.nil?

  runnable, total = cols[4].split('/')

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

#read(containers) {|lavg| ... } ⇒ Object

Read container load averages

Parameters:

  • containers (Array<Hash>)

    list of containers as received from osctld

Yield Parameters:

Yield Returns:

  • (:stop, any)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/libosctl/loadavg_reader.rb', line 37

def read(containers)
  cgns_ids = get_cgroup_ns_ids(containers)

  File.open(FILE, 'r') do |f|
    f.each_line do |line|
      lavg = parse(line, cgns_ids)
      next if lavg.nil?
      break if yield(lavg) == :stop
    end
  end

  nil
rescue Errno::ENOENT
  # ignore
end