-
Notifications
You must be signed in to change notification settings - Fork 43
Using in Ruby
The point of the DSL is that you can parse the files and get a Graphite URL out of it, these are some examples of doing that.
Given a graph file in cpu.graph that looks like:
title "This is a test graph"
field :foo, :data => "my.munin.load.load"
You can read this file and obtain a URL for it:
require 'graphite_graph'
graph = GraphiteGraph.new("cpu.graph")
puts graph.url
When run this will output the following:
title=This is a test graph&vtitle=&from=-1hour&width=500&height=250&areaMode=none&hideLegend=&target=alias(my.munin.load.load,"Foo")
You do not need to use a graph file you can create graphs programatically:
require 'graphite_graph'
graph = GraphiteGraph.new(:none)
graph.title "This is a test graph"
graph.field :foo, :data => "my.munin.load.load"
puts graph.url
This will have the exact same effect as the graph file top of this page.
Graphite can output both graphs and data, you can get a url to the data by doing this:
graph = GraphiteGraph.new("cpu.graph")
puts graph.url(:json)
When run the url will have format=json appended to it.
If you do not want the url completely built you can get an array of each part of the URL:
graph = GraphiteGraph.new("cpu.graph")
pp graph.url(nil, false)
This will produce:
["title=This is a test graph",
"from=-1hour",
"width=500",
"height=250",
"areaMode=none",
"hideLegend=",
"target=alias(my.munin.load.load,\"Foo\")",
"format=json"]
Most of the graph properties are accessible:
graph = GraphiteGraph.new("cpu.graph")
puts "Title: %s" % graph[:title]
puts "From: %s" % graph[:from]
This will produce:
Title: This is a test graph
From: -1hour
You can override graph properties, for example here we force specific graph dimensions regardless of what the cpu.graph file says.
graph = GraphiteGraph.new("cpu.graph", {:width => 800, :height => 600})
This graph will have the dimensions 800x600 no matter what is in cpu.graph
You can pass arbitrary data into the graph object that your graph files can use. Here we pass in a prefix into the graph so that we can reuse the same .graph file for many hosts.
graph = GraphiteGraph.new("cpu.graph", {}, {:fqdn => "my.example.com"})
Given a graph description file cpu.graph:
title "Load average for #{info[:fqdn]}"
field :foo, :data => "#{info[:fqdn]}.load.load"
This will create the url:
title=Load average for my.example.com&from=-1hour&width=500&height=250&areaMode=none&hideLegend=&target=alias(my.example.com.load.load,"Foo")