使用graphite和cabot搭建监控服务
Contents
说起监控,我们一般会首先想到zabbix
,nagios
,ganglia
等等。但是对于非ops
开发人员而言,这些东西,多多少少让人感到陌生。所以本文将从一个服务端开发人员
的视角,介绍如何通过graphite
,cabot
,加一个shell
定时脚本,搭建监控报警服务。
python环境安装
虽然linux系统上一般都有python环境,但是默认的python版本较低。而且yum
等系统工具,都依赖于默认的python。所以推荐的做法是再安装一个python,并使用virtualenv
等工具,分项目进行环境管理,并与系统默认的python环境进行隔离。
以python2.7.3为例,介绍python环境的安装。
安装步骤
1 | sudo yum install bzip2-devel.x86_64 |
如果遇到 [FATAL] Failed to create text with cairo, this probably means cairo cant find any fonts. Install some system fonts and try again。可以尝试安装bitmap font。
1 | sudo yum install bitmap.x86_64 |
相关配置
- 创建管理python环境的用户
为了便于环境的统一管理,创建一个普通用户进行新创建python环境的管理和相关python扩展的安装。同时,向数字公司的addops
们致敬。
1 | useradd appops |
- 创建python环境安装目录
1 | sudo mkdir -p /data/server/python-envs |
- 配置新安装的python2.7.3环境
1 | sudo su appops -c 'vim ~/.bashrc' |
添加如下内容
1 | export WORKON_HOME=/data/server/python-envs |
搭建graphite监控服务
安装步骤
- 创建安装目录
1 | sudo mkdir /opt/graphite |
- 创建python虚拟环境
1 | sudo su appops |
- graphite安装
1 | pip install whisper |
graphite使用cairo
进行绘图,由于系统自带的cairo版本较低(需要cairo1.10以上),使用pip安装cairo会出错,所以采用编译安装。
1 | wget http://cairographics.org/releases/pycairo-1.8.8.tar.gz |
- 目录说明
1 | bin -- 数据收集相关工具 |
配置Graphite-web
- 初始化配置文件
1 | cd /opt/graphite/webapp/graphite |
修改或者增加如下配置:
1 | TIME_ZONE |
示例配置文件local_settings.py
- 初始化数据库
1 | python manage.py syncdb |
- 启动graphite-web
1 | uwsgi --http localhost:8085 --master --processes 1 --home /data/server/python-envs/graphite --pythonpath /opt/graphite/webapp/graphite --wsgi-file=/opt/graphite/conf/graphite.wsgi --enable-threads --thunder-lock |

配置数据收集服务
1 | cp /opt/graphite/conf/carbon.conf.example /opt/graphite/conf/carbon.conf |
编辑/opt/graphite/lib/carbon/util.py
,将
from twisted.scripts._twistd_unix import daemonize
替换成
import daemonize
否则启动cabon时会遇到ImportError: cannot import name daemonize
。
- 配置存储白名单
1 | vim /opt/graphite/conf/whitelist.conf |
添加
^test\..*
^server\..*
即只存储以test.
和server.
开头的metrics。
- 配置存储Schemas
1 | vim /opt/graphite/conf/storage-schemas.conf |
添加
1 | [server] |
上面的配置,会对于test.
开头的metrics,以60秒为精度存储一天,以5分钟为精度存储7天。即查询一天内的数据时,可以精确到1分钟,查询7天内的数据时,只能精确到5分钟。
- 启动cabon
1 | python /opt/graphite/bin/carbon-cache.py --config=/opt/graphite/conf/carbon.conf --debug start |
收集监控数据
etsy开源了一个叫statsd的daemon,可用于将监控数据收集到graphite,但那玩意是nodejs写的。
为了保持方案的简单,采用crontab
的方式,利用shell脚本将要收集的数据通过udp协议直接发送至graphite。
1 | #!/bin/sh |

监控数据收集和展示流图

搭建cabot报警服务
cabot
是一个轻量级的监控报警服务。其报警可以基于:
graphite收集的监控数据
url的响应内容和状态码
jenkins编译任务的状态
- 安装依赖
1 | sudo gem sources --remove http://rubygems.org/ |
因为foreman要求ruby版本需要在1.9.3以上,如果系统自带ruby版本过低,可以通过rvm安装ruby,再安装foreman。
1 | sudo yum install npm |
- 初始化目录
1 | sudo su appops |
- 安装cabot
1 | git clone https://github.com/arachnys/cabot.git |
修改setup.py,添加
'MySQL-python==1.2.5',
1 | python setup.py install |
- 配置cabot
使用foreman启动cabot时,会先读取.foreman
# vi: set ft=yaml :
procfile: Procfile.dev
env: conf/development.env
Procfile.dev
内容如下:
web: python manage.py runserver 0.0.0.0:$PORT
celery: celery -A cabot worker –loglevel=DEBUG -B -c 8 -Ofair
其中定义了启动cabot-web和celery任务队列时使用的命令,针对不同的环境,可以酌情修改.foreman
和对应的procfile
及env
。
对于邮件报警,需要修改conf/development.env中的如下内容:
1 | DATABASE_URL -- 数据库配置 |
- 启动cabot
1 | nohup foreman start 2>&1 > /dev/null & |



reference:
[^1] http://graphite.readthedocs.org/en/latest/overview.html
Author: GaoYuan
Link: https://blog.gaoyuan.xyz/2014/10/01/use-graphite-and-alter-build-monitor-system/
Use Soulens: Know Destiny, Chart Journey
License: 知识共享署名-非商业性使用 4.0 国际许可协议