用graphite diamond做監控
無論是什么系統,只要上線,就需要運維,這時候很想看一些監控的圖表,graphite就很方便的實現了這個需求。 而graphite采用metrics的方式,又有很多其他的tool為他做支持,所監控的不僅僅是機器的一些東西,你可以監控你爬蟲的指標, log的INFO,ERROR頻次,nginx網站的訪問數量等等,基本是你需要監控什么,很容易的就可以做到。
文章
開局先貼兩個文章,值得一讀
恩怨
無論是什么系統,只要上線,就需要運維,這時候很想看一些監控的圖表,graphite就很方便的實現了這個需求。
而graphite采用metrics的方式,又有很多其他的tool為他做支持,所監控的不僅僅是機器的一些東西,你可以監控你爬蟲的指標,
log的INFO,ERROR頻次,nginx網站的訪問數量等等,基本是你需要監控什么,很容易的就可以做到。
我從2014年初就在自己的TODOList添加了要玩graphite, 陸續玩了3、4次都失敗了,原因都是安裝里面某些步驟失敗,
這兩天終于搞成功了,寫個博客記錄一下。
graphite-web 大部分的安裝方式比較簡單,都是用pip就可以安裝,但是裝完后有個坑,
文檔中說使用pip install graphite-web
,但是pip中的graphite-web太老了,
導致有個cairo,庫在ubuntu下打死也裝不上,在新的源碼中此bug已經修復。我已經提了issue 1004
因為用的graphite-index,直接拿了他的幾張圖來看最終效果
安裝
我用的是ubuntu, 寫在最上面, 并且我假設你了解基本的python語法,用過pip, virtualenv, 沒用過也沒問題。
文檔需國內或許不能訪問,因此貼出主要的安裝步驟.
最好安裝到python的virtualenv中,具體virtualenv的使用可以參考這里
首先,查看graphite-web的requirements.txt,發現需要裝一些系統的庫, sudo apt-get install libcairo2-dev
。
pip install https://github.com/graphite-project/ceres/tarball/master pip install whisper pip install carbon pip install graphite-web
這里我先貼下最終整個系統搭起來后的各個python庫版本, 其中logster是一個做日志監控的東西,先git clone
的本機,然后pip install -e logster
項目地址即可
Django==1.4.8 Twisted==11.1.0 argparse==1.2.1 astroid==1.2.1 cairocffi==0.6 ceres==0.10.0 cffi==0.8.6 configobj==5.0.6 diamond==3.5.0 django-tagging==0.3.3 ipython==2.3.0 logilab-common==0.62.1 -e git+https://github.com/etsy/logster.git@4606bfc6b000ec0fd57de639d08cea9629525304#egg=logster-master mock==1.0.1 psutil==2.1.3 pycparser==2.10 pylint==1.3.1 pylint-django==0.5.5 pylint-plugin-utils==0.2.2 pyparsing==1.5.7 python-memcached==1.47 simplejson==2.1.6 six==1.8.0 txAMQP==0.4 whisper==0.9.12 wsgiref==0.1.2 zope.interface==4.1.1
graphite配置與啟動
根據文檔的步驟安裝完成后,你會發現/opt/graphite
下多了一堆東西,將/opt/graphite/conf
下的*.example,拷貝到去掉example即可
graphite有個服務在2003,2004接口上,你的metrics需要扔到2003上,具體請看文檔,現在不用在意這些細節。
metrics就是類似這樣的字符串 前綴.前綴.前綴……. blabala, graphite就是根據這種東西畫圖的,具體請看文檔,不用在意這些細節,
因為其他的工具都有封裝。
*. 啟動carbon, metrics會扔到carbon這個小屋里面
/opt/graphite/bin/carbon-cache.py start
*. 制造一些metrics, 更改host,或者server, 這里只是做測試,之后會用diamond來采集metrics
vim /etc/hosts 添加 127.0.0.1 graphite, 或者其他的東西python /opt/graphite/examples/example-client.py 這些數據存在 /opt/graphite/storage/whisper, 嘗試修改example-client.py發點不一樣的東西</pre>
*. 配置并修改graphite-web的幾行代碼,啟動這個django項目
cp /opt/graphite/webapp/graphite/local_settings.py{.example,} python /opt/graphite/webapp/graphite/manage.py syncdb vim /opt/graphite/webapp/graphite/render/glypy.py 找到import cairo, ....(這就是坑) 改為import ... try: import cairo except ImportError: import cairocffi as cairo啟動django python /opt/graphite/webapp/graphite/manage.py runserver 0.0.0.0:12222(或者其他端口)</pre>
- 瀏覽器打開http://127.0.0.1:12222, http://127.0.0.1:12222/dashboard這兩個頁面玩一下,你會看到左側tree那邊有一些數據
這些數據存在/opt/graphite/storage/whisper
</ol>
使用diamond收集metrics
給graphite填數據的方式太多了,這里使用diamond,因為豆瓣有一層graphite+diamond的皮, 下面會說
安裝
git clone https://github.com/BrightcoveOS/Diamond.git cd Diamond pip install -e ./配置并啟動
cp /etc/diamond/diamond.conf{.example,}vim /etc/diamond/diamond.conf 找到host, host = graphite(還記得之前配的host么) 查看下這個文件,你可以cd到collectors_path, handlers_path去看看里面的文件, 因為定制自己的 diamand collector時需要根據這些東西來寫(繼承Collector,重寫collect方法),此篇不談
service diamond restart</pre>
給graphite換層皮, graphite-index
graphite的界面實在是不敢恭維,因此很多人為它寫UI,這里選擇豆瓣的graphite-index
選擇它是因為配置簡單下載
git clone https://github.com/douban/graph-index.git cd graph-index配置
vim config.py graphite_url天上你graphite的ip已經端口 graphite_url = 'http://127.0.0.1:12222'更新metrics
./update-metrics.py crontab -e */5 * * * * python 絕對路徑到/update-metrics.py ./graph-index.py使用logster做日志監控
日志監控還是需要的,出了nginx的訪問日志之外,對于application的異常等等可能也需要監控,這時候使用logster,就非常方便了,因為他內置了像graphite發metrics的方法,so easy, 這里給了一個我fork的地址,因為我是一個pythoner,logster默認
的parser有apache等等,但是沒有python的,我寫了一個,提了一個patch.安裝:
git clone git@github.com:duoduo369/logster.git cd logster pip install -e ./用法:
logster --output=graphite --graphite-host=graphite的ip已經端口 你的parser 日志絕對路徑 logster --output=graphite --graphite-host=127.0.0.1:2003 PythonLogster /var/log/adx/adxsterr.log如果你需要自己定制parser,參照
logster/logster/parsers
下的東西寫一個就好。因為logster自帶向graphite發metrics,無須向diamond集成(寫Collector),只要起一個定時任務即可。
Finally
當然,如果你熟悉django,可以把graphite, graphite-index人給gunicorn和supervisor,這不是重點,需要的可以參考我github上的demo.
至于定制你的diamond Collector,監控你想監控的東西,請自己翻閱文檔 (繼承Collector,重寫collect方法),將寫好的Collector放在collectors_path下.
來自:http://my.oschina.net/duoduo3369/blog/338142