Git Product home page Git Product logo

ilovemd.github.io's People

Watchers

 avatar  avatar

ilovemd.github.io's Issues

graphite简单入门

Table of Contents generated with DocToc

简介

  1. Graphite是一个企业级的监控工具

  2. Graphite 仅仅是一个画图工具,不主动收集数据,而是将收到的数据以图形方式展现出来

  3. Graphite的功能

    • 存储时间序列数据
    • 根据需要呈现数据的图形
  4. Graphite的组成

    • carbon :一个twisted守护进程,端口号是2003,监听时间序列数据
    • whisper 一个简单的数据库,用来存储时间序列数据
    • graphite webapp 使用django框架使用Cairo 来呈现数据
  5. Graphite原理:
    carbon接收到数据,然后将数据写到whisper数据库中,然后graphite webapp 去读取这些数据,然后显示图形。但是实际上这个体系采用了缓存,数据先可能到缓存中,然后webapp读取,呈现图形。

  6. Graphite是一个用于采集网站实时信息并进行统计分析的开源项目,可用于采集多种网站服务运行状态。

graphite 特点:

  • 支持Metrics的精度递减,比如一天之内10秒一条,7天之内聚合到1分钟一条,一年之内聚合到1小时一条
  • 存储数值型时序列数据
  • 根据请求对数据进行可视化(画图

架构图

graphite-arch

监控数据收集和展示流图

graphite-data-flow

理解graphite消息格式

graphite理解消息使用下列格式

metric_path value timestamp
  • metric_path 你想要操作的指标
  • value 当前给这个指标赋值
  • timestamp unix时间戳

举例

echo "test3.count 30 `date +%s`" | nc -q0 127.0.0.1 2003;

官方的例子

PORT=2003
SERVER=graphite.your.org
echo "local.random.diceroll 4 `date +%s`" | nc ${SERVER} ${PORT};
PORT=2003
SERVER=graphite.your.org
echo "local.random.diceroll 4 `date +%s`" | nc -c ${SERVER} ${PORT}

详细介绍了 -c 参数的作用
The -c parameter instructs nc to close socket once data is sent. Without this option, nc will keep the connection open and won’t end. 一旦数据已经发送,就关闭socket。 否则会保持连接状态

使用shell script以每分钟一次的频率产生当前进程总数,并发送给graphite

i=1;
while [ $i -le 60 ]
do
    echo "local.system.proc_numbers  `ps aux |sed 1d |wc -l` `date +%s`" |nc 127.0.0.1 2003 
    sleep 60 ;  
    i=$(($i+1))

done

用shell 实现如下的效果

[root@localhost  ~]#   /opt/graphite/examples/example-client.py
sending message
 
------------------------------------------
system.loadavg_1min 0.03 1397360637
system.loadavg_5min 0.02 1397360637
system.loadavg_15min 0.00 1397360637

实现方式;bq_load_graphite.sh

#!/bin/bash
while [ true ]
do

    timestamp=`date +%s`
    hostname=`hostname`
    msg=$(uptime | awk -F":" -v hn_var=$hostname '{
        split($NF,arr,","); 
        print "prod.web." hn_var ".loadavg_1min",arr[1];
        print "prod.web." hn_var ".loadavg_5min",arr[2]; 
        print "prod.web." hn_var ".loadavg_15min",arr[3] 
        }')

    echo  "$msg" | while read p
    do
    echo $p $timestamp  |nc  10.10.58.230 2003
    done
    sleep 1
done

发布指标项

指标项(metric) 是一种随着时间不断变化的可度量的数量,例如:

  • 每秒的请求量
  • 请求处理时间
  • CPU使用情况

数据点(datapoint) 是包含信息的3元组

  • 指标项的名称
  • 度量值
  • 时间序列上某个特点的点(通常是指的时间戳-timestamp)

指标项导航

echo "test3.count 30 `date +%s`" | nc -q0 127.0.0.1 2003;

-q0 表示一旦数据发送了,关闭socket

-c 表示一旦数据发送了,关闭socket

我们用nc命令将test3.count这个指标项发布到carbon-cache进程中

graphite的web应用程序以树状结构展示这些指标项,如果在左侧面板操作指标数,就可以看到全部的指标项。

graphite-start-1

点击任意一个指标项,右边的面板上将绘制出这个指标项的图表(默认情况下是过去24小时数据),可以通过composer面板中图表上方的按钮修改查询的时间范围。

graphite-start-2

批量填充数据

for i in `seq 100`;
do 
    v=$(($i*$i))
    echo "test2.count $v `date +%s`" | nc -q0 127.0.0.1 2003;
    sleep 2;
done

有的时候 不能显示图像 line mode -> draw null as zero 打勾

Carbon 和 Whisper

配置carbon

使用 「apt-get」安装时默认选型

  • carbon 配置文件的位置 /etc/carbon/carbon.conf
  • carbon 存储策略的文件位置 /etc/carbon/storage-schemas.conf
  • carbon-cache 的默认端口是 2003
  • 在cache区段下,接受端口这一行包含一个默认值,
    用于通过平文本协议(plaintext protocol),接收输入指标项
[cache]
LINE_RECEIVER_INTERFACE = 0.0.0.0
LINE_RECEIVER_PORT = 2003

配置whisper数据库

Whisper是一个固定大小的数据库,在设计上类似于RRD(round-robin-database)。它可以为随时间不断变化的数值型数据提供快速,可靠的存储。Whisper还可以把高精度的指标数据转换成低精度的指标数据以满足存储长期的历史数据的需求。比如说把按秒采集的指标转换成按分钟采集的指标,以减少数据量,进行长期存储。

graphite配置文件

配置文件位置: 「/etc/graphite/local_settings.py」

时区的修改

TIME_ZONE = 'Asia/Shanghai'

查看whisper存储路径

cat /etc/graphite/local_settings.py | grep whis

得到whisper文件的存储路径

STORAGE_DIR = '/var/lib/graphite/whisper'
WHISPER_DIR = '/var/lib/graphite/whisper'

采集和存储策略

每个whisper数据库可以包含一个或者多个针对不同数据的 采集和存储策略 的定义。这些定义保存在下面的路径中
storage-schemas.conf 文件的详细位置

「/etc/carbon/storage-schemas.conf」

策略的定义语法如下:

[name]
pattern = regex
retentions = timePerPoint:timeToStore, timePerPoint:timeToStore

语法结构:

  • name 策略名称,可随意指定
  • pattern 用来匹配具体指标名的正则表达式
  • retentions 定义了数据采集精度和存储时长。。timePerPoint就是多长时间采集一个数据点,timeToStore就是采集的数据最长存储多长时间

为了能准确地从高精度数据转换到低精度数据,两个相邻的timePerPoint:timeToStore对定义必须满足低精度定义能被高精度定义整除这个条件。比如上面的1m:21d就能被15s:7d整除,因为1分钟能被15秒整除,而21天可以被7天整除。相反,每180秒采集一次数据的定义就不能被每300秒采集一次数据的定义整除,因为300不能被180整除。

Whisper数据库的最长存储时间由最长的时长定义来决定。比如上面的例子,数据库的最长存储时长就是5年。

carbon 配置

[carbon]
pattern = ^carbon\.
retentions = 60s:1d, 1h:7d

表明以carbon.开头的指标,每1分钟保存一个点,保存1天。1天之前的数据1小时一个点(意味着60个1 分钟分辨率的点通过「aggregate」(聚合方法有 max,sum,avg 等)得到一个1小时的点)

默认配置

[default_1min_for_1day]
pattern = .*
retentions = 10s:1d

注意事项:

  1. 数据的分布定义在文件 「storage-schemas.conf」中,
    默认是按10秒一个数据点的方式,存一天的数据,一天前的数据就没了。
  1. 如果agent数据发送的间隔低于Graphite刷新的间隔,Graphite只会取最后一个值。比如
    「retentions = 10s:1d」,而客户端每5秒发来一条数据,前一条数据会被丢弃。

statsd建议配置

[stats]
pattern = ^stats.*
retentions = 10s:6h,1min:7d,10min:5y

注意事项:

将10s的数据降为1分钟时,默认是算平均值,但也可以采用合计值、最大值、最小值。

例子配置

[server]
pattern = ^server\..*
retentions = 60s:1d,5m:7d,15m:3y

[default]
pattern = ^test\..*
retentions = 60s:1d,5m:7d

上面的配置,会对于test. 开头的metrics,以60秒为精度存储一天,以5分钟为精度存储7天。即查询一天内的数据时,可以精确到1分钟,查询7天内的数据时,只能精确到5分钟。

由于60s是一个点,那么假设我要看3分钟前的数字,默认的图上,只有三个点,用红色标注。如图所示

graphite-3min1

graphite-3min2

数据写入流程

Metric数据来临时,将写入不同的whisper文件

如果数据来临的间隔低于Graphite刷新的间隔,Graphite只会取最后一个值。比如retentions = 10s:1d,而客户端每5秒发来一条数据,前一条数据会被丢弃。

数据压缩流程

修改storage-schema.conf 配置,添加下面内容

[stats]
pattern = ^api.*
retentions = 10s:1d,30s:7d,1m:28d,15m:5y

重启carbon-cache

/etc/init.d/carbon-cache restart

shell脚本模拟向carbon-cache填充数据

for i in {1..100}
do
    echo "api.num $i `date +%s`" | nc -q0 127.0.0.1 2003;
    sleep 10
done

30s聚合的数据

whisper-dump /var/lib/graphite/whisper/api/num.wsp | grep -A 10 'Archive 1 data'

「Archive 0 data」 是10s的数据 ,「Archive 1 data」是从前面10s压缩聚合后
有重新得到的数据。聚合的方式是「aggregation method: average」

结果如下

Archive 1 data:
0: 1475740890,          2
1: 1475740920,          5
2: 1475740950,          8
3: 1475740980,         11
4: 1475741010,         14
5: 1475741040,         17

分析

1
2
3
— - 平均值 2
4
5
6
- - 平均值 5
7
8
9
- - 平均值 8
10
11
12
- - 平均值 11

whisper相关命令和文件路径

whisper 是一个非常简单直观的时间序列存储引擎,其通过文件系统实现,每个指标是一个独立的文件。
如指标「test.server.cpu-load 」对应于文件系统的路径为「test/server/cpu-load.wsp」

/usr/bin/whisper-info 检索关于 Whisper 文件的元数据信息

/usr/bin/whisper-dump 以输出所有存储保留时期的原始数据以及关于 Whisper 文件的元数据信息:

读取whisper文件

通过Yum方式安装的话,whisper文件数据库的默认数据文件位置

/var/lib/graphite/whisper

dump出数据文件命令

/usr/bin/whisper-dump num.wsp 

carbon

查看正在运行的carbon

# ps aux | grep carbon

启动 carbon-cache

 /etc/init.d/carbon-cache start

关闭 carbon-cache

 /etc/init.d/carbon-cache stop

查看 creates.log 文件

/var/log/carbon/creates.log

显示内容

06/10/2016 11:04:02 :: new metric test20.count matched schema default_1min_for_1day
06/10/2016 11:04:02 :: new metric test20.count matched aggregation schema default
06/10/2016 11:04:02 :: creating database file /var/lib/graphite/whisper/test20/count.wsp (archive=[(10, 8640)] xff=None agg=None)

配置文件

/etc/carbon/storage-schemas.conf

默认配置

[stats]
pattern = ^stats.*
retentions = 10s:1d,30s:7d,1m:28d,15m:5y
/usr/bin/whisper-dump you.wsp  |grep Archive

显示

Archive 0 info:
Archive 1 info:
Archive 2 info:
Archive 3 info:
Archive 0 data:
Archive 1 data:
Archive 2 data:
Archive 3 data:

总过有4组

Archive 0 info:
  offset: 64
  seconds per point: 10
  points: 8640
  retention: 86400
  size: 103680

Archive 1 info:
  offset: 103744
  seconds per point: 30
  points: 20160
  retention: 604800
  size: 241920

Archive 2 info:
  offset: 345664
  seconds per point: 60
  points: 40320
  retention: 2419200
  size: 483840

Archive 3 info:
  offset: 829504
  seconds per point: 900
  points: 175200
  retention: 157680000
  size: 2102400

Archive 3为例:

点数 points = 365 * 24 * 3600 / seconds per point = 175200
保留时间 retention = 5 * 365 * 24 * 3600 = 157680000  
[collectd]
pattern = ^collectd.*
retentions = 60s:30d

解读:

collectd指标发送的数据每60秒一个点,保留30天

用tcpdump 抓取carbon数据

tcpdump -i eth0 -s 0 -l -w - dst port 2003 | strings

用tcpdump 抓取collectd发送的数据

 tcpdump -i eth0 -s 0 -l -w - dst port 25826 | strings

graphite管理

启动graphite

/etc/init.d/apache2 start

关闭graphite

/etc/init.d/apache2 stop

额外补充

https://www.hostedgraphite.com/docs/dashboards/graphmenu.html
//一键安装包
http://graphiteapp.org/

grahite菜单相关的总结

Graphite composer 是一个非常棒的工具,我们在这里列出比较常见的选项

  • Graph Options
    • Graph Title 设置图的标题
    • Display 控制字体,颜色 以及别的风格,以及网格线等
    • Line Mode 控制曲线如何连接指标的点
      • Slope Line 倾斜线 该图线通过相邻的数据点是一个倾斜的路径
      • Staircase Line 阶梯线 该图线通过相邻的数据点是一个阶梯状的
      • Connected Line 连接线 创建连续的线,填充数据
      • Draw Null As Zero 缺少指标数据的点用0来填充。也就是没有数据等于0
    • Area Mode 控制图路径的区域填充,相当于面积图
      • None - 不填充
        First Only - 图的第一个指标会填充
        Stacked - 所有的指标会填充,用不同颜色区别
    • X-Axis X轴
      • 时间格式
      • 时区
    • Y-Axis Y轴
      • Label 标签
      • Minimum 最小值
      • Maximum 最大值
    • Graph Data
      这个菜单能够让你在图中添加多个指标。也能够再图展现前在指标数据里转化数据
    • Auto Refresh 自动刷新
      • 启用的话,图自动刷新

自定义线的颜色

graph options -> color ->line colors 

输入几个以逗号分割的值比如 blue,green,black 或者 FFE957,F29F3F,F2753F 。

如何把线图转化成面积图

Area Mode -> stacked/All

如何给图表添加透明的效果

graph options -> Display -> Color -> Filled Area Alpha Value 

输入0 到 1 ,数字越小越透明

如何画零值

graph options ->Line Mode ->Draw Null as Zero

如何给图添加一个标题

graph options -> Graph Title

area mode 区别

stack 图
graphite-area-stack

none 图
graphite-area-none

first 图
graphite-area-mode-first

创建第一个issue

Table of Contents generated with DocToc

#graphite业务监控

安装graphite

Graphite 分为三个组件:

  • whisper 读音 /hwɪspɚ/
  • carbon 读音 /kɑ:rbən/
  • graphite-web 读音 /ɡræfˌaɪt/

创建一个隔离环境 graphite

virtualenv /opt/graphite
source /opt/graphite/bin/activate

安装图形libffi-devel

yum install libffi-devel

这些依赖的介绍

  • cairo是graphite的绘图库,是必须的软件。
  • bitmap-fonts字体也是需要的,否则可能图形显示不正常
  • python-twisted必须安装rpmforge源的python-twisted-core-8.2.0-1.el5.rf,这也是为什么添加rpmforge源的原因。
  • mod_wsgi是apache的模块

安装包依赖。

# 进入虚拟环境
# 已经进入虚拟环境
pip install https://github.com/graphite-project/ceres/tarball/master
pip install whisper
pip install carbon
pip install graphite-web
pip install django==1.5 --proxy http://127.0.0.1:8118
pip install django-tagging
pip install uwsgi

#之前要装yum install mysql-devel 
pip install MySQL-python
pip install daemonize
pip install cairocffi

注意:

  • Graphite的默认安装位置是 /opt/graphite/
  • graphite使用cairo进行绘图 因此需要安装 pip install cairocffi
  • cairocffi依赖于libffi,所以在创建虚拟环境之前 yum install libffi-devel

1. 设置Graphite配置文件

cd /opt/graphite/webapp/graphite
cp local_settings.py.example local_settings.py
cp /opt/graphite/conf/graphite.wsgi.example /opt/graphite/conf/graphite.wsgi
cp /opt/graphite/conf/graphTemplates.conf.example /opt/graphite/conf/graphTemplates.conf
cp /opt/graphite/conf/dashboard.conf.example /opt/graphite/conf/dashboard.conf

目录结构说明 /opt/graphite

.
|-- bin 数据收集相关工具
|   |-- build-index.sh
|   |-- carbon-cache.py 数据收集程序
|   `-- run-graphite-devel-server.py
|-- conf
|   |-- carbon.conf 数据收集carbon进程涉及的配置
|   |-- dashboard.conf 控制面板的UI设置
|   |-- graphTemplates.conf 图形化展示数据时使用的模板
|   |-- graphite.wsgi  wsgi进程的设置
|   |-- storage-schemas.conf Whisper files表的定义
|-- examples
|   |-- example-client.py
|   `-- example-graphite-vhost.conf
|-- lib
|   |-- carbon
|   `-- twisted
|-- storage 数据文件存储目录
|   |-- carbon-cache-a.pid
|   |-- graphite.db
|   |-- index
|   |-- lists
|   |-- log 
|   |-- rrd
|   `-- whisper
`-- webapp
    |-- content 前端展示页面
    |-- graphite 程序目录
    |   |-- local_settings.py graphite的配置文件

目录结构说明

  • 启动/carbon-cache.py收集程序 ./carbon-cache.py start
  • 配置数据库等信息 /opt/graphite/webapp/graphite/local_settings.py
  • 发送负载的样本程序 /opt/graphite/examples/example-client.py

2. 初始化数据库

 (graphite)[root@puppetmaster graphite]#python manage.py syncdb

3. 启动 uwsgi

uwsgi --http 0.0.0.0:8085 --master --processes 1 --home /usr/local/graphite --pythonpath /opt/graphite/webapp/graphite --wsgi-file=/opt/graphite/conf/graphite.wsgi --enable-threads --thunder-lock

###常见错误

error: can't copy 'distro/redhat/init.d/carbon-cache': doesn't exist or not a regular file

只能手动安装carbon

tar -zxvf carbon-0.9.13.tar.gz
cd carbon
python setup.py install
  • 注意: 使用python setup.py安装时,一定要在隔离环境中进行,
  • 隔离环境的标识 (graphite)[root@puppetmaster src]#
  • 运行carbon时,也应该在隔离环境中运行。

Graphite信息格式

所有graphite信息格式如下:

metric_path value timestamp\n

官方示例如下:

echo "local.random.diceroll 4 `date +%s`" | nc 127.0.0.1 2003;

比如使用shell script以每分钟一次的频率产生当前进程总数的数据,并将数据发送到本机的graphite,脚本运行120秒:

for i in `seq 60`
do   
echo "local.system.proc_numbers  `date +%s`" |nc 127.0.0.1 2003; 
sleep 2;
done

安装 statsd

官网地址: https://github.com/etsy/statsd

statsD是什么?

  • Graphite/Carbon之间的代理
  • nodejs进程
  • 接收udp数据
  • 在本地缓存所有的指标
  • 周期性的发送数据到Graphite/Carbon
  • 客户端能够使用任何语言
  • 可以发送你关心的指标(metric)

git 安装

yum install git

node安装

wget http://nodejs.org/dist/v0.12.2/node-v0.12.2.tar.gz
tar -zxvf node-v0.12.2.tar.gz
cd node-v0.12.2
./configure --prefix=/usr/local/node
make && make install

配置环境变量

vi ~/.bash_profile
#假如
PATH=$PATH:/usr/local/node/bin

使得环境变量生效 source ~/.bash_profile

下载statsd

cd /usr/local/
git clone git://github.com/etsy/statsd.git
cd statsd
cp exampleConfig.js config.js

查看config.js文件

{
  graphitePort: 2003
, graphiteHost: "127.0.0.1"
, port: 8125
, backends: [ "./backends/graphite" ]
}
  • 启动 node stats.js config.js
  • 测试 echo "anything.you.like:1|c" | nc -w 1 -u 127.0.0.1 8125
  • 测试 echo "foo:1|c" | nc -u -w0 127.0.0.1 8125

##statsd集群架构

###字体不清楚的问题

yum install bitmap bitmap-fonts-compat

##mysql-statsd监控插件安装

启动之前注意事项

  • 创建日志文件夹 mkdir /var/log/mysql_statsd/
  • 创建配置文件 /etc/mysql-statsd.conf

##使用sysbench测试数据,看监控

for i in `seq 20`
do
sysbench \
    --test=/usr/share/doc/sysbench/tests/db/oltp.lua \
    --db-driver=mysql  \
    --oltp-table-size=1000000  \
    --mysql-host=127.0.0.1 \
    --mysql-user=root \
    --max-time=60 \
    --max-requests=0 \
    --num-threads=$(($i**2)) \
    --oltp-test-mode=complex \
    --report-interval=1 \
    run
done
  • 可以使用指数方式: --num-threads=$((2**$i)) \
  • 如果是指数方式: for i in seq 8 循环少点
  • graphite时间不正确 :timezone 时区: Asia/Shanghai

翻译: StatsD/Graphite 监控实践

原文标题: Practical Guide to StatsD/Graphite Monitoring
原文地址: http://matt.aimonetti.net/posts/2013/06/26/practical-guide-to-graphite-monitoring/

https://kevinmccarthy.org/blog/2013/07/18/10-things-i-learned-deploying-graphite/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.