运维联盟俱乐部

 找回密码
 立即注册
查看: 2071|回复: 3

[日常管理] listener.log分时段统计连接数

[复制链接]
  • TA的每日心情
    开心
    2023-8-9 11:05
  • 发表于 2021-8-7 15:26:33 | 显示全部楼层 |阅读模式
    有的时候需要从listener.log分析连接数情况:
    截取特定时间段的日志
    1. sed -n '/开始时间/,/结束时间/p' listener.log
    复制代码
    同一天按小时统计连接数
    1. fgrep "13-JAN-2015 " listener.log  |fgrep "establish" |awk '{print $1 " " $2}' |awk -F: '{print $1 }' |sort |uniq -c|sort -k 3
    复制代码
    同一小时按分钟统计连接数
    1. fgrep "13-JAN-2015 11:" listener.log  |fgrep "establish" |awk '{print $1 " " $2}' |awk -F: '{print $1 ":" $2 }' |sort |uniq -c|sort -n
    复制代码
    同一小时按秒统计连接数
    1. fgrep "29-NOV-2023 16:42" listener.log  |fgrep "establish" |awk '{print $1 " " $2}' |awk -F: '{print $1 ":" $2 ":" $3 }' |sort |uniq -c|sort -k 3
    复制代码
    同一小时按分钟统计连接失败数
    1. fgrep "11-JAN-2015 11:" listener.log |awk  '{ if ( $NF != 0 ) print $0 }'|awk '{print $1 " " $2}' |awk -F: '{print $1 ":" $2 }' |sort |uniq -c|sort -n
    复制代码
    同一小时按ip统计连接数
    1. fgrep "11-JAN-2015 11:" listener.log|fgrep "establish"|awk -F* '{print $3}'|awk -F= '{ print $4}'|sed -e 's/......$//g'|sort |uniq -c|sort -n
    复制代码
    每分钟ip请求数
    1. fgrep "11-JAN-2015 11:30" listener.log|fgrep "establish"|awk -F* '{print $3}'|awk -F= '{ print $4}'|sed -e 's/......$//g'|sort |uniq -c|sort -n
    复制代码
    全天每小时IP请求数
    1. fgrep "09-JAN-2015 " istener.log|fgrep "establish"|awk -F* '{print $1 " " $3}'|awk -F= '{ print $1 " "  $4}'|sed -e 's/......$//g'| awk '{print $1 " " $2 " " $4}'|cut -b-14,21- |sort |uniq -c
    复制代码
    指定时间指定主机上不同主机USER的请求统计
    1. fgrep "04-JUL-2018 15:"  listener.log|fgrep " <span style="color: rgb(0, 0, 0); font-size: 14px;">133.96.65.45" | awk -F= '$11 ~ /USER/ {print $12 }' |  sed 's/).*$//'|sort|uniq -c|sort -n
    复制代码
    指定时间指定条件的PROGRAM的请求统计
    1. fgrep "01-NOV-2018 " listener.log|sed 's/^.*PROGRAM//;s/).*$//;s/^.*:...\*//;s/).*$//;s/\*.*$//'|sort|uniq -c|sort -n
    复制代码
    vi cnt.awk
    1. BEGIN{FS="*";p="NULL";c="NULL";n=0;}
    2. {
    3. if(match($0,"CONNECT_DATA") != 0){
    4. n++;
    5. p=c;
    6. c=$1;
    7. if(p!=c){
    8. print p,"-",n;
    9. n=0;
    10. }
    11. }
    12. }
    复制代码
    awk cnt.awk listener.log
    1. 15-FEB-2022 23:53:17  - 1
    2. 15-FEB-2022 23:54:17  - 1
    3. 15-FEB-2022 23:54:47  - 1
    4. 15-FEB-2022 23:55:17  - 1
    5. 15-FEB-2022 23:56:17  - 1
    6. 15-FEB-2022 23:57:17  - 1
    7. 15-FEB-2022 23:58:17  - 1
    8. 15-FEB-2022 23:59:17  - 1
    9. 16-FEB-2022 00:00:00  - 2
    10. 16-FEB-2022 00:00:17  - 1
    11. 16-FEB-2022 00:01:17  - 1
    12. 16-FEB-2022 00:02:17  - 1
    13. 16-FEB-2022 00:03:17  - 1
    14. 16-FEB-2022 00:04:17  - 1
    15. 16-FEB-2022 00:04:49  - 1
    16. 16-FEB-2022 00:05:17  - 1
    17. 16-FEB-2022 00:06:17  - 1
    18. 16-FEB-2022 00:07:17  - 1
    19. 16-FEB-2022 00:08:17  - 1
    20. 16-FEB-2022 00:08:51  - 1
    复制代码








    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-8-9 11:05
  •  楼主| 发表于 2023-12-12 09:01:44 | 显示全部楼层
    你可以使用以下的shell命令来统计listener.log中不同主机的连接次数:
    grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" listener.log | sort | uniq -c
    这行命令会首先使用grep命令匹配listener.log中的IP地址,并使用sort和uniq命令来对IP地址进行排序和去重,并且使用uniq -c来统计每个IP地址出现的次数

    这个命令将会:

    使用grep命令和正则表达式\b([0-9]{1,3}\.){3}[0-9]{1,3}\b在listener.log文件中匹配并提取IP地址。
    \b用于匹配单词边界,确保我们只获取完整的IP地址。
    ([0-9]{1,3}\.){3}用于匹配3个由数字和点号组成的分组,表示IP地址的前三部分。
    [0-9]{1,3}用于匹配最后一个IP地址部分,即最后一个数字。
    使用sort命令对提取的IP地址进行排序。
    使用uniq -c命令统计每个唯一IP地址出现的次数,并在每行前面显示计数。
    请确保将listener.log替换为实际的监听器日志文件路径。运行命令后,你将会得到一个包含IP地址和计数的列表。



    如果你想要从 listener.log 文件中提取 program,并统计它们的数量,可以使用以下命令:

    grep -oE "PROGRAM=[^)]+" listener.log | sort | uniq -c
    该命令会执行以下操作:

    使用 grep 命令和正则表达式 PROGRAM=[^)]+ 从 listener.log 文件中匹配并提取 program。
    PROGRAM= 匹配 program 字符串。
    [^)]+ 匹配不包含 ) 的任意字符,确保我们只提取一个完整的 program。
    使用 sort 命令对提取的 program 进行排序。
    使用 uniq -c 命令统计每个唯一的 program 出现的次数,并在每行前面显示计数。
    请确保将 listener.log 替换为实际的监听器日志文件路径。执行该命令后,你将获得一个包含 program 和计数的列表。



    如果你想要从 listener.log 文件中提取 user,并统计它们的数量,可以使用以下命令:

    grep -oE "USER=[^)]+" listener.log | sort | uniq -c
    该命令会执行以下操作:

    使用 grep 命令和正则表达式 USER=[^)]+ 从 listener.log 文件中匹配并提取 user。
    USER= 匹配 user 字符串。
    [^)]+ 匹配不包含 ) 的任意字符,确保我们只提取一个完整的 user。
    使用 sort 命令对提取的 user 进行排序。
    使用 uniq -c 命令统计每个唯一的 user 出现的次数,并在每行前面显示计数。
    请确保将 listener.log 替换为实际的监听器日志文件路径。执行该命令后,你将获得一个包含 user 和计数的列表



    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2023-8-9 11:05
  •  楼主| 发表于 2023-12-12 09:03:53 | 显示全部楼层
    要统计特定时间段内不同主机的连接次数,你可以使用以下的shell命令:

    grep '开始时间' listener.log -A $(($(grep -n '结束时间' listener.log | cut -d: -f1) - $(grep -n '开始时间' listener.log | cut -d: -f1))) | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | sort | uniq -c
    请将"开始时间"和"结束时间"替换为你想要统计的时间段。这个命令会首先使用第一个grep命令找到开始时间行所在的位置,并通过-A参数来匹配从开始时间到结束时间之间的行。然后使用第二个grep命令匹配IP地址,并使用sort和uniq命令来统计每个IP地址出现的次数。

    请确保时间格式与listener.log中的日期和时间格式匹配,否则可能无法正确匹配到时间段内的内容。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2023-8-9 11:05
  •  楼主| 发表于 2023-12-20 17:40:13 | 显示全部楼层
    1. #!/bin/sh
    2. #usage:view listener status/config;sqlnet.ora;du size and analyze connect count by hour/minute/second to diag connection storm.
    3. #yu.c.zhang@oracle.com 20231219
    4. #modification:add by ip/program/user. 20240113
    5. #modification:add by program+host+user+ip;no_timed item sort by count desc. 20240115

    6. #define select prompt
    7. PS3="please select a num from menu:"

    8. #define logo
    9. function csslogo {
    10.         echo -e "\033[31m*************************************************************************************************************************\033[0m"
    11.         echo
    12.         echo -e "                                      \033[31m ${bold}ORACLE CSS DATABASE TOOLBOX\033[0m"
    13.         echo -e
    14.         echo -e "\033[31m b\033[0m: back to above menu"
    15.         echo -e "\033[31m q\033[0m: exit the program"
    16.         echo -e "\033[31m*************************************************************************************************************************\033[0m"
    17. }

    18. #define function to get lsnr
    19. function get_lsnr () {
    20. LSNR_COUNT=$(ps -ef|grep "/bin/tnslsnr"|grep -v grep|wc -l)

    21. if [ $LSNR_COUNT -eq 0 ]
    22.         then
    23.                 echo No LISTENER Running !
    24.         exit
    25. fi

    26. if [ $LSNR_COUNT -gt 0 ]
    27.         then       
    28.                         clear
    29.                         csslogo
    30.                         echo
    31.                         echo "Select the listener you want to operate: [Enter the Number]"
    32.                         echo "----------------------------------------------------------"
    33.                         select listeners in $( ps -ef | grep "/bin/tnslsnr"| grep -v grep | awk -v OFS=':' '{print $1,$8,$9}' )
    34.      do       
    35.                         integ='^[1-9]+$'
    36.                         if ! [[ ${REPLY} =~ ${integ} ]] || [ ${REPLY} -gt ${LSNR_COUNT} ];then
    37.                 echo "Error: Not a valid number!"                       
    38.                         else
    39.                                 lsnr_owner=`echo ${listeners}|awk -v FS=':' '{print $1}'`
    40.                                 lsnr_name=`echo $listeners|awk -v FS=':' '{print $3}'|tr "[:upper:]" "[:lower:]"`
    41.                                 tnslsnr=`echo $listeners|awk -v FS=':' '{print $2}'`
    42.                                 lsnr_path=`dirname $tnslsnr`
    43.                                 lsnr_home=`dirname $lsnr_path`
    44.                                 lsnr_logdir=`$lsnr_home/bin/lsnrctl <<EOF | grep log_directory | awk '{print $6}'
    45. set current_listener $lsnr_name
    46. show log_directory
    47. EOF`
    48.                                 lsnr_trcfile=`$lsnr_home/bin/lsnrctl<<EOF | grep trc_directory | awk '{print $6"/"$1".log"}'
    49. set current_listener $lsnr_name
    50. show trc_directory
    51. EOF`
    52.                         if [ $USER != $lsnr_owner ];then
    53.                                 echo "Please execute as LISTENER ownr:${lsnr_owner} !"       
    54.                                 exit
    55.                         fi                               
    56.                         break                                               
    57.                         fi
    58.         done
    59. fi
    60. }

    61. #define function to view config and status
    62. menul_view () {
    63.     while true; do
    64.                 clear
    65.                 csslogo
    66.         echo -e "====ORACLE-TOOLBOX-listener-analyze==="
    67. echo -e "\t1.vies status"
    68. echo -e "\t2.vies service"
    69. echo -e "\t3.view listener.ora"
    70. echo -e "\t4.view sqlnet.ora"
    71. echo -e "\t5.view file size"
    72.         echo "================"
    73.         read -p $'\e\033[33;5m$please enter option:\e\033[0m' choice
    74.         case $choice in
    75.             1)       
    76.                                 echo
    77.                                 echo -e "\t\033[31mlisnrctl status\033[0m"
    78.                                 echo "-------------------------------------------"
    79.                                 $lsnr_path/lsnrctl status $v_lsnr_name
    80.                 read -p "execute succesfully,enter any to continue..."
    81.                 ;;
    82.             2)       
    83.                                 echo
    84.                                 echo -e "\t\033[31mlisnrctl service\033[0m"
    85.                                 echo "-------------------------------------------"
    86.                                 $lsnr_path/lsnrctl service $v_lsnr_name
    87.                 read -p "execute succesfully,enter any to continue..."
    88.                 ;;
    89.                         3)       
    90.                                 echo
    91.                                 echo -e "\t\033[31mlistener.ora\033[0m"
    92.                                 echo "-------------------------------------------"                               
    93.                                 cat `$lsnr_path/lsnrctl status $lsnr_name|grep "Parameter File"|awk '{print $4}'`
    94.                 read -p "execute succesfully,enter any to continue..."
    95.                 ;;
    96.                         4)       
    97.                                 echo
    98.                                 echo -e "\t\033[31msqlnet.ora\033[0m"
    99.                                 echo "-------------------------------------------"
    100.                                 cat $lsnr_home/network/admin/sqlnet.ora
    101.                 read -p "execute succesfully,enter any to continue..."
    102.                 ;;
    103.                         5)       
    104.                                 echo
    105.                                 echo -e "\t\033[31mLOG DIR USED in MB\033[0m"
    106.                                 echo "-------------------------------------------"
    107.                                 du -sm $lsnr_logdir
    108.                                 echo
    109.                                 echo -e "\t\033[31mTRACE FILE USED in MB\033[0m"
    110.                                 echo "-------------------------------------------"
    111.                                 du -sm $lsnr_trcfile
    112.                 read -p "execute succesfully,enter any to continue..."
    113.                 ;;
    114.                         b)
    115.                 menul;;
    116.             q)
    117.                 exit;;
    118.             *)
    119.                 echo "invalid option!"
    120.                 sleep 1;;
    121.         esac
    122.     done
    123. }

    124. #define function to analyze connect count
    125. menul_analyze () {
    126.     while true; do
    127.                 clear
    128.                 csslogo
    129.         echo -e "====ORACLE-TOOLBOX-listener-analyze==="
    130. echo -e "\t1.analyze by hour"
    131. echo -e "\t2.analyze by minite"
    132. echo -e "\t3.analyze by second"
    133. echo -e "\t4.analyze by ip "
    134. echo -e "\t5.analyze by program"
    135. echo -e "\t6.analyze by user"
    136. echo -e "\t7.analyze by program+host+user+ip"
    137. echo -e "\t8.analyze by specified ip and second"
    138.         echo "================"
    139.         read -p $'\e\033[33;5m$please enter option::\e\033[0m' choice
    140.         case $choice in
    141.             1)       
    142.                                 echo -e "please type time like [$(date '+%d-%b-%Y'| tr '[:lower:]' '[:upper:]')]"
    143.                                 read -p 'enter your time:' v_date
    144.                                 echo
    145.                                 echo "  count             by hour"
    146.                                 echo "--------- ----------------------------------------------------------"
    147.                                 fgrep "$v_date" $lsnr_trcfile | fgrep "establish" | awk '{print $1 " " $2}' | awk -F: '{print $1 }' | sort | uniq -c | sort -k 3
    148.                 read -p "execute succesfully,enter any to continue..."
    149.                 ;;
    150.             2)       
    151.                                 echo -e "please type time like [$(date '+%d-%b-%Y %H'| tr '[:lower:]' '[:upper:]')]"
    152.                                 read -p 'enter your time:' v_date
    153.                                 echo
    154.                                 echo "  count             by minute"
    155.                                 echo "--------- ----------------------------------------------------------"
    156.                                 fgrep "$v_date" $lsnr_trcfile |fgrep "establish" |awk '{print $1 " " $2}' |awk -F: '{print $1 ":" $2 }' |sort |uniq -c|sort -k 3
    157.                 read -p "execute succesfully,enter any to continue..."
    158.                 ;;
    159.                         3)       
    160.                                 echo -e "please type time like [$(date '+%d-%b-%Y %H:%M'| tr '[:lower:]' '[:upper:]')]"
    161.                                 read -p 'enter your time:' v_date
    162.                                 echo
    163.                                 echo "  count             by second"
    164.                                 echo "--------- ----------------------------------------------------------"
    165.                                 fgrep "$v_date" $lsnr_trcfile |fgrep "establish" |awk '{print $1 " " $2}' |awk -F: '{print $1 ":" $2 ":" $3 }' |sort |uniq -c|sort -k 3
    166.                 read -p "execute succesfully,enter any to continue..."
    167.                 ;;
    168.                         4)       
    169.                                 echo -e "please type time like [$(date '+%d-%b-%Y %H:%M'| tr '[:lower:]' '[:upper:]')] and cut until day/hour/minute"
    170.                                 read -p 'enter your time:' v_date
    171.                                 echo
    172.                                 echo "  count             ip_address"
    173.                                 echo "--------- ----------------------------------------------------------"
    174.                                 fgrep "$v_date" $lsnr_trcfile|fgrep "establish"|grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | sort | uniq -c|sort -k1,1nr
    175.                                 echo
    176.                                 read -p "execute succesfully,enter any to continue..."
    177.                 ;;
    178.                         5)       
    179.                                 echo -e "please type time like [$(date '+%d-%b-%Y %H:%M'| tr '[:lower:]' '[:upper:]')] and cut until day/hour/minute"
    180.                                 read -p 'enter your time:' v_date
    181.                                 echo
    182.                                 echo "  count             program"
    183.                                 echo "--------- ----------------------------------------------------------"
    184.                                 fgrep "$v_date" $lsnr_trcfile|fgrep "establish"|grep -oE "PROGRAM=[^)]+" | sort | uniq -c|sort -k1,1nr
    185.                                 echo
    186.                                 read -p "execute succesfully,enter any to continue..."
    187.                 ;;
    188.                         6)       
    189.                                 echo -e "please type time like [$(date '+%d-%b-%Y %H:%M'| tr '[:lower:]' '[:upper:]')] and cut until day/hour/minute"
    190.                                 read -p 'enter your time:' v_date
    191.                                 echo
    192.                                 echo "  count             user"
    193.                                 echo "--------- ----------------------------------------------------------"
    194.                                 fgrep "$v_date" $lsnr_trcfile|fgrep "establish"|grep -oE "USER=[^)]+" | sort | uniq -c|sort -k1,1nr
    195.                                 echo
    196.                                 read -p "execute succesfully,enter any to continue..."
    197.                 ;;
    198.                         7)       
    199.                                 echo -e "please type time like [$(date '+%d-%b-%Y %H:%M'| tr '[:lower:]' '[:upper:]')] and cut until day/hour/minute"
    200.                                 read -p 'enter your time:' v_date
    201.                                 echo
    202.                                 echo "  count             program+host+user+ip"
    203.                                 echo "--------- ----------------------------------------------------------"
    204.                                 fgrep "$v_date" $lsnr_trcfile|fgrep "establish"|grep -oP "PROGRAM=(\S+)\)\(HOST=(\S+)\)\(USER=(\S+).*?HOST=([\d\.]+)"|sort|uniq -c|sort -k1,1nr
    205.                                 echo
    206.                                 read -p "execute succesfully,enter any to continue..."
    207.                 ;;
    208.                         8)       
    209.                                 echo -e "please type time like [$(date '+%d-%b-%Y %H:%M'| tr '[:lower:]' '[:upper:]')] and cut until day/hour/minute"
    210.                                 read -p 'enter your time:' v_date
    211.                                 read -p 'enter your ip_address:' v_address
    212.                                 echo
    213.                                 echo "  count             time"
    214.                                 echo "--------- ----------------------------------------------------------"
    215.                                 fgrep "$v_date" $lsnr_trcfile|fgrep "establish"|fgrep $v_address|awk -F* '{print $1 " " $3}'|awk -F= '{ print $1 " "  $4}'|sed -e 's/......$//g'| awk '{print $1 " " $2 " " $4}'|sort |uniq -c
    216.                 read -p "execute succesfully,enter any to continue..."
    217.                 ;;
    218.                         b)
    219.                 menul;;
    220.             q)
    221.                 exit;;
    222.             *)
    223.                 echo "invalid option!"
    224.                 sleep 1;;
    225.         esac
    226.     done
    227. }

    228. #define main program
    229. function menul() {
    230.     while true; do
    231.         clear
    232.         csslogo
    233.     echo -e "====ORACLE-TOOLBOX-listener==="
    234.     echo -e "\t1.status and config file"
    235.         echo -e "\t2.analyze conn rate"
    236.     echo -e "================"
    237.     read -p $'\e\033[33;5m$please enter option::\e\033[0m' choice

    238.         case $choice in
    239.             1)       
    240.                                 get_lsnr
    241.                                 menul_view
    242.                                 read -p "execute succesfully,enter any to continue..."
    243.                                 ;;
    244.                         2)       
    245.                                 get_lsnr
    246.                                 menul_analyze
    247.                 read -p "execute succesfully,enter any to continue..."
    248.                 ;;
    249.                         b)
    250.                                 menua
    251.                                 ;;
    252.             q)
    253.                 exit
    254.                                 ;;
    255.             *)
    256.                 echo "invalid option!"
    257.                 sleep 1
    258.                                 ;;
    259.         esac
    260.     done
    261. }

    262. #execute main program
    263. menul
    复制代码

    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    运维联盟俱乐部 ( 冀ICP备19036648号 )

    GMT+8, 2024-5-4 05:03 , Processed in 0.047734 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表