Commit 02b44db8 authored by Robert May's avatar Robert May
Browse files

Initial probably working version

parents
source "https://rubygems.org"
gem "httparty"
gem "prometheus-client", require: "prometheus/client"
gem "sinatra"
gem "sinatra-contrib", require: "sinatra/config_file"
GEM
remote: https://rubygems.org/
specs:
backports (3.17.1)
httparty (0.18.0)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.0425)
multi_json (1.14.1)
multi_xml (0.6.0)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
prometheus-client (2.0.0)
rack (2.2.2)
rack-protection (2.0.8.1)
rack
ruby2_keywords (0.0.2)
sinatra (2.0.8.1)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.8.1)
tilt (~> 2.0)
sinatra-contrib (2.0.8.1)
backports (>= 2.8.2)
multi_json
mustermann (~> 1.0)
rack-protection (= 2.0.8.1)
sinatra (= 2.0.8.1)
tilt (~> 2.0)
tilt (2.0.10)
PLATFORMS
ruby
DEPENDENCIES
httparty
prometheus-client
sinatra
sinatra-contrib
BUNDLED WITH
2.1.4
#!/usr/bin/env ruby
#
# Fetch A&A line stats and return them to Prometheus
#
require "httparty"
require "prometheus/client"
require "prometheus/client/formats/text"
require "sinatra"
require "sinatra/config_file"
require "yaml"
# Settings
set :environment, :production
set :bind, "0.0.0.0"
set :port, 9729
set :stats_url, nil
config_file "/etc/prometheus/aanet.yml"
exit 0 if settings.stats_url.nil?
# Routes
get "/" do
"<a href='/metrics'>Metrics</a>"
end
before do
@registry = Prometheus::Client.registry
# General metrics
@tx_bytes_to_date = @registry.gauge(:aanet_tx_bytes_to_date, docstring: "Transmitted bytes to date")
@rx_bytes_to_date = @registry.gauge(:aanet_rx_bytes_to_date, docstring: "Received bytes to date")
@tx_pkts_to_date = @registry.gauge(:aanet_tx_pkts_to_date, docstring: "Transmitted packets to date")
@rx_pkts_to_date = @registry.gauge(:aanet_rx_pkts_to_date, docstring: "Received packets to date")
@tx_drops_to_date = @registry.gauge(:aanet_tx_drops_to_date, docstring: "Packet transmission failures to date")
# Metrics from the most recent record returned
@min_latency_ns = @registry.gauge(:aanet_min_latency_ns, docstring: "Minimum latency in nanoseconds from most recent record")
@avg_latency_ns = @registry.gauge(:aanet_avg_latency_ns, docstring: "Average latency in nanoseconds from most recent record")
@max_latency_ns = @registry.gauge(:aanet_max_latency_ns, docstring: "Maximum latency in nanoseconds from most recent record")
@avg_rx_bps = @registry.gauge(:aanet_avg_rx_bps, docstring: "Average bytes per second received from most recent record")
@avg_tx_bps = @registry.gauge(:aanet_avg_tx_bps, docstring: "Average bytes per second transmitted from most recent record")
@score = @registry.gauge(:aanet_score, docstring: "Score from most recent record")
# Counters across all records
@sent_polls = @registry.counter(:aanet_sent_polls, docstring: "Number of pings sent")
@lost_polls = @registry.counter(:aanet_lost_polls, docstring: "Number of pings failed")
end
get "/metrics" do
stats_doc = HTTParty.get(settings.stats_url)
if stats_doc.success?
_, cqm = *stats_doc.first
graph = cqm["graph"]
@tx_bytes_to_date.set(graph["tx_bytes_to_date"].to_i)
@rx_bytes_to_date.set(graph["rx_bytes_to_date"].to_i)
@tx_pkts_to_date.set(graph["tx_pkts_to_date"].to_i)
@rx_pkts_to_date.set(graph["rx_pkts_to_date"].to_i)
@tx_drops_to_date.set(graph["tx_drops_to_date"].to_i)
graph["record"].each do |record|
@sent_polls.increment(by: record["sent_polls"].to_i)
@lost_polls.increment(by: record["lost_polls"].to_i)
end
graph["record"].last.tap do |record|
@min_latency_ns.set(record["min_latency_ns"].to_i)
@avg_latency_ns.set(record["ave_latency_ns"].to_i)
@max_latency_ns.set(record["max_latency_ns"].to_i)
@avg_rx_bps.set(record["ave_rx_bps"].to_i)
@avg_tx_bps.set(record["ave_tx_bps"].to_i)
end
end
Prometheus::Client::Formats::Text.marshal(@registry)
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment