使用graphite和grafana进行应用程序监控

graphite+grafana 介绍

grafana,按照官方的说法是 Beautiful metric & analytic dashboards。grafana 负责数据的展示,可以配置各种不同的数据源,其中包括 graphite。

graphite 包含多个模块,这里我们使用的模块包括:

  • Whisper:固定大小的数据库,存储方式类似RRD (round-robin-database),用来存储收集到的 metrics
  • Carbon:metrics 接收服务,接收到 metrics 以后调用 Whisper 进行存储
  • graphite-api:WSGI webapp 接口服务,grafana 在需要展现数据的时候使用其提供的 REST API 进行数据的获取

本文的搭建的监控系统结构如下:

输入图片说明

在本文档中,我们会尽量将相关文件安装在/opt/graphite目录

准备Python 2.7 环境

对于某些默认Python环境不是2.7的系统,需要安装Python2.7。

从源码编译Python2.7

configure
make
make install

创建Python2.7的virtualenv环境

virtualenv /opt/graphite --python=/usr/local/bin/python

加载virtualenv环境

source /opt/graphite/bin/activate

安装Carbon+Whisper

pip install carbon --install-option="--prefix=/opt/graphite" --install-option="--install-lib=/opt/graphite/lib"
pip install whisper

使用默认的配置文件:

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

启动 Carbon

/opt/graphite/bin/carbon-cache.py start

carbon的文件目录在配置文件 /opt/graphite/conf/carbon.conf 中进行定义,下面是默认的配置:

LOCAL_DATA_DIR = /opt/graphite/storage/whisper/

安装graphite-api

yum install libffi-devel
pip install graphite-api --install-option="--prefix=/opt/graphite"

使用nginx+uwsgi的方式部署graphite-api

首先安装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展现metric

首先配置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不会生效,需要删除数据重新写入或者进行数据迁移才行。

转载于:https://my.oschina.net/u/575122/blog/791745

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值