使用Zabbix批量監控網站可用性方案二

一 應用場景描述

  在上一篇文章中介紹了 使用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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章