grafana,按照官方的说法是 Beautiful metric & analytic dashboards
。grafana 负责数据的展现,能够配置各类不一样的数据源,其中包括 graphite。python
graphite 包含多个模块,这里咱们使用的模块包括:nginx
本文的搭建的监控系统结构以下:git
在本文档中,咱们会尽可能将相关文件安装在/opt/graphite目录github
对于某些默认Python环境不是2.7的系统,须要安装Python2.7。web
从源码编译Python2.7shell
configure make make install
建立Python2.7的virtualenv环境数据库
virtualenv /opt/graphite --python=/usr/local/bin/python
加载virtualenv环境centos
source /opt/graphite/bin/activate
pip install carbon --install-option="--prefix=/opt/graphite" --install-option="--install-lib=/opt/graphite/lib" pip install whisper
使用默认的配置文件:api
cp /opt/graphite/conf/storage-schemas.conf.example /opt/graphite/conf/storage-schemas.conf cp /opt/graphite/conf/carbon.conf.example /opt/graphite/conf/carbon.conf
启动 Carbonbash
/opt/graphite/bin/carbon-cache.py start
carbon的文件目录在配置文件 /opt/graphite/conf/carbon.conf 中进行定义,下面是默认的配置:
LOCAL_DATA_DIR = /opt/graphite/storage/whisper/
yum install libffi-devel pip install graphite-api --install-option="--prefix=/opt/graphite"
首先安装uwsgi
pip install uwsgi
建立graphite-api的配置文件:/opt/graphite/etc/graphite-api.yml
search_index: /opt/graphite/index finders: - graphite_api.finders.whisper.WhisperFinder functions: - graphite_api.functions.SeriesFunctions - graphite_api.functions.PieFunctions whisper: directories: - /opt/graphite/storage/whisper carbon: hosts: - 127.0.0.1:7002 timeout: 1 retry_delay: 15 carbon_prefix: carbon replication_factor: 1
在这个配置文件中,whisper的数据路径配置为/opt/graphite/storage/whisper,这个是在carbon配置文件 /opt/graphite/conf/carbon.conf 中使用配置项LOCAL_DATA_DIR进行定义的。
centos中没有uwsgi的package,须要自行下载相关的启动脚本,这里使用 https://github.com/jgoldschrafe/rpm-uwsgi
wget -O /etc/init.d/uwsgi https://raw.githubusercontent.com/jgoldschrafe/rpm-uwsgi/master/SOURCES/uwsgi.init chmod +x /etc/init.d/uwsgi
编辑文件 /etc/init.d/uwsgi 进行目录配置
uwsgi="/opt/graphite/bin/uwsgi" prog=$(basename "$uwsgi") UWSGI_CONF_DIR="/etc/uwsgi" UWSGI_LOG_DIR="/var/log/uwsgi" PIDFILE_DIR="/var/run/uwsgi"
建立文件/etc/uwsgi/graphite-api.ini
[uwsgi] processes = 2 socket = 0.0.0.0:5000 module = graphite_api.app:app home = /opt/graphite env = GRAPHITE_API_CONFIG=/opt/graphite/conf/graphite-api.yml
启动uwsgi
service uwsgi start
启动之后会监听5000端口,注意这里5000端口是uwsgi协议,须要后面配置nginx进行代理。
server { listen 81; location / { include uwsgi_params; uwsgi_pass localhost:5000; } }
nginx启动后能够访问洗面的连接检查数据返回是否正常
http://127.0.0.1:81/render?target=test.metric
可使用多种个方式向监控系统中写入数据,例如 dropwizard metrics。这里为了方便使用Python进行数据上报:
import socket import time CARBON_SERVER = 'xxx.xxx.xxx.xxx' CARBON_PORT = 2003 for k in xrange(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(message) sock.close() time.sleep(5)
程序运行之后能够在carbon的数据目录中会发现以下的文件结构:
test |-- metric.wsp |-- meter | |-- qps.wsp
首先配置grafana使用graphite做为数据源
配置数据显示:
对于监控系统,长期运行之后必然会积攒大量的历史数据,whisper 经过配置数据保存的时间段以及在时间短内每间隔多长时间保存一条数据来解决历史数据问题。
配置文件 /opt/graphite/conf/storage-schemas.conf 中描述了上述信息:
[default] pattern = .* retentions = 1m:30d,1h:1y
这个配置中,30天内的数据每间隔1分钟保存一条数据,30天-1年之间的数据,每一个小时保存一条数据。
因为 whisper 是一个固定大小的数据库,因此当 storage-schemas.conf 设定之后,一个metrics所占的磁盘空间就已经肯定了。在系统运行的周期中只要根据 metrics 的增长进行适当扩容便可。
注意:storage-schemas.conf修改之后对于已经在磁盘上进行记录的Metrics不会生效,须要删除数据从新写入或者进行数据迁移才行。