|
Page 2 of 2 Experts state that “1.0 second is about the limit for the user’s flow of thought to stay uninterrupted, even though the user will notice the delay.” That log file is only 12MB in size and there is a different in speed which you can notice at the terminal. Imagine if the log is 300MB? Awk also has extremely accessible associative arrays. Here I use an array to count HTTP response codes: $ awk '{counts[$(NF-1)]+=1}; END {for(code in counts) print code, counts[code]}' \ access_log-2008-01 206 177 301 1212 302 302 304 5051 403 5 200 82539 404 906 405 1 500 183 The previous command in English says: for each line, using the second to last field as our index, increment our array. Once we have proccessed all lines, loop through the array assigning “code” to the array index. Lets count the number of requests for each URL: $ awk '{counts[$(NF-3)]+=1}; END {for(url in counts) print counts[url], url}' \ access_log-2008-01 | sort -n ...output removed... 796 /media/centos5.0_install/common/AA-bios.jpg 846 /robots.txt 1063 /media/misc/why-bad-interpreter-premature-end-of-script-headers.png 1425 /media/10-linux-commands-youve-never-used/mkfifo-write-to-pipe.png 1443 /media/10-linux-commands-youve-never-used/read-from-pipe.png 1629 / 2066 /feed/ 3073 /10-linux-commands-youve-never-used.html 3909 /wp2.3/wp-content/themes/minn-01/style.css 6989 /favicon.ico Now lets sum the responses sizes each URL and display it in MB: $ awk '{sizes[$(NF-3)]+=$NF}; END {for(url in sizes) print (sizes[url]/1024/1024) "MB", url}' \ access_log-2008-01 | sort -n ...output removed... 68.6784MB /media/centos5.0_install/gui_common/AQ-install-in-progress-3.png 72.0453MB /media/centos5.0_install/gui_common/AP-install-in-progress-2.png 74.0067MB /media/centos5.0_install/gui_common/AT-setup-agent-welcome.png 74.6089MB /media/centos5.0_install/gui_common/AV-setup-agent-firewall-r-u-sure.png 78.2652MB /media/centos5.0_install/gui_common/BA-setup-agent-sound-card.png 80.3148MB /media/centos5.0_install/gui_common/AG-bootloader-configuration.png 85.8359MB /media/centos5.0_install/gui_common/AI-set-timezone.png 101.836MB /media/centos_4.4_boot.iso 137.622MB / 263.253MB /media/centos_5.0_boot.iso Lets do the same for IP addresses: $ awk '{counts[$1]+=1}; END {for(ip in counts) print counts[ip], ip}' \ access_log-2008-01 | sort -n ...output removed... 378 67.202.20.7 402 65.214.45.100 476 195.225.177.39 493 87.207.147.201 702 66.150.96.121 704 213.239.195.172 968 82.150.18.3 1335 65.28.61.246 2330 66.249.73.75 2883 71.63.249.40
$ awk '{sizes[$1]+=$NF}; END {for(ip in sizes) print (sizes[ip]/1024/1024) "MB", ip}' \ access_log-2008-01 | sort -n ...output removed... 20.9338MB 61.64.209.144 21.8517MB 116.71.182.210 23.4265MB 85.102.126.48 31.5194MB 213.239.195.172 32.732MB 67.176.123.158 37.9046MB 66.249.73.75 56.1901MB 71.63.249.40 57.9892MB 67.202.20.7 78.6117MB 65.28.61.246 Sum the size of all responses by ip address if the response code is 200: $ awk '$(NF-1) == 200 {sizes[$1]+=$NF}; END {for(ip in sizes) print (sizes[ip]/1024/1024) "MB", ip}' \ access_log-2008-01 | sort -n ...output removed... 16.5405MB 220.181.38.245 16.7031MB 207.67.117.178 16.7661MB 128.227.0.66 16.9171MB 67.176.123.158 18.2246MB 71.72.54.173 31.5194MB 213.239.195.172 37.3774MB 66.249.73.75 53.6944MB 71.63.249.40 57.9885MB 67.202.20.7 76.9965MB 65.28.61.246 The command in English: for each line, if the response code is 200 ($(NF-1)), then increment our array at index ip address ($1), by response size ($NF).
|