Python性能监控Graphite

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

一、简介

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源

1
2
3
rpm  - ivh http: / / dl.fedoraproject.org / pub / epel / 6 / x86_64 / epel - release - 6 - 8.noarch .rpm
sed  - 's@^#@@'  / etc / yum.repos.d / epel.repo
sed  - 's@mirrorlist@#mirrorlist@'  / etc / yum.repos.d / epel.repo

2、安装适应版本的Django软件包,版本过高会出现bug

1
2
3
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

1
yum install graphite - web python - carbon python - whisper

4、安装MySQL数据库

1
2
3
4
5
yum install mysql mysql - server MySQL - python
service mysqld start
chkconfig mysqld on
mysqladmin  - uroot password  123456
mysql  - uroot  - p123456  - 'create database graphite;'

5、修改graphite配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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、同步数据库

1
2
3
4
5
6
7
8
mkdir  - / opt / graphite / storage
cd  / etc / graphite - web /
django - admin syncdb  - - settings = local_settings  - - pythonpath = .
yes
root
zhengys@allentuns.com
123456
123456

7、修改graphite数据目录

1
chown  - R apache.apache  / opt / graphite / storage

8、启动服务

1
2
3
4
/ 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、提供监控网卡流量的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[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):
     =  Popen(shlex.split(f),stdout = PIPE,stderr = PIPE)
     result  =  p.stdout.read()
     =  [i  for  in   result.split( '\n' )[ 3 :]  if  i]
     dic_traffic  =  {}
     for  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
         =  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  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、后台方式运行监控网卡流量脚本

1
[root@Allentuns ~] # python network_traffic.py &

四、安装Diamond

diamond :搜集器、用于搜集数据

diamond的github官方站点:https://github.com/python-diamond/Diamond/wiki

1、安装Diamond

1
2
3
4
5
6
7
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、配置

1
2
3
4
5
6
7
8
9
10
11
12
13
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服务

1
2
3
chmod  + / 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的方式

1
2
3
4
5
6
[root@Allentuns ~] # cd /usr/share/diamond/collectors
[root@Allentuns collectors] # mkdir httpcode && cd $_
[root@Allentuns httpcode] # ll
总用量  8
- rwxr - xr - 1  root root  1356  3 月   31  11 : 12  filerev.py
- rwxr - xr - 1  root root  3737  3 月   31  11 : 12  httpcode.py

3、运行搜集httpcode的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
首先删除原来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中的配置文件中自动启用此选项

1
2
3
4
5
# 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开源者的贡献!!!




     本文转自zys467754239 51CTO博客,原文链接:http://blog.51cto.com/467754239/1626736,如需转载请自行联系原作者




相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
监控 Ubuntu API
Python脚本监控Ubuntu系统进程内存的实现方式
通过这种方法,我们可以很容易地监控Ubuntu系统中进程的内存使用情况,对于性能分析和资源管理具有很大的帮助。这只是 `psutil`库功能的冰山一角,`psutil`还能够提供更多关于系统和进程的详细信息,强烈推荐进一步探索这个强大的库。
86 1
|
10月前
|
监控 Python Windows
使用python脚本来监控进程
使用python脚本来监控进程
100 0
|
监控 数据安全/隐私保护 Python
用 Python 写摸鱼监控进程,用这个!
继打游戏、看视频等摸鱼行为被监控后,现在打工人离职的倾向也会被监控。 有网友爆料称知乎正在低调裁员,视频相关部门几乎要裁掉一半。而在知乎裁员的讨论区,有网友表示企业安装了行为感知系统,该系统可以提前获知员工跳槽念头。
230 0
用 Python 写摸鱼监控进程,用这个!
|
监控 Python
|
监控 Python Windows
python os.startfile python实现双击运行程序 python监控windows程序 监控进程不在时重新启动
用python监控您的window服务 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://world77.blog.51cto.com/414605/782935     最近比较烦,研发给的pc服务版本在虚拟机上已经开始给客户使用了,服务老是莫名的死翘翘,客户不停的电话给我,搞的我心情很差,于是在一个下午,静下心来,用python写了个简单的监控进程的脚本,当发现进程消失的时候,立即调用服务,开启服务。
2101 0
|
11天前
|
机器学习/深度学习 存储 设计模式
Python 高级编程与实战:深入理解性能优化与调试技巧
本文深入探讨了Python的性能优化与调试技巧,涵盖profiling、caching、Cython等优化工具,以及pdb、logging、assert等调试方法。通过实战项目,如优化斐波那契数列计算和调试Web应用,帮助读者掌握这些技术,提升编程效率。附有进一步学习资源,助力读者深入学习。
|
11天前
|
机器学习/深度学习 数据可视化 TensorFlow
Python 高级编程与实战:深入理解数据科学与机器学习
本文深入探讨了Python在数据科学与机器学习中的应用,介绍了pandas、numpy、matplotlib等数据科学工具,以及scikit-learn、tensorflow、keras等机器学习库。通过实战项目,如数据可视化和鸢尾花数据集分类,帮助读者掌握这些技术。最后提供了进一步学习资源,助力提升Python编程技能。
|
11天前
|
设计模式 机器学习/深度学习 前端开发
Python 高级编程与实战:深入理解设计模式与软件架构
本文深入探讨了Python中的设计模式与软件架构,涵盖单例、工厂、观察者模式及MVC、微服务架构,并通过实战项目如插件系统和Web应用帮助读者掌握这些技术。文章提供了代码示例,便于理解和实践。最后推荐了进一步学习的资源,助力提升Python编程技能。
|
13天前
|
数据采集 搜索推荐 C语言
Python 高级编程与实战:深入理解性能优化与调试技巧
本文深入探讨了Python的性能优化和调试技巧,涵盖使用内置函数、列表推导式、生成器、`cProfile`、`numpy`等优化手段,以及`print`、`assert`、`pdb`和`logging`等调试方法。通过实战项目如优化排序算法和日志记录的Web爬虫,帮助你编写高效稳定的Python程序。

热门文章

最新文章