Use shell and redis to collect and analyze logs in cluster services in real time

Source: Internet
Author: User

 

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/1T1113S9-0.png "/>

In the statistical project, the most difficult to implement is the collection of log data. Logs are distributed in various data centers across the country, and the data volume is large. Such methods as rsync + inotify obviously cannot meet the requirements of fast log synchronization. Of course, you can also use fluentd and flume to collect log data. In addition to this, you can also write a simple set.

 

The process of writing this log analysis system is:

 
 
  1. 1. Collect data on the client and send the data to the server through redis pub.
  2.  
  3. 2. The server is a redis sub, which stores data in a single file, or filters out the data currently.

 

Update Data of logs collected by the client

 
 
  1. #! /Bin/bash
  2.  
  3. DATE = 'date + % s'
  4. LOGFILE = $1
  5. If [! -F $1]; then
  6. Echo "LOG file did not give or it's not a file"
  7. Fi
  8. Sleep_time = "2"
  9.  
  10. Count_init = 'wc-l $ {LOGFILE} | awk '{print $1 }''
  11. While true
  12. Do
  13. DATE_NEW = 'date + % s'
  14. # DATE = $ (date + % s)
  15. Count_new = 'wc-l $ {LOGFILE} | awk '{print $1 }''
  16. Add_count = $ ($ {count_new}-$ {count_init }))
  17. Count_init =$ {count_new}
  18. If [! -N "$ {add_count}"]
  19. Then
  20. Add_count = 0
  21. Fi
  22. QPS = $ ($ {add_count}/$ {sleep_time }))
  23. Info = 'Tail-n $ {add_count }$ {LOGFILE }'
  24. Echo $ info
  25. # We can pass out the info value.
  26. Echo "Then QPS at 'date-d" 1970-01-01 UTC $ {DATE_NEW} seconds "+" % Y-% m-% d % H: % M: % S "'is" $ {QPS}
  27. # Echo "DATE_NEW:" $ DATE_NEW "DATE_PLUS:" $ DATE_PLUS
  28. Sleep $ sleep_time
  29. Done

 

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/1T111AH-1.png "/>

Print real-time logs

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/1T1116350-2.jpg "/>

 

To upload the command to the server, you only need to add the following command in the script to get it done ~

/Root/redis-bash-cli-h 10.10.10.61 PUBLISH rui "$ info"

 

Redis-bash-cli this is a client script that can pass the data publish

 
 
  1. #!/bin/bash   
  2. source /usr/share/redis-bash/redis-bash-lib 2> /dev/null  
  3. if [ $? -ne 0 ]; then  
  4.   LIBFOLDER=${0%/${0##*/}}  
  5.   source ${LIBFOLDER}/redis-bash-lib 2> /dev/null  
  6.   if [ $? -ne 0 ]; then  
  7.     echo "can't find redis-bash-lib in /usr/share/redis-bash or ${LIBFOLDER}"  
  8.     exit 127   
  9.   fi  
  10. fi  
  11. REDISHOST=localhost  
  12. REDISPORT=6379  
  13. REPEAT=1  
  14. DELAY=0  
  15. while getopts ":h:n:p:r:a:i:" opt  
  16. do  
  17.     case ${opt} in  
  18.         h) REDISHOST=${OPTARG};;  
  19.         n) REDISDB=${OPTARG};;  
  20.         p) REDISPORT=${OPTARG};;  
  21.         r) REPEAT=${OPTARG};;  
  22.         a) AUTH=${OPTARG};;  
  23.         i) DELAY=${OPTARG};;  
  24.     esac  
  25. done  
  26. shift $((${OPTIND} - 1))  
  27. if [ "${REDISHOST}" != "" ] && [ "${REDISPORT}" != "" ]  
  28. then  
  29.     exec 6<>/dev/tcp/${REDISHOST}/${REDISPORT} # open fd  
  30.     if [ $? -ne 0 ]; then  
  31.         exit 1  
  32.     fi  
  33. else  
  34.     echo "Wrong arguments"  
  35.     exit 255  
  36. fi  
  37. [ "${AUTH}" != "" ] && redis-client 6 AUTH ${AUTH} > /dev/null  
  38. [ "${REDISDB}" != "" ] && redis-client 6 SELECT ${REDISDB} > /dev/null  
  39. for ((z=1;z<=${REPEAT};z++))  
  40. do  
  41.     redis-client 6 "${@}"  
  42.     if [ $? -ne 0 ]; then  
  43.         exit 1  
  44.     fi  
  45.     [ ${DELAY} -gt 0 ] && sleep ${DELAY}  
  46. done  
  47. exec 6>&- #close fd  

 

Log Service

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/1T1115196-3.png "/>

 

Redis-publish-test: This is the log server and can receive publish data.

 

 
 
  1. #!/bin/bash   
  2. source /usr/share/redis-bash/redis-bash-lib 2> /dev/null  
  3. if [ $? -ne 0 ]; then  
  4.   LIBFOLDER=${0%/${0##*/}}  
  5.   echo $LIBFOLDER  
  6.   source ${LIBFOLDER}/redis-bash-lib 2> /dev/null  
  7.   if [ $? -ne 0 ]; then  
  8.     echo "can't find redis-bash-lib in /usr/share/redis-bash or ${LIBFOLDER}"  
  9.     exit 127   
  10.   fi  
  11. fi  
  12. REDISHOST=localhost  
  13. REDISPORT=6379  
  14. while getopts ":h:p:" opt  
  15. do  
  16.     case ${opt} in  
  17.         h) REDISHOST=${OPTARG};;  
  18.         p) REDISPORT=${OPTARG};;  
  19.     esac  
  20. done  
  21. shift $((${OPTIND} - 1))  
  22. while true  
  23. do  
  24.     exec 5>&-  
  25.     if [ "${REDISHOST}" != "" ] && [ "${REDISPORT}" != "" ]  
  26.     then  
  27.         exec 5<>/dev/tcp/${REDISHOST}/${REDISPORT} # open fd  
  28.     else  
  29.         echo "Wrong arguments"  
  30.         exit 255  
  31.     fi  
  32.     redis-client 5 SUBSCRIBE ${1} > /dev/null # subscribe to the pubsub channel in fd 5  
  33.     while true  
  34.     do  
  35.         unset ARGV  
  36.         OFS=${IFS};IFS=$'\n' # split the return correctly  
  37.         ARGV=($(redis-client 5))  
  38.         IFS=${OFS}  
  39.         if [ "${ARGV[0]}" = "message" ] && [ "${ARGV[1]}" = "${1}" ]  
  40.         then  
  41.         echo ${ARGV[2]}  
  42.         a=${ARGV[2]}  
  43.             echo $($a)  
  44.             echo "Message from pubsub channel: ${ARGV[2]}"  
  45.         elif [ -z ${ARGV} ]  
  46.         then  
  47.             sleep 1  
  48.             break  
  49.         fi  
  50.     done  
  51. done  

 

 

This article is from "Fengyun, it's her ." Blog, please be sure to keep this source http://rfyiamcool.blog.51cto.com/1030776/1191926

Related Article

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.