Traffic monitoring scripts for Kafka

Source: Internet
Author: User
Tags mysql code shebang

KAFKA specifies the total amount of data received by topic per minute to monitor
Requirements: Get the total amount of data received by Kafka per minute, and save it in a timestamp-topicname-flow format in MySQL
Design ideas:
1. Get sum (logsize) at the current point of Kafka and deposit to the specified file file.
2. Execute the script again in a minute, get an instant sum (logsize), and read the number x,sum (logsize)-X in the file to Kafka specify topic flow (traffic) within one minute.
3. Save the acquired flow to MySQL and update the numbers in the file files.
4. Cycle the above process through the timer crontab.

Shell script:

#!/bin/bash#mysql-hostmysql_host=' 192.168.60.161 '#mysql-portmysql_port=' 3306 '#mysql-usernameUsername=' Root '#mysql-password#注意: Here if the MySQL password is not empty, you need to go below to switch the MySQL code, comment out the line below, let go above. password="'#mysql-dbnameDbname=' Kafka_monitor '#mysql-tablenameTablename=' Kafka_flow_monitor '#zookeeper-ipZookeeper=' 192.168.60.158:2181 '#topicIDTopic=' Nifi_test '#KAFKA_HOME (KAFKA installation directory)Kafka_home='/home/kafka '#filePath (Intermediate result store file path)file='/tmp/kafka/datacount.txt '#groupgroup=$ (grep' group.id= ' $KAFKA _home/config/consumer.properties | Cut- D ' = ' - F2)if[$# -eq 1]; ThenTopic= $fiout=$ (SH$KAFKA _home/bin/kafka-run-class.sh Kafka.tools.ConsumerOffsetChecker--zookeeper$zookeeper--group$group--topic$topic)if[ $?-ne 0]; ThenSh$KAFKA _home/bin/kafka-run-class.sh Kafka.tools.UpdateOffsetsInZK Earliest$KAFKA _home/config/consumer.properties$topic>>/dev/null out=$ (sh$KAFKA _home/bin/kafka-run-class.sh Kafka.tools.ConsumerOffsetChecker--zookeeper$zookeeper--group$group--topic$topic)fiI=0sum=0 forLineinch$(Echo "$out"); Do         Leti++if[$i -GT 7]; Thenb=$ (($i%7))if[$b -eq 5]; Thensum=$ (($sum+$line))fi        fi Donex=$ (cat$file) datacount=$[$sum-$x]if[$dataCount -lt 0]; ThenDatacount=$sumfidatetime=$ (date' +%y-%m-%d%h:%m:%s ') insert_sql="INSERT INTO ${tablename} values ('$dateTime', '$topic',$dataCount)"Echo $insert _sql

This script is then added to the Linux scheduled task
[[email protected] bin]# crontab -e
To edit a file:

* * ** *<topicName> >> /dev/null

Here is the topicname you want to monitor, the front 5 stars from left to right, respectively, representing minutes, hours, days, weeks, months. If you want to execute it in 5 minutes, you can write it as */5 * * * * , 10 minutes per hour, then write as * * *. Of course you can also output logs to a specified directory for later analysis:

* * ** *<topicName> >> /home/kafka/bin/getDataCount.log

Because only the need to analyze the total amount of data received per minute Kafka, so as to identify the Kafka or the client responsible for sending the data is not a problem, so here only to monitor the LogSize property, you can expand this, You can monitor the offset of the Kafka specified topic and the redundancy lag as the basis for the Kafka performance analysis.

Traffic monitoring scripts for Kafka

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.