Graphite 是一个Python写的web应用,采用django框架,Graphite用来进行收集服务器所有的即时状态,用户请求信息,Memcached命中率,RabbitMQ消息服务器的状态,Unix操作系统的负载状态,Graphite服务器大约每分钟需要有4800次更新操作,Graphite采用简单的文本协议和绘图功能可以方便地使用在任何操作系统上
环境介绍:
OS: CentOS6.5 x86_64 Minimal
1. 安装EPEL扩展库
# yum install -y epel-release
# sed -i s/#baseurl=/baseurl=/g /etc/yum.repos.d/epel.repo
# sed -i s/mirrorlist=/#mirrorlist=/g /etc/yum.repos.d/epel.repo
# yum clean all
2. 安装系统所需套件
yum install -y python python-devel python-pip
yum groupinstall -y 'Development Tools'
3. 安装配置Graphite相关软件(MySQL部分可以分开配置,使用独立的服务器)
# yum install -y graphite-web graphite-web-selinux mysql mysql-server MySQL-python
# mysql_secure_installation
Set root password? [Y/n] Y
New password: graphite
Re-enter new password: graphite
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
# mysql -uroot -pgraphite
mysql> CREATE DATABASE graphite;
mysql> GRANT ALL PRIVILEGES ON graphite.* TO 'graphite'@'localhost' IDENTIFIED BY 'graphite';
mysql> FLUSH PRIVILEGES;
mysql> exit;
# vi /etc/graphite-web/local_settings.py
DATABASES = {
'default': {
'NAME': 'graphite',
'ENGINE': 'django.db.backends.mysql',
'USER': 'graphite',
'PASSWORD': 'graphite',
}
}
# /usr/lib/python2.6/site-packages/graphite/manage.py syncdb
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'root'): root
E-mail address: guosuiyu@gmail.com
Password: graphite
Password (again): graphite
4. 启动Apache服务,作为Graphite的Web服务器
# /etc/init.d/httpd start
5. 安装底层的绘图与数据采集软件
# yum install -y python-carbon python-whisper
6. 启动数据采集进程
# /etc/init.d/carbon-cache start
7. 更新配置,保留所有metrics目录下数据90天(默认仅保留1天,也就是说看不到1天以前的数据绘图)
# vi /etc/carbon/storage-schemas.conf
[carbon]
priority = 101
pattern = ^carbon\.
retentions = 60:90d
[default_1min_for_90days]
priority = 100
pattern = .*
retentions = 60:90d
发送一些测试数据
# python /usr/share/doc/graphite-web-0.9.12/example-client.py
sending message
--------------------------------------------------------------------------------
system.loadavg_1min 0.01 1446086849
system.loadavg_5min 0.03 1446086849
system.loadavg_15min 0.05 1446086849
8. 查看当前服务器进程
# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2131/httpd
tcp 0 0 0.0.0.0:2003 0.0.0.0:* LISTEN 2210/python
tcp 0 0 0.0.0.0:2004 0.0.0.0:* LISTEN 2210/python
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1566/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 976/master
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 2210/python
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2063/mysqld
9. 生成24小时的模拟数据,体验Graphite的绘图功能
安装nc命令
# yum install -y nc
创建生成模拟数据的脚本
# vi feed_random_data.sh
#!/bin/bash
#
# Generate random pageview data and feed Graphite
tree_path=metrics.random.pageview
time_period_hours=24
now_timestamp=$(date +%s)
minutes_number=$((${time_period_hours}*60))
echo ${minutes_number}
timestamp=$((${now_timestamp}-${minutes_number}*60))
for i in $(seq 1 ${minutes_number}); do
echo "echo ${tree_path} $(($RANDOM%5000)) ${timestamp} | nc localhost 2003"
echo ${tree_path} $(($RANDOM%5000)) ${timestamp} | nc localhost 2003
let timestamp+=60
done
执行脚本,将数据喂给Graphite,在使用nc命令的时候固定格式为:
echo 目录结构 数值 时间戳 | nc 服务器地址 服务端口
例如:
echo metrics.random.pageview 3680 1446095415 | nc localhost 2003
# chmod +x feed_random_data.sh
# ./feed_random_data.sh
当然,也可以参考上面的example-client.py脚本,使用Python的方式来喂数据。
然后,打开Graphite Web,即可看到如下所示的绘图:
使用账号root/graphite登陆以后,还可以创建一个Dashboard,将多个绘图放在一起方便查看:
Graphite还支持通过API生成图片,方便我们获取,如下所示:
API URL:http://graphite.readthedocs.org/en/latest/render_api.html