Graphite 是一個Python寫的web應用,採用django架構,Graphite用來進行收集伺服器所有的即時狀態,使用者請求資訊,Memcached命中率,RabbitMQMessage Service器的狀態,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