Skip to content

Instantly share code, notes, and snippets.

@odony
Last active April 9, 2020 17:24
Show Gist options
  • Save odony/63b61f218d7039063fe98ef8ff8b937c to your computer and use it in GitHub Desktop.
Save odony/63b61f218d7039063fe98ef8ff8b937c to your computer and use it in GitHub Desktop.
# Setup an appropriate nginx log_format in the `http` section of your nginx config:
log_format main '$remote_addr $time_iso8601 "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_host" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time ';
# Then save the following as a munin plugin to monitor the last 5 minutes of your nginx access.log file
#!/bin/bash
#%# family=manual
LOGFILE=/var/log/nginx/access.log
[[ -e $LOGFILE ]] || exit 0
case $1 in
config)
echo graph_title Nginx requests time
echo graph_category nginx
echo "avg_request_time.label Average request time (s)"
echo avg_request_time.warning 1
echo avg_request_time.critical 3
exit 0
;;
esac
avg=$(cat $LOGFILE | grep -av '/longpolling' | tail -n 60000 | awk -v threshold=`date -Isec -d '5 min ago'` 'threshold < $2 { sum += $NF; n++ } END { if(n > 0) print sum/n }')
echo -n "avg_request_time.value "
echo ${avg:-0}
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment