Graphite 系列 #2:Carbon 和 Whisper

標籤(空格分隔): Graphite Carbon Whisper 監控前端


注:該系列文章絕對是學習 Graphite 的好文章,是我見過的將的最全面,最清晰,最簡單明瞭的系列文章。我會把該系列的全部文章所有翻譯完成,可是仍是推薦各位讀讀原文。python

在 Graphite 系列博客中,我將提供一個指南,以幫助使用 Graphite 技術棧完成搭建一個監控和報警系統的全部步驟。聲明:我不是一個專家,我僅僅是經過提供更詳細的文檔幫助 Graphite 社區發展。若是出現一些錯誤,請在文章下面評論或者是給我發送一封郵件到 feangulo@yaipan.com。git


Graphite 後端

Graphite 是由多個後端和前端組件組合而成。後端組件被用於存儲數值的時間序列數據。前端組件被用於檢索度量數據和可選的圖像渲染。在這篇博客文章中,我將重點介紹後端組件:Carbon 和 Whisper。github

此處輸入圖片的描述

度量指標能夠被髮布到一個負載均衡器或者是直接到一個 Carbon 進程。Carbon 進程與 Whisper 數據庫包交互來存儲時間序列數據到文件系統。web

安裝 Carbon

Carbon 是一個 daemon,其使用一個名爲 Twisted 的事件驅動網絡引擎來監聽時間序列數據。Twisted 框架支持 Carbon 進程以較低的開銷處理大量的客戶端和處理大量的流量。數據庫

爲了安裝 Carbon,運行如下命令(假設是 RHEL 操做系統):後端

# sudo yum groupinstall "Development Tools"
# sudo yum install python-devel
# sudo yum install git
# sudo easy_install pip
# sudo pip install twisted
# cd /tmp
# git clone https://github.com/graphite-project/carbon.git
# cd /tmp/carbon
# sudo python setup.py install

/opt/graphite 如今應該有 carbon 的 lib 包和配置文件:網絡

# ls -l /opt/graphite
total 16
drwxr-xr-x. 2 root root 4096 May 18 23:56 bin
drwxr-xr-x. 2 root root 4096 May 18 23:56 conf
drwxr-xr-x. 4 root root 4096 May 18 23:56 lib
drwxr-xr-x. 6 root root 4096 May 18 23:56 storage

安裝 Whisper

Whisper 是一個用於存儲時間序列數據的數據庫包,它被應用經過使用 create, update, 和 fetch 操做來檢索以及操做。app

爲了安裝 Whisper,運行如下命令:負載均衡

# cd /tmp
# git clone https://github.com/graphite-project/whisper.git
# cd /tmp/whisper
# sudo python setup.py install

Whisper 腳本如今應該在應有的位置上:

# ls -l /usr/bin/whisper*
-rwxr-xr-x. 1 root root 1711 May 19 00:00 /usr/bin/whisper-create.py
-rwxr-xr-x. 1 root root 2902 May 19 00:00 /usr/bin/whisper-dump.py
-rwxr-xr-x. 1 root root 1779 May 19 00:00 /usr/bin/whisper-fetch.py
-rwxr-xr-x. 1 root root 1121 May 19 00:00 /usr/bin/whisper-info.py
-rwxr-xr-x. 1 root root  674 May 19 00:00 /usr/bin/whisper-merge.py
-rwxr-xr-x. 1 root root 5982 May 19 00:00 /usr/bin/whisper-resize.py
-rwxr-xr-x. 1 root root 1060 May 19 00:00 /usr/bin/whisper-set-aggregation-method.py
-rwxr-xr-x. 1 root root  969 May 19 00:00 /usr/bin/whisper-update.py

啓動一個 Carbon 進程

Carbon 安裝自帶了默認的端口和不少其餘的配置文件。拷貝已經存在的示例文件。

# cd /opt/graphite/conf
# cp aggregation-rules.conf.example aggregation-rules.conf
# cp blacklist.conf.example blacklist.conf
# cp carbon.conf.example carbon.conf
# cp carbon.amqp.conf.example carbon.amqp.conf
# cp relay-rules.conf.example relay-rules.conf
# cp rewrite-rules.conf.example rewrite-rules.conf
# cp storage-schemas.conf.example storage-schemas.conf
# cp storage-aggregation.conf.example storage-aggregation.conf
# cp whitelist.conf.example whitelist.conf
# vi carbon.conf

在 cache 段下面,line receiver port 已經被指定:

[cache]
LINE_RECEIVER_INTERFACE = 0.0.0.0
LINE_RECEIVER_PORT = 2003

經過運行如下命令啓動一個 carbon-cache 進程:

# cd /opt/graphite/bin
# ./carbon-cache.py start
Starting carbon-cache (instance a)

該進程如今應該監聽在 2003 端口上:

# ps -efla | grep carbon-cache
1 S root      2674     1  0  80   0 - 75916 ep_pol 00:18 ?        00:00:03 /usr/bin/python ./carbon-cache.py start

# netstat -nap | grep 2003
tcp        0      0 0.0.0.0:2003                0.0.0.0:*                   LISTEN      2674/python

發佈度量指標

一個度量值是任何能夠隨時間變化的可測量的值:

  • 每秒的請求數
  • 進程請求時間
  • CPU 利用率

