运维工作是一个比较复杂的工作,有时候面对上万条的日志,如何作分析?难道一条条的分析?
引用>
聪明的人会选择脚本,这就是为什么现在提倡自动化运维的原因吧,废话不多说,直接上脚本。<代码> vim/数据/脚本/log_analysis.sh # !/bin/bash ############################################### # Desc: nginx日志分析脚本# #作者:Bertram # 日期:2019-12-21 # 个人是# #版权: ############################################### 公共(){ 回声”“ 阅读- p“请输入要分析的访问日志:“log_file 回声”“ 如果[!- f $ log_file];然后 回声”未找到:$ {log_file}” 出口1 fi 如果[!- s $ log_file];然后 echo " $ {log_file}是空文件” 出口1 fi #输出日志访问量排名前top_num条数据,可自定义 top_num=5 input_file=' echo $ log_file | awk - f”/皗打印$ (NF)} ' ' analyze_dir=/home/Bertram/叭掌? % F ' top_ip_file=$ analyze_dir/ngx_log_top_ip_ $ {input_file} . txt top_src_url_file=$ analyze_dir/ngx_log_top_src_url_ $ {input_file} . txt top_dest_url_file=$ analyze_dir/ngx_log_top_dest_url_ $ {input_file} . txt top_code_file=$ analyze_dir/ngx_log_top_code_ $ {input_file} . txt top_terminal_file=$ analyze_dir/ngx_log_top_terminal_ $ {input_file} . txt mkdir - p analyze_dir美元 start_time='头1美元log_file | awk的{打印4美元}|削减- d”(“- f2 尾巴end_time=' 1美元log_file | awk的{打印4美元}|削减- d”(“- f2 total_nums=' wc - l $ log_file | awk的{打印1美元}' ' 杜大?' sh log_file美元| awk的{打印1美元}' ' #获取起始与截止时间 回声”访问起始时间:$ start_time;截止时间:$ end_time” #获取总行数与大小 回声”共访问美元total_nums次;日志大小:$规模” #获取最活跃IP # #猫log_file美元| awk}{打印1美元的排序| | uniq排序rn - c | |头- $ {top_num}祝辞top_ip_file美元 awk的{ips(1元)+ +}结束为ips(我){{打印ips(我),我}}“美元log_file排序| | uniq - c |排序k1 nr - $ {top_num} |头比;top_ip_file美元 #获取访问来源最多的url 猫log_file美元| awk的{打印$ 13}| |排序uniq排序rn - c | |头- $ {top_num}祝辞top_src_url_file美元 #获取请求最多的url 猫log_file美元| awk的{打印8美元}| |排序uniq排序rn - c | |头- $ {top_num}祝辞top_dest_url_file美元 #获取返回最多的状态码 猫log_file美元| awk的{打印$ 11}| |排序uniq排序rn - c | |头- $ {top_num}祝辞top_code_file美元 #获取返回最多的终端类型 猫log_file美元| awk的{打印14美元}| |排序uniq排序rn - c | |头- $ {top_num}祝辞top_terminal_file美元 } 简单(){ 回声“+ - + - + - + - + - + -下面是分析内容+ - + - + - + - + - + -” #获取最活跃IP printf“最活跃的前$ {top_num}个访问IP: \ n” 猫top_ip_file美元 回声”“ #获取访问来源最多的url printf“访问来源最多的前$ {top_num}个url: \ n” 猫top_src_url_file美元 回声”“ #获取请求最多的url printf“请求最多的前$ {top_num}个url: \ n” 猫top_dest_url_file美元 回声”“ #获取返回最多的状态码 printf“返回最多的前$ {top_num}个状态码:\ n” 猫top_code_file美元 回声”“ printf“ #获取返回最多的终端号 printf“返回最多的前$ {top_num}个终端号:\ n” 猫top_terminal_file美元 回声”“ printf“ printf“返回最多的前$ {top_num}个IP所属城市(查询时间有点慢,耐心等待!):\ n” 回声” printf -30年代-15年代-15年代“% % % \ n”“访问次数”“IP地址”“归属地” 回声”- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -” 一个=0 猫analyze_dir美元/ngx_log_top_ip_ $ {input_file}。txt |而读 做 ip |=$ (echo $行切- d '“' - f2) 数=(echo线|削减美元- d '“f1) printf -30年代-15年代-10年代“% % % \ n”数美元ip (curl - s”http://freeapi.ipip.net/(echo $美元线|切- d”- f2)”| awk - f”\“{”打印2美元”——“4”——“6美元的}) 回声”- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -” 让一个=a + 1 完成 回声”“ printf“ } 案例1美元 帮助) 回声”“ echo - e”用法:美元0输入日志文件\ n” ;; *) 公共 简单的 ;; esac 退出0Nginx日志分析脚本