Python性能監控Graphite
- 2020 年 1 月 10 日
- 筆記
一、簡介
Graphite 是一個Python寫的web應用,採用django框架,Graphite用來進行收集伺服器所有的及時狀態,用戶請求資訊,Memcached命中率,RabbitMQ消息伺服器的狀態,Unix作業系統的負載狀態,Graphite伺服器大約每分鐘需要有4800次更新操作,Graphite採用簡單的文本協議和繪圖功能可以方便地使用在任何作業系統上。
graphite有三個組件:
- graphite-web:web介面
- carbon:相當於network interface
- whisper:相當於rrdtool
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.rpm sed -i 's@^#@@' /etc/yum.repos.d/epel.repo sed -i 's@mirrorlist@#mirrorlist@' /etc/yum.repos.d/epel.repo
2、安裝適應版本的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
3、安裝graphite
yum install graphite-web python-carbon python-whisper
4、安裝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;'
5、修改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
6、同步資料庫
mkdir -p /opt/graphite/storage cd /etc/graphite-web/ django-admin syncdb --settings=local_settings --pythonpath=. yes root [email protected] 123456 123456
7、修改graphite數據目錄
chown -R apache.apache /opt/graphite/storage
8、啟動服務
/etc/init.d/carbon-cache start chkconfig carbon-cache on /etc/init.d/httpd start chkconfig httpd on
三、訪問展示graphite
1、Chrome瀏覽器訪問Ghipte的地址:

2、提供監控網卡流量的腳本
[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 %sn' % (k,v['in'],now) net_out = 'network.%s_out %s %sn' % (k,v['out'],now) s.sendall(net_in) s.sendall(net_out) time.sleep(5)
3、後台方式運行監控網卡流量腳本
[root@Allentuns ~]# 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 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
2、配置
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
3、啟動diamond服務
chmod +x /etc/init.d/diamond /etc/init.d/diamond start chkconfig diamond on
五、繼續訪問展示diamond自動採集資訊
1、Chrome瀏覽器訪問Ghipte的地址:
你會發現在Graphite下多了一個servers的目錄,這個目錄就是diamond自動採集的資訊

2、在這裡提供了兩個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
3、運行搜集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有針對類的配置文件,在配置文件中開啟會比在腳本中開啟看起來更統一
4、在腳本中關閉,在diamond中的配置文件中自動啟用此選項
# cd /etc/diamond/collectors/ # cp CPUCollector.conf HttpCodeCollector.conf # cat HttpCodeCollector.conf byte_unit = byte, enabled = true
5、瀏覽器查看
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開源者的貢獻!!!