博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用graphite和grafana进行应用程序监控
阅读量:6988 次
发布时间:2019-06-27

本文共 3545 字,大约阅读时间需要 11 分钟。

hot3.png

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

configuremakemake 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.confcp /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-develpip 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/indexfinders: - graphite_api.finders.whisper.WhisperFinderfunctions: - graphite_api.functions.SeriesFunctions - graphite_api.functions.PieFunctionswhisper: directories: - /opt/graphite/storage/whispercarbon: 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,需要自行下载相关的启动脚本,这里使用

wget -O /etc/init.d/uwsgi https://raw.githubusercontent.com/jgoldschrafe/rpm-uwsgi/master/SOURCES/uwsgi.initchmod +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 = 2socket = 0.0.0.0:5000module = graphite_api.app:apphome = /opt/graphiteenv = 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

向监控系统中写入数据

可以使用多种个方式向监控系统中写入数据,例如 。这里为了方便使用Python进行数据上报:

import socketimport timeCARBON_SERVER = 'xxx.xxx.xxx.xxx'CARBON_PORT = 2003for 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

你可能感兴趣的文章
测试Application.Idle
查看>>
sizeof与strlen的区别与联系
查看>>
我的友情链接
查看>>
数组名取地址以及数组名作为sizeof操作符的操作数
查看>>
Citrix发布支持Framehawk技术的HDX协议,用户体验优势进一步扩大
查看>>
Android各种访问权限Permission详解
查看>>
asp.net 页面生命周期
查看>>
什么是钩子函数?
查看>>
Linux命令(基本)
查看>>
实战Active Directory站点部署与管理,Active Directory系列之十二
查看>>
信息和知识
查看>>
7.2 函数的参数
查看>>
Flex + Servlet 实现断点上传
查看>>
Linux学习笔记之用户登录
查看>>
【Linux】第二章系统设置及基本操作
查看>>
docker-compose ,docker-stack
查看>>
Myeclipse10安装设置配置Aptana插件
查看>>
RHEL5.5安装中文支持
查看>>
web前端开发中浏览器兼容问题(五)
查看>>
H3C 交换机的基础配置
查看>>