一 应用场景描述

  在上一篇文章中介绍了 使用Zabbix批量监控网站可用性方案一 Zabbix自带的Web监控只是利用libcurl库在Zabbix server或者proxy端来检测所有的url,这样实际上是不能检查到区域访问各个url的网络质量的。本文则使用Zabbix LLD,pycurl模块以及Zabbix sender来收集不同区域的Zabbix agent访问各个url的网络质量。


二 编写脚本

python多线程版本

#!/usr/bin/python
#this script is used to check multiple urls within different websites from a given file which contains all the urls of different websites
#use zabbix low level discovery(LLD) to discovery different websites and different urls,and then use pycurl to check those urls,finally send the result to zabbix proxy or zabbix server using zabbix_sender
# here has two zabbix macros: {#WEBSITE_NAME} and {#WEBSITE_URL}
# tested on zabbix 3.0
#
#written by john wang
#
#curl_easy_perform()
#    |
#    |--NAMELOOKUP
#    |--|--CONNECT
#    |--|--|--APPCONNECT
#    |--|--|--|--PRETRANSFER
#    |--|--|--|--|--STARTTRANSFER
#    |--|--|--|--|--|--TOTAL
#    |--|--|--|--|--|--REDIRECT




import json
import logging
import os,sys,time
import threading
import Queue
import subprocess

try:
   from cStringIO import StringIO
except ImportError:
   from StringIO import StringIO
import pycurl

# We should ignore SIGPIPE when using pycurl.NOSIGNAL - see
# the libcurl tutorial for more info.
try:
   import signal
   from signal import SIGPIPE,SIG_ING
   signal.signal(signal.SIGPIPE,signal.SIG_IGN)
except ImportError:
   pass


# need a given txt file contains urls
#eg. 
#baidu	www.baidu.com
#taobao www.taobao.com
try: 
   if sys.argv[1]=="-":
      urls=sys.stdin.readlines()
   else:
      urls=open(sys.argv[1],'rb').readlines()
   #print urls
except:
   print "Usage: %s check_urls.txt (list_websites)" %sys.argv[0]
   raise SystemExit
 

#logging.basicConfig(filename='/tmp/check_urls.log', level=logging.WARNING, format='%(asctime)s %(levelname)s: %(message)s')
zabbix_conf='/opt/app/zabbix/conf/zabbix_agentd.conf'



class Curl:
   def __init__(self,url):
       self.url=url
       self.body=StringIO()
       self.http_code=0
       
       self._curl=pycurl.Curl()
       self._curl.setopt(pycurl.URL,self.url)
       self._curl.setopt(pycurl.FOLLOWLOCATION,True)
       self._curl.setopt(pycurl.DNS_CACHE_TIMEOUT,0)
       self._curl.setopt(pycurl.DNS_USE_GLOBAL_CACHE,False)
       self._curl.setopt(pycurl.CONNECTTIMEOUT,30)
       self._curl.setopt(pycurl.TIMEOUT,60)
       self._curl.setopt(pycurl.FRESH_CONNECT,True)
       self._curl.setopt(pycurl.FORBID_REUSE,True)
       self._curl.setopt(pycurl.WRITEFUNCTION,self.body.write)
       self._curl.setopt(pycurl.NOSIGNAL,1)
       self._curl.debug=0
   def perform(self):
       try: 
         self._curl.perform()
       except Exception as e:
         #logging.warning(url+"\t" + str(e))
           return
   
   def close(self):
      try:
       self.http_code=self._curl.getinfo(pycurl.HTTP_CODE)
              self.total_time=self._curl.getinfo(pycurl.TOTAL_TIME)
              self.namelookup_time=self._curl.getinfo(pycurl.NAMELOOKUP_TIME)
              self.connect_time=self._curl.getinfo(pycurl.CONNECT_TIME)-self._curl.getinfo(pycurl.NAMELOOKUP_TIME)
              self.appconnect_time=max(0,(self._curl.getinfo(pycurl.APPCONNECT_TIME) - self._curl.getinfo(pycurl.CONNECT_TIME)))
              self.pretransfer_time=self._curl.getinfo(pycurl.PRETRANSFER_TIME) - max(self._curl.getinfo(pycurl.APPCONNECT_TIME),self._curl.getinfo(pycurl.CONNECT_TIME))
              self.starttransfer_time=self._curl.getinfo(pycurl.STARTTRANSFER_TIME) - self._curl.getinfo(pycurl.PRETRANSFER_TIME)
              self.redirect_time=max(0,self._curl.getinfo(pycurl.REDIRECT_TIME) - self._curl.getinfo(pycurl.TOTAL_TIME))
              self.speed_download=self._curl.getinfo(pycurl.SPEED_DOWNLOAD)
      except Exception as e:
        #logging.warning(url+"\t"+str(e))
        self.http_code=0 
        self.total_time=0
        self.namelookup_time=0
        self.connect_time=0
        self.appconnect_time=0
        self.pretransfer_time=0
        self.starttransfer_time=0
        self.redirect_time=0
        self.speed_download=0        
      self._curl.close()

