广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python性能监控Graphite
  • 565
分享到

Python性能监控Graphite

性能PythonGraphite 2023-01-31 07:01:20 565人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

一、简介Graphite 是一个python写的WEB应用,采用Django框架,Graphite用来进行收集服务器所有的及时状态,用户请求信息,Memcached命中率,RabbitMQ消息服务器的状态,Unix操作系统的负载状态,Gra

一、简介

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
zhengys@allentuns.com
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的地址:

wKiom1UaGbvgMgkrAAIEjT1B2Zk181.jpg

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 %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、后台方式运行监控网卡流量脚本

[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自动采集的信息

wKioL1UaG8fRUruvAAPm1blb_Gk327.jpg

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的状态码

wKioL1UaHCijRVkoAAMpNTmsVaU913.jpg

另外补充一个截图

wKioL1Uecj3hzB26AAMHwHEaGbs679.jpg

wKioL1Uoj-fhVkN9AAREo3dHnig752.jpg


  • 目前主流的开源监控有Cacti、NagiOS、Zabbix等等,社区活跃,功能强大

  • Graphite虽然在功能上和社区在无法与此对比,但是在灵活度上还是值得一提的,轻量级的监控程序,更为重要的是Graphite是Python编写的,所以在问题排查,脚本编写等都会非常顺手

  • 同样也非常感谢更多Python开源者的贡献!!!

--结束END--

本文标题: Python性能监控Graphite

本文链接: https://www.lsjlt.com/news/191563.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python性能监控Graphite
    一、简介Graphite 是一个Python写的web应用,采用django框架,Graphite用来进行收集服务器所有的及时状态,用户请求信息,Memcached命中率,RabbitMQ消息服务器的状态,Unix操作系统的负载状态,Gra...
    99+
    2023-01-31
    性能 Python Graphite
  • graphite,python监控网卡流
    由于我发的东西在51CTO上都变成转载了,蛋疼。想跟我交流的朋友,去我的百度个人空间吧,51CTO不再更新了http://hi.baidu.com/wsdfw...
    99+
    2023-01-31
    网卡 graphite python
  • 系统性能监控-Windows自带性能监控器
    1.引言 在性能测试过程中,往往需要对系统应用服务器,数据库服务器等服务端资源使用情况进行监控,以获取服务器性能数据,协助判断服务器性能是否满足性能需求;对于不同的服务器类型,监控服务器的方式也不尽相...
    99+
    2023-09-01
    服务器 java 数据库
  • mongodb性能监控
    mongostat: mongostat -u admin --authenticationDatabase admin ...
    99+
    2022-10-18
  • mongodb 性能监控
    使用/var/soft/mongodb2.2/bin/mongostat --port 端口号可以实时监控·inserts/s 每秒插入次数 ·query/s 每秒查询次数 ·update/s 每秒更新次数...
    99+
    2022-10-18
  • Prometheus MySQL 性能监控
    一、 介绍 Prometheus 是一种开源的监控系统和时序数据库,旨在收集和处理大量数据并提供可视化、监控警报等功能。它支持多种语言、多种部署方式,并且非常灵活,而且社区支持非常活跃,为用户提供了很...
    99+
    2023-09-01
    mysql prometheus 数据库
  • zabbix监控mysql性能
    今天来看看zabbix如何监控mysql性能,这边使用mysql自带的模板,可以监控如下内容:OPS(增删改查)、mysql请求流量带宽,mysql响应流量带宽,最后会附上相应的监控图!编写check_my...
    99+
    2022-10-18
  • redis性能监控工具
    环境描述OS:centos 6.6_x64DB:redis 3.0.3192.168.1.91 -->数据库服务器192.168.1.92 -->监控程序主机2.监控工具redis-stat下载...
    99+
    2022-10-18
  • 如何监控mysql性能
    这篇文章主要介绍了如何监控mysql性能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。查看mysql服务器静态参数:show variabl...
    99+
    2022-10-18
  • PHP与Oracle性能监控
    随着现代软件开发越来越注重性能,PHP与Oracle也成为了开发中不可避免的组合。然而,在实际部署和使用中,我们常常面临性能问题,如:响应速度慢、资源占用过大、负载过高等等。针对这些问题,PHP与Oracle性能监控也变得十分重要。一、PH...
    99+
    2023-05-15
    PHP Oracle 性能监控
  • PHP与MongoDB性能监控
    随着互联网和移动互联网的飞速发展,Web应用越来越重要,而PHP语言因其易学易用的特点,成为了Web应用开发的一个主流语言。而在数据存储方面,MongoDB因其高性能和可扩展性被越来越多的开发者选择。然而,PHP与MongoDB的应用性能监...
    99+
    2023-05-17
    PHP MongoDB 性能监控
  • PHP与Memcached性能监控
    随着现代互联网应用的快速发展,用户体验对于一个应用的成功至关重要。如何保证应用的高性能和高可用性,成为了开发人员需要解决的重要问题之一。PHP作为一种广泛应用的编程语言之一,它的性能监控和优化也是非常重要的。Memcached是一个高性能、...
    99+
    2023-05-15
    PHP memcached 性能监控
  • PHP与Redis性能监控
    随着互联网技术的快速发展,网站访问量越来越大,对服务器的性能要求也越来越高。PHP这种脚本语言,由于其开发效率高、易学易用等优点,已经成为了互联网开发中的一种热门语言。而Redis这个高性能的数据缓存处理软件,也成为了众多网站和应用的首选。...
    99+
    2023-05-17
    PHP redis 性能监控
  • MongoDB实战(8)性能监控
    一、mongosniff此工具可以从底层监控到底有哪些命令发送给了 MongoDB 去执行,从中就可以进行分析:以 root 身份执行:./mongosniff --source NET lo然后其会监控位...
    99+
    2022-10-18
  • mongodb自带web性能监控
    mongodb自带一个web界面监控。配置比较简单,只需要在启动时候加参数:--httpinterface 或者将httpinterface=true写到配置文件中 重启。在浏览器中输入:ip:mongod...
    99+
    2022-10-18
  • Zabbix3.0.2监控Mongodb性能状态
    一.原理Zabbix监控mongodb的原理是通过echo "db.serverStatus()" | mongo admin 来查看mongodb的状态二.服务器配置1)添加mongo的全局变量# vim...
    99+
    2022-10-18
  • Redis性能监控的介绍
    本篇内容主要讲解“Redis性能监控的介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Redis性能监控的介绍”吧! redis_...
    99+
    2022-10-18
  • 如何监控Nodejs的性能
    这篇文章给大家分享的是有关如何监控Nodejs的性能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。监控Nodejs的性能,最近想监控一下Nodejs的性能。记录分析Log太麻烦,...
    99+
    2022-10-19
  • Redis性能监控的实现
    目录1.redis_exporter2.prometheus3.grafanaredis_exporter + prometheus +grafana监控Redis服务指标 本文使用...
    99+
    2022-11-12
  • ​性能监控工具集合
    性能监控工具集合性能监控工具集合(vmstat,dstat,htop,top,iotop,sar,iostat,iftop,netlog,glances,lm_sersors)一.vmstatvmstat 虚拟内存工具vmstat 刷新时间...
    99+
    2023-06-05
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作