mindong 发表于 2018-1-1 11:24:15

zabbix 监控 WEB 应用性能

#!/usr/bin/env python  
#
coding=utf-8  
import os
  
import sys
  
import fileinput
  
import pycurl
  
import logging
  
hostname = "\"Zabbix server\""
  
zabbix_server = "127.0.0.1"
  
zabbix_sender = "/usr/local/zabbix/bin/zabbix_sender"
  
list = ['www.ksgame.com','cdn.ksgame.com']
  
key = ['HTTP_ResSize','HTTP_ResTime','HTTP_ResCode','HTTP_ResSpeed']
  
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)
  
print run_cmd
  

  
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_Download_speed = round((c.getinfo(pycurl.SPEED_DOWNLOAD) ),2)
  Http_Total_time = round((c.getinfo(pycurl.TOTAL_TIME) * 1000),2)
  Http_Response_code = c.getinfo(pycurl.HTTP_CODE)
  logging.info(hostname +' ' +key + '[' + k + ']' + ' '+str(Http_Document_size))
  logging.info(hostname +' ' +key + '[' + k + ']' + ' '+str(Http_Total_time))
  logging.info(hostname +' ' +key + '[' + k + ']' + ' '+str(Http_Response_code))
  logging.info(hostname +' ' +key + '[' + k + ']' + ' '+str(Http_Download_speed))
  

  
def runCmd(command):
  for u in list:
  URL = u
  global k
  if u.startswith('https:'):
  k = u.split('/')
  else:
  k=u.split('/')
  Check_Http(URL)
  

  for line in fileinput.input(log_file,inplace=1):
  print line.replace('INFO:root:',''),
  return os.system(command)
  
runCmd(run_cmd)
页: [1]
查看完整版本: zabbix 监控 WEB 应用性能