写一个完整的脚本(shell/python),统计各个 IP 错误日志([error])的次数并按错误数由高到低输出统计情况
上面例子log 的输出结果如下:

IP                       error_cnt
118.124.94.110      3
118.124.94.173      2
118.124.94.27        1
118.124.94.75        1
118.124.94.29        0

日志内容为:

2019/03/12 21:23:25 [error] client: 118.124.94.110 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=4e8227b63&need_update=0 HTTP/1.1", host: "www.minerbabe.com"
2019/03/12 21:23:25 [error] client: 118.124.94.110 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=4e8227b63&need_update=0 HTTP/1.1", host: "www.minerbabe.com"
2019/03/12 21:23:25 [info] client: 118.124.94.110 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=4e8227b63&need_update=0 HTTP/1.1", host: "www.minerbabe.com"
2019/03/12 21:23:25 [error] client: 118.124.94.110 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=4e8227b63&need_update=0 HTTP/1.1", host: "www.minerbabe.com"
2019/03/12 21:23:25 [error] client: 118.124.94.173 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=2d20b53c5&need_update=0 HTTP/1.1", host: "www.minerbabe.com"
2019/03/12 21:23:25 [error] client: 118.124.94.173 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=2d20b53c5&need_update=0 HTTP/1.1", host: "www.minerbabe.com"
2019/03/12 21:23:25 [error] client: 118.124.94.27 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=fa7c727f0&need_update=0 HTTP/1.1", host: "www.minerbabe.com"
2019/03/12 21:23:25 [error] client: 118.124.94.75 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=e7c5fbd34&need_update=0 HTTP/1.1", host: "www.minerbabe.com"
2019/03/12 21:23:25 [info] client: 118.124.94.29 server: www.minerbabe.com, request: "GET
/keys/ssh/pb?device_id=d4b3050af&need_update=0 HTTP/1.1", host: "www.minerbabe.com"

 

使用shell实现这一功能


#!/bin/bash
function x(){
  IP=`awk '{print $5}'  log|sort|uniq`
  for i in $IP
    do
     cnt=`cat log|grep $i|grep error|wc -l`
     echo "$i $cnt" 
    done
}
x|sort -rk2 | awk 'BEGIN{printf "IP\t\t\terror_cnt\n"}{printf $1"\t\t"$2"\n"}'

打印结果:

IP               error_cnp
 10
IP            error_cnt
118.124.94.110        3
118.124.94.173        2
118.124.94.75        1
118.124.94.27        1
118.124.94.29        0