debian wheezy下以uwsgi安装graphite

# abstract #
graphite是一个python写的性能监控系统。这个系统是由多个分离的部分组成的。
* graphite-web: 由django写的web界面系统。
* carbon: 数据收集的守护进程。
* whisper: 一种python写的数据库,类似rrd,便于大量的性能日志数据收集和处理。上两个组件会调用这个库。
* collectd: 数据收集守护进程,向carbon中喂数据的数据源。
另外,有一点黑色幽默的就是,graphite的意思是石墨,是炭(carbon)的一种同素异形体。因此在graphite项目中,多次出现carbon这个名字。当然,另两个同素异形体是钻石(diamond)和足球烯(footballene),你就暂时别指望看到他们的身影了。
另一个用python写的,以元素命名的著名软件是mercurial。化学元素中的汞,俗称水银,符号hg。因此mercurial的命令行简写才是hg。
以上几个的结构大概是这样的:
collectd(source) -network-> carbon -writing-> whisper database -reading-> graphite-web
下文描述了在debian wheezy下,以nginx+uwsgi模式安装graphite的过程。之所以用这个模式,是因为我的大部分系统都是python写的,同样安装在uwsgi下面。一事不烦二主。
# carbon #
carbon有对应的debian包,可以很简单的安装。

sudo aptitude install graphite-carbon
默认的数据端口是2003,默认的数据路径是/var/lib/graphite/,这个在下文需要用到。
# graphite #
## virtual ##
graphite有部分需要安装到系统中,因此最好用virtualenv进行安装。
cd /var/web/
sudo aptitude install python-virtualenv
virtualenv –system-site-packages graphite
我假定你的安装路径是/var/web/graphite,这个在下面要反复用到。
## install ##

在安装路径下,执行以下内容
source bin/activite
pip install graphite-web –install-option="–prefix=/var/web/graphite" –install-option="–install-lib=/var/web/graphite/webapp"
注意,/var/web/graphite需要根据上面的设定自行修改,webapp是你的django基础路径。
## configure ##
在/var/web/graphite/webapp/graphite下面,执行以下内容
cp local_settings.py.example local_settings.py
然后编辑local_settings.py
GRAPHITE_ROOT = '/var/web/graphite'
WHISPER_DIR = '/var/lib/graphite/whisper'
DATABASES = …
注意databases,不要在settings.py里面修改,要改这里的才有效。我用的是sqlite,如果你高兴,可以改为postgres/mysql。WHISPER_DIR是上文carbon中设定的目录。

最后,执行以下指令,完成数据库初始化。
python manage.py syncdb
## uwsgi ##
编辑/etc/uwsgi/apps-enabled/graphite.ini,包含以下内容。
[uwsgi]
plugins         = python
workers         = 1
chdir           = /var/web/graphite/webapp
pythonpath      = /var/web/graphite
env             = DJANGO_SETTINGS_MODULE=graphite.settings
module          = django.core.handlers.wsgi:WSGIHandler()
注意,这里的chdir,是你的webapp基础路径。而pythonpath则是virtualenv的路径。两者在上文都有说明的。env里面那个graphite是webapp/graphite这个app(不详细说明,自己学一下django就懂了)。module是django的固定写法。
## nginx ##
最后,在nginx中包含以下内容,将部分url转发到uwsgi上。
location ~ ^/(graphite|content|metrics|dashboard|render|browser|composer)/ {
     include        uwsgi_params;
uwsgi_param    UWSGI_SCHEME $scheme;
uwsgi_pass     unix:/run/uwsgi/app/graphite/socket;
}
# collectd #
## install ##
collectd的安装很简答,有包,直接安装就好。

sudo aptitude install collectd

## configure ##
在/etc/collectd/这个路径,能够看到collectd.conf这个文件。反注释掉以下内容:
<LoadPlugin python>
     Globals true
</LoadPlugin>
然后,再编辑以下内容。
<Plugin python>
     ModulePath "/etc/collectd/carbon"
     Import “carbon_writer”
     <Module “carbon_writer”>
     LineReceiverHost “localhost″
     LineReceiverPort 2003
     DifferentiateCountersOverTime true
     LowercaseMetricNames true
     TypesDB "/usr/share/collectd/types.db"
     </Module>

    </Plugin>

上文中假定你把python插件放在了/etc/collectd/carbon下面,所以下文需要按照这个路径安装carbon。
## collectd-carbon ##
在/etc/collectd下面,执行
sudo git clone  https://github.com/indygreg/collectd-carbon.git carbon
注意,sudo其实是不安全的。不过目前就这样吧,问题不明显。
## restart whole system ##
执行以下指令重启服务
sudo /etc/init.d/nginx restart
sudo /etc/init.d/uwsgi restart
sudo /etc/init.d/carbon-cache restart
sudo /etc/init.d/collectd restart

完成这步后,可以在/var/log/syslog中看到collectd的输出,数据确实的被灌入了carbon。而/var/log/uwsgi/app/graphite.log中可以看到uwsgi的输出。如果一切正常的话,你可以去[ http://localhost/graphite/](http://localhost/graphite/)下面看你要的东西了。
# 评价 #
数据收集的挺完整的。既然是基于collectd,那么应当没什么问题的,openwrt也可以支持的。问题是拿到数据后展现的一方面。由于默认配置太差,因此需要很长时间调教。我没那个功夫,搞定测试后就直接删除了。

你可能感兴趣的:(debian wheezy下以uwsgi安装graphite)