1、簡介python
Graphite 是一個Python寫的web應用,採用django框架,Graphite用來進行收集服務器全部的及時狀態,用戶請求信息,Memcached命中率,RabbitMQ消息服務器的狀態,Unix操做系統的負載狀態,Graphite服務器大約每分鐘須要有4800次更新操做,Graphite採用簡單的文本協議和繪圖功能能夠方便地使用在任何操做系統上。mysql
graphite有三個組件:ios
graphite-web:web接口c++
carbon:至關於network interfacegit
whisper:至關於rrdtoolgithub
graphite官方文檔:web
http://graphite.wikidot.com/documentationsql
http://graphite.readthedocs.org/en/latest/數據庫
2、安裝graphiteapache
一、安裝epel源
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm sed -i 's@^#@@' /etc/yum.repos.d/epel.repo sed -i 's@mirrorlist@#mirrorlist@' /etc/yum.repos.d/epel.repo
二、安裝適應版本的Django軟件包,版本太高會出現bug
yum install python-simplejson wget https://kojipkgs.fedoraproject.org//packages/Django14/1.4.14/1.el6/noarch/Django14-1.4.14-1.el6.noarch.rpm rpm -ivh Django14-1.4.14-1.el6.noarch.rpm
三、安裝graphite
yum install graphite-web python-carbon python-whisper
四、安裝MySQL數據庫
yum install mysql mysql-server MySQL-python service mysqld start chkconfig mysqld on mysqladmin -uroot password 123456 mysql -uroot -p123456 -e 'create database graphite;'
五、修改graphite配置文件
# cat >> /etc/graphite-web/local_settings.py << EOF SECRET_KEY = '123qwe' ALLOWED_HOSTS = [ '*' ] TIME_ZONE = 'Asia/Shanghai' DEBUG = True DATABASES = { 'default': { 'NAME': 'graphite', 'ENGINE': 'django.db.backends.mysql', 'USER': 'root', 'PASSWORD': '123456', 'HOST': '127.0.0.1', 'PORT': '3306' } } from graphite.app_settings import * EOF
六、同步數據庫
mkdir -p /opt/graphite/storage cd /etc/graphite-web/ django-admin syncdb --settings=local_settings --pythonpath=. yes root zhengys@allentuns.com 123456 123456
七、修改graphite數據目錄
chown -R apache.apache /opt/graphite/storage
八、啓動服務
/etc/init.d/carbon-cache start chkconfig carbon-cache on /etc/init.d/httpd start chkconfig httpd on
3、訪問展現graphite
一、Chrome瀏覽器訪問Ghipte的地址:
二、提供監控網卡流量的腳本
[root@Allentuns ~]# cat network_traffic.py #!/usr/bin/env python from subprocess import Popen,PIPE import socket import shlex import time import sys import os def get_traffic(f): p = Popen(shlex.split(f),stdout=PIPE,stderr=PIPE) result = p.stdout.read() d = [i for i in result.split('\n')[3:] if i] dic_traffic = {} for i in d: devname = i.split(':')[0].strip() Receive = i.split(':')[1].split()[0].strip() Transmit = i.split(':')[1].split()[8].strip() dic_traffic[devname] = {'in':Receive,'out':Transmit} return dic_traffic if __name__ == '__main__': try: HOST = '127.0.0.1' PORT = 2003 s = socket.socket() s.connect((HOST,PORT)) except: print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % {'server':HOST,'port':PORT} sys.exit(1) while True: cur_traffic = get_traffic('cat /proc/net/dev') time.sleep(5) five_s_traffic = get_traffic('cat /proc/net/dev') diff_dic = {} for k in cur_traffic: traffic_in = int(five_s_traffic[k]['in']) - int(cur_traffic[k]['in']) traffic_out = int(five_s_traffic[k]['out']) - int(cur_traffic[k]['out']) diff_dic[k] = {'in':traffic_in,'out':traffic_out} now = int(time.time()) for k,v in diff_dic.items(): net_in = 'network.%s_in %s %s\n' % (k,v['in'],now) net_out = 'network.%s_out %s %s\n' % (k,v['out'],now) s.sendall(net_in) s.sendall(net_out) time.sleep(5)
三、後臺方式運行監控網卡流量腳本
[root@Allentuns ~]# python network_traffic.py &
4、安裝Diamond
diamond :蒐集器、用於蒐集數據
diamond的github官方站點:https://github.com/python-diamond/Diamond/wiki
一、安裝Diamond
yum install gcc gcc-c++ python-configobj python-pip python-devel pip install diamond==3.4.421 (有時候會安裝不成功) 若是下載安裝不成功可使用如下方式進行 wget https://pypi.python.org/packages/source/d/diamond/diamond-3.4.421.tar.gz#md5=080ab9f52a154d81f16a4fd27d11093a tar xf diamond-3.4.421.tar.gz cd diamond-3.4.421 python setup.py install
二、配置
cd /etc/diamond/ cp diamond.conf.example diamond.conf 主要修改三個配置文件: [root@Allentuns diamond]# vim /etc/diamond/diamond.conf `GraphiteHandler` //59行 host = localhost `default` //173行 interval = 10 //時間蒐集一次 [root@Allentuns diamond]# vim /etc/diamond/handlers/ArchiveHandler.conf #log_file = ./storage/archive.log //註釋此行 [root@Allentuns diamond]# vim /etc/diamond/handlers/GraphiteHandler.conf host = localhost
三、啓動diamond服務
chmod +x /etc/init.d/diamond /etc/init.d/diamond start chkconfig diamond on
5、繼續訪問展現diamond自動採集信息
一、Chrome瀏覽器訪問Ghipte的地址:
你會發如今Graphite下多了一個servers的目錄,這個目錄就是diamond自動採集的信息
二、在這裏提供了兩個python腳本,用來蒐集web站點的httpcode,是基於diamond的方式
[root@Allentuns ~]# cd /usr/share/diamond/collectors [root@Allentuns collectors]# mkdir httpcode && cd $_ [root@Allentuns httpcode]# ll 總用量 8 -rwxr-xr-x 1 root root 1356 3月 31 11:12 filerev.py -rwxr-xr-x 1 root root 3737 3月 31 11:12 httpcode.py
三、運行蒐集httpcode的腳本
首先刪除原來diamond生成的servers目錄 [root@Allentuns httpcode]# rm -rf /var/lib/carbon/whisper/servers/ 而後手動運行diamond的httpcode腳本 [root@Allentuns httpcode]# diamond -f -l -r ./httpcode.py -c /etc/diamond/diamond.conf ERROR: Pidfile exists. Server already running? #須要手動中止diamond服務 [root@Allentuns httpcode]# /etc/init.d/diamond stop Stopping diamond: [肯定] [root@Allentuns httpcode]# diamond -f -l -r ./httpcode.py -c /etc/diamond/diamond.conf [2015-03-31 11:13:56,198] [MainThread] Changed UID: 0 () GID: 0 (). [2015-03-31 11:13:56,198] [MainThread] Loaded Handler: diamond.handler.graphite.GraphiteHandler [2015-03-31 11:13:56,201] [MainThread] GraphiteHandler: Established connection to graphite server localhost:2003. [2015-03-31 11:13:56,202] [MainThread] Loaded Handler: diamond.handler.archive.ArchiveHandler [2015-03-31 11:13:56,206] [MainThread] Loading Collectors from: . [2015-03-31 11:13:56,209] [MainThread] Loaded Module: httpcode [2015-03-31 11:13:56,209] [MainThread] Loaded Collector: httpcode.HttpCodeCollector [2015-03-31 11:13:56,209] [MainThread] Initialized Collector: HttpCodeCollector [2015-03-31 11:13:56,210] [MainThread] Skipped loading disabled Collector: HttpCodeCollector [2015-03-31 11:13:56,210] [MainThread] Started task scheduler. [2015-03-31 11:13:57,211] [MainThread] Stopping task scheduler. [2015-03-31 11:14:01,217] [MainThread] Stopped task scheduler. [2015-03-31 11:14:01,217] [MainThread] Exiting. 若是沒有報錯,則查看瀏覽器會發現多了一個servers目錄;可是當時目錄就是沒有生成,我還真納悶了。原來在配置文件中沒有啓動此配置 [root@Allentuns httpcode]# vim httpcode.py ...... config = super(HttpCodeCollector, self).get_default_config() config.update({ 'path': 'weblog', 'enabled': 'True' #開啓此選項 }) 若是用diamond來蒐集,則無需此選項,由於diamond有針對類的配置文件,在配置文件中開啓會比在腳本中開啓看起來更統一
四、在腳本中關閉,在diamond中的配置文件中自動啓用此選項
# cd /etc/diamond/collectors/ # cp CPUCollector.conf HttpCodeCollector.conf # cat HttpCodeCollector.conf byte_unit = byte, enabled = true
五、瀏覽器查看
Chrome刷新Ghipte的web頁面,查看
Ghipte -> servers -> ec2-54-201-82-69 -> weblog(自定義) -> http 會出現如下監控曲線圖
咱們可使用ab -c 100 -n 100 http://localhost/ 產生200狀態碼
使用刷新Ghipte的瀏覽器頁面產生304的狀態碼
另外補充一個截圖
目前主流的開源監控有Cacti、Nagios、Zabbix等等,社區活躍,功能強大
Graphite雖然在功能上和社區在沒法與此對比,可是在靈活度上仍是值得一提的,輕量級的監控程序,更爲重要的是Graphite是Python編寫的,因此在問題排查,腳本編寫等都會很是順手
一樣也很是感謝更多Python開源者的貢獻!!!