以下内容为原创,转载请注明出处!



部署graphite,环境:ubuntu


创建虚拟环境

cd /opt/ virtualenv graphite_env --python=/usr/bin/python3.5

安装graphite

source /opt/graphite_env/bin/activate 
apt-get install libffi-dev 
apt-get install python-cairo-devte 
pip install cairocffi==0.6 
pip install --no-binary=:all: https://github.com/graphite-project/whisper/tarball/master 
pip install --no-binary=:all: https://github.com/graphite-project/carbon/tarball/master 
pip install --no-binary=:all: https://github.com/graphite-project/graphite-web/tarball/master 
cd /opt/graphite/conf/ 
cp aggregation-rules.conf.example aggregation-rules.conf 
cp carbon.amqp.conf.example carbon.amqp.conf 
cp dashboard.conf.example dashboard.conf 
cp graphTemplates.conf.example graphTemplates.conf 
cp rewrite-rules.conf.example rewrite-rules.conf 
cp storage-schemas.conf.example storage-schemas.conf 
cp blacklist.conf.example blacklist.conf 
cp carbon.conf.example carbon.conf 
cp graphite.wsgi.example graphite.wsgi 
cp relay-rules.conf.example relay-rules.conf 
cp storage-aggregation.conf.example storage-aggregation.conf 
cp whitelist.conf.example whitelist.conf

创建mysql库

用mysql来替代sqllite3数据库

CREATE DATABASE IF NOT EXISTS graphite DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

修改settins.py文件

cd /opt/graphite/webapp/graphite 
cp local_settings.py.example local_settings.py 
pip install mysqlclient vim local_settings.py 
//下面添加上 
SECRET_KEY = 'graphite_secret_key'  
ALLOWED_HOSTS = [ '*' ] 
TIME_ZONE = 'Asia/Shanghai' 
DATABASES = {
     'default': {
              'NAME': 'graphite',
              'ENGINE': 'django.db.backends.mysql',         
              'USER': 'root',         
              'PASSWORD': 'xxxx',         
              'HOST': '127.0.0.1',         
              'PORT': 3306     
     } 
}

初始化数据库

PYTHONPATH=/opt/graphite/webapp django-admin.py migrate --settings=graphite.settings --run-syncdb

启动carbon

cd /opt/graphite/bin ./carbon-cache.py star

启动服务(与下面这个二选一)

python run-graphite-devel-server.py --port=8085 --libs=/opt/graphite/webapp /opt/graphite

启动服务(gunicorn启动,线上推荐gunicorn+nginx;与上面这个二选一)

cp /opt/graphite/conf/graphite.wsgi ../webapp/graphite/graphite_wsgi.py
cd /opt/graphite/webapp/graphite 
gunicorn graphite_wsgi:application -b 0.0.0.0:8085 --daemon

执行测试样列

// 可根据具体需求修改休眠时间 
python /opt/graphite/examplesexample-client.py

查看样列推送的数据

/opt/graphite/storage/whisper/system 
ls 

loadavg_15min.wsp  loadavg_1min.wsp  loadavg_5min.wsp

查看其中具体的数据

whisper-dump.py loadavg_1min.wsp

推送数据python实例,也是参照example-client.py写的

import socket
import time
CARBON_SERVER = 'xx.xx.xx.xx'
CARBON_PORT = 2003

for k in range(100000):
    sock = socket.socket()
    sock.connect((CARBON_SERVER, CARBON_PORT))
    message = "test.meter.qps %d %d\n" % (k % 10, int(time.time()))
    print(message)
    sock.sendall(bytes(message, encoding='utf-8'))
    sock.close()
    time.sleep(1)