Graphite

From Unix Wiki
Jump to: navigation, search

Graphite on smartos

Create user and group:

groupadd graphite
useradd -c "Graphite User" -m -d /opt/graphite -g graphite -s /bin/bash graphite

Install packages:

pkgin up
pkgin -y ug
pkgin -y in gcc47 openldap-client py27-amqplib py27-cairo py27-memcached py27-sqlite2 py27-twisted ap22-py27-wsgi-3.3

Configure Apache web server to run under 'graphite' user to avoid permission issues:

vi /opt/local/etc/httpd/includes/core.conf
- User www
- Group www
+ User graphite
+ Group graphite

Create wsgi directory:

mkdir /opt/local/etc/httpd/wsgi

Create virtual host config file: /opt/local/etc/httpd/virtualhosts/graphite.conf:

# This needs to be in your server's config somewhere, probably
# the main httpd.conf
# NameVirtualHost *:80

# This line also needs to be in your server's config.
# LoadModule wsgi_module modules/mod_wsgi.so

# You need to manually edit this file to fit your needs.
# This configuration assumes the default installation prefix
# of /opt/graphite/, if you installed graphite somewhere else
# you will need to change all the occurances of /opt/graphite/
# in this file to your chosen install location.

<IfModule !wsgi_module.c>
    LoadModule wsgi_module lib/httpd/mod_wsgi.so
</IfModule>

# XXX You need to set this up!
# Read http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGISocketPrefix
WSGISocketPrefix etc/httpd/wsgi
WSGIPythonHome /opt/graphite

<VirtualHost *:80>
        ServerName www.example.com
        DocumentRoot "/opt/graphite/webapp"
        ErrorLog /opt/graphite/storage/log/webapp/error.log
        CustomLog /opt/graphite/storage/log/webapp/access.log common

        # I've found that an equal number of processes & threads tends
        # to show the best performance for Graphite (ymmv).
        WSGIDaemonProcess graphite processes=5 threads=5 display-name='%{GROUP}' inactivity-timeout=120
        WSGIProcessGroup graphite
        WSGIApplicationGroup %{GLOBAL}
        WSGIImportScript /opt/graphite/conf/graphite.wsgi process-group=graphite application-group=%{GLOBAL}

        # XXX You will need to create this file! There is a graphite.wsgi.example
        # file in this directory that you can safely use, just copy it to graphite.wgsi
        WSGIScriptAlias / /opt/graphite/conf/graphite.wsgi

        Alias /content/ /opt/graphite/webapp/content/
        <Location "/content/">
                SetHandler None
        </Location>

        # XXX In order for the django admin site media to work you
        # must change @DJANGO_ROOT@ to be the path to your django
        # installation, which is probably something like:
        # /usr/lib/python2.6/site-packages/django
        Alias /static/admin/ "/opt/graphite/lib/python2.7/site-packages/django/contrib/admin/static/admin/"
        <Location "/media/">
                SetHandler None
        </Location>

        # The graphite.wsgi file has to be accessible by apache. It won't
        # be visible to clients because of the DocumentRoot though.
        <Directory /opt/graphite/conf/>
                Order deny,allow
                Allow from all
        </Directory>

        <Directory /opt/graphite/webapp/>
                Order deny,allow
                Allow from all
        </Directory>

        <Directory /opt/graphite/lib/python2.7/site-packages/django/contrib/admin/static/admin/>
                Order deny,alloW
                Allow from all
        </Directory>
</VirtualHost>

Switch to graphite user:

su - graphite

Dowload Virtual env script:

wget --no-check-certificate https://raw.github.com/pypa/virtualenv/master/virtualenv.py

Create Virtual Env:

python virtualenv.py --system-site-packages /opt/graphite

Activate env:

echo "source ~/bin/activate" >> ~/.bashrc
exit
su - graphite

Install python packages:

pip install django django-tagging django-admin-tools gunicorn python-ldap carbon whisper graphite-web

Copy configuration files (note: Standart configuration is very good):

cd ~/conf/
cp carbon.conf.example carbon.conf
cp graphite.wsgi.example graphite.wsgi
cp dashboard.conf.example dashboard.conf
cp storage-schemas.conf.example storage-schemas.conf
cp graphTemplates.conf.example graphTemplates.conf

Configure graphite:

cd ~/webapp/graphite
cp local_settings.py.example local_settings.py
vi local_settings.py

