Sunday, June 1, 2014

Graphite on Nginx/uwsgi - Debian Wheezy

Graphite it an opensource application used to track metrics on a time series database. You can send metrics using almost any programming language and its way easier than other good options that may result a little more difficult to understand tools like OpenTSDB and RRDtool.

There are lots of tutorials and scripts that show how to install it,  most of them use apache to deploy the web interface. The graphite web app is written in Django and as such it's really easy to deploy using nginx.

Enough talking, let's get to work:

Install the required packages:
# Install the required packages for graphite
aptitude install -y python2.6 python-pip python-cairo python-django \
  python-django-tagging python-twisted python-memcache python-pysqlite2 \

# Install nginx and the required cgi modules
aptitude install -y nginx uwsgi-plugin-python uwsgi

# install graphite via PIP
pip install whisper carbon graphite-web
Create the configuration files:
cd /opt/graphite/conf/
# Create a directory with all configurations 
# It will allow us to keep this dir clean and use the original
# files as templates
mkdir examples; mv *.example examples

# create a copy of the required files
cp examples/storage-schemas.conf.example storage-schemas.conf
cp examples/storage-aggregation.conf.example storage-aggregation.conf
cp examples/carbon.conf.example carbon.conf
cp examples/graphite.wsgi.example
Create webapp configuration file
cp /opt/graphite/webapp/graphite/{,}
Uncomment the parameter "TIME_ZONE" on "/opt/graphite/webapp/graphite/" and replace with the result of "cat /etc/timezone". In my case the resulting edit looks like:
TIME_ZONE = 'America/Caracas'
Otherwise the webapp will assume the default time zone for Django apps: 'America/Chicago'.
Create nginx configuration file:
server {
  listen 8080;
  charset utf-8;
  access_log /var/log/nginx/graphite.access.log;
  error_log /var/log/nginx/graphite.error.log;

  location / {
  include uwsgi_params;
Enable the virtualhost:
ln -s /etc/nginx/sites-available/graphite /etc/nginx/sites-enabled/
Create the uwsgi configuration file:
processes = 2
socket =
gid = www-data
uid = www-data
chdir = /opt/graphite/conf
module = wsgi:application
Final uwsgi application configurations:
# enable the uwsgi app 
ln -s /etc/uwsgi/apps-available/graphite.ini /etc/uwsgi/apps-enabled/

# create the local database for the webapp
python /opt/graphite/webapp/graphite/ syncdb

# Allow access to  www-data to the webapp and storage  directories
chown -R www-data:www-data /opt/graphite/webapp/  /opt/graphite/storage/
Start carbon, the uwsgi and nginx daemons:
/opt/graphite/bin/ start
/etc/init.d/uwsgi restart
/etc/init.d/nginx restart
Check http://yourIPAddress:8080 and you should see the graphite web interface.


If you have any issues, remember to check the following log files for possible errors:
As a last resource you can set the Django app into debug mode by setting "Debug = True" on "/opt/graphite/webapp/graphite/".It should provide enough information to give you a hint on what may be causing the problem.