Class: DockerBoss::Module::InfluxDB::Worker

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/docker_boss/module/influxdb.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Worker

Returns a new instance of Worker.



98
99
100
# File 'lib/docker_boss/module/influxdb.rb', line 98

def initialize(config)
  @config = config
end

Instance Method Details

#blkio_sample(id, type, file, key_prefix) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/docker_boss/module/influxdb.rb', line 142

def blkio_sample(id, type, file, key_prefix)
  return to_enum(:blkio_sample, id, type, file, key_prefix) unless block_given?
  data = {}

  File.readlines(build_path(id, type, file)).each do |line|
    (maj_min,k,v) = line.chomp.split(/\s+/, 3)
    if maj_min != 'Total'
      data["#{key_prefix}_#{k.downcase}"] ||= 0
      data["#{key_prefix}_#{k.downcase}"] += v.to_i
    end
  end

  data.each { |k,v| yield k, v }
end

#blkio_v_sample(id, type, file, key) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/docker_boss/module/influxdb.rb', line 157

def blkio_v_sample(id, type, file, key)
  return to_enum(:blkio_v_sample, id, type, file, key) unless block_given?
  data = {}

  File.readlines(build_path(id, type, file)).each do |line|
    (maj_min,v) = line.chomp.split(/\s+/, 2)
    data[key] ||= 0
    data[key] += v.to_i
  end

  data.each { |k,v| yield k, v }
end

#build_path(id, type, file) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/docker_boss/module/influxdb.rb', line 102

def build_path(id, type, file)
  if @config['docker_cg']
    "#{@config['cgroup_path']}/#{type}/docker/#{id}/#{file}"
  else
    "#{@config['cgroup_path']}/#{type}/system.slice/docker-#{id}.scope/#{file}"
  end
end

#kv_sample(id, type, file, key_prefix) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/docker_boss/module/influxdb.rb', line 133

def kv_sample(id, type, file, key_prefix)
  return to_enum(:kv_sample, id, type, file, key_prefix) unless block_given?

  File.readlines(build_path(id, type, file)).each do |line|
    (k,v) = line.chomp.split(/\s+/, 2)
    yield "#{key_prefix}_#{k.downcase}", v.to_i
  end
end

#sample_container(container) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/docker_boss/module/influxdb.rb', line 110

def sample_container(container)
  time_now = Time.now.to_i
  data = { time: time_now }

  kv_sample(container[:id], 'memory', 'memory.stat', 'memory') { |k,v| data[k] = v }
  kv_sample(container[:id], 'cpuacct', 'cpuacct.stat', 'cpuacct') { |k,v| data[k] = v }
  ['blkio.io_serviced', 'blkio.io_service_bytes',
   'blkio.io_wait_time', 'blkio.io_service_time', 'blkio.io_queued'].each do |f|
    blkio_sample(container[:id], 'blkio', f, f.gsub(/\./, '_')) { |k,v| data[k] = v }
  end
  ['blkio.sectors'].each do |f|
    blkio_v_sample(container[:id], 'blkio', f, f.gsub(/\./, '_')) { |k,v| data[k] = v }
  end

  name = container[:name].empty? ? container[:id] : container[:name]

  {
    name:           "#{@config['prefix']}#{name}",
    columns:        data.keys,
    points:         [ data.values ]
  }
end