ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
root@test:/usr/local/nginx/logs# cat search_log.sh ``` #!/bin/bash #Author:zhangya #Blog:doc.vim26.com #Time:2017-08-02 22:35:07 #Name:log.sh #Version:V1.0 #Description:This is check nginx log script. #用户输入的查询开始时间 #input_log_time1="2017:"$1 #input_log_time2="2017:"$2 #将用户输入的时间转换为日志记录的时间 log_time1="2017:"$1 log_time2="2017:"$2 #日志路径 log_path=$3 #生成的临时日志 tmp_path=/tmp/${log_path}.tmp #访问最高的IP记录 top_ip_path=/tmp/top_ip.log #IP归属地查询 ip_where_path=/tmp/ip_where.log #响应时间排名 nginx_time_path=/tmp/nginx_time.log #平均响应时间 avg_time_path=/tmp/avg_time.log #pv数量 pv_num_path=/tmp/pv_num.log #服务器IP地址 server_ip=$(hostname -I) #服务器名为 server_name=$(cat /etc/hostname) #搜索关键词 key_word_bbs="GET /thread-" key_word_m="GET /bbs/thread-" key_word_mall="GET /mall/list" #输入帮助 usage(){ echo "执行格式为:\nsh $0 \"18:00\" \"22:00\" bbs.vim26.com_access.log" } #查询服务器的信息 server_info(){ echo "==============================" echo "服务器名:${server_name}" echo "服务器IP:${server_ip}" echo "查询日志为:${log_path}" echo "查询时间为:${input_log_time1} - ${input_log_time2}" echo "==============================" } #提取行数并生成临时日志 time_line(){ line1=$(grep -n "${log_time1}" ${log_path}|head -1|awk -F":" '{print $1}') line2=$(grep -n "${log_time2}" ${log_path}|head -1|awk -F":" '{print $1}') sed -n "${line1},${line2}p" $log_path > ${tmp_path} } #PV数 pv_num(){ awk '{print $1}' ${tmp_path}|wc -l > ${pv_num_path} echo " PV数量为: $(cat ${pv_num_path})" echo "==============================" } #最多IP top_ip(){ awk '{print $1}' ${tmp_path}|sort|uniq -c|sort -n|tail -10 > ${top_ip_path} echo "访问最多IP前10为:" cat ${top_ip_path} echo "==============================" } #IP归属地查询 ip_where(){ > ${ip_where_path} for i in $(cat ${top_ip_path}|awk '{print $2}') do ip_where_host=$(curl -s "http://ip138.com/ips138.asp?ip=${i}&action=2"|iconv -f gb2312 -t utf-8|grep '<ul class="ul1"><li>'|awk -F "[:<>]+" '{print $5}'|cut -c 15-) echo "$i $ip_where_host" >> ${ip_where_path} done echo "==============================" echo "IP归属地查询结果为:" cat -n ${ip_where_path} } #日志查询 log_info(){ [ "${log_path}" = "bbs.vim26.com_access.log" ] && key_word=${key_word_bbs} [ "${log_path}" = "m.vim26.com_access.log" ] && key_word=${key_word_m} [ "${log_path}" = "mall.vim26.com_access.log" ] && key_word=${key_word_mall} #监控关键词 echo "监控关键链接为:${key_word}" echo "==============================" #关键词PV次数 key_word_pv=$(grep "${key_word}" ${tmp_path}|wc -l) echo "关键链接PV访问次数: ${key_word_pv}" echo "==============================" #关键词PV次数 #关键词平均响应时间 grep "${key_word}" ${tmp_path}|awk '{sum+=$NF} END {print sum/NR}' > ${avg_time_path} echo "关键链接平均响应时间为: $(cat ${avg_time_path})" echo "==============================" #关键词访问响应时间排名 echo "关键链接访问响应时间排名" grep "${key_word}" ${tmp_path}|awk '{printf ("%.1f\n",$NF)}'|sort -nk 1 -t.|uniq -c> ${nginx_time_path} cat ${nginx_time_path} echo "==============================" } #搜索引擎搜索情况 bot_search(){ echo "搜索情况汇总" bot_all=$(egrep "bot|spider|brandwatch" ${tmp_path}|wc -l) bot_baidu=$(grep "Baiduspider" ${tmp_path}| wc -l) bot_bing=$(grep "bingbot" ${tmp_path}| wc -l) bot_Google=$(grep "Googlebot" ${tmp_path}| wc -l) bot_sougou=$(grep "Sogou web spider" ${tmp_path}| wc -l) bot_toutiao=$(grep "ToutiaoSpider" ${tmp_path}| wc -l) bot_brandwatch=$(grep "brandwatch" ${tmp_path}| wc -l) echo "搜索引擎总计访问次数" ${bot_all} echo " Baidu访问次数: ${bot_baidu}" echo " bing访问次数: ${bot_bing}" echo " Google访问次数: ${bot_Google}" echo " sougou访问次数: ${bot_sougou}" echo " toutiao访问次数: ${bot_toutiao}" echo " brandwatch访问次数: ${bot_brandwatch}" echo "==============================" } #bbs的执行动作 all_info(){ time_line server_info pv_num time_line bot_search top_ip log_info #ip_where } #判断参数是否正确 [ $# != 3 ] && { usage exit 1; } all_info ```