Module: Ocular::DSL::Graphite

Included in:
RunContext, Event::DefinitionProxy
Defined in:
lib/ocular/dsl/graphite.rb

Instance Method Summary collapse

Instance Method Details

#graphite(target, options = {}) ⇒ Object

add_help “graphite”, “what?”



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ocular/dsl/graphite.rb', line 11

def graphite(target, options = {})
    datasources = ::Ocular::Settings::get(:datasources)
    if !datasources or !datasources[:graphite]
        raise "No graphite client settings"
    end
    settings = datasources[:graphite]

    uri = URI.parse(settings[:url])
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = uri.instance_of?(URI::HTTPS)

    if !options[:from]
        options[:from] = "-1min"
    end

    if options[:format]
        options.delete(:format)
    end

    query = "/render?target=#{CGI.escape(target)}&format=json"
    options.each do |k, v|
        query += "&#{k}=#{CGI.escape(v)}"
    end

    request = Net::HTTP::Get.new(query)
    response = http.request(request)
    if !response
        raise "error, no response from post request"
    end
    if response.code.to_i != 200
        raise "Invalid response from graphite for query #{query}. Response code: #{response.code}, response body: #{response.body}"
    end

    begin
        return JSON.parse(response.body)
    rescue
        return nil
    end
end

#graphite_get_latests(target, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ocular/dsl/graphite.rb', line 51

def graphite_get_latests(target, options = {})
    values = {}
    response = graphite(target, options)
    response.each do |reply|
        if reply and reply["datapoints"] and reply["datapoints"].length > 0
            # Discard null replies
            not_null = reply["datapoints"].select {|x| x and x[0] }

            # sort in descending order
            not_null.sort! {|a,b| b[1] <=> a[1]}
            if not_null.length > 0
                # pick latests datapoint
                # from that pick the value                            
                values[reply["target"]] = not_null.first[0]
            end
        end
    end

    return values
end