Setting up “graphite-api” & Grafana on a Raspberry Pi

Graphite is a great graphing system with a very simple API for importing data, and a lot of support from other tools.

There are two parts to a Graphite installation:

  • “Carbon”  which is the process that handles receiving and storing data
  • “graphite-web” which provides a front-end and HTTP API

Graphite-web is pretty complex to install however – especially if you have minimal python knowledge – with a number of dependencies (e.g. django, MySQL) and associated configuration.  It’s also not the most elegant application to use.

As a result, a number of other front ends have been developed, one of which is the excellent Grafana.  Using alternative front-ends means you only really need the HTTP API from Graphite, and not the whole web application (with django etc), but the main Graphite project doesn’t support installing just this element.  There is however a project on Github that aims to provide just this – graphite-api.

This blog post will cover how to install carbon, graphite-api, and finally Grafana v1

Installing Carbon

Carbon can be install using apt:

apt-get install graphite-carbon

Once installed you should be able to start it with the standard “service carbon-cache start”.  This will silently fail however, because for some inexplicable reason, the package is configured by default to be disabled, and the init script only reports this if it is in “verbose” mode, which again by default it isn’t.  So the default install will just silently fail to do anything!

To fix this, edit /etc/default/graphite-carbon and change the line below to true:

CARBON_CACHE_ENABLED=true

Then “service carbon-cache start” should start the service.

Check carbon is running with the following python script:

If this returns a “socket.error”, check if carbon is running with “ps -ef | grep carbon”, and check for errors in /var/log/carbon/console.log

Installing graphite-api

Follow the “Python” instructions  at http://graphite-api.readthedocs.org/en/latest/installation.html#python-package

Install required dependencies

apt-get install python python-pip build-essential python-dev libcairo2-dev libffi-dev

And then graphite-api

pip install graphite-api

This will download and compile graphite-api.  If you have cryptic errors about “gcc”, check you have installed “build-essential” and all the required “*-dev” libraries.  Depending on your system, you may also need to install other dependencies, but “apt” should take care of this for you.

Configure carbon

Once installed you need to create the configuration file.  Graphite-api will run without a config file, but the default file locations are different to what graphite-carbon used so we need to manually specify them.

Create “/etc/graphite-api.yml” with the following contents.

search_index: /var/lib/graphite/index
finders:
  - graphite_api.finders.whisper.WhisperFinder
functions:
  - graphite_api.functions.SeriesFunctions
  - graphite_api.functions.PieFunctions
whisper:
  directories:
    - /var/lib/graphite/whisper
carbon:
  hosts:
    - 127.0.0.1:7002
  timeout: 1
  retry_delay: 15
  carbon_prefix: carbon
  replication_factor: 1

If you want to change the data locations, ensure you edit “/etc/carbon/carbon.conf” as well to match.

Deployment

Graphite-api doesn’t install a daemon like carbon, it needs to be run inside a web server.  There are several options documented on the website.  The simplest (although not most performant) is probably to use Apache and mod_wsgi

apt-get install libapache2-mod-wsgi

Then just follow the documented instructions.

Create /var/www/wsgi-scripts/graphite-api.wsgi

# /var/www/wsgi-scripts/graphite-api.wsgi

from graphite_api.app import app as application

And /etc/apache2/sites-available/graphite.conf

# /etc/apache2/sites-available/graphite.conf
LoadModule wsgi_module modules/mod_wsgi.so
WSGISocketPrefix /var/run/wsgi
Listen 8013
<VirtualHost *:8013>

 WSGIDaemonProcess graphite-api processes=5 threads=5 display-name='%{GROUP}' inactivity-timeout=120
 WSGIProcessGroup graphite-api
 WSGIApplicationGroup %{GLOBAL}
 WSGIImportScript /var/www/wsgi-scripts/graphite-api.wsgi process-group=graphite-api application-group=%{GLOBAL}

 WSGIScriptAlias / /var/www/wsgi-scripts/graphite-api.wsgi

 <Directory /var/www/wsgi-scripts/>
 Order deny,allow
 Allow from all
 </Directory>
 </VirtualHost>

Then symlink this into /etc/apache2/sites-enabled/

ln -s ../sites-available/graphite.conf .

Finally restarting Apache:

service apache2 restart

This should start graphite-api on port 8013.

You can check this by browsing to http://<IP_OF_PI >:8013/render?target=test.metric

This should return a fairly dull graph showing the data entered using the basic test python script above. If you get a image back that says “No Data”, check you have run the test python above successfully, and that your data paths in /etc/carbon/carbon.conf and “/etc/graphite-api.yml” match.

Any errors will be logged into the standard Apache error log at /var/log/apache2/error.log

Installing Grafana

The final step is to install the “Grafana” frontend. The original Grafana is a pure HTML5 application that connected directly to the graphite API and didn’t require anything other than a webserver to host the pages.  Grafana 2 has now been released, which as well as connecting to Graphite, also provides it’s own backend that is written in Go.

There aren’t prebuilt packages of Grafana 2 available for the Raspberry Pi, and building it from source would be quite a bit of time and hassle (if it’s even possible), so I’d recommend sticking to Grafana 1. The main limitation of Grafana 1 is being unable to directly save dashboards from the GUI. To save a dashboard, you will need to copy the JSON for it from the GUI, and save it manually as a file on the Pi to “/var/www/grafana/app/dashboards/”

Installation

  • Download the latest 1.x release from http://grafana.org/download/
  • Unzip this into /var/www/grafana
  • copy “config.sample.js” to “config.js” and edit the datasources section to point at your graphite instance above. This is likely to be http://IP_OF_PI:8013
  • Open a browser and point it at: http://IP_OF_PI/grafana and you should get the Grafana UI.
    • If you don’t get this, check your Javascript console log for errors or typos in config.js file

Explaining how to use Grafana is out of scope of this blog post, but have fun graphing all your r-pi stats!

Advertisement