一個數據點是一個元組包含:

  • 一個度量值的名字
  • 一個可度量的值
  • 在一個指定時間點(一般是一個 timestamp)

客戶端應用經過發送數據點到一個 Carbon 進程來發布度量值。這個應用在 Carbon 進程的端口上創建一個 TCP 鏈接並以一個簡單的純文本格式發送數據點。在咱們的例子中,端口是 2003。TCP 鏈接或許依舊是打開並根據須要重複屢次使用。Carbon 進程監聽進入的數據可是不給客戶端發送任何響應。

數據點格式被定義成:

  • 每一個數據點是一個單行文本
  • 在位置 0 是一個帶點的度量名稱
  • 在位置 1 是一個值
  • 在位置 2 是一個 Unix Epoch 的 timestamp
  • 以空格做爲位置的分隔符

好比,這裏有一些有效的數據點:

  • carbon.agents.graphite-tutorial.metricsReceived 28198 1400509108
  • carbon.agents.graphite-tutorial.creates 8 1400509110
  • PRODUCTION.host.graphite-tutorial.responseTime.p95 0.10 1400509112

客戶端應用有多個方式來發布度量值:

  • 使用一個純文本協議工具好比 netcat (nc) 命令
  • 使用 pickle 協議
  • 使用高級消息隊列協議(AMQP)
  • 使用 lib 包好比 Coda Hale metrics library

爲了簡單起見,在這個教程中我將經過 netcat 命令使用純文本協議。爲了發佈以上列出的示例數據點,運行如下命令:

sudo yum install nc
echo "carbon.agents.graphite-tutorial.metricsReceived 28198 `date +%s`" | nc localhost 2003
echo "carbon.agents.graphite-tutorial.creates 8 `date +%s`" | nc localhost 2003
echo "PRODUCTION.host.graphite-tutorial.responseTime.p95 0.10 `date +%s`" | nc localhost 2003

carbon-cache 日誌文件將包含關於新的被接收到的度量值的信息,信息被存儲在:

# tail -f /opt/graphite/storage/log/carbon-cache/carbon-cache-a/creates.log
19/05/2014 10:42:44 :: creating database file /opt/graphite/storage/whisper/carbon/agents/graphite-tutorial/metricsReceived.wsp (archive=[(60, 129600)] xff=0.5 agg=average)
19/05/2014 10:42:53 :: creating database file /opt/graphite/storage/whisper/carbon/agents/graphite-tutorial/creates.wsp (archive=[(60, 129600)] xff=0.5 agg=average)
19/05/2014 10:42:57 :: creating database file /opt/graphite/storage/whisper/PRODUCTION/host/graphite-tutorial/responseTime/p95.wsp (archive=[(60, 1440)] xff=0.5 agg=average)

Carbon 與 Whisper 交互來存儲時間序列數據到文件系統。操做文件系統來確保數據文件已經被建立:

# ls -l /opt/graphite/storage/whisper/carbon/agents/graphite-tutorial/
total 3040
-rw-r--r--. 1 root root 1555228 May 19 10:42 creates.wsp
-rw-r--r--. 1 root root 1555228 May 19 10:42 metricsReceived.wsp
# ls -l /opt/graphite/storage/whisper/PRODUCTION/host/graphite-tutorial/responseTime/
total 20
-rw-r--r--. 1 root root 17308 May 19 10:42 p95.wsp

最後,你能夠檢索關於 Whisper 文件的元數據信息,使用 whisper-info 腳本:

# whisper-info.py /opt/graphite/storage/whisper/PRODUCTION/host/graphite-tutorial/responseTime/p95.wsp 
maxRetention: 86400
xFilesFactor: 0.5
aggregationMethod: average
fileSize: 17308

Archive 0
retention: 86400
secondsPerPoint: 60
points: 1440
size: 17280
offset: 28

whisper-dump 腳本是一個更完整的腳本,其能夠輸出全部存儲保留時期的原始數據以及關於 Whisper 文件的元數據信息:

# whisper-dump.py /opt/graphite/storage/whisper/PRODUCTION/host/graphite-tutorial/responseTime/p95.wsp 
Meta data:
  aggregation method: average
  max retention: 86400
  xFilesFactor: 0.5

Archive 0 info:
  offset: 28
  seconds per point: 60
  points: 1440
  retention: 86400
  size: 17280

Archive 0 data:
0: 1400609220, 0.1000000000000000055511151231257827
1: 0,          0
2: 0,          0
3: 0,          0
4: 0,          0
5: 0,          0
...
1437: 0,          0
1438: 0,          0
1439: 0,          0

弄明白 Aggregation 方法,最大的保留期, xFilesFactor 和 Whisper 文件的全部其餘屬性是很是重要的。不要擔憂,就算你在這點上沒有學習到,我將會在接下來的博客文章會更詳細地講訴這些。

Graphite 系列

  1. Provision Hardware
  2. Carbon & Whisper
  3. Whisper Storage Schemas & Aggregations
  4. Graphite Webapp
  5. Stress Testing Carbon Caches
  6. Carbon Aggregators

本文的做者是 franklinangulo,本文的原文是 GRAPHITE SERIES #2: CARBON & WHISPER

相關文章
相關標籤/搜索