Created
November 21, 2016 15:59
-
-
Save ptqa/dce3c852b3e0d81c3bc1230ac48253e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "webrick" | |
require 'sys/proctable' | |
include Sys | |
# Respond with 200 OK. | |
def respond_ok(response) | |
response.status = 200 | |
response.body = "OK" | |
end | |
# Respond with 404 Not Found. | |
def respond_404(response) | |
response.status = 404 | |
response.body = "Not Found" | |
end | |
def count_procs(state, ps) | |
ps.map(&:state).select {|x| x == state}.size | |
end | |
def respond_metrics(response) | |
ps = ProcTable.ps | |
states = {'R' => 'running', | |
'S' => 'sleeping', | |
'D' => 'disk sleep', | |
'T' => 'stopped', | |
't' => 'tracing stop', | |
'X' => 'dead', | |
'Z' => 'zombie'} | |
# node_processes{status="stopped"} 1 | |
r_body = '' | |
states.each do |k,v| | |
r_body += "node_processes{status=\"#{v}\"} #{count_procs(k,ps)}\n" | |
end | |
response.status = 200 | |
response.body = r_body | |
end | |
# A simple HTTP router. | |
class HTTPRouter < WEBrick::HTTPServlet::AbstractServlet | |
def do_GET(request, response) | |
if request.path == "/health" | |
respond_ok(response) | |
elsif request.path == "/metrics" | |
respond_metrics(response) | |
else | |
respond_404(response) | |
end | |
end | |
end | |
server = WEBrick::HTTPServer.new(:Port => 9150, :DoNotReverseLookup => true) | |
server.mount("/", HTTPRouter) | |
server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment