![牛逼了|從0到1 搭建Web性能監控系統](http://static.javashuo.com/static/loading.gif)
工具介紹前端
![牛逼了|從0到1 搭建Web性能監控系統](http://static.javashuo.com/static/loading.gif)
1. Statsd 是一個使用Node開發網絡守護進程,它的特色是經過UDP(性能好,及時掛了也不影響主服務)或者TCP來監聽各類數據信息,而後發送聚合數據到後端服務進行處理。常見支持的「Graphite」,「ElasticaSearch」,「InfluxDB」 等等 ,它集成了各類語言的客戶端API,這裏咱們使用了jsocol/pystatsd: A Python client for statsd進行數據收集。nginx
2. Graphite 是一套Python寫的開源編程接口,主要是用來收集服務器的及時狀態,在這裏主要做爲statsd的數據後端。分爲了三個子項目git
- carbon 守護進程,接收StatsD發送過來的原始統計數據。github
- whisper 用來存儲統計數據的時間序列數據庫。web
- graphite webapp 用來圖形化展現統計數據的web項目數據庫
3. Grafana 使用Go開發,能夠在界面上設計調整本身的統計圖表,支持多重報警,可定製化。apache
安裝編程
在這裏我使用了[synthesize(https://github.com/obfuscurity/synthesize)進行快速安裝Graphite和Statsd包,這裏面須要注意安裝數據在 **/opt/graphite ** 目錄下面。這裏面咱們安裝啓動了後端
service carbon-cache start # statsd數據處理後會進入中轉
service memcached start # 緩存
service collectd start # 收集服務負載可選
service apache2 start # 這能夠經過使用nginx替換
service statsite start # statsd的後端服務
這裏面須要單獨啓動graphite-web應用,端口啓動:0.0.0.0:8080,後面配置數據源須要使用。緩存
cd /opt/graphite/
sudo ./run-graphite-devel-server.py /opt/graphite/
Grafana數據包能夠經過手動下載安裝也能夠經過apt-get 安裝
- sudo apt-get install grafana
啓動以後訪問3000端口就能看到這樣一個頁面,默認的帳號密碼:admin,admin能夠經過配置github或者google登陸。
![牛逼了|從0到1 搭建Web性能監控系統](http://static.javashuo.com/static/loading.gif)
login
數據收集
Python端咱們使用statsd進行數據統一打點到到監控服務器收集
>>> import statsd
>>> c = statsd.StatsClient('localhost', 8125)
>>> c.incr('foo') # Increment the 'foo' counter.
>>> c.timing('stats.timed', 320) # Record a 320ms 'stats.timed'.
這裏面咱們經過Python把數據收集到statsite,經過Graphite Web服務來進行展現。
配置Graphite
進入Grafana後臺,進行配置點擊 「Data Sources」進行配置Graphite的數據(來自前端statsd收集)
![牛逼了|從0到1 搭建Web性能監控系統](http://static.javashuo.com/static/loading.gif)
data_source
接下來配置對應的數據展現:
![牛逼了|從0到1 搭建Web性能監控系統](http://static.javashuo.com/static/loading.gif)
add-graph
到此基本的數據配置就完了,恭喜你擁有了一個完整的監控系統。
爲了方便理解這裏我補充一下,數據從應用內,添加agent,把應用的時間性能數據經過statsd收集發送到服務端,經過carbon統計,whisper存儲最後展現在graphite,其中這裏面咱們用grafana來替代了graphite原生的UI展現,也就是咱們題圖的效果。