Uncomment and configure following:

TIME_ZONE = 'America/Los_Angeles'
GRAPHITE_ROOT = '/opt/graphite'
STORAGE_DIR = '/opt/graphite/storage'
CONTENT_DIR = '/opt/graphite/webapp/content'
DASHBOARD_CONF = '/opt/graphite/conf/dashboard.conf'
GRAPHTEMPLATES_CONF = '/opt/graphite/conf/graphTemplates.conf'
WHISPER_DIR = '/opt/graphite/storage/whisper'
RRD_DIR = '/opt/graphite/storage/rrd'
DATA_DIRS = [WHISPER_DIR, RRD_DIR] # Default: set from the above variables
LOG_DIR = '/opt/graphite/storage/log/webapp'
INDEX_FILE = '/opt/graphite/storage/index'  # Search index file
DATABASES = {
    'default': {
        'NAME': '/opt/graphite/storage/graphite.db',
        'ENGINE': 'django.db.backends.sqlite3',
#        'USER': ,
#        'PASSWORD': ,
#        'HOST': ,
#        'PORT': 
    }
}

Set SECRET_KEY:

vi ~/webapp/graphite/app_settings.py
- SECRET_KEY = 
+ SECRET_KEY = 'secret00'

Set Allowed hosts:

vi ~/webapp/graphite/settings.py
+ ALLOWED_HOSTS = ["www.example.com","examle.com"]
+ STATIC_URL = '/static/'

Create DATABASE and set Admin password:

python ~/webapp/graphite/manage.py syncdb

Logout:

exit

Create carboon-cache service script:

cd /var/svc/manifest/site/

/var/svc/manifest/site/carbon-cache.xml:

<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<service_bundle type='manifest' name='carbon-cache'>
  <service name='site/carbon-cache' type='service' version='0'>
    <create_default_instance enabled="false" />
    <single_instance />
    <dependency name='network' grouping='require_all' restart_on='error' type='service'>
      <service_fmri value='svc:/milestone/network:default' />
    </dependency>
    <dependency name='filesystem-local' grouping='require_all' restart_on='none' type='service'>
      <service_fmri value='svc:/system/filesystem/local:default' />
    </dependency>
    <dependency name='autofs' grouping='optional_all' restart_on='error' type='service'>
      <service_fmri value='svc:/system/filesystem/autofs:default' />
    </dependency>
    <exec_method name='start' type='method' exec='/usr/local/svc/method/carbon-cache start' timeout_seconds='300' />
    <exec_method name='stop' type='method' exec='/usr/local/svc/method/carbon-cache stop' timeout_seconds='300' />
    <exec_method name='status' type='method' exec='/usr/local/svc/method/carbon-cache status' timeout_seconds='300' 

/>
    <exec_method name='restart' type='method' exec='/usr/local/svc/method/carbon-cache restart' timeout_seconds='300' 

/>
    <property_group name='startd' type='framework'>
      <propval name='ignore_error' type='astring' value='core,signal' />
    </property_group>
    <stability value='Evolving' />
    <template>
      <common_name>
        <loctext xml:lang='C'>Carbon-cache service</loctext>
      </common_name>
    </template>
  </service>
</service_bundle>
mkdir -p /usr/local/svc/method/

/usr/local/svc/method/carbon-cache:

#!/sbin/sh

# Solaris carbon-cache startup script
# ident "@(#)carbon-cache SMI"

USER=graphite
CARBONBIN=/opt/graphite/bin

case "$1" in
'start')
  su - $USER -c "$CARBONBIN/carbon-cache.py start"
  ;;
'stop')
  su - $USER -c "$CARBONBIN/carbon-cache.py stop"
  ;;
'status')
  su - $USER -c "$CARBONBIN/carbon-cache.py status"
  ;;
'restart')
  su - $USER -c "$CARBONBIN/carbon-cache.py stop"
  sleep 1
  su - $USER -c "$CARBONBIN/carbon-cache.py start"
  ;;
*)
  echo $"Usage: $0 {start|stop|status|restart}"
  exit 1
  ;;
esac

exit 0
chmod +x /usr/local/svc/method/carbon-cache
svccfg import /var/svc/manifest/site/carbon-cache.xml

Start carbon-cache and check status:

svcadm enable carbon-cache
/usr/local/svc/method/carbon-cache status

Restart apache service:

svcadm restart apache