--- title: nginxのアクセス数を集計するプラグイン author: kazu634 date: 2012-03-27 wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";s:1:"1";s:5:"delay";s:1:"0";s:7:"enabled";s:1:"1";s:10:"separation";i:60;s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5453;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - つれづれ ---
muninでnginxのアクセス数を集計するプラグインを作成してみました。
nginxのログファイルからお手軽にアクセス数を集計したい。
詳細は HowToWritePlugins ? Munin を参照のこと。
#!/bin/bash # ----------- # varibales # ----------- LOGFILE="/var/log/nginx/front_proxy.access.log" LASTDATA="/tmp/.munin-nginx" # ----------------------- # Handling the arguments # ----------------------- case $1 in config) cat <<'EOM' graph_args -l 0 graph_scale no graph_category nginx graph_title Nginx Access graph_vlabel access access.label access EOM exit ;; esac # ----------------------- # Collecting data # ----------------------- CURRENT_LINENUM=`wc -l ${LOGFILE} | perl -pe "s/^ +//" | cut -f 1 -d " "` # checks if the last recorded data exists or not: if [ -e ${LASTDATA} ]; then # Get the last line number LASTLINE=`cat ${LASTDATA}` expr ${CURRENT_LINENUM} \< ${LASTLINE} > /dev/null RESULT=$? # when the target log file is rotated, if [ $RESULT -eq ]; then # it returns 0 echo "access.value 0" # and sets 0 to the $LASTDATA echo 0 > ${LASTDATA} # not rotated else echo -n "access.value " echo `expr ${CURRENT_LINENUM} - ${LASTLINE}` echo ${CURRENT_LINENUM} > ${LASTDATA} fi else # if the log file does not exist, it returns 0 echo "access.value 0" # and sets the current line number to the $LASTDATA echo ${CURRENT_LINENUM} > ${LASTDATA} fi exit