Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Using in Ruby

ripienaar edited this page Oct 16, 2011 · 2 revisions

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.

Loading graphs from a file

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")

Creating graphs programatically

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.

Creating URLs to access graph data

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.

Getting the URL Parts

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"]

Acessing graph properties

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

Overriding graph properties

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

Parameterizing graph contents

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")