웹 트래픽 로그 순위 추출

Posted by 주원이^^
2016. 11. 3. 11:43 리눅스/스크립트

#!/bin/bash
stty erase ^H
check="no"
#출력 기본값
view_number="20"

declare -a http_log_size_array
declare -a http_log_ip_array
declare -a http_log_filename_array

_comm() {
apache_log_file=""

#같은파일 중복제거 , 카운트
http_log_cnt="`cat $2 | awk '!x[$7]++' | sort -rn -k 10 | wc -l`"

#같은파일 중복제거, 문자열 내용 변수저장
IFS=$'\n' http_log_text=(`cat $2 | awk '!x[$7]++' | sort -rn -k 10`)

view_number=$1

#트래픽합산결과
total_sum=`cat $2 | awk '{sum += $10} END {print sum/1024000,"MByte"}'`

#리스트 배열에 담기 ip,filename,size 3개
#line 한줄출력
k="0"
http_log_number="0"

for value in "${http_log_text[@]}"; do
 if [[ $k -ge $view_number ]]; then
  break;
 fi

 echo "number : $k        total_number : $view_number "
 http_log_filename=`echo $value | awk '{print $7}'`
 http_log_ip=`echo $value | awk '{print $1}'`
 http_log_size=`echo $value | awk '{print $10}'`

 http_log_filename_array[$http_log_number]=${http_log_filename}
 http_log_ip_array[$http_log_number]=${http_log_ip}
 http_log_size_array[$http_log_number]=`echo ${http_log_size} | awk '{sum += $1} END {print sum/1024000,"MByte"}'`

 k=`expr $k + 1`
 http_log_number=`expr $http_log_number + 1`
 clear
done

#트래픽 용량 출력
echo -ne "Total Traffic $1 List
"
m=0

#트래픽 출력

for ((j=1;j<=$view_number;j++)); do
  echo  "${j}) ${http_log_filename_array[$m]} ${http_log_ip_array[$m]} ${http_log_size_array[$m]}"

  m=`expr $m + 1`
done

echo -ne "
1) Menu  2) Log File modify 3) View number modify x) exit
select number "

read select_comm
 case $select_comm in
  1)
   clear
   _default_select
   ;;
  2)
   if [[ ! -z $http_log_file_menu ]]; then
    http_log_dir="${http_log_file_menu}"
    http_log_dir_list="`ls $http_log_dir | grep log$`"
    http_log_dir_cnt=`ls $http_log_dir | grep log$ | wc -l`
    for ((i=1;i<=$http_log_dir_cnt;i++)); do
     http_log_dir_list=`ls $http_log_dir | grep log$ | sed -n "${i},${i}p"`
     http_log_dir_array[$i]=$http_log_dir_list
     echo "${i}) $http_log_dir_list"
    done
    echo "select log file number?"
    read http_log_file_number
    http_log_file="${http_log_dir}/${http_log_dir_array[$http_log_file_number]}"
   fi
   _comm "$view_number" "$http_log_file"
   ;;
  3)
   echo -ne " before : $view_number
modify number?"
                        read modify_view_number
                        view_number=`echo $modify_view_number | sed 's/[^0-9]//g'`
    
   clear
   _comm "$view_number" "$http_log_file"

   ;;
  x)
   exit
   ;;
 esac
}

_default_select() {
clear
while [ $check=no ]; do
echo -ne "
================ Default Information ================
1)Http Log File : $http_log_file

ex) /usr/local/apache/logs/transferlog

2)View number : $view_number

s) Start        x) Exit
======================================================
select number?"
read menu_select
        case $menu_select in
                1)
   echo -ne "
1)/usr/local/apache/logs/transferlog 2)input Dir
select number?"
#폴더 선택 및 파일 선택
   read http_log_file_menu
    case $http_log_file_menu in
     1)
      http_log_dir="/usr/local/apache/logs/transferlog"
      http_log_dir_list="`ls $http_log_dir | grep log$`"
      http_log_dir_cnt=`ls $http_log_dir | grep log$ | wc -l`
      for ((i=1;i<=$http_log_dir_cnt;i++)); do
       http_log_dir_list=`ls $http_log_dir | grep log$ | sed -n "${i},${i}p"`
       http_log_dir_array[$i]=$http_log_dir_list
       echo "${i}) $http_log_dir_list"
      done
      echo "select log file number?"
      read http_log_file_number
      http_log_file="${http_log_dir}/${http_log_dir_array[$http_log_file_number]}"
      _default_select
      ;;
     2) 
      echo "input your directory "
      read http_log_file_menu
      if [[ ! -z $http_log_file_menu ]]; then
       http_log_dir="${http_log_file_menu}"
       http_log_dir_list="`ls $http_log_dir | grep log$`"
       http_log_dir_cnt=`ls $http_log_dir | grep log$ | wc -l`
       for ((i=1;i<=$http_log_dir_cnt;i++)); do
        http_log_dir_list=`ls $http_log_dir | grep log$ | sed -n "${i},${i}p"`
        http_log_dir_array[$i]=$http_log_dir_list
        echo "${i}) $http_log_dir_list"
       done
       echo "select log file number?"
       read http_log_file_number
       http_log_file="${http_log_dir}/${http_log_dir_array[$http_log_file_number]}"
      fi
      _default_select
      ;;
    esac


   _default_select
   ;;
                2)
                        echo -ne "View number?"
                        read tmp_view_number
                        tmp2_view_number=`echo $tmp_view_number | sed 's/[^0-9]//g'`
                        if [[ ! -z "$tmp2_view_number" ]]; then
                                view_number=${tmp2_view_number}
                                _default_select
                        else
                                _default_select
   fi
                        ;;

  x)
   exit
   ;;
   
  *)
  clear
  if [[ ! -z $http_log_dir ]] && [[ ! -z $view_number ]]; then
   _comm "$view_number" "$http_log_file"
  fi
  _default_select
  ;;

        esac
clear
_default_select
done
}

_default_select

 

log.txt

 

'리눅스 > 스크립트' 카테고리의 다른 글

system 정보보기 스크립트  (0) 2017.01.31
ssh_bruteforce 자동차단  (0) 2016.11.04
웹 트래픽 로그 순위 추출  (0) 2016.11.03
apache 자동다운받기  (0) 2016.03.04
mysql replication 상태체크  (0) 2016.01.13
폴더내 파일 소문자로 일괄변경  (0) 2015.07.16
이 댓글을 비밀 댓글로