Automatic statistics Kafka cluster logs

Source: Internet
Author: User

Writing a Python script statistic.py

#!/usr/bin/python "" "Pip install kazoo" "" "" "Pip install kafka-python" "" import  Timeimport threadingfrom kazoo.client import kazooclientfrom  kafka.consumer  import kafkaconsumerimport elasticsearchearlyres={}def _get_partitions_logsize (  topic,zookeepers,broker_list ):     zk=kazooclient ( hosts = zookeepers, read_only = true )     try:         Zk.start ()        # res={}    path =  "/ brokers/topics/"+topic+"/partitions "    if zk.exists ( path ):         partitions = zk.get_children ( path )          eachsave = {}        consumer  = kafkaconsumer (  topic,group_id= "Kafka_monitor", Metadata_broker_list=broker_list.split (",")  )          fetch = consumer._offsets.fetch         for partition in partitions:             logsize = fetch[  ( topic, int (partition )  )  ]             eachsave[ int ( partition )  ]  = logsize    else:        return { }    return eachsave    except exception as e:      #   print e    return {}     finally:    zk.stop () def analyze_logsize ( zookeepers,broker_ list )  :  &NBsp; zk = kazooclient (hosts=zookeepers,read_only=true)     try:         zk.start ()         path =   "/brokers/topics/"         sum=0         if zk.exists (PATH):             topics = zk.get_children ( path )              for topic in topics:                 add_dict = {}                 nowpartitions = _get_partitions_logsize ( Topic,zookeepers,broker_list)                  if nowpartitions !=  {}:                     for partition in nowpartitions:                          sum += nowpartitions [ partition ]             return sum             else:            pass     except Exception as e:         pass        print e    finally:         zk.stop () if __name__ ==  ' __main__ ':     A = analyze_logsize ( "127.0.0.1:2181/kafka/1001",  "127.0.0.1"  )     utc = time.localtime (  time.time ()  )     File = open  (  "/usr/home/shixi_kaiwen/ Script/develop/logsizecheck/2016/increment_day. " +time.strftime (  '%m-%d  ', UTC), "W"  )     File2 = open  (  " /usr/home/shixi_kaiwen/script/develop/logsizecheck/2016/lastlogsize "," r+ ")     last =  int  ( file2.read ()  )     increment = a - last     increment = str  ( increment )      File.write ( increment )     file.close ()     file2.close ()     file3 = open (  "/usr/home/shixi_kaiwen/script/develop/logsizecheck/2016/ Lastlogsize ", " W ")     File3.write  ( str ( a )  ) &Nbsp;   file3.close ()     print  "last = ",last     print  "now_logsize = ",a    print  "increment = " , increment

Create a lastlogsize file under its directory

echo "" >./lastlogsize

Add Crontab Task

Crontab-e

1 * * * python/usr/home/shixi_kaiwen/script/develop/logsizecheck/2016/statistic.py >> Python/usr/home/shixi_ kaiwen/script/develop/logsizecheck/2016/statistic.py >>/usr/home/shixi_kaiwen/script/develop/logsizecheck /2016/output.log 2>&1


This article is from the "humble" blog, please be sure to keep this source http://openex.blog.51cto.com/6280069/1731406

Automatic statistics Kafka cluster logs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.