-
-
Save anonymous/82dcbea6f27f2eb765c9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module Flume | |
| require 'json' | |
| class Metric | |
| attr_reader :name | |
| def initialize(name,attrs) | |
| @name = name | |
| @attr = attrs | |
| end | |
| def attribute(name) | |
| @attr[name].to_i | |
| end | |
| end | |
| class Channel < Metric | |
| end | |
| class Sink < Metric | |
| end | |
| class Metrics | |
| def initialize(uri) | |
| @uri = URI.parse(uri) | |
| get_metrics(@uri) | |
| end | |
| def channels | |
| @channels.values | |
| end | |
| def sinks | |
| @sinks.values | |
| end | |
| def channel(name) | |
| @channels[name] | |
| end | |
| def sink(name) | |
| @sinks[name] | |
| end | |
| private | |
| def get_metrics(uri) | |
| begin | |
| http = Net::HTTP.new(@uri.host, @uri.port) | |
| request = Net::HTTP::Get.new(@uri.request_uri) | |
| @response = http.request(request) | |
| rescue | |
| puts "CRITICAL - Unable to hit Flume metrics end point" | |
| exit 2 | |
| parse_json | |
| end | |
| end | |
| def parse_json | |
| begin | |
| metrics = JSON.parse(@response.body) | |
| rescue | |
| puts "CRITICAL - Unable to parse json" | |
| exit2 | |
| end | |
| channels = {} | |
| sinks = {} | |
| metrics.each do |key,attr| | |
| name = key.split('.')[1] | |
| if attr['Type'] == 'CHANNEL' | |
| channels[name] = Channel.new(name, attr) | |
| elsif attr['Type'] == 'SINK' | |
| sinks[name] = Sink.new(name, attr) | |
| else | |
| warn "Unexpected type #{attr['Type']} found. Skipping" | |
| end | |
| end | |
| @channels = channels | |
| @sinks = sinks | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment