Skip to content

Instantly share code, notes, and snippets.

@sphinxid
Last active September 6, 2023 01:53
Show Gist options
  • Save sphinxid/2a3c8136b95c756e66d1d54a0466bfde to your computer and use it in GitHub Desktop.
Save sphinxid/2a3c8136b95c756e66d1d54a0466bfde to your computer and use it in GitHub Desktop.
This bash script is to help count request per seconds (rps) of nginx access log. (method: tailing the streamed file logs)
#!/bin/bash
#
# sphinxid <firman.gautama@gmail.com>
#
# Example: ./count_per_second_nginx_log.sh /var/log/nginx/*.log
#
RAND_STR=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1`
INTERVAL=10 #seconds
LOG_FILES=$1
TMP_FILE="/tmp/$RAND_STR.log"
BASEVAL=`tail -q -n0 -F $LOG_FILES > $TMP_FILE & sleep $INTERVAL; kill $! ; wc -l $TMP_FILE | cut -f1 -d " " && rm -f $TMP_FILE`
REALVAL=$((($BASEVAL / $INTERVAL) + ($BASEVAL % $INTERVAL > 0)))
echo $REALVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment