Rack of Ethernet switches.

Visualizing Log Patterns with Color

Apache Logs in Color

Web server logs reveal patterns of activity by web crawlers. Some are indexing crawlers operated by search engines, some are mysterious. Another pattern is systematic blind searches for vulnerable server-side executables or other configuration problems. The pattern you want to see is the interested user who follows some path through the hyperlinks on your site, taking time to read the pages.

Maybe we could use color to help spot these patterns?

Maybe...

First, let's look at the result, then the explanation comes later. Here are the most recent client requests, starting most recent first. Your request for this page won't appear there as it isn't complete by the time this page was automatically generated with PHP. But if you reload the page you should see your initial request near the top.

100.24.209.47 US, United States 21/Aug/2019:02:32:21 /travel/usa/new-york-roosevelts/Index.html
100.24.209.47 US, United States 21/Aug/2019:02:32:15 /cybersecurity/cloud-survey.html
100.24.209.47 US, United States 21/Aug/2019:02:32:15 /robots.txt
66.249.79.151 US, United States 21/Aug/2019:02:32:02 /radio/j-poles.html?s=tweetbot
65.128.37.150 US, United States 21/Aug/2019:02:31:52 /open-source/raspberry-pi/sdr-ads-b-flight-tracking.html
74.207.241.229 US, United States 21/Aug/2019:02:31:28 /travel/syria/damascus.html?s=tweetbot
74.207.241.229 US, United States 21/Aug/2019:02:31:18 /travel/italy/umbria/?s=tweetbot
66.249.79.151 US, United States 21/Aug/2019:02:30:37 /travel/usa/new-york-sro-flophouses/
66.249.79.151 US, United States 21/Aug/2019:02:30:36 /robots.txt
216.244.66.242 US, United States 21/Aug/2019:02:30:24 /robots.txt
54.36.148.67 FR, France 21/Aug/2019:02:29:52 /travel/italy/cinque-terre/
37.187.165.31 FR, France 21/Aug/2019:02:29:28 /travel/estonia/?s=tb
72.203.107.40 US, United States 21/Aug/2019:02:29:24 /travel/belgium/bastogne-ardennes/
70.73.147.179 CA, Canada 21/Aug/2019:02:29:14 /radio/j-poles.html
66.249.79.153 US, United States 21/Aug/2019:02:29:10 /travel/usa/new-york-internet/
116.202.33.238 IN, India 21/Aug/2019:02:28:40 /travel/estonia/?s=tb
116.202.33.238 IN, India 21/Aug/2019:02:28:40 /robots.txt
54.162.105.137 US, United States 21/Aug/2019:02:28:34 /travel/estonia/?s=tb
41.46.183.12 EG, Egypt 21/Aug/2019:02:28:33 /turkish/verbs.html
34.227.76.78 US, United States 21/Aug/2019:02:28:32 /radio/tv-antenna.html
104.218.65.209 US, United States 21/Aug/2019:02:28:30 /radio/tv-antenna.html
41.46.183.12 EG, Egypt 21/Aug/2019:02:28:24 /turkish/verbs.html
94.130.167.109 DE, Germany 21/Aug/2019:02:28:19 /travel/estonia/?s=tb
3.217.157.17 US, United States 21/Aug/2019:02:28:16 /travel/estonia/?s=tb
107.178.200.188 US, United States 21/Aug/2019:02:28:06 /travel/estonia/?s=tb
17.58.101.135 US, United States 21/Aug/2019:02:28:01 /travel/estonia/?s=tb
66.249.79.153 US, United States 21/Aug/2019:02:27:44 /travel/turkey/nemrut-dagi/
72.203.107.40 US, United States 21/Aug/2019:02:27:26 /travel/belgium/bastogne-ardennes/
122.46.209.59 KR, Korea, Republic of 21/Aug/2019:02:27:15 /travel/japan/kofun/ishibutai.html
130.56.46.84 AU, Australia 21/Aug/2019:02:26:47 /open-source/pdf-not-authorized.html
73.171.122.13 US, United States 21/Aug/2019:02:26:44 /travel/usa/detroit/
3.93.238.32 US, United States 21/Aug/2019:02:26:41 /travel/japan/kofun/ishibutai.html
72.21.217.84 US, United States 21/Aug/2019:02:26:38 /robots.txt
122.46.209.59 KR, Korea, Republic of 21/Aug/2019:02:26:32 /travel/japan/kofun/ishibutai.html
66.249.79.155 US, United States 21/Aug/2019:02:26:18 /travel/france/marseille/
18.233.194.247 US, United States 21/Aug/2019:02:26:00 /turkish/word-order.html
18.233.194.247 US, United States 21/Aug/2019:02:25:59 /robots.txt
41.46.183.12 EG, Egypt 21/Aug/2019:02:25:57 /turkish/word-order.html
18.233.194.247 US, United States 21/Aug/2019:02:25:50 /robots.txt
212.129.11.42 FR, France 21/Aug/2019:02:25:50 /travel/uk/orkney-neolithic/?s=tb
41.46.183.12 EG, Egypt 21/Aug/2019:02:25:27 /turkish/verbs.html
77.75.73.26 CZ, Czech Republic 21/Aug/2019:02:25:20 /robots.txt
181.56.118.68 CO, Colombia 21/Aug/2019:02:25:06 /radio/ic-3220.html
66.249.79.155 US, United States 21/Aug/2019:02:24:52 /open-source/raspberry-pi/ads-b-antenna.html
199.16.157.182 US, United States 21/Aug/2019:02:24:20 /travel/japan/kyoto/nishiki-koji-dori/?s=tweetbot
46.229.168.131 US, United States 21/Aug/2019:02:24:09 /networking/
66.249.79.153 US, United States 21/Aug/2019:02:23:26 /travel/gsm-sim-card/
74.207.241.229 US, United States 21/Aug/2019:02:23:02 /travel/greece/greek/?s=tweetbot
46.229.168.151 US, United States 21/Aug/2019:02:22:32 /fun/brilliant-movie-ideas/unholy-unions.html
5.255.250.17 US, United States 21/Aug/2019:02:22:24 /open-source/google-freebsd-tls/apache-log-cache.html
66.249.79.155 US, United States 21/Aug/2019:02:22:00 /radio/ic2sat-part-2.html
154.207.3.154 ZA, South Africa 21/Aug/2019:02:21:52 /open-source/openbsd-qemu-windows-howto.html
5.255.250.17 US, United States 21/Aug/2019:02:21:36 /open-source/performance-tuning/ethernet.html
74.207.241.229 US, United States 21/Aug/2019:02:21:35 /travel/uk/edinburgh/?s=tweetbot
59.46.50.4 CN, China 21/Aug/2019:02:21:15 /open-source/performance-tuning/ethernet.html
195.69.170.39 RU, Russian Federation 21/Aug/2019:02:20:59 /technical/pal-to-ntsc.html
66.249.79.151 US, United States 21/Aug/2019:02:20:34 /travel/belgium/bastogne-ardennes/background.html
40.77.167.41 US, United States 21/Aug/2019:02:20:09 /cybersecurity/hostile/bagel.html
174.219.8.240 US, United States 21/Aug/2019:02:19:47 /turkish/turkish-verbs.pdf
54.88.222.30 US, United States 21/Aug/2019:02:19:10 /travel/france/thomas-jefferson/
66.249.79.155 US, United States 21/Aug/2019:02:19:08 /radio/tek2445a.html
184.18.51.43 US, United States 21/Aug/2019:02:18:47 /travel/france/thomas-jefferson/
41.46.183.12 EG, Egypt 21/Aug/2019:02:18:40 /turkish/verbs.html
148.64.56.127 GB, United Kingdom 21/Aug/2019:02:18:18 /travel/uk/the-road-to-the-isles/creaguaineach-staoineag.html
54.175.74.27 US, United States 21/Aug/2019:02:18:03 /networking/nat.html
52.71.155.178 US, United States 21/Aug/2019:02:18:03 /networking/
66.249.79.153 US, United States 21/Aug/2019:02:17:42 /technical/wma-or-flac-to-mp3.html
116.66.221.34 HK, Hong Kong 21/Aug/2019:02:17:40 /open-source/performance-tuning/tcp.html
46.229.168.143 US, United States 21/Aug/2019:02:17:31 /open-source/crashdumps.html
148.64.56.123 GB, United Kingdom 21/Aug/2019:02:17:23 /travel/turkey/ephesus/
174.203.17.225 US, United States 21/Aug/2019:02:17:09 /travel/usa/new-york-mcgees/
46.229.168.149 US, United States 21/Aug/2019:02:17:00 /networking/commands.html
165.225.98.95 AU, Australia 21/Aug/2019:02:16:46 /open-source/performance-tuning/ethernet.html
212.129.11.42 FR, France 21/Aug/2019:02:16:16 /travel/france/avignon/chateauneuf-du-pape.html
66.249.79.151 US, United States 21/Aug/2019:02:16:16 /travel/turkey/
12.202.15.2 US, United States 21/Aug/2019:02:15:58 /open-source/performance-tuning/ethernet.html
66.249.79.155 US, United States 21/Aug/2019:02:15:57 /travel/uk/dover/
46.229.168.151 US, United States 21/Aug/2019:02:15:32 /cybersecurity/isc2-ccsp/
66.249.79.153 US, United States 21/Aug/2019:02:14:50 /travel/japan/tokyo-ameya-yokocho/
17.58.101.135 US, United States 21/Aug/2019:02:14:47 /turkish/turkish-nouns.tex
66.188.163.33 US, United States 21/Aug/2019:02:14:20 /open-source/tar-and-ssh.html
54.36.150.95 FR, France 21/Aug/2019:02:14:17 /3d/histogram/Index.html
17.58.101.135 US, United States 21/Aug/2019:02:13:31 /open-source/performance-tuning/applications.html
5.255.250.17 US, United States 21/Aug/2019:02:13:27 /travel/belgium/namur-dinant-ardennes/
66.249.79.155 US, United States 21/Aug/2019:02:13:24 /travel/greece/thessaloniki.html
107.77.219.38 US, United States 21/Aug/2019:02:12:47 /

