awkã¨ã·ã§ã«ã§LTSVã®åãæ±ããç°¡åã«ãããã£ã«ã¿ãæ¸ãã¦ã¿ã
æè¿ãLTSVããæ°ã«å ¥ãã§Apacheã®ãã°å§ãå種ãã°ãLTSVã«ãã¦ãã¾ãã
LTSVã®ä½ã便å©ã£ã¦ãæ®éã®CSVãã¨ãã¨éã£ã¦ããã®åã®éãã©ãã«ãä»ãã¦ãã®ã§é çªã§ã¯ç¡ãååãã¼ã¹ã§ç°¡åã«ã¢ã¯ã»ã¹ã§ãããã¨ã
ããããé çªã§ã¯ç¡ãã®ã§ãå¾ããé ç®ã¨ãã追å ã¨ãå ¥ãæ¿ããããããã¦ãéè¨ã¹ã¯ãªããã¨ãã¨é常ã«ç¸æ§ãè¯ããã¨ã§ãã
Â
ãã ãæ¥æ¬¡ã§éè¨ãã¦ã°ã©ãåãããã ã¨ããDBã«å ¥ãããã¨ãããããã£ãã·ã¹ãã åããå®å¸¸çãªè§£æã¯Rubyãªãã§æ¸ãã°ããã®ã§åé¡ãªãã®ã§ãããé害対å¿ãã¡ãã£ã¨ããæãã¤ããªããã§ãawkã§ã¯ã³ã©ã¤ãã¼ãªåæ/éè¨ããã¡ããå ´åã«ã¯LTSVã¯ä¸ä¾¿ã ã£ãããã¾ãã
Â
ãã¨ãã°ãä¸è¨ã®ãããªLTSVãã¼ã¹ã®ãã°ãããã¨ãã¾ãã
time:[08/Feb/2013:14:17:53 +0900] req:/ response:100 time:[08/Feb/2013:14:17:54 +0900] req:/test1 response:1 time:[08/Feb/2013:14:17:55 +0900] req:/test2 response:2 time:[08/Feb/2013:14:17:56 +0900] req:/test1 response:10
ä¸è¨ã®ãã°ã®test1ã ãã®å¹³åã¬ã¹ãã³ã¹ãããã£ã¨æ±ããããã¨ãã¯ããããã±ã¼ã¹ã§ããã
æ®éã«awkã§æ¸ãã¨ãããªæãã
% cat access.log| awk -F'\t' '$2 == "req:/test1" {sum += (substr($3,10));cnt++} END{print sum/cnt}'
ã«ã©ã çªå·ãæèããå¿ è¦ããã£ã¦ããããLTSV使ã£ã¦ãå¬ããããªãã§ãï¼_ï¼ã
ã¨ãããããããªãCSVãTSVã®ã»ããã¾ã 楽ã§ãããsubstrããããªãã®ã§ã
Â
ãã®åé¡ã解決ããã¹ããç°¡åãªawkã¨ã·ã§ã«ã§ltsv.shã¨ãããã£ã«ã¿ãæ¸ãã¦ã¿ã¾ããã
ããã使ãã¨å ã»ã©ã®ã³ã¼ãã¯ä¸è¨ã®ããã«æ¸ãã¾ãã
% cat access.log| ltsv.sh 'ltsv("req") == "/test1"{sum += ltsv("response");cnt++};END{print sum/cnt}'
è¦ã¦åããéããawkããã®ã¾ã¾æ¸ããã®ã§ãããååãã¼ã¹ã§ã«ã©ã ã«ã¢ã¯ã»ã¹ãã¦ãããã¨ãåããã¾ãã
å®éã®æ¯ãèãã¨ãã¦ã¯ãltsvã¨ããé¢æ°ã追å ãã¦LTSVãé£æ³é åã«å¤æãã¦ããããã§ãã
ãã£ãããã ãã§ããã¡ããã¡ãå¯èªæ§ããããã¾ããï¼
Â
ãã¡ããããã¤ãã§ç¹ããã¨ãå¯è½ã§ããããã¾ãè¯ãããnç§ä»¥ä¸ã®ãªã¯ã¨ã¹ãã®ã«ã¦ã³ãã¯ãããªæãã
% cat access.log| ltsv.sh 'int(ltsv("response")) > 2{print $0}'|wc -l
ã¤ãã¤ï¼ æãªãã便å©ã ï¼ ã¡ãªã¿ã«intã§ãã£ã¹ãããªãã¨æååãªã®ã§æ£ããæ°å¤æ¯è¼ã§ããªãã§ãã
Â
ãã¡ãã®å®è£ ã¯é常ã«ã·ã³ãã«ã§ãä¸è¨ã®ããã«ãªãã¾ãã
This is AWK wrapper to parse LTSV
Â
è¦ãã¾ãã¾åã«ltsvã¨ããé¢æ°ãå®ç¾©ãã¦æååã¨ãã¦ãã£ã¤ããã ãã§ãï½
Bashã®ãã¢ããã¥ã¡ã³ãã§å®ç¾©ã§ããã®ã§ãæ¯åawkã«ãã®é¢æ°ãæã§é©ç¨ãããããããªãã®ã§ãã¨ã¦ãç°¡åã§ãã
ããã¯includeçãªãã¨ããã¦ãããã§ãããawkã§ã©ãããã°è¯ãã®ãããããªãã®ã§ããããªåæãªæ¹æ³ã«ãªã£ã¦ãã¾ãã¾ããã
ã¨ãããããåãã®ã§ããã§ããã¨ã¬ã¬ã³ããããªãã®ã§ã
ä»ã«è¯ãæ¹æ³ãããã°èª°ãæãã¦ä¸ããï¼_ï¼
Â
ãªãã«ãã¦ããããã§èªåçã«ã¯ltsvã§å°ããã¨ãã»ã¼ãªããªã£ãã®ã§ãããå©ç¨ãä¿é²ãã¦è¡ãããã¨ãããã¾ãã
Â
ããã§ã¯Happy Hacking!
Â
åèURL: