2024-01-20 23:22:16 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'pathname'
|
|
|
|
require 'prometheus/client'
|
|
|
|
require 'lxc'
|
2024-09-03 16:22:47 +02:00
|
|
|
require 'ostruct'
|
2024-09-22 11:25:28 +02:00
|
|
|
require 'pmap'
|
2024-01-20 23:22:16 +01:00
|
|
|
|
|
|
|
require_relative 'ns'
|
|
|
|
require_relative 'file-statfs'
|
2024-01-22 14:27:06 +01:00
|
|
|
require_relative 'cborio'
|
|
|
|
require_relative 'lxc_extend'
|
2024-01-20 23:22:16 +01:00
|
|
|
require_relative 'lxc-exporter/os_release'
|
|
|
|
require_relative 'lxc-exporter/mount_info'
|
|
|
|
|
|
|
|
class LxcCollector
|
|
|
|
include Prometheus::Client
|
|
|
|
|
2024-01-22 14:27:06 +01:00
|
|
|
CTStates = { aborting: -1, stopped: 0, starting: 0.75, stopping: 0.25, running: 1 }
|
2024-01-20 23:22:16 +01:00
|
|
|
|
|
|
|
def statfs ct, mis
|
|
|
|
mis.each do |mi|
|
|
|
|
next if %w[devpts devtmpfs fuse.lxcfs].include? mi.type
|
|
|
|
statfs = File::Statfs.new mi.mp
|
|
|
|
yield ct, mi.mp, statfs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize registry = nil
|
|
|
|
@prometheus ||= Registry.new
|
|
|
|
@up = Gauge.new :lxc_up, labels: %i[id name], docstring: 'Is CT running?'
|
|
|
|
@state = Gauge.new :lxc_state, labels: %i[id name],
|
|
|
|
docstring: "State of CT (#{CTStates.map{|n,i|"#{i}=#{n}"}.join', '})"
|
|
|
|
@info = Gauge.new :lxc_filesystem_info,
|
|
|
|
labels: %i[id mountpoint name fstype], docstring: 'CT Info'
|
|
|
|
@totalb = Gauge.new :lxc_filesystem_total_bytes,
|
|
|
|
labels: %i[id mountpoint], docstring: 'Total bytes in filesystem'
|
|
|
|
@freeb = Gauge.new :lxc_filesystem_free_bytes,
|
|
|
|
labels: %i[id mountpoint], docstring: 'Free bytes in filesystem'
|
|
|
|
@availb = Gauge.new :lxc_filesystem_available_bytes,
|
|
|
|
labels: %i[id mountpoint], docstring: 'Free bytes available to unprivileged user'
|
|
|
|
@totalf = Gauge.new :lxc_filesystem_total_files,
|
|
|
|
labels: %i[id mountpoint], docstring: 'Total files in filesystem'
|
|
|
|
@freef = Gauge.new :lxc_filesystem_free_files,
|
|
|
|
labels: %i[id mountpoint], docstring: 'Free files in filesystem'
|
|
|
|
@os_release = Gauge.new :lxc_os_release_info,
|
|
|
|
labels: %i[id name os_id os_name os_pretty_name os_version_codename],
|
|
|
|
docstring: 'OS release info'
|
|
|
|
@os_version = Gauge.new :lxc_os_version_id,
|
|
|
|
labels: %i[id], docstring: 'OS release (distribution) version'
|
|
|
|
@pkgs_upgradable = Gauge.new :lxc_packages_upgradable,
|
|
|
|
labels: %i[id], docstring: 'Count of upgradable packages'
|
|
|
|
@pkgs_last_update = Gauge.new :lxc_packages_last_update,
|
|
|
|
labels: %i[id], docstring: 'Last successfull source update'
|
|
|
|
[
|
|
|
|
@up, @state,
|
|
|
|
@info, @totalb, @freeb, @availb, @totalf, @freef,
|
|
|
|
@os_release, @os_version,
|
|
|
|
@pkgs_upgradable, @pkgs_last_update
|
|
|
|
].each {|g| @prometheus.register g }
|
|
|
|
end
|
|
|
|
|
|
|
|
AptLastUpdateFile = Pathname.new '/var/cache/apt/pkgcache.bin'
|
|
|
|
|
2024-09-03 16:22:47 +02:00
|
|
|
def forkedo wr
|
2024-01-20 23:22:16 +01:00
|
|
|
LXC.running_containers do |ct|
|
|
|
|
pid = ct.init_pid
|
|
|
|
mis = MountInfo.of pid
|
|
|
|
NS.change pid, :pid, :mnt do
|
|
|
|
name = ct.config_item( 'lxc.uts.name')
|
2024-01-22 14:27:06 +01:00
|
|
|
|
|
|
|
# Filesystems
|
2024-01-20 23:22:16 +01:00
|
|
|
statfs ct, mis do |ct, mp, st|
|
|
|
|
next unless st.fstypename and 0 < st.blocks
|
|
|
|
st = st.to_h.merge mp: mp, id: ct.name, name: name
|
2024-01-22 14:27:06 +01:00
|
|
|
wr.write [:mountpoint, st]
|
2024-01-20 23:22:16 +01:00
|
|
|
end
|
2024-01-22 14:27:06 +01:00
|
|
|
|
|
|
|
# OS / Distribution
|
2024-01-20 23:22:16 +01:00
|
|
|
osr = OsRelease.read
|
|
|
|
if osr
|
2024-01-22 14:27:06 +01:00
|
|
|
wr.write [:os_release, {
|
2024-01-20 23:22:16 +01:00
|
|
|
id: ct.name, name: name,
|
|
|
|
os_id: osr[:id],
|
|
|
|
os_name: osr[:name],
|
|
|
|
os_pretty_name: osr[:pretty_name],
|
|
|
|
os_version_id: osr[:version_id].to_i,
|
|
|
|
os_version_codename: osr[:version_codename]
|
2024-01-22 14:27:06 +01:00
|
|
|
}]
|
|
|
|
|
2024-01-20 23:22:16 +01:00
|
|
|
case osr[:id].to_sym
|
2024-01-22 14:27:06 +01:00
|
|
|
# Special things for Debian(-based) distributions
|
2024-01-20 23:22:16 +01:00
|
|
|
when :debian
|
|
|
|
upgradable = nil
|
|
|
|
pid =
|
|
|
|
IO.popen %w[apt list --upgradable], err: "/dev/null" do |apt|
|
|
|
|
upgradable = apt.readlines.length - 1
|
|
|
|
end
|
2024-01-22 14:27:06 +01:00
|
|
|
wr.write [:pkgs, {
|
2024-01-20 23:22:16 +01:00
|
|
|
id: ct.name, name: name, upgradable: upgradable,
|
2024-11-29 22:21:19 +01:00
|
|
|
last_update: AptLastUpdateFile.exist? ? AptLastUpdateFile.stat.mtime.to_f : -1,
|
2024-01-22 14:27:06 +01:00
|
|
|
}]
|
2024-01-20 23:22:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
wr.close
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
2024-09-03 16:22:47 +02:00
|
|
|
def collecto
|
2024-01-20 23:22:16 +01:00
|
|
|
LXC.containers do |ct|
|
|
|
|
labels = { id: ct.name, name: ct.config_item( 'lxc.uts.name') }
|
|
|
|
@up.set ct.running? ? 1 : 0, labels: labels
|
|
|
|
@state.set CTStates[ct.state], labels: labels
|
|
|
|
end
|
2024-01-22 14:27:06 +01:00
|
|
|
pid, rd = CBORIO.popen( &method( :forked))
|
|
|
|
rd.each do |l|
|
2024-01-20 23:22:16 +01:00
|
|
|
case l[0].to_sym
|
|
|
|
|
|
|
|
when :mountpoint
|
|
|
|
st = OpenStruct.new l[1]
|
|
|
|
labels = { id: st.id, mountpoint: st.mp }
|
|
|
|
@info.set 1, labels: labels.merge( name: st.name, fstype: st.fstypename)
|
|
|
|
@totalb.set st.bsize*st.blocks, labels: labels
|
|
|
|
@freeb.set st.bsize*st.bfree, labels: labels
|
|
|
|
@availb.set st.bsize*st.bavail, labels: labels
|
|
|
|
@totalf.set st.files, labels: labels
|
|
|
|
@freef.set st.ffree, labels: labels
|
|
|
|
|
|
|
|
when :os_release
|
|
|
|
osr = OpenStruct.new l[1]
|
|
|
|
@os_release.set 1, labels: {
|
|
|
|
id: osr.id, name: osr.name,
|
|
|
|
os_id: osr.os_id,
|
|
|
|
os_name: osr.os_name,
|
|
|
|
os_pretty_name: osr.os_pretty_name,
|
|
|
|
os_version_codename: osr.os_version_codename
|
|
|
|
}
|
|
|
|
@os_version.set osr.os_version_id, labels: { id: osr.id }
|
|
|
|
|
|
|
|
when :pkgs
|
|
|
|
as = OpenStruct.new l[1]
|
|
|
|
@pkgs_last_update.set as.last_update, labels: { id: as.id }
|
2024-01-22 14:27:06 +01:00
|
|
|
@pkgs_upgradable.set as.upgradable, labels: { id: as.id }
|
2024-01-20 23:22:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rd.close
|
|
|
|
Process.wait pid
|
|
|
|
@prometheus
|
|
|
|
end
|
2024-09-03 16:22:47 +02:00
|
|
|
|
|
|
|
def forked2 io, ct
|
|
|
|
pid = ct.init_pid
|
|
|
|
mis = MountInfo.of pid
|
|
|
|
id, name = ct.name, ct.config_item( 'lxc.uts.name')
|
|
|
|
NS.enter pid, keep_open: [io, STDERR, STDOUT]
|
|
|
|
|
|
|
|
# Filesystems
|
|
|
|
statfs ct, mis do |ct, mp, st|
|
|
|
|
next unless st.fstypename and 0 < st.blocks
|
|
|
|
st = st.to_h.merge mp: mp, id: id, name: name
|
|
|
|
io.write [:mountpoint, st]
|
|
|
|
end
|
|
|
|
|
|
|
|
# OS / Distribution
|
|
|
|
osr = OsRelease.read
|
|
|
|
unless osr.nil?
|
|
|
|
io.write [:os_release, {
|
|
|
|
id: id, name: name,
|
|
|
|
os_id: osr[:id],
|
|
|
|
os_name: osr[:name],
|
|
|
|
os_pretty_name: osr[:pretty_name],
|
|
|
|
os_version_id: osr[:version_id].to_i,
|
|
|
|
os_version_codename: osr[:version_codename]
|
|
|
|
}]
|
|
|
|
|
|
|
|
case osr[:id].to_sym
|
|
|
|
# Special things for Debian(-based) distributions
|
|
|
|
when :debian
|
|
|
|
upgradable = nil
|
|
|
|
pid =
|
|
|
|
IO.popen %w[apt list --upgradable], err: "/dev/null" do |apt|
|
|
|
|
upgradable = apt.readlines.length - 1
|
|
|
|
end
|
|
|
|
io.write [:pkgs, {
|
|
|
|
id: id, name: name, upgradable: upgradable,
|
|
|
|
last_update: AptLastUpdateFile.stat.mtime.to_f,
|
|
|
|
}]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Object
|
|
|
|
STDERR.puts "#{$!} (#{$!.class})", $!.backtrace.map {|x| " #{x}"}
|
|
|
|
end
|
|
|
|
|
|
|
|
def collect2
|
|
|
|
LXC.containers do |ct|
|
|
|
|
labels = { id: ct.name, name: ct.config_item( 'lxc.uts.name') }
|
|
|
|
@up.set ct.running? ? 1 : 0, labels: labels
|
|
|
|
@state.set CTStates[ct.state], labels: labels
|
|
|
|
end
|
|
|
|
|
2024-09-22 11:25:28 +02:00
|
|
|
LXC.running_containers.peach do |ct|
|
|
|
|
pid, io = CBORIO.popen(:r) {|io| forked2 io, ct }
|
|
|
|
begin
|
|
|
|
io.map do |l|
|
|
|
|
case l[0].to_sym
|
|
|
|
|
|
|
|
when :mountpoint
|
|
|
|
st = OpenStruct.new l[1]
|
|
|
|
labels = { id: st.id, mountpoint: st.mp }
|
|
|
|
@info.set 1, labels: labels.merge( name: st.name, fstype: st.fstypename)
|
|
|
|
@totalb.set st.bsize*st.blocks, labels: labels
|
|
|
|
@freeb.set st.bsize*st.bfree, labels: labels
|
|
|
|
@availb.set st.bsize*st.bavail, labels: labels
|
|
|
|
@totalf.set st.files, labels: labels
|
|
|
|
@freef.set st.ffree, labels: labels
|
|
|
|
|
|
|
|
when :os_release
|
|
|
|
osr = OpenStruct.new l[1]
|
|
|
|
@os_release.set 1, labels: {
|
|
|
|
id: osr.id, name: osr.name,
|
|
|
|
os_id: osr.os_id,
|
|
|
|
os_name: osr.os_name,
|
|
|
|
os_pretty_name: osr.os_pretty_name,
|
|
|
|
os_version_codename: osr.os_version_codename
|
|
|
|
}
|
|
|
|
@os_version.set osr.os_version_id, labels: { id: osr.id }
|
|
|
|
|
|
|
|
when :pkgs
|
|
|
|
as = OpenStruct.new l[1]
|
|
|
|
@pkgs_last_update.set as.last_update, labels: { id: as.id }
|
|
|
|
@pkgs_upgradable.set as.upgradable, labels: { id: as.id }
|
2024-09-03 16:22:47 +02:00
|
|
|
end
|
|
|
|
end
|
2024-09-22 11:25:28 +02:00
|
|
|
ensure
|
|
|
|
io.close
|
|
|
|
Process.wait pid
|
2024-09-03 16:22:47 +02:00
|
|
|
end
|
2024-09-22 11:25:28 +02:00
|
|
|
end
|
2024-09-03 16:22:47 +02:00
|
|
|
@prometheus
|
|
|
|
end
|
|
|
|
|
|
|
|
alias forked forked2
|
|
|
|
alias collect collect2
|
2024-01-20 23:22:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if Pathname.new( __FILE__).expand_path == Pathname.new( $0).expand_path
|
|
|
|
require 'prometheus/client/formats/text'
|
2024-09-03 16:22:47 +02:00
|
|
|
puts Prometheus::Client::Formats::Text.marshal( LxcCollector.new.collect2)
|
2024-01-20 23:22:16 +01:00
|
|
|
end
|