Friday, 6 April 2012

Installing Graphite on CentOS - Part 2 - Setting up Graphite

The first post in this series detailed how to build rpms for graphite. This next post details how to get a working graphite installation setup on your production CentOS servers.

These instructions make a few assumptions - the system can use EPEL repositories and it has SELINUX disabled. This sets up a minimum graphite install - literally the carbon-cache daemon to receive the data and the graphite web front end to present the data via a web browser. You can extend the setup to carbon-cache clusters fronted by relay servers, duplicate your carbon data across datacentres, run mysql backends and much more. I'll touch on some of these scenarios in future posts.

1. Install the dependencies:

yum -y install Django django-tagging bitmap bitmap-fonts python-zope-interface python-memcached python-sqlite2 python-ldap python-twisted pycairo memcached

2. Install the graphite RPMs:

yum --nogpgcheck localinstall carbon-0.9.9-1.noarch.rpm graphite-web-0.9.9-1.noarch.rpm whisper-0.9.9-1.noarch.rpm

3. Setup the carbon and graphite-web configuration files:

cd /opt/graphite/conf/
cp graphite.wsgi.example graphite.wsgi
cp storage-schemas.conf.example storage-schemas.conf
cp carbon.conf.example carbon.conf
cd ../webapp/graphite
cp local_settings.py.example local_settings.py

4. Update local_settings.py above with correct Timezone, memcache location, ldap database for authentication. For a basic setup this is the only config file you need to modify.

5. Create the the django DB. Note that you can host this within mysql. You will need to install python-MySQL to do so and configure local_settings.py accordingly. Otherwise it will create a local file based database.

# python /opt/graphite/webapp/graphite/manage.py syncdb
Creating table account_profile
Creating table account_variable
Creating table account_view
Creating table account_window
Creating table account_mygraph
Creating table dashboard_dashboard_owners
Creating table dashboard_dashboard
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_session
Creating table django_admin_log
Creating table django_content_type
Creating table tagging_tag
Creating table tagging_taggeditem
Creating table events_event

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no
Installing index for account.Variable model
Installing index for account.View model
Installing index for account.Window model
Installing index for account.MyGraph model
Installing index for dashboard.Dashboard_owners model
Installing index for auth.Permission model
Installing index for auth.Group_permissions model
Installing index for auth.User_user_permissions model
Installing index for auth.User_groups model
Installing index for auth.Message model
Installing index for admin.LogEntry model
Installing index for tagging.TaggedItem model
No fixtures found.

6. Now Install and configure Apache - we use wsgi to create running web enabled graphite cgi processes. To enable this install mod_wsgi:

yum -y install httpd mod_wsgi

7. Create a virtualhost file for graphite /etc/httpd/conf.d/graphite.conf - your virtualhost config will probably differ slightly depending on how you want your virtualhost configuration:

<VirtualHost *:80>
  ServerName graphite.somehost.com
  DocumentRoot "/opt/graphite/webapp"
  ErrorLog logs/graphite_error_log
  TransferLog logs/graphite_access_log
  LogLevel warn
  WSGIDaemonProcess graphite processes=5 threads=5 display-name=" {GROUP}" inactivity-timeout=120
  WSGIProcessGroup graphite
  WSGIScriptAlias / /opt/graphite/conf/graphite.wsgi
  Alias /content/ /opt/graphite/webapp/content/
  <Location "/content/">
   SetHandler None
  </Location>
  Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media/"
  <Location "/media/">
   SetHandler None
  </Location>
  <Directory /opt/graphite/conf/>
   Order deny,allow
   Allow from all
  </Directory>
</VirtualHost>

8. Update /etc/httpd/conf.d/wsgi.conf with a socket prefix:

LoadModule wsgi_module modules/mod_wsgi.so
WSGISocketPrefix /var/run/wsgi

9. Allow apache access to the storage data:

chown -R apache:apache /opt/graphite/storage/

10. Create the init script /etc/init.d/carbon-cache. My script here also can start the carbon relay if you uncomment the relevant sections - carbon-relay is useful if you are running graphite in a high-availability setup but I'll talk about that in future posts....

#!/bin/bash
#
# This is used to start/stop the carbon-cache daemon

# chkconfig: - 99 01
# description: Starts the carbon-cache daemon

# Source function library.
. /etc/init.d/functions

RETVAL=0
prog="carbon-cache"

start_relay () {
    /usr/bin/python /opt/graphite/bin/carbon-relay.py start
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success || failure
        echo
        return $RETVAL
}

start_cache () {
     /usr/bin/python /opt/graphite/bin/carbon-cache.py start
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success || failure
        echo
        return $RETVAL
}

stop_relay () {
    /usr/bin/python /opt/graphite/bin/carbon-relay.py stop
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success || failure
        echo
        return $RETVAL
}

stop_cache () {
          /usr/bin/python /opt/graphite/bin/carbon-cache.py stop
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success || failure
        echo
        return $RETVAL
}

# See how we were called.
case "$1" in
  start)
    #start_relay
    start_cache
        ;;
  stop)
    #stop_relay
    stop_cache
        ;;
  restart)
    #stop_relay
    stop_cache
    #start_relay
    start_cache
    ;;

  *)
        echo $"Usage: $0 {start|stop}"
        exit 2
        ;;
esac


11. Start the services:

service memcached start
service carbon-cache start
service httpd start

You should now have a running carbon-cache process and be able to browse your graphite data through the web-interface. In the next post I'll discuss some techniques for injecting your data into graphite.

4 comments:

Peto Velas said...

Hi buddy, perfect blog posts[1,2,3] about graphite...

if found small typo /etc/init.d/carbon-cache :)

start_relay must point to /opt/graphite/bin/carbon-relay.py and not /opt/graphite/bin/carbon-cache.py
and
start_cache must point to /opt/graphite/bin/carbon-cache.py and not /opt/graphite/bin/carbon-relay.py

Al Rix said...

Good spot! Sorry about that - will update! Thanks for the positive feedback though...

Tobias said...

Excellent post! I have followed it using CentOS 6.2 in Vagrant, but when starting apache I get the following output in /var/log/httpd/graphite_error_log

IOError: [Errno 13] Permission denied: '/opt/graphite/storage/log/webapp/info.log'

What did I miss?

- Tobi

Tobias said...

I have found an answer to my question before. Had to run the following command:

chown -R apache:apache /opt/graphite/storage

That's all.

Tobi