queue=Queue.Queue()
websites=[]
tmpfile='/tmp/check_url_items.txt'
for line in urls:
    line=line.strip()
    if not line or line[0] == "#":
       continue
    name,url=line.split()
    element={'{#WEBSITE_NAME}':name,
             '{#WEBSITE_URL}':url
            }
    websites.append(element)
    #logging.debug('Discovered website ' + name + '\t' + url)
    queue.put((name,url))
    
assert queue.queue, "no urls are given"
num_urls=len(queue.queue)
#num_conn=min(num_conn,num_urls)
num_conn=num_urls
#assert 1 <= num_conn < = 1000,"invalid number of concurrent connections"

class WorkerThread(threading.Thread):
     def __init__(self,queue):
         threading.Thread.__init__(self)
         self.queue=queue
  
     def run(self):
         while 1:
             try:
                name,url=self.queue.get_nowait()
             except Queue.Empty:
                raise SystemExit
             c=Curl(url)
             c.perform()
             c.close()

             for item in [ 'http_code','total_time','namelookup_time','connect_time','appconnect_time','pretransfer_time','starttransfer_time','redirect_time','speed_download' ]:
                key='website[{0},{1},{2}]'.format(name,url,item)
                if item=='http_code':
                   value=c.http_code
                elif item=='total_time':
                   value=c.total_time
                elif item=='namelookup_time':
                   value=c.namelookup_time
                   #print key+":"+str(value)
                elif item=='connect_time':
                   value=c.connect_time
                elif item=='appconnect_time':
                   value=c.appconnect_time
                elif item=='pretransfer_time':
                   value=c.pretransfer_time
                elif item=='starttransfer_time':
                   value=c.starttransfer_time
                elif item=='redirect_time':
                   value=c.redirect_time
                elif item=='speed_download':
                   value=c.speed_download

                f=open(tmpfile,'a')
                f.write("- %s %s\n" % (key, value))

def send_zabbix_data(tmpfile):
        '''Send the queue data to Zabbix.'''
        '''Get key value from temp file. '''
        args = '/opt/app/zabbix/sbin/zabbix_sender -c {0} -i {1} -vv'
        return_code = 0
        process = subprocess.Popen(args.format(zabbix_conf, tmpfile),
                                           shell=True, stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE)
        out, err = process.communicate()
        #print err
        #logging.debug("Finished sending data")
        return_code = process.wait()
        #logging.info("Found return code of " + str(return_code))
        #if return_code != 0:
        #    logging.warning(out)
        #    logging.warning(err)
        #else:
        #    logging.debug(err)
        #    logging.debug(out)
        #print return_code
        #print len(open(tmpfile).readlines())
        #for line in open(tmpfile).readlines():
        #    print line


def main():
   try:
        if  sys.argv[1] and   sys.argv[2]=="list_websites":
           print json.dumps({'data': websites},indent=4,separators=(',',':'))
        elif sys.argv[1] and  sys.argv[2]=="send_data":

           #delete tmpfile first
           os.unlink(tmpfile)
           
           #start a bunch of threads                
           threads=[]
           for dummy in range(num_conn):
               t=WorkerThread(queue)
               t.start()
               threads.append(t)
 
           #wait for all threads to finish
           for thread in threads:
               thread.join()    

           send_zabbix_data(tmpfile)
        else:
           print "Usage: %s check_urls.txt (list_websites|send_data)" %sys.argv[0] 
           raise SystemExit
   except:
        raise SystemExit

if __name__=='__main__':
   main()
/usr/bin/python /opt/app/zabbix/sbin/check_urls.py /opt/app/zabbix/sbin/check_urls.txt list_websites

url自动发现

/usr/bin/python /opt/app/zabbix/sbin/check_urls.py /opt/app/zabbix/sbin/check_urls.txt send_data

使用zabbix_sender发送数据到zabbix proxy或者server

需要注意zabbix host添加了模板才能收到数据,要不然zabbix_sender会发送失败

脚本中把所有的key,vale写入到一个临时文件,然后通过zabbix_sender -i 参数从这个文件中读取key,value批量发送




通过使用python的多线程模块threading结合pycurl处理很多个url时间确实会节省很多,但是pycurl也就是libcurl在多线程情况下的DNS解析有很大的问题,线程数目一多,有些url的DNS解析速度会很慢,甚至本来单独使用curl去访问只需几十毫秒,在多线程下访问这个url可能会达到30秒。这是个很大的问题。所以,为了更为准确地得到每个url的响应时间,只能不使用python多线程,而是采用for循环挨个去访问。


python for循环版本

#!/usr/bin/python
#this script is used to check multiple urls within different websites from a given file which contains all the urls of different websites
#use zabbix low level discovery(LLD) to discovery different websites and different urls,and then use pycurl to check those urls,finally send the result to zabbix proxy or zabbix server using zabbix_sender
# here has two zabbix macros: {#WEBSITE_NAME} and {#WEBSITE_URL}
# tested on zabbix 3.0
#
#written by john wang
#
#curl_easy_perform()
#    |
#    |--NAMELOOKUP
#    |--|--CONNECT
#    |--|--|--APPCONNECT
#    |--|--|--|--PRETRANSFER
#    |--|--|--|--|--STARTTRANSFER
#    |--|--|--|--|--|--TOTAL
#    |--|--|--|--|--|--REDIRECT




import json
import logging
import os,sys,time
import threading
import Queue
import subprocess

try:
   from cStringIO import StringIO
except ImportError:
   from StringIO import StringIO
import pycurl

# We should ignore SIGPIPE when using pycurl.NOSIGNAL - see
# the libcurl tutorial for more info.
try:
   import signal
   from signal import SIGPIPE,SIG_ING
   signal.signal(signal.SIGPIPE,signal.SIG_IGN)
except ImportError:
   pass


# need a given txt file contains urls
#eg. 
#baidu	www.baidu.com
#taobao www.taobao.com
try: 
   if sys.argv[1]=="-":
      urls=sys.stdin.readlines()
   else:
      urls=open(sys.argv[1],'rb').readlines()
   #print urls
except:
   print "Usage: %s check_urls.txt (list_websites)" %sys.argv[0]
   raise SystemExit
 

#logging.basicConfig(filename='/tmp/check_urls.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s: %(message)s')
zabbix_conf='/opt/app/zabbix/conf/zabbix_agentd.conf'



class Curl:
   def __init__(self,url):
       self.url=url
       self.body=StringIO()
       self.http_code=0
       
       self._curl=pycurl.Curl()
       self._curl.setopt(pycurl.URL,self.url)
       self._curl.setopt(pycurl.FOLLOWLOCATION,True)
       self._curl.setopt(pycurl.DNS_CACHE_TIMEOUT,0)
       self._curl.setopt(pycurl.DNS_USE_GLOBAL_CACHE,False)
       self._curl.setopt(pycurl.CONNECTTIMEOUT,10)
       self._curl.setopt(pycurl.TIMEOUT,30)
       self._curl.setopt(pycurl.FRESH_CONNECT,True)
       self._curl.setopt(pycurl.FORBID_REUSE,True)
       self._curl.setopt(pycurl.WRITEFUNCTION,self.body.write)
       self._curl.setopt(pycurl.NOSIGNAL,1)
       self._curl.debug=0
   def perform(self):
       try: 
         self._curl.perform()
       except Exception as e:
         #logging.warning(url+"\t" + str(e))
           return
   
   def close(self):
      try:
       self.http_code=self._curl.getinfo(pycurl.HTTP_CODE)
              self.total_time=self._curl.getinfo(pycurl.TOTAL_TIME)
              self.namelookup_time=self._curl.getinfo(pycurl.NAMELOOKUP_TIME)
              self.connect_time=self._curl.getinfo(pycurl.CONNECT_TIME)-self._curl.getinfo(pycurl.NAMELOOKUP_TIME)
              self.appconnect_time=max(0,(self._curl.getinfo(pycurl.APPCONNECT_TIME) - self._curl.getinfo(pycurl.CONNECT_TIME)))
              self.pretransfer_time=self._curl.getinfo(pycurl.PRETRANSFER_TIME) - max(self._curl.getinfo(pycurl.APPCONNECT_TIME),self._curl.getinfo(pycurl.CONNECT_TIME))
              self.starttransfer_time=self._curl.getinfo(pycurl.STARTTRANSFER_TIME) - self._curl.getinfo(pycurl.PRETRANSFER_TIME)
              self.redirect_time=max(0,self._curl.getinfo(pycurl.REDIRECT_TIME) - self._curl.getinfo(pycurl.TOTAL_TIME))
              self.speed_download=self._curl.getinfo(pycurl.SPEED_DOWNLOAD)
      except Exception as e:
        #logging.warning(url+"\t"+str(e))
        self.http_code=0 
        self.total_time=0
        self.namelookup_time=0
        self.connect_time=0
        self.appconnect_time=0
        self.pretransfer_time=0
        self.starttransfer_time=0
        self.redirect_time=0
        self.speed_download=0        
      self._curl.close()

queue=Queue.Queue()
websites=[]
tmpfile='/tmp/check_url_items.txt'
for line in urls:
    line=line.strip()
    if not line or line[0] == "#":
       continue
    name,url=line.split()
    element={'{#WEBSITE_NAME}':name,
             '{#WEBSITE_URL}':url
            }
    websites.append(element)
    #logging.debug('Discovered website ' + name + '\t' + url)
    queue.put((name,url))
    
assert queue.queue, "no urls are given"
num_urls=len(queue.queue)
#num_conn=min(num_conn,num_urls)
#num_conn=num_urls
#assert 1 <= num_conn < = 1000,"invalid number of concurrent connections"

class Get_url_data:
     def __init__(self,queue):
         self.queue=queue
  
     def run(self):
             try:
                name,url=self.queue.get_nowait()
             except Queue.Empty:
                raise SystemExit
             c=Curl(url)
             c.perform()
             c.close()

             for item in [ 'http_code','total_time','namelookup_time','connect_time','appconnect_time','pretransfer_time','starttransfer_time','redirect_time','speed_download' ]:
                key='website[{0},{1},{2}]'.format(name,url,item)
                if item=='http_code':
                   value=c.http_code
                elif item=='total_time':
                   value=c.total_time
                elif item=='namelookup_time':
                   value=c.namelookup_time
                   #print key+":"+str(value)
                elif item=='connect_time':
                   value=c.connect_time
                elif item=='appconnect_time':
                   value=c.appconnect_time
                elif item=='pretransfer_time':
                   value=c.pretransfer_time
                elif item=='starttransfer_time':
                   value=c.starttransfer_time
                elif item=='redirect_time':
                   value=c.redirect_time
                elif item=='speed_download':
                   value=c.speed_download

                f=open(tmpfile,'a')
                f.write("- %s %s\n" % (key, value))

def send_zabbix_data(tmpfile):
        '''Send the queue data to Zabbix.'''
        '''Get key value from temp file. '''
        args = '/opt/app/zabbix/sbin/zabbix_sender -c {0} -i {1} -vv'
        return_code = 0
        process = subprocess.Popen(args.format(zabbix_conf, tmpfile),
                                           shell=True, stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE)
        out, err = process.communicate()
        #print out
        #logging.debug("Finished sending data")
        return_code = process.wait()
        #logging.info("Found return code of " + str(return_code))
        #if return_code != 0:
        #    logging.warning(out)
        #    logging.warning(err)
        #else:
        #    logging.debug(err)
        #    logging.debug(out)
        #print return_code
        #print len(open(tmpfile).readlines())
        #for line in open(tmpfile).readlines():
        #    print line


def main():
   try:
        if  sys.argv[1] and   sys.argv[2]=="list_websites":
           print json.dumps({'data': websites},indent=4,separators=(',',':'))
        elif sys.argv[1] and  sys.argv[2]=="send_data":
           if os.path.isfile(tmpfile):
              #delete tmpfile first
              os.unlink(tmpfile)
          
           #print num_urls 
           for dummy in range(num_urls):
               #print dummy
               t=Get_url_data(queue)
               t.run()
 
           #for line in urls:
           #    line=line.strip()
           #    if not line or line[0] == "#":
           #       continue
           #    name,url=line.split()   
           #    c=Curl(url)
           #    c.perform()
           #    c.close()
           #    print url+"\t"+str(c.http_code)+"\t"+str(c.namelookup_time)
 
           print "sending zabbix data"
           send_zabbix_data(tmpfile)
        else:
           print "Usage: %s check_urls.txt (list_websites|send_data)" %sys.argv[0] 
           raise SystemExit
   except:
        raise SystemExit

if __name__=='__main__':
   main()



添加定时任务

*/3 * * * * /usr/bin/python /opt/app/zabbix/sbin/check_urls.py /opt/app/zabbix/sbin/check_urls.txt send_data



添加zabbix配置文件 zabbix_agentd.conf.d/check_urls.conf

UserParameter=website.discovery,/usr/bin/python /opt/app/zabbix/sbin/check_urls.py /opt/app/zabbix/sbin/check_urls.txt list_websites
#UserParameter=website.data,/usr/bin/python /opt/app/zabbix/sbin/check_urls.py /opt/app/zabbix/sbin/check_urls.txt send_data
UserParameter=website[*],/usr/bin/python /opt/app/zabbix/sbin/check_urls.py /opt/app/zabbix/sbin/check_urls.txt send_data




三 制作Zabbix模板

模板参见附件






参考文档:

http://john88wang.blog.51cto.com/

http://john88wang.blog.51cto.com/2165294/1665253