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