#!/usr/bin/ruby # card10-bme680-exporter - Prometheus exporter for card10 BME680 env sensor require 'rack' require 'rack/handler/thin' HOSTNAME = `hostname`.strip TTY = ARGV[1] || "/dev/ttyACM0" $data = {} Thread.new { File.open(TTY) { |tty| while line = tty.gets if line =~ /^BSECData\((.*)\)/ new_data = {} $1.split(/,\s+/).each { |s| k, v = s.split("=", 2) new_data[k] = v.to_f } $data = new_data end end } } class BME680Exporter def call(env) r = Rack::Response.new r.content_type = 'text/plain' $data.each { |k,v| r.write %Q|cardio_bsecdata_#{k}{hostname=#{HOSTNAME.dump}} #{v}\n| } r.finish end end Rack::Handler::Thin.run(BME680Exporter.new, :Port => 9882, :Host => '::', :debug => true)