Awemonster

Logstash Progress

Jan 25th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.39 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. FILES_TO_BE_PARSED="/var/log/remotelogs/*-*.log*"
  4. SINCE_DB_FILES="/opt/logstash/data/plugins/inputs/file/.sincedb_[a-z]*"
  5.  
  6. function posInFile() {
  7.     inode=$1
  8.     pid=$2
  9.     fdnr=$3
  10.     filename=$4
  11.     filesize=$5
  12.  
  13.     fd=$(echo "$fdnr" | grep -o '[0-9]\+')
  14.     readpos=$(grep 'pos' /proc/${pid}/fdinfo/${fd}| grep -o '[0-9]\+')
  15.  
  16.     echo "${inode} ${readpos} ${filesize} ${filename}" | awk '{ printf "%-30s %.1f%% %.0f %.0f %ld\n", $4, 100 * $2 / $3, $3, $2, $1 }'
  17. }
  18.  
  19. function readPositions() {
  20.     while read -r line; do
  21.         posInFile ${line}
  22.     done < ${tmpPositionsOfFilesToBeParsed}
  23. }
  24.  
  25. function addIfFileNotOpen() {
  26.     inode=$1
  27.     filename=$2
  28.     filesize=$3
  29.    
  30.     inode_exists=$(grep "^${inode} " ${tmpPositionsOfFilesToBeParsed})
  31.     if [[ -z "${inode_exists}" ]]; then
  32.         # Gets the biggest file position from sincedb files
  33.         readpos=$(grep -h "^${inode} " ${SINCE_DB_FILES} | sort -rn -k 4,4 | head -n1 | awk '{print $4}')
  34.        
  35.         if [[ -z "${readpos}" ]]; then
  36.             # File position not saved to sincedb
  37.             echo "${filename} 0.0% ${filesize} 0 ${inode}"
  38.         else
  39.             echo "${inode} ${readpos} ${filesize} ${filename}" | awk '{ printf "%-30s %.1f%% %.0f %.0f %ld\n", $4, 100 * $2 / $3, $3, $2, $1 }'
  40.         fi
  41.     fi
  42. }
  43.  
  44. function addUnopenedFiles() {
  45.     while read -r line; do
  46.         addIfFileNotOpen ${line}
  47.     done < ${tmpFilesToBeParsedInfo}
  48. }
  49.  
  50. LOGSTASH_PID=$(ps ax | grep logstash | grep java | awk '{print $1}')
  51.  
  52. if [[ -n "${LOGSTASH_PID}" ]]; then
  53.     # Creates temporary files
  54.     tmpLogstashReadHandles=$(mktemp); tmpFilesToBeParsedInfo=$(mktemp); tmpPositionsOfFilesToBeParsed=$(mktemp)
  55.  
  56.     # Gets the INODE, PID and FD
  57.     su logstash -s /bin/bash -c "lsof -n -p ${LOGSTASH_PID}" | grep ' [0-9]\+r' | awk '{print $8" "$2" "$4}' | sort > ${tmpLogstashReadHandles}
  58.  
  59.     # Gets the INODE, File name and File size in bytes
  60.     stat -c "%i %n %s" ${FILES_TO_BE_PARSED} | sort > ${tmpFilesToBeParsedInfo}
  61.  
  62.     # Appends the Logstash PID and FD to the lines that contains the INODE, File name and File Size. Remove lines with non-matching inode numbers
  63.     join ${tmpLogstashReadHandles} ${tmpFilesToBeParsedInfo} > ${tmpPositionsOfFilesToBeParsed}
  64.  
  65.     # Obtain the Filename, %complete, file size, scanned size and inode nr from the files that are currently read by Logstash
  66.     results=$(readPositions)
  67.  
  68.     # Obtain the Filename, N/A, file size, N/A and inode nr from the files that are not currently being read by Logstash
  69.     # These files may include files that were previously scanned by Logstash and written to the sincedb file
  70.     unopenedFiles=$(addUnopenedFiles)
  71.  
  72.     totalsize=$(cat ${tmpFilesToBeParsedInfo} | awk '{ sum += $3; }
  73.         END { printf "%.0f", sum; }' "$@")
  74.     scannedsize=$(echo "$results
  75. $unopenedFiles" | awk '{ sum += $4; }
  76.         END { printf "%.0f", sum; }' "$@")
  77.     totalsummary=$(echo "$totalsize $scannedsize" | awk '{printf "%.1f%%", 100 * $2 / $1 }')
  78.    
  79.     # Cleanup temporary files
  80.     rm -f ${tmpLogstashReadHandles} ${tmpFilesToBeParsedInfo} ${tmpPositionsOfFilesToBeParsed}
  81.  
  82.     # Output positions
  83.     echo "<Filename> <% Complete> <Total Size> <Scanned Size> <inode>"
  84.     echo "$results"
  85.     echo "$unopenedFiles"
  86.     echo "Total Summary: $scannedsize/$totalsize = $totalsummary"
  87.  
  88. else
  89.     >&2 echo "Error: Unable to obtain Logstash PID"
  90. fi
Advertisement
Add Comment
Please, Sign In to add comment