• 隐藏侧边栏
  • 展开分类目录
  • 关注微信公众号
  • 我的GitHub
  • QQ:1753970025
Chen Jiehua

Graphite + Collectd + Statsd + Grafana安装配置 

这一周碰巧有机会接触到 Statsd,仔细研究了一下,发现涉及的东西还是挺多的,于是顺便把配置部署的过程记录一下备忘。

一开始是项目中需要统计一些数据,用golang开发,于是采用了g2s。但是首先总得在自己机器上面部署配置一下 Statsd的服务,google了一下发现后端依赖的服务还是挺多的,而且各种选择也是挺多的。一般的选择用:

  • grafana + influxdb
  • statsd + graphite
  • collectd + graphite
  • grafana + graphite

参考 introduction-to-tracking-statistics-on-servers 这里的介绍说明,我选择了 Graphite + Collectd + Statsd + Grafana 作为测试。

Graphite

graphite主要由三大部分组成:web界面 graphite-web,数据聚合存储 carbon-cache,数据库 whisper。对于whisper,数据默认存储在 var/lib/graphite/whisper 目录下

graphite-web

graphite-web是由Python Django开发的 wsgi 应用,它的数据不与 carbon、whisper共同存储,因为我们需要额外配置数据库,这里我们采用 postgres 作为数据库

修改 graphite 的配置

更新数据库

Graphite-web的基本部分就搞定了,接下来是 Carbon-cache。

carbon-cache

修改 carbon-cache 服务开机启动

修改配置文件

配置数据存储 schemas

对于 retentions 的说明:

The retention policy is defined by sets of numbers. Each set consists of a metric interval (how often a metric is recorded), followed by a colon and then the length of time to store those values. You can define multiple sets of numbers separated by commas.

配置数据 aggregation

启动服务

whisper

在使用过程中,如果whisper的数据已经生成了,然后才修改 storage-schemas 的retentions,则新的规则将不起作用,解决方法是:

  • 使用 whisper-resize 命令调整
  • 删除已有的whisper数据

nginx & uwsgi

graphite-web默认提供了 apache 的配置文件 (/usr/share/graphite-web/apache2-graphite.conf),配置方法可以参考这里。但是我们将以 nginx 和 uwsgi 来启动 graphite-web这个wsgi应用。配置文件参考gist

依赖的包:

Collectd

collectd主要用来收集服务器上面的各种指标数据如:CPU,内存,磁盘使用率等等,同时也可以收集一些软件如nginx的工作状态

修改配置

开启常用的插件

配置 df 插件

配置 write_graphite,这里配置如何将collectd的数据发送到graphite/carbon。carbon-cache默认绑定的端口是2003/2004,tcp连接

修改 carbon-cache 的配置,添加一个 collectd 的规则,必须在默认规则 [default_1min_for_1day] 之前,否则新规则将不起作用。

重启服务

Statsd

statsd是一个轻量级的数据收集程序(deamon),采用nodejs开发,对于其与graphite共同协作的工作原理:

StatsD flushes stats to Graphite in sync with Graphite’s configured write interval. To do this, it aggregates all of the data between flush intervals and creates single points for each statistic to send to Graphite.

In this way, StatsD lets applications work around the effective rate-limit for sending Graphite stats. It has many libraries written in different programming languages that make it trivial to build in stats tracking with your applications.

如果已经安装了collectd,其实也可以直接在collectd中添加statsd的插件,具体方法见:StatsD embedded into CollectD。不过这里我们为了多折腾一下,我们还是直接安装Statsd:

修改配置文件

修改 carbon-cache 的配置,添加一个 statsd 的规则

修改 carbon-cache 的 aggregation 方法

重新启动服务

The StatsD service connects to the Graphite service using a TCP connection. This allows for a reliable transfer of information.

However, StatsD itself listens for UDP packets. It collects all of the packets sent to it over a period of time (10 seconds by default). It then aggregates the packets it has received and flushes a single value for each metric to Carbon.

It is important to realize that the 10 second flush interval is exactly what we configured in our storage-schema as the shortest interval for storage. It is essential that these two configuration values match because it is what allows StatsD to get around the Carbon limitation of only accepting one value for each interval.

简单点说就是:第三方应用与Statsd采用UDP连接,而Statsd与Graphite则采用TCP连接。Statsd收集数据的时间间隔默认为10s,因此 carbon 里面的配置也必须是10s。

Note: It is important to realize that if you send Graphite data points more frequently than the shortest archive interval length, some of your data will be lost!

This is because Graphite only applies aggregation when going from detailed archives to generalized archives. When creating the detailed data point, it only writes the most recent data sent to it when the interval has passed. We will discuss StatsD in another guide, which can help alleviate this problem by caching and aggregating data that comes in at a more frequent interval.

Grafana

默认情况下的graphite-web界面实在是比较简陋,而且查看数据也比较麻烦,因为我们再安装一下Grafana,具体安装方法参考官方说明(官方文档写得很清晰明了)。

接下来就是 修改配置($ vim /etc/grafana/),启动服务($ sudo service grafana-server start),配置nginx,添加数据源、图表等。

码字很辛苦,转载请注明来自ChenJiehua《Graphite + Collectd + Statsd + Grafana安装配置》

评论