设为首页 收藏本站
查看: 891|回复: 0

[经验分享] zabbix如何监控WEB应用性能

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-7-18 11:40:01 | 显示全部楼层 |阅读模式
HTTP服务目前最流行的互联网应用之一,如何监控服务的健康状态对系统运维来说至关重要。
  Zabbix本身提供了对WEB应用程序的监控,比如监控WEB程序的Download Speed,Response Time和Response Code等性能指标,但是配置起来比较繁琐和复杂。下面通过 python pycurl模块来获取HTTP响应时间,下载速度,状态吗等性能指标。然后通过zabbix trapper的方式来监控WEB应用的性能。
  Zabbix trapper监控是客户端收集监控数据,然后以zabbix_sender的方式发送给zabbix server或者proxy服务器。发送的数据主要包括zabbix server或者proxy主机名,监控项和值。zabbix_sender具体用法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[iyunv@monitor]# /usr/local/zabbix/bin/zabbix_sender -help
Zabbix Sender v2.2.3 (revision 44105) (7 April 2014)
usage: zabbix_sender [-Vhv] {[-zpsI] -ko | [-zpI] -T -i <file> -r} [-c <file>]
Options:
  -c --config <file>                   Absolute path to the configuration file
  -z --zabbix-server <server>          Hostname or IP address of Zabbix server
  -p --port <server port>              Specify port number of server trapper running on the server. Default is 10051
  -s --host <hostname>                 Specify host name. Host IP address and DNS name will not work
  -I --source-address <IP address>     Specify source IP address
  -k --key <key>                       Specify item key
  -o --value <key value>               Specify value
  -i --input-file <input file>         Load values from input file. Specify - for standard input
                                       Each line of file contains whitespace delimited: <hostname> <key> <value>
                                       Specify - in <hostname> to use hostname from configuration file or --host argument
  -T --with-timestamps                 Each line of file contains whitespace delimited: <hostname> <key> <timestamp> <value>
                                       This can be used with --input-file option
                                       Timestamp should be specified in Unix timestamp format
  -r --real-time                       Send metrics one by one as soon as they are received
                                       This can be used when reading from standard input
  -v --verbose                         Verbose mode, -vv for more details
Other options:
  -h --help                            Give this help
  -V --version                         Display version number



  下面是我用python写的监控脚本,如果要监控多个网站,只需在list列表里面添加即可。

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[iyunv@monitor cron]# cat Check_HTTP_Response_Time.py
#!/usr/bin/env python
#coding=utf-8
#Auth:david
import os
import sys
import fileinput
import pycurl
import logging
hostname = "monitor"
#IP from Zabbix Server or proxy where data should be send to.
zabbix_server = "192.168.100.200"
zabbix_sender = "/usr/local/zabbix/bin/zabbix_sender"
#If add url of website, please update list.
list = ['www.zmzblog.com','img.zmzblog.com']
#This list define zabbix key.
key = ['HTTP_ResSize','HTTP_ResTime','HTTP_ResCode','HTTP_ResSpeed']
#In the file to define the monitor host, key and value.
log_file = "/tmp/HTTP_Response.log"
logging.basicConfig(filename=log_file,level=logging.INFO,filemode='w')
run_cmd="%s -z %s -i %s > /tmp/HTTP_Response.temp" % (zabbix_sender,zabbix_server,log_file)
class Test():
        def __init__(self):
                self.contents = ''
        def body_callback(self,buf):
                self.contents = self.contents + buf
def Check_Http(URL):
        t = Test()
        #gzip_test = file("gzip_test.txt", 'w')
        c = pycurl.Curl()
        c.setopt(pycurl.WRITEFUNCTION,t.body_callback)
    #请求采用Gzip传输
        #c.setopt(pycurl.ENCODING, 'gzip')
    try:
        c.setopt(pycurl.CONNECTTIMEOUT, 60)
            c.setopt(pycurl.URL,URL)
                c.perform()
    except pycurl.error:
        print "URL %s" % URL

        Http_Document_size = c.getinfo(c.SIZE_DOWNLOAD)
        Http_Download_speed = round((c.getinfo(pycurl.SPEED_DOWNLOAD) /1024),2)
        Http_Total_time = round((c.getinfo(pycurl.TOTAL_TIME) * 1000),2)
        Http_Response_code = c.getinfo(pycurl.HTTP_CODE)

        logging.info(hostname +' ' +key[0] + '[' + k + ']' + ' '+str(Http_Document_size))
        logging.info(hostname +' ' +key[1] + '[' + k + ']' + ' '+str(Http_Total_time))
        logging.info(hostname +' ' +key[2] + '[' + k + ']' + ' '+str(Http_Response_code))
        logging.info(hostname +' ' +key[3] + '[' + k + ']' + ' '+str(Http_Download_speed))


def runCmd(command):
    for u in list:
            URL = u
        global k
        if u.startswith('https:'):
           k = u.split('/')[2]
        else:
                   k=u.split('/')[0]
            Check_Http(URL)
    for line in fileinput.input(log_file,inplace=1):
        print line.replace('INFO:root:',''),
    return os.system(command)
runCmd(run_cmd)



  添加crontab,定期收集数据并发送给zabbix server服务器。

1
*/5 * * * * /zabbix/python/cron/Check_HTTP_Response.py



  然后在前端配置监控项,可以调用zabbix API批量添加监控项。下面以www.zmzblog.com为例来说明如何监控HTTP的响应时间。这里所有的监控类型都是Zabbix_trapper的方式。监控key HTTP_ResTime[www.zmzblog.com],
HTTP_ResCode[www.zmzblog.com],HTTP_ResSize[www.zmzblog.com],HTTP_ResSpeed[www.zmzblog.com]分别表示HTTP的响应时间,状态吗,文档大小和下载速度。

QQ截图20160718113946.png
  配置完监控项之后我们配置触发器,因为现在网站的响应时间都是毫秒级别的,如果超过1000ms就报警。

  HTTP响应状态吗。

   总结:WEB应用性能监控主要从下面两个方面进行监控。
   1)HTTP的响应时间,随着互联网的发展,用户体验提升。网站的打开速度监控一定要快,至少要在毫秒级别。
   2)HTTP的状态吗,实时监控网站的响应吗是否正常,是否出现了404,500这样的错误,这种错误是用户无法忍受的,如果出现要第一时间解决。
   3)由于网络或者其它原因,为了减少误报,建议用下面的触发器,即检测2次如果状态吗不为200或者大于400的时候报警。
  {Template HTTP Response:HTTP_ResCode[www.zmzblog.com].count(#2,200,”ne”)}=2
  {Template HTTP Response:HTTP_ResCode[www.zmzblog.com].count(#2,400,”ge”)}=2




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-245864-1-1.html 上篇帖子: zabbix 3.0.3安装 下篇帖子: zabbix安装与配置使用 监控 如何
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表