Here's what's going on.

Each line above is a request from a client, extracted from Apache's /var/www/logs/access_log file. The client IP address, timestamp, and requested path were selected with awk and the client IP address converted to a country if possible with geoiplookup.

The first 3 octets or first 24 bits of the IP address are used to specify the hue, with chroma at 75% and intensity at 100%. The resulting red, green, and blue values are scaled to the range of 0-255 and printed as two-character hexadecimal in an HTML style string.

Low-numbered /8 networks appear as red, 20.0.0.0/8 through 40.0.0.0/8 are orange shifting to yellow, 50.0.0.0/8 through 110.0.0.0/8 are shades of green, the /16 networks 130.0.0.0/16 through about 180.0.0.0/16 are shades of blue, then it's shades of purple into magenta for the /24 networks 192.0.0.0/24 and up through 223.255.255.0/24.

The HTML file on the server has a line where PHP uses passthru() to call the following shell script:

#!/bin/sh

# Initial pipeline:
# tail		Just the last 200 (or slightly less after the grep)
# grep		... just the requests out of that
# cat | sort	... put into reverse order
# sed		... remove the quotes and square brackets
# awk		... print the IP address twice, timestamp, and requested path
# sed		... remove the first 3 dots to split first version of IP
#			address into octets, and remove any characters that
#			could cause trouble when inserted into this page
# I need to use the client IP address, field #5 at that point, to call
# geoiplookup.  So, send the initial pipeline into a while loop that
# assigns variables, sets a new variable, and then echoes the resulting
# collection into awk.
tail -200 /var/www/logs/access_log |
	grep 'GET.*200' |
	cat -n | sort -nr |
	sed -e 's/"/ /g' -e 's/\[//g' -e 's/\]//g' |
	awk '{print $2, $2, $5, $8}' |
	sed -e 's/\./ /' -e 's/\./ /' -e 's/\./ /' -e 's/[<>]//g' |
	while read IP1 IP2 IP3 IP4 CLIENTIP TIMESTAMP URL
	do
		COUNTRY=$( geoiplookup $CLIENTIP |
				sed 's/.*Edition: //' |
				sed 's/IP Address not found/Unknown/' )
		echo $IP1 $IP2 $IP3 $IP4 $CLIENTIP $COUNTRY $TIMESTAMP $URL |
		awk '{
			ip1 = $1;
			ip2 = $2;
			ip3 = $3;
			chroma = 0.75;
			hue = 6*(ip1*255*255 + ip2*255 + ip3)/(255*255*255);
			if (hue%2 > 1) {
				x = chroma*(1.0 - (hue%2 - 1));
			} else {
				x = chroma*(1.0 - (1 - hue%2));
			}
			if (hue < 1.0) {
				r = chroma;
				g = x;
				b = 0;
			} else if (hue < 2.0) {
				r = x;
				g = chroma;
				b = 0;
			} else if (hue < 3.0) {
				r = 0;
				g = chroma;
				b = x;
			} else if (hue < 4.0) {
				r = 0;
				g = x;
				b = chroma;
			} else if (hue < 5.0) {
				r = x;
				g = 0;
				b = chroma;
			} else {
				r = chroma;
				g = 0;
				b = x;
			}
			r = (r + 0.25)*255;
			g = (g + 0.25)*255;
			b = (b + 0.25)*255;

			printf("<div class=\"col-12 textleft\" ");
			printf("style=\"color:#000; background:#%02x%02x%02x;\"> ", r, g, b);
			for (i = 5; i <= NF; i++) {
				printf("%s ", $i);
			}
			printf("</div>\n");
		}'
	done 

Other Pages