俺ã¨ãã¾ãã¨awk
ãEFK (Elasticsearch + Fluentd + Kibana) ãªãã¦çãã§ãããæ¼¢ã¯é»ã£ã¦ awk | sort | uniq -c ã§ãããã¨èª°ããè¨ã£ã¦ããããªè¨ã£ã¦ãªãã£ããããªæ°ãããã®ã§ãã°ãããæã«èªåããã使ã awk è¸ã«ã¤ãã¦æ¸ãã¾ãã
æ³å®ãã¼ã¿ãµã³ãã«
ãããªãã©ã¼ãããã§åºã TSV å½¢å¼ã® Web ã¢ããªã±ã¼ã·ã§ã³ãã°ããã£ãã¨ãã¾ãã[TAB] ã¯ã¿ãæåã§ãã
æé[TAB]ã¹ãã¼ã¿ã¹[TAB]HTTPã¡ã½ãã[TAB]URI[TAB]ãªã¯ã¨ã¹ãã¿ã¤ã
ä¾ãã°ãããªæãã§ãããã®ãã°ã awk è¸ã§å¦çãã¦ããã¾ãã
access.log
2014-12-05 12:00:00[TAB]200[TAB]GET[TAB]/api/v1/ping[TAB]0.017832 2014-12-05 12:00:01[TAB]200[TAB]POST[TAB]/api/v1/auth[TAB]1.001628 2014-12-05 12:10:00[TAB]404[TAB]GET[TAB]/favicon.ico[TAB]0.017832 2014-12-05 12:10:01[TAB]500[TAB]POST[TAB]/api/v1/login[TAB]5.00003
ï¼åãããã¢ã¯ã»ã¹éã調ã¹ã
ï¼åãããã®ã¢ã¯ã»ã¹éã¯ãããªãããã§èª¿ã¹ãããã§ãããã
$ awk '{print $2}' access.log | awk -F: '{print $1 ":" $2}' | sort | uniq -c 2 12:00 2 12:10
解説ãã¦ããã¨ãæåã® awk ã§æå»ãã£ã¼ã«ããæãåºãã¦ãã¾ãã
$ awk '{print $2}' access.log 12:00:00 12:00:01 12:10:00 12:10:01
ããã©ã«ãã®ç©ºç½æåãã¿ãæåãåºåãæåã¨ãã¦ã
{print $2}
ã®ããã«ãã¦ç¬¬2ãã£ã¼ã«ãã print ãã¦ãã¾ããä»åã®å ´åã¯æå»ã®ãã£ã¼ã«ããåºåããã¾ãã
ãã¤ãã§ã¤ãªãã次㮠awk ã§ãæ:å:ç§ãããæ:åãã«å¤æãã¦ãã¾ãã -F ãªãã·ã§ã³ã使ã£ã¦åºåãæåã : ã«å¤æ´ãã第ï¼ãã£ã¼ã«ã(æ)ã:ã第ï¼ãã£ã¼ã«ã(å) ã print ãã¦ãã¾ãã
$ awk '{print $2}' access.log | awk -F: '{print $1 ":" $2}' 12:00 12:00 12:10 12:10
ãã¨ã¯æ £ç¨å¥ã¨è¨ã£ã¦ãè¯ã sort | uniq -c ã«æ¸¡ãã¦ãã¦ãã¼ã¯ãªå¤ã®æ°ãã«ã¦ã³ããã¦åºåãã¦ãã¾ãã
$ awk '{print $2}' access.log | awk -F: '{print $1 ":" $2}' | sort | uniq -c 2 12:00 2 12:10
ã¡ãªã¿ã«ãä¸ã®ä¾ã§ã¯ awk ãï¼å使ãã¾ããããawk ã«ã¯ substr
é¢æ°ãªããããã£ããããã®ã§ã以ä¸ã®ããã«ããã¨ï¼çºã§æ¸ãããããã¾ãã
使ãæ¨ã¦ã¯ã³ã©ã¤ãã¼ãªã®ã§ãè¦ããããæ¹ã使ãã°è¯ããã¨æãã¾ãã
$ awk '{print substr($2,1,5)}' access.log 12:00 12:00 12:10 12:10
1ç§ä»¥ä¸æéãããã£ã¦ããã¢ã¯ã»ã¹ããªã¹ãã¢ãããã
ä»åã®ãã°ãã©ã¼ãããã§ã¯ãä¸çªæå¾ã®ãã£ã¼ã«ãããªã¯ã¨ã¹ãã¿ã¤ã ã§ããã ä¸çªæå¾ã®ãã£ã¼ã«ã㯠$NF ã§æå®ãããã¨ãã§ãã¾ãã
$ awk '{print $NF}' access.log 0.017832 1.001628 0.017832 5.00003
ã§ããã®ãã£ã¼ã«ãã«æ¡ä»¶ãæå®ãã¦ã1ç§ä»¥ä¸æéãããã£ã¦ããè¡ã®ã¿ã«çµãæ··ãã§ã¿ã¾ãã次ã®ããã«ãã¾ãã
$ awk '$NF > 1.0' access.log 2014-12-05 12:00:01 200 POST /api/v1/auth 1.001628 2014-12-05 12:10:01 500 POST /api/v1/login 5.00003
ããã¯ä»¥ä¸ã®ã³ãã³ãã¨åçã§ãã
{}
ãçç¥ããå ´åãããã©ã«ã㧠{print $0}
(å
¨ãã£ã¼ã«ãã®è¡¨ç¤º) ã«ãªãã¾ãã
$ awk '$NF > 1.0 {print $0}' access.log 2014-12-05 12:00:01 200 POST /api/v1/auth 1.001628 2014-12-05 12:10:01 500 POST /api/v1/login 5.00003
ãã¾ãï¼ä¸çªæå¾ããï¼ååã®ãã£ã¼ã«ã㯠$(NF-1)
ã§æå®ãããã¨ãã§ãã¾ãã
ï¼ååãªã $(NF-2)
ã§ãããä¾ãã°ããããªãããã§ã¤ããã¾ãã
$ awk '$NF > 1.0 {print $(NF-1)}' access.log /api/v1/auth /api/v1/login
5xx ã¹ãã¼ã¿ã¹ã³ã¼ãã®ã¢ã¯ã»ã¹ããªã¹ãã¢ãããã
ã¹ãã¼ã¿ã¹ã³ã¼ã 500 ã®è¡ãæãåºãã«ã¯æ¬¡ã®ããã«ããã°ããã§ãããã
$ awk '$3 == 500' access.log 2014-12-05 12:10:01 500 POST /api/v1/login 5.00003
æ£è¦è¡¨ç¾ã使ããã¨ãã§ãã¦ã5xx ãªè¡ãæãåºãããå ´åã«ã¯æ¬¡ã®ããã«ããã°ããã§ãã
$ awk '$3 ~ /^5/' access.log 2014-12-05 12:10:01 500 POST /api/v1/login 5.00003
ãã¡ãã§ãã{print ãã£ã¼ã«ãçªå·}
ã¨ããã°æå®ãããã£ã¼ã«ãã ãåºåããããã¨ãã§ãã¾ãã
$ awk '$3 ~ /^5/ {print $5}' access.log /api/v1/login
ãã¾ãï¼æ£è¦è¡¨ç¾ãããã¯ãã£ã¼ã«ããæå®ããªãå ´åãè¡å
¨ä½($0) ã«å¯¾ããæ£è¦è¡¨ç¾ããã($0 ~ /æ£è¦è¡¨ç¾/
)ã¨ãªãã®ã§ã以ä¸ã®ããã«æ¸ã㨠grep ã®ãããªå¹æãåºãã¾ãã
$ awk '/500/' access.log 2014-12-05 12:10:01 500 POST /api/v1/login 5.00003
åè¨ãªã¯ã¨ã¹ãã¿ã¤ã ãæ±ãã
ã¢ã¯ã»ã¹ãã°ã®ãã¡åè¨ãªã¯ã¨ã¹ãã¿ã¤ã ãæ±ããã«ã¯ãã¡ãã£ã¨ããã©ãããã§ããã次ã®ããã«ãã¾ãã
$ awk 'BEGIN{sum=0}{sum+=$NF}END{print sum}' access.log 6.03732
å¤æ°ã¯ããã©ã«ã㧠0 ã«åæåãããã®ã§ãBEGIN{sum=0}
ã¯çç¥å¯è½ã§ãã
æ大å¤ãæ±ãããå ´åã¯ã
$ awk '{if($NF > max)max=$NF}END{print max}' access.log 5.00003
ã¨ãã§ããããããã©ãããã§ãããã¾ããããªããã§ããã
LTSV ãªãã°ãå¦çãã
æè¿ã¯ LTSV ãã©ã¼ãããã人æ°ã§ãããã©ãã«:å¤
ã®ãã£ã¼ã«ããåé¢ã㦠å¤
ã ããåãåºãã TSV å½¢å¼ã«å¤æããªã㨠awk ã§ã¯æ±ãã¥ããã§ããä¾ãã°ã次ã®ãã㪠LTSV ãã°ããã£ãã¨ãã¦ã
time:2014-08-13T14:10:10Z[TAB]status:200 time:2014-08-13T14:10:12Z[TAB]status:500
status ãã£ã¼ã«ãã®å¤ã ãã awk ã§æãåºããã¨ããã¨ãããªãããã«ãªãã§ããããã é¢åã§ããã
$ awk -F\t '{split($2, status, ":"); print status[2]}' ltsv.log 200 500
awk ã§ãæ±ããããããããã«çãããè²ã 試è¡é¯èª¤ããã¦ããããã§ãã
- awkã¨ã·ã§ã«ã§LTSVã®åãæ±ããç°¡åã«ãããã£ã«ã¿ãæ¸ãã¦ã¿ã
- LTSV ã®ãã°ã jq ã§ãã£ã«ã¿ãã
- lltsv ã¨ãã LTSV ã®ç¹å®ãã¼ã ãåãåºã golang ã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã
ãã¡ãã® lltsv ã¨ãããã¼ã«ã使ãã¨
$ lltsv -k time,status -K ltsv.log 2014-08-13T14:10:10Z 200 2014-08-13T14:10:12Z 500
ã®ããã«å¤ãåãåºãäºãã§ããã®ã§ããã®ã¾ã¾ awk ã«ã¤ãªãã¦
$ lltsv -k time,status -K ltsv.log | awk '$2 == 500' 2014-08-13T14:10:12Z 500
ã®ããã«æ±ããããã§ãã便å©ã£ã½ãã§ããã
ãããã«
ããã¼ãawk ãµã¤ã³ã¼ã§ããããã°ãææã¡ã§ awk ã³ãã³ãæã£ã¦éè¨ãããªãã¦å¹¸ãããã¦æ³£ãã¦ãã¾ãããEnjoy happy awk life!
追è¨: awk è¸ã®é«ã¿ã¸
ãçµ±è¨å±ã®ããã®AWKå ¥éãã®è¨äºã大å¤åèã«ãªãã®ã§ç©è¶³ããªãæ¹ã¯èªãã¨è¯ãã¨æãã¾ããï¼