標籤:graphite diamond apache
一、簡介
Graphite 是一個Python寫的web應用,採用django架構,Graphite用來進行收集伺服器所有的及時狀態,使用者請求資訊,Memcached命中率,RabbitMQMessage Service器的狀態,Unix作業系統的負載狀態,Graphite伺服器大約每分鐘需要有4800次更新操作,Graphite採用簡單的文本協議和繪圖功能可以方便地使用在任何作業系統上。
graphite有三個組件:
graphite官方文檔:
http://graphite.wikidot.com/documentation
http://graphite.readthedocs.org/en/latest/
二、安裝graphite
1、安裝epel源
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmsed -i ‘[email protected]^#@@‘ /etc/yum.repos.d/epel.reposed -i ‘[email protected]@#[email protected]‘ /etc/yum.repos.d/epel.repo
2、安裝適應版本的Django軟體包,版本過高會出現bug
yum install python-simplejsonwget https://kojipkgs.fedoraproject.org//packages/Django14/1.4.14/1.el6/noarch/Django14-1.4.14-1.el6.noarch.rpmrpm -ivh Django14-1.4.14-1.el6.noarch.rpm
3、安裝graphite
yum install graphite-web python-carbon python-whisper
4、安裝MySQL資料庫
yum install mysql mysql-server MySQL-pythonservice mysqld startchkconfig mysqld onmysqladmin -uroot password 123456mysql -uroot -p123456 -e ‘create database graphite;‘
5、修改graphite設定檔
# cat >> /etc/graphite-web/local_settings.py << EOFSECRET_KEY = ‘123qwe‘ALLOWED_HOSTS = [ ‘*‘ ]TIME_ZONE = ‘Asia/Shanghai‘DEBUG = TrueDATABASES = { ‘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
6、同步資料庫
mkdir -p /opt/graphite/storagecd /etc/graphite-web/django-admin syncdb --settings=local_settings --pythonpath=.yesroot[email protected]123456123456
7、修改graphite資料目錄
chown -R apache.apache /opt/graphite/storage
8、啟動服務
/etc/init.d/carbon-cache startchkconfig carbon-cache on/etc/init.d/httpd startchkconfig httpd on
三、訪問展示graphite
1、Chrome瀏覽器訪問Ghipte的地址:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5C/11/wKiom1UaGbvgMgkrAAIEjT1B2Zk181.jpg" title="1.png" width="650" height="322" border="0" hspace="0" vspace="0" style="width:650px;height:322px;" alt="wKiom1UaGbvgMgkrAAIEjT1B2Zk181.jpg" />
2、提供監控網卡流量的指令碼
[[email protected] ~]# cat network_traffic.py #!/usr/bin/env pythonfrom subprocess import Popen,PIPEimport socketimport shleximport timeimport sysimport osdef 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)
3、後台方式運行監控網卡流量指令碼
[[email protected] ~]# python network_traffic.py &
四、安裝Diamond
diamond :搜集器、用於搜集資料
diamond的github官方網站:https://github.com/python-diamond/Diamond/wiki
1、安裝Diamond
yum install gcc gcc-c++ python-configobj python-pip pip install diamond==3.4.421(有時候會安裝不成功)如果下載安裝不成功可以使用以下方式進行wget https://pypi.python.org/packages/source/d/diamond/diamond-3.4.421.tar.gz#md5=080ab9f52a154d81f16a4fd27d11093atar xf diamond-3.4.421.tar.gzcd diamond-3.4.421python setup.py install
2、配置
cd /etc/diamond/cp diamond.conf.example diamond.conf主要修改三個設定檔:[[email protected] diamond]# vim /etc/diamond/diamond.conf[[GraphiteHandler]] //59行host = localhost[[default]]//173行interval = 10//時間搜集一次[[email protected] diamond]# vim /etc/diamond/handlers/ArchiveHandler.conf#log_file = ./storage/archive.log//注釋此行[[email protected] diamond]# vim /etc/diamond/handlers/GraphiteHandler.conf host = localhost
3、啟動diamond服務
chmod +x /etc/init.d/diamond /etc/init.d/diamond startchkconfig diamond on
五、繼續訪問展示diamond自動採集資訊
1、Chrome瀏覽器訪問Ghipte的地址:
你會發現在Graphite下多了一個servers的目錄,這個目錄就是diamond自動採集的資訊
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5C/0B/wKioL1UaG8fRUruvAAPm1blb_Gk327.jpg" title="diamond.png" width="650" height="320" border="0" hspace="0" vspace="0" style="width:650px;height:320px;" alt="wKioL1UaG8fRUruvAAPm1blb_Gk327.jpg" />2、在這裡提供了兩個python指令碼,用來搜集web網站的httpcode,是基於diamond的方式
[[email protected] 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
3、運行搜集httpcode的指令碼
首先刪除原來diamond產生的servers目錄[[email protected] httpcode]# rm -rf /var/lib/carbon/whisper/servers/然後手動運行diamond的httpcode指令碼[[email protected] httpcode]# diamond -f -l -r ./httpcode.py -c /etc/diamond/diamond.confERROR: Pidfile exists. Server already running?#需要手動停止diamond服務[[email protected] httpcode]# /etc/init.d/diamond stopStopping diamond: [確定][[email protected] 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目錄;但是當時目錄就是沒有產生,我還真納悶了。原來在設定檔中沒有啟動此配置[[email protected] httpcode]# vim httpcode.py......config = super(HttpCodeCollector, self).get_default_config() config.update({ ‘path‘: ‘weblog‘, ‘enabled‘: ‘True‘#開啟此選項 })如果用diamond來搜集,則無需此選項,因為diamond有針對類的設定檔,在設定檔中開啟會比在指令碼中開啟看起來更統一
4、瀏覽器查看
Chrome重新整理Ghipte的web頁面,查看
Ghipte -> servers -> ec2-54-201-82-69 -> weblog(自訂) -> http 會出現以下監控曲線圖
我們可以使用ab -c 100 -n 100 http://localhost/ 產生200狀態代碼
使用重新整理Ghipte的瀏覽器頁面產生304的狀態代碼
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5C/0B/wKioL1UaHCijRVkoAAMpNTmsVaU913.jpg" title="手動httpcode.png" width="650" height="322" border="0" hspace="0" vspace="0" style="width:650px;height:322px;" alt="wKioL1UaHCijRVkoAAMpNTmsVaU913.jpg" />
本文出自 “鄭彥生” 部落格,請務必保留此出處http://467754239.blog.51cto.com/4878013/1626736
Python效能監控graphite