1. 程式人生 > >Ubuntu 14.04 安裝圖形監控工具Graphite

Ubuntu 14.04 安裝圖形監控工具Graphite

什麼是graphite?

先看看百度百科是怎麼介紹

Graphite 是一個Python寫的web應用,採用django框架,Graphite用來進行收集伺服器所有的即時狀態,使用者請求資訊,Memcached命中率,RabbitMQ訊息伺服器的狀態,Unix作業系統的負載狀態,Graphite伺服器大約每分鐘需要有4800次更新操作,Graphite採用簡單的文字協議和繪圖功能可以方便地使用在任何作業系統上。

百度百科講的還算是比較清楚了,Graphite是用來監控系統的, 比如作業系統,快取服務系統等,但是監控的資料怎麼得到呢?Graphite並不負責,它只負責顯示,資料哪裡來人家不care,你只要按照他的資料格式給它,Graphite

就可以機智的用漂亮的頁面顯示給你,不過不用擔心,graphite的安裝的一系列套件,提供了API去傳資料給它,而且資料如何儲存的也不用我們擔心,只管發資料給它就行。說的這麼好,到底怎麼安裝呢?

先別急,事情總不是那麼完美,Graphite不支援windows,因此對於只使用WindowsCoder就有點小失落了,不過沒關係,相信作為程式設計師都是有辦法的,這些都是小事情。

下面就進入Graphite的世界!

安裝

作業系統:Ubuntu 14.04
Python :2.7.6

安裝graphite的環境

Graphite的需要的支援環境如下:
* a UNIX-like Operating System
* Python 2.6 or greater
* Pycairo
* Django 1.4 or greater
* django-tagging 0.3.1 or greater
* Twisted 8.0 or greater (10.0+ recommended)
* zope-interface (often included in Twisted package dependency)
* pytz
* fontconfig and at least one font package (a system package usually)
* A WSGI server and web server. Popular choices are:
* Apache with mod_wsgi
* gunicorn with nginx
* uWSGI with nginx

Ubuntu已經安裝了python,所以不需要安裝再安裝了,只用確保版本大於等於2.6即可。這裡我們伺服器選擇Apache,如果已經安裝了就不用安裝了,只用安裝WSGI的模組libapache2-mod-wsgi
下面是安裝所有支援環境的命令,建議一個一個安裝,可以檢視每個安裝成功與否。

$sudo apt-get update
$ sudo apt-get install apache2 libapache2-mod-wsgi python-django python-twisted python-cairo python-pip python-django-tagging

安裝Graphite
三大元件

  • whisper(資料庫)
  • carbon(監控資料,預設埠2003,外部程式StatsD通過這個埠,向Graphite輸送取樣的資料)
  • graphite-web(網頁UI)

使用pip命令可以快速的安裝

$sudo pip install whisper
$sudo pip install carbon
$sudo pip install graphite-web

安裝完成後預設在/opt/graphite目錄

然後使用Pip安裝pytz,用於轉換TIME_ZONE,後面會介紹

$ sudo pip install pytz

配置graphite

進入/opt/graphite/conf目錄,使用給的example配置

$ sudo cp carbon.conf.example carbon.conf 
$ sudo cp storage-schemas.conf.example storage-schemas.conf 
$ sudo cp graphite.wsgi.example graphite.wsgi  

apache新增Graphite的虛擬主機

安裝graphite的時候會生成一個/opt/graphite/example的資料夾,裡面有一個配置好的虛擬主機檔案,將其複製到Apache放置虛擬主機的配置檔案的地方,預設是/etc/apache2/sites-available資料夾

$sudo cp /opt/graphite/example/example-graphite-vhost.conf    /etc/apache2/sites-available/graphite-vhost.conf

然後在編輯修改監聽埠為8008以及一個WSGISocketPrefix的預設目錄,修改後如下:

/etc/apache2/sites-enable下建立該配置檔案的軟連結

$cd /etc/apache2/sites-enable
$sudo ln -s ../sites-available/graphite-vhost.conf   graphite-vhost.conf 

初始化資料庫

初始化 graphite需要的資料庫,修改 storage 的許可權,用拷貝的方式建立 local_settings.py檔案(中間會問你是不是要建立一個superuser,選擇no,把<使用者名稱>改成你當前的Ubuntu的使用者名稱,這是為了讓carbon有許可權寫入whisper資料庫,其實carbon裡面也可以指定使用者的,更新:graphite需要admin許可權的使用者才能建立User Graph,所以superuser是很重要的,可以使用 python manage.py createsuperuser建立):

$ cd /opt/graphite/webapp/graphite/

$ sudo python manage.py syncdb
$ sudo chown -R <使用者名稱>:<使用者名稱> /opt/graphite/storage/  
$ sudo cp local_settings.py.example local_settings.py

$ sudo /etc/init.d/apache2 restart  #重啟apache

上面程式碼中的使用者名稱為Apache對應的使用者,一般為www-data,可以使用下面的程式碼獲得,在apacheweb根目錄(預設:var/www/html)穿件control.php

<?php
    echo exec("whoami");
?>

在瀏覽器訪問http://localhost/control.php既可以看到對應的使用者名稱

啟動Carbon

$ cd /opt/graphite/

$ sudo ./bin/carbon-cache.py start

如果出現沒有許可權訪問的錯誤頁面,可以修改Apache配置檔案/etc/pache2/apache2.conf,找到下圖中的位置,註釋掉Require all denied ,然後重啟Apache再次訪問。

修改Graphite預設的時區

開啟/opt/graphite/webapp/graphite/setting.py,找到TIME_ZONE,預設是UTC,將其修改為Asia/Shanghai
,然後找到USE_TZ,沒有的話自己在檔案末尾新增,設定為True

傳送資料到graphite

傳送資料的方法比較多,科一參考官方文件Feeding In Your Data,此外,在/opt/graphite/examples下提供了一份通過Socket傳送資料的例子examples-client.py