renamed to lxc-exporter. os-release, apt update / upgradable.

This commit is contained in:
Denis Knauf 2023-10-13 22:50:09 +02:00
parent d623efde64
commit e225cc96db
2 changed files with 146 additions and 61 deletions

View file

@ -1,12 +1,12 @@
require 'rack'
require_relative 'pvect-exporter'
require_relative 'lxc-exporter'
require 'prometheus/client/formats/text'
run lambda {|env|
req = Rack::Request.new env
case req.path
when "/metrics"
collector = PveCtCollector.new
collector = LxcCollector.new
[200, {"Content-Type" => "text/plain"}, [Prometheus::Client::Formats::Text.marshal( collector.collect)]]
else
[404, {"Content-Type" => "text/plain"}, ["Not found\nYou want to try /metrics?\n"]]

203
pvect-exporter.rb → lxc-exporter.rb Normal file → Executable file
View file

@ -26,7 +26,6 @@ end
class File
module Statfs
Magics = {}
#=begin
Line = <<~EOF
ADFS 0xadf5
AFFS 0xadff
@ -121,7 +120,6 @@ class File
end
Magics[v] = n.downcase.to_sym
end
#=end
extend Fiddle::Importer
dlload Fiddle.dlopen( nil)
@ -164,17 +162,12 @@ class File
end
class NS
#extend FFI::Library
#ffi_lib 'c'
#attach_function :setns_without_exception_handling, :setns, %i[int int], :int
extend Fiddle::Importer
dlload Fiddle.dlopen( nil)
SetNS_Function = Fiddle::Function.new handler['setns'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT
class <<self
def setns fd, t
#r = setns_without_exception_handling fd.to_i, t.to_i
#SystemCallError.new "setns(#{fd.to_i})", FFI::LastError.error if -1 == r
r = SetNS_Function.call fd.to_i, t.to_i
SystemCallError.new "setns(#{fd.to_i})", Fiddle.last_error if -1 == r
r
@ -247,81 +240,173 @@ class MountInfo
end
end
=begin
LxcStartExe = Pathname.new "/usr/bin/lxc-start"
def get_running_ct_pids &block
return to_enum( __method__) unless block_given?
class OsRelease
Path = Pathname.new '/etc/os-release'
class <<self
def parse txt
r =
txt.each_line.
map do |l|
k, v =
*case l.chomp
when /^([^=]+)="(.*)"$/ then [$1, $2]
when /^([^=]+)=(.*)$/ then [$1,$2]
else next
end
[ k.downcase.to_sym, v.sub( /^"/, '').sub( /"$/, '') ]
end.
compact.
to_h
new r
end
cts = []
Pathname.new( "/proc").each_child do |fn|
next if /\D/ =~ fn.basename.to_s
exepath = fn + 'exe'
next unless exepath.exist? and LxcStartExe == exepath.readlink
pid = fn.basename.to_s.to_i
cts.push pid
def read() parse Path.read if Path.exist? end
end
Pathname.new( "/proc").each_child do |fn|
next if /\D/ =~ fn.basename.to_s
statuspath = fn + 'status'
ppid = statuspath.read.match( /^PPid:\s+(\d+)$/)[1].to_i
next unless cts.include? ppid
yield fn.basename.to_s.to_i
def initialize( h) @h = h.freeze end
def to_h() @h.dup end
def []( n) @h[n] end
def missing_method n, *a
if a.empty? and @h.has_key?( n)
@h[n]
else
super n, *a
end
end
end
pids = get_running_ct_pids
=end
class PveCtCollector
def fetch
LXC.list_containers.map {|n|LXC::Container.new n}.select( &:running?).each do |ct|
pid = ct.init_pid
mis = MountInfo.of pid
NS.change pid, :pid, :mnt do
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
class LxcCollector
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 ||= Prometheus::Client::Registry.new
@info = Prometheus::Client::Gauge.new :pvect_filesystem_info, labels: %i[id mountpoint name fstype], docstring: 'CT Info'
@totalb = Prometheus::Client::Gauge.new :pvect_filesystem_total_bytes, labels: %i[id mountpoint], docstring: 'Total bytes in filesystem'
@freeb = Prometheus::Client::Gauge.new :pvect_filesystem_free_bytes, labels: %i[id mountpoint], docstring: 'Free bytes in filesystem'
@availb = Prometheus::Client::Gauge.new :pvect_filesystem_available_bytes, labels: %i[id mountpoint], docstring: 'Free bytes available to unprivileged user'
@totalf = Prometheus::Client::Gauge.new :pvect_filesystem_total_files, labels: %i[id mountpoint], docstring: 'Total files in filesystem'
@freef = Prometheus::Client::Gauge.new :pvect_filesystem_free_files, labels: %i[id mountpoint], docstring: 'Free files in filesystem'
[@info, @totalb, @freeb, @availb, @totalf, @freef].each {|g| @prometheus.register g }
@info = Prometheus::Client::Gauge.new :lxc_filesystem_info,
labels: %i[id mountpoint name fstype], docstring: 'CT Info'
@totalb = Prometheus::Client::Gauge.new :lxc_filesystem_total_bytes,
labels: %i[id mountpoint], docstring: 'Total bytes in filesystem'
@freeb = Prometheus::Client::Gauge.new :lxc_filesystem_free_bytes,
labels: %i[id mountpoint], docstring: 'Free bytes in filesystem'
@availb = Prometheus::Client::Gauge.new :lxc_filesystem_available_bytes,
labels: %i[id mountpoint], docstring: 'Free bytes available to unprivileged user'
@totalf = Prometheus::Client::Gauge.new :lxc_filesystem_total_files,
labels: %i[id mountpoint], docstring: 'Total files in filesystem'
@freef = Prometheus::Client::Gauge.new :lxc_filesystem_free_files,
labels: %i[id mountpoint], docstring: 'Free files in filesystem'
@os_release = Prometheus::Client::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 = Prometheus::Client::Gauge.new :lxc_os_version_id,
labels: %i[id], docstring: 'OS release (distribution) version'
@pkgs_upgradable = Prometheus::Client::Gauge.new :lxc_packages_upgradable,
labels: %i[id], docstring: 'Count of upgradable packages'
@pkgs_last_update = Prometheus::Client::Gauge.new :lxc_packages_last_update,
labels: %i[id], docstring: 'Last successfull source update'
[
@info, @totalb, @freeb, @availb, @totalf, @freef,
@os_release, @os_version,
@pkgs_upgradable, @pkgs_last_update
].each {|g| @prometheus.register g }
end
def running_containers &exe
return to_enum __method__ unless block_given?
LXC.
list_containers.
map {|n| LXC::Container.new n }.
select( &:running?).
each &exe
end
AptLastUpdateFile = Pathname.new '/var/cache/apt/pkgcache.bin'
def forked wr
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')
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
wr.puts [:mountpoint, st].to_json
end
osr = OsRelease.read
if osr
wr.puts [:os_release, {
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]
}].to_json
case osr[:id].to_sym
when :debian
upgradable = nil
pid =
IO.popen %w[apt list --upgradable], err: "/dev/null" do |apt|
upgradable = apt.readlines.length - 1
end
wr.puts [:pkgs, {
id: ct.name, name: name, upgradable: upgradable,
last_update: AptLastUpdateFile.stat.mtime.to_f,
}].to_json
end
end
end
end
wr.close
exit 0
end
def collect
rd, wr = IO.pipe
pid = fork do
rd.close
fetch do |ct, mp, st|
next unless st.fstypename and 0 < st.blocks
wr.puts st.to_h.merge( mp: mp, id: ct.name, name: ct.config_item( 'lxc.uts.name')).to_json
end
wr.close
exit 0
forked wr
end
wr.close
rd.each_line do |l|
st = OpenStruct.new JSON.parse( l)
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
l = JSON.parse l, symbolize_names: true
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 }
end
end
rd.close
Process.wait pid
@prometheus
end
end
if Pathname.new( __FILE__).expand_path == Pathname.new( $0).expand_path
require 'prometheus/client/formats/text'
puts Prometheus::Client::Formats::Text.marshal( LxcCollector.new.collect)
end