Skip to content

Instantly share code, notes, and snippets.

@apeiros

apeiros/foo.rb Secret

Last active December 17, 2015 12:08
Show Gist options
  • Save apeiros/3b61ce6f16840b0636ad to your computer and use it in GitHub Desktop.
Save apeiros/3b61ce6f16840b0636ad to your computer and use it in GitHub Desktop.
Foreign code, refactored
#!/usr/bin/env ruby
require "json"
require "net/http"
require 'trollop'
require 'statsd'
Statsd.host = 'localhost'
Statsd.port = 8125
opts = Trollop.options do
opt :warn, "Warning threshold", type: Integer, default: 1000
opt :crit, "Critical threshold", type: Integer, default: 10000
end
begin
uri = URI.parse("http://localhost:8080/metrics")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
rescue Errno::ECONNREFUSED
puts "CRITICAL - Connection Refused"
exit 2
end
unless response.code == "200"
puts "CRITICAL - Not 200 OK"
exit 2
end
Channel = Struct.new(:name, :size) do
def to_s
"#{name}: #{size}"
end
end
json = JSON.parse(response.body)
channels = json.select { |_k,v| v["Type"] == "CHANNEL" }.map { |name, data|
Channel.new(name.sub(/\ACHANNEL\./, ""), data["ChannelSize"].to_i)
}
max_size = channels.map(&:size).max
if max_size >= opts[:crit]
status = :critical
elsif max_size >= opts[:warn]
status = :warning
else
status = :ok
end
size_limit = {ok: 0, warn: opts[:warn], critical: opts[:crit]}[status]
exit_code = {ok: 0, warn: 1, critical: 2}[status]
relevant_channels = channels.select { |channel| channel.size >= size_limit }
channels.each do |channel|
Statsd.increment("flume.#{hostname}.#{name}.channel_size", channel.size) # from wherever hostname and name come…
end
puts "#{status.upcase} - #{relevant_channels.join(";")}"
exit exit_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment