telegraf-procio/procio.cr
2021-11-17 17:41:51 +01:00

28 lines
760 B
Crystal

dir = ENV.fetch("PROCIO_DIR", "/proc")
result = {} of String => Array(UInt64)
time = Time.local.to_s("%s%9N")
Dir.entries(dir).each do |entry|
entry =~ /^\d+$/ || next
begin
comm = File.read("#{dir}/#{entry}/comm").chomp
result[comm] ||= [0.to_u64, 0.to_u64]
File.read_lines("#{dir}/#{entry}/io").each do |line|
if line.starts_with?("read_bytes: ")
result[comm][0] += line.split(" ").last.to_u64
elsif line.starts_with?("write_bytes: ")
result[comm][1] += line.split(" ").last.to_u64
end
end
rescue File::NotFoundError
# process might already have exited
end
end
result.each do |comm, (r, w)|
if r + w > 0
puts %(procio,comm="#{comm}" read_bytes=#{r},write_bytes=#{w} #{time})
end
end