使用graphite和grafana进行应用程序监控
2019獨角獸企業重金招聘Python工程師標準>>>
graphite+grafana 介紹
grafana,按照官方的說法是 Beautiful metric & analytic dashboards。grafana 負責數據的展示,可以配置各種不同的數據源,其中包括 graphite。
graphite 包含多個模塊,這里我們使用的模塊包括:
- Whisper:固定大小的數據庫,存儲方式類似RRD (round-robin-database),用來存儲收集到的 metrics
- Carbon:metrics 接收服務,接收到 metrics 以后調用 Whisper 進行存儲
- graphite-api:WSGI webapp 接口服務,grafana 在需要展現數據的時候使用其提供的 REST API 進行數據的獲取
本文的搭建的監控系統結構如下:
在本文檔中,我們會盡量將相關文件安裝在/opt/graphite目錄
準備Python 2.7 環境
對于某些默認Python環境不是2.7的系統,需要安裝Python2.7。
從源碼編譯Python2.7
configure make make install創建Python2.7的virtualenv環境
virtualenv /opt/graphite --python=/usr/local/bin/python加載virtualenv環境
source /opt/graphite/bin/activate安裝Carbon+Whisper
pip install carbon --install-option="--prefix=/opt/graphite" --install-option="--install-lib=/opt/graphite/lib" pip install whisper使用默認的配置文件:
cp /opt/graphite/conf/storage-schemas.conf.example /opt/graphite/conf/storage-schemas.conf cp /opt/graphite/conf/carbon.conf.example /opt/graphite/conf/carbon.conf啟動 Carbon
/opt/graphite/bin/carbon-cache.py startcarbon的文件目錄在配置文件 /opt/graphite/conf/carbon.conf 中進行定義,下面是默認的配置:
LOCAL_DATA_DIR = /opt/graphite/storage/whisper/安裝graphite-api
yum install libffi-devel pip install graphite-api --install-option="--prefix=/opt/graphite"使用nginx+uwsgi的方式部署graphite-api
首先安裝uwsgi
pip install uwsgi創建graphite-api的配置文件:/opt/graphite/etc/graphite-api.yml
search_index: /opt/graphite/index finders:- graphite_api.finders.whisper.WhisperFinder functions:- graphite_api.functions.SeriesFunctions- graphite_api.functions.PieFunctions whisper:directories:- /opt/graphite/storage/whisper carbon:hosts:- 127.0.0.1:7002timeout: 1retry_delay: 15carbon_prefix: carbonreplication_factor: 1在這個配置文件中,whisper的數據路徑配置為/opt/graphite/storage/whisper,這個是在carbon配置文件 /opt/graphite/conf/carbon.conf 中使用配置項LOCAL_DATA_DIR進行定義的。
centos中沒有uwsgi的package,需要自行下載相關的啟動腳本,這里使用 https://github.com/jgoldschrafe/rpm-uwsgi
wget -O /etc/init.d/uwsgi https://raw.githubusercontent.com/jgoldschrafe/rpm-uwsgi/master/SOURCES/uwsgi.init chmod +x /etc/init.d/uwsgi編輯文件 /etc/init.d/uwsgi 進行目錄配置
uwsgi="/opt/graphite/bin/uwsgi" prog=$(basename "$uwsgi") UWSGI_CONF_DIR="/etc/uwsgi" UWSGI_LOG_DIR="/var/log/uwsgi" PIDFILE_DIR="/var/run/uwsgi"創建文件/etc/uwsgi/graphite-api.ini
[uwsgi] processes = 2 socket = 0.0.0.0:5000 module = graphite_api.app:app home = /opt/graphite env = GRAPHITE_API_CONFIG=/opt/graphite/conf/graphite-api.yml啟動uwsgi
service uwsgi start啟動以后會監聽5000端口,注意這里5000端口是uwsgi協議,需要后面配置nginx進行代理。
server {listen 81;location / {include uwsgi_params;uwsgi_pass localhost:5000;} }nginx啟動后可以訪問洗面的鏈接檢查數據返回是否正常
http://127.0.0.1:81/render?target=test.metric向監控系統中寫入數據
可以使用多種個方式向監控系統中寫入數據,例如 dropwizard metrics。這里為了方便使用Python進行數據上報:
import socket import time CARBON_SERVER = 'xxx.xxx.xxx.xxx' CARBON_PORT = 2003for k in xrange(100000):sock = socket.socket()sock.connect((CARBON_SERVER, CARBON_PORT))message = "test.meter.qps %d %d\n" % (k % 10, int(time.time()))print messagesock.sendall(message)sock.close()time.sleep(5)程序運行以后可以在carbon的數據目錄中會發現如下的文件結構:
test|-- metric.wsp|-- meter| |-- qps.wsp配置grafana展現metric
首先配置grafana使用graphite作為數據源
配置數據顯示:
歷史數據處理
對于監控系統,長期運行以后必然會積攢大量的歷史數據,whisper 通過配置數據保存的時間段以及在時間短內每間隔多長時間保存一條數據來解決歷史數據問題。
配置文件 /opt/graphite/conf/storage-schemas.conf 中描述了上述信息:
[default] pattern = .* retentions = 1m:30d,1h:1y這個配置中,30天內的數據每間隔1分鐘保存一條數據,30天-1年之間的數據,每個小時保存一條數據。
由于 whisper 是一個固定大小的數據庫,所以當 storage-schemas.conf 設定以后,一個metrics所占的磁盤空間就已經確定了。在系統運行的周期中只要根據 metrics 的增加進行適當擴容即可。
注意:storage-schemas.conf修改以后對于已經在磁盤上進行記錄的Metrics不會生效,需要刪除數據重新寫入或者進行數據遷移才行。
轉載于:https://my.oschina.net/u/575122/blog/791745
總結
以上是生活随笔為你收集整理的使用graphite和grafana进行应用程序监控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql通过binlog日志来恢复数据
- 下一篇: JWT实现token-based会话管理