Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- FILES_TO_BE_PARSED="/var/log/remotelogs/*-*.log*"
- SINCE_DB_FILES="/opt/logstash/data/plugins/inputs/file/.sincedb_[a-z]*"
- function posInFile() {
- inode=$1
- pid=$2
- fdnr=$3
- filename=$4
- filesize=$5
- fd=$(echo "$fdnr" | grep -o '[0-9]\+')
- readpos=$(grep 'pos' /proc/${pid}/fdinfo/${fd}| grep -o '[0-9]\+')
- echo "${inode} ${readpos} ${filesize} ${filename}" | awk '{ printf "%-30s %.1f%% %.0f %.0f %ld\n", $4, 100 * $2 / $3, $3, $2, $1 }'
- }
- function readPositions() {
- while read -r line; do
- posInFile ${line}
- done < ${tmpPositionsOfFilesToBeParsed}
- }
- function addIfFileNotOpen() {
- inode=$1
- filename=$2
- filesize=$3
- inode_exists=$(grep "^${inode} " ${tmpPositionsOfFilesToBeParsed})
- if [[ -z "${inode_exists}" ]]; then
- # Gets the biggest file position from sincedb files
- readpos=$(grep -h "^${inode} " ${SINCE_DB_FILES} | sort -rn -k 4,4 | head -n1 | awk '{print $4}')
- if [[ -z "${readpos}" ]]; then
- # File position not saved to sincedb
- echo "${filename} 0.0% ${filesize} 0 ${inode}"
- else
- echo "${inode} ${readpos} ${filesize} ${filename}" | awk '{ printf "%-30s %.1f%% %.0f %.0f %ld\n", $4, 100 * $2 / $3, $3, $2, $1 }'
- fi
- fi
- }
- function addUnopenedFiles() {
- while read -r line; do
- addIfFileNotOpen ${line}
- done < ${tmpFilesToBeParsedInfo}
- }
- LOGSTASH_PID=$(ps ax | grep logstash | grep java | awk '{print $1}')
- if [[ -n "${LOGSTASH_PID}" ]]; then
- # Creates temporary files
- tmpLogstashReadHandles=$(mktemp); tmpFilesToBeParsedInfo=$(mktemp); tmpPositionsOfFilesToBeParsed=$(mktemp)
- # Gets the INODE, PID and FD
- su logstash -s /bin/bash -c "lsof -n -p ${LOGSTASH_PID}" | grep ' [0-9]\+r' | awk '{print $8" "$2" "$4}' | sort > ${tmpLogstashReadHandles}
- # Gets the INODE, File name and File size in bytes
- stat -c "%i %n %s" ${FILES_TO_BE_PARSED} | sort > ${tmpFilesToBeParsedInfo}
- # 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
- join ${tmpLogstashReadHandles} ${tmpFilesToBeParsedInfo} > ${tmpPositionsOfFilesToBeParsed}
- # Obtain the Filename, %complete, file size, scanned size and inode nr from the files that are currently read by Logstash
- results=$(readPositions)
- # Obtain the Filename, N/A, file size, N/A and inode nr from the files that are not currently being read by Logstash
- # These files may include files that were previously scanned by Logstash and written to the sincedb file
- unopenedFiles=$(addUnopenedFiles)
- totalsize=$(cat ${tmpFilesToBeParsedInfo} | awk '{ sum += $3; }
- END { printf "%.0f", sum; }' "$@")
- scannedsize=$(echo "$results
- $unopenedFiles" | awk '{ sum += $4; }
- END { printf "%.0f", sum; }' "$@")
- totalsummary=$(echo "$totalsize $scannedsize" | awk '{printf "%.1f%%", 100 * $2 / $1 }')
- # Cleanup temporary files
- rm -f ${tmpLogstashReadHandles} ${tmpFilesToBeParsedInfo} ${tmpPositionsOfFilesToBeParsed}
- # Output positions
- echo "<Filename> <% Complete> <Total Size> <Scanned Size> <inode>"
- echo "$results"
- echo "$unopenedFiles"
- echo "Total Summary: $scannedsize/$totalsize = $totalsummary"
- else
- >&2 echo "Error: Unable to obtain Logstash PID"
- fi
Advertisement
Add Comment
Please, Sign In to add comment