#!/bin/bash
#######################################################################################
# Path 설정 및 환경 설정
#######################################################################################
# 서버 캐릭터셋 확인
char=`echo $LANG|cut -d"." -f2|tr [A-Z] [a-z]`
# IP 추출
ServerIp=`hostname -I`
# 폴더 리스트 확인 Function
function FileList {
for FILE in `ls $1`
do
echo "$FILE"
done
}
# 오늘날짜
Today=`/bin/date '+%F' | sed -e 's/-//g'`
LogDir="$Today.access_log"
Search_Day=`/bin/date | awk '{print $3"/"}' | sed -e 's/\.//g'`
#######################################################################################
echo -e "\e[32;1m조회할 도메인을 입력해주세요. ex) infree.kr \e[0m"|iconv -futf-8 -t$char
echo ""
echo -n "도메인 : "|iconv -futf-8 -t$char
read Search_Domain
echo ""
# virtual.conf path
HttpPath=`ps -ef | grep httpd | head -1 | awk '{print $8}' | cut -f1,2,3,4 -d"/"`
VirPath=`grep -r $Search_Domain $HttpPath | sed -e 's/:/ /g' | awk '{print $1}' | head -1`
# access_log path
LogPath="$HttpPath/logs/access_log"
# 기본 폴더 생성
CheckSearchDomain=`/bin/grep "ServerName $Search_Domain" $VirPath | wc -l`
UserPath=`/bin/grep -B1 "ServerName $Search_Domain" $VirPath | grep DocumentRoot | head -1 | cut -f2 -d" "`
BasePath=$UserPath/$LogDir
# 조회할 도메인 존재 여부 체크
echo -e "\e[33;1m입력하신 도메인은 [ $Search_Domain ]입니다. [ $Search_Domain ]으로 조회 시작하겠습니다. \e[0m"|iconv -futf-8 -t$char
echo ""
rm -rf $BasePath
mkdir -p $BasePath
# 로그 파일
UserLogFile="$UserPath/$LogDir/1.$Search_Domain.txt"
# 기본 로그 생성
grep $Search_Domain $LogPath | grep $Search_Day | grep -v $ServerIp | awk '{print $2 " : " $5 "] : " $1$8 " : " $12 " : "$11}' > $UserLogFile
# 접속 IP 리스트 (방문자 리스트)
cat $UserLogFile | awk '{print $1 }' | sort | uniq -c | sort -r | awk '{print $2 }' > $UserPath/$LogDir/2.iplist.txt
Today_visit=`cat $UserPath/$LogDir/2.iplist.txt | wc -l`
# 파일별 접속 파일 리스트 (많은 순 정렬)
cat $UserLogFile | awk '{print $5 }' | sort | uniq -c | sort -r > $UserPath/$LogDir/3.max_pageview_list.txt
echo -e "\e[32;1m$(date '+%H:%M:%S') :: IP별 트래픽 사용량 조사를 합니다. 오래 걸리니 차 한잔하고 오세요.^^ \e[0m"|iconv -futf-8 -t$char
echo ""
# 접속 IP 별 트래픽 발생량 (많은 순 정렬)
for IP in `cat $UserPath/$LogDir/2.iplist.txt`; do
touch $UserPath/$LogDir/4.ip_traffic.txt
IPTrafficCheck=`grep $IP $UserLogFile | awk '{print NF}' | sed '/-/d' | awk '{print $1/1048576 }' | awk '{sum +=$1} END {print sum,"MByte" }'`
echo $IP " : " $IPTrafficCheck
done >> $UserPath/$LogDir/4.ip_traffic.txt
echo -e "\e[32;1m$(date '+%H:%M:%S') :: IP별 트래픽 사용량 조사가 완료되었습니다. \e[0m"|iconv -futf-8 -t$char
echo ""
# 오늘 사용한 전체 트래픽
TodayTraffic=`cat $UserLogFile | awk '{print NF}' | sed '/-/d' | awk '{print $1/1048576 }' | awk '{sum +=$1} END {print sum,"MByte" }'`
# 로그 퍼미션 변경
UserID=`grep -B 1 "ServerName $Search_Domain" $VirPath | head -1 | cut -f2 -d" " | cut -f4 -d"/"`
chown $UserID.free -R $UserPath/$LogDir
echo ""
echo -e "\e[32;1m$Today :: 금일 전체 트래픽 : $TodayTraffic / 오늘 방문자 $Today_visit 명 \e[0m"|iconv -futf-8 -t$char
echo ""
echo -e "\e[32;1m가장 많이 접속한 IP 리스트 TOP 10 \e[0m"|iconv -futf-8 -t$char
head $UserPath/$LogDir/2.iplist.txt
echo ""
echo -e "\e[32;1m가장 많이 접속한 IP의 트래픽 사용양 TOP 10 \e[0m"|iconv -futf-8 -t$char
head $UserPath/$LogDir/4.ip_traffic.txt
echo ""
echo -e "\e[32;1m가장 많이 접속한 페이지 리스트 TOP 10 \e[0m"|iconv -futf-8 -t$char
head $UserPath/$LogDir/3.max_pageview_list.txt
echo ""
echo -e "\e[32;1m로그파일은 $UserPath/$LogDir 에서 확인 할 수 있습니다. \e[0m"|iconv -futf-8 -t$char
echo ""
cd $UserPath/$LogDir ; ls -lh