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.

54.81.45.122 US, United States 18/Oct/2017:00:03:31 /networking/switch-programming.html
54.81.45.122 US, United States 18/Oct/2017:00:03:18 /cybersecurity/generalinfo.html
54.81.45.122 US, United States 18/Oct/2017:00:03:06 /cybersecurity/ssl-tls.html
54.81.45.122 US, United States 18/Oct/2017:00:02:54 /cybersecurity/syslog-tls-cloud.html
66.171.34.85 US, United States 18/Oct/2017:00:02:47 /linux/sendmail-ssl.html
68.180.228.90 US, United States 18/Oct/2017:00:02:44 /linux/linux-kernel-details.html
54.81.45.122 US, United States 18/Oct/2017:00:02:41 /linux/linux-blog.html
54.81.45.122 US, United States 18/Oct/2017:00:02:28 /cybersecurity/ec2-secure-storage.html
54.81.45.122 US, United States 18/Oct/2017:00:02:14 /technical/samsung-galaxy/linux.html
54.81.45.122 US, United States 18/Oct/2017:00:02:01 /linux/apache-referer-log-analysis.html
54.81.45.122 US, United States 18/Oct/2017:00:01:49 /technical/bios.html
54.81.45.122 US, United States 18/Oct/2017:00:01:37 /3d/xray/scanner.html
54.81.45.122 US, United States 18/Oct/2017:00:01:23 /travel/usa/Index.html
112.202.170.120 PH, Philippines 18/Oct/2017:00:01:20 /technical/samsung-galaxy/secret-code.html
131.253.25.92 US, United States 18/Oct/2017:00:01:14 /travel/turkey/halicarnassus/
54.81.45.122 US, United States 18/Oct/2017:00:01:09 /3d/histogram/3d-histogram.html
66.249.79.18 US, United States 18/Oct/2017:00:01:07 /travel/italy/driving.html
95.91.250.141 DE, Germany 18/Oct/2017:00:01:01 /linux/sysfs.html
54.81.45.122 US, United States 18/Oct/2017:00:00:53 /linux/ssh.html
54.81.45.122 US, United States 18/Oct/2017:00:00:40 /linux/raspberry-pi/sdr-ads-b-flight-tracking.html
51.255.65.47 FR, France 18/Oct/2017:00:00:40 /fun/latin.html
54.81.45.122 US, United States 18/Oct/2017:00:00:28 /radio/internet-radio.html
54.81.45.122 US, United States 18/Oct/2017:00:00:27 /robots.txt
54.81.45.122 US, United States 18/Oct/2017:00:00:27 /robots.txt
66.249.79.18 US, United States 18/Oct/2017:00:00:08 /travel/belgium/namur-dinant-ardennes/
66.249.79.14 US, United States 17/Oct/2017:23:59:28 /travel/greece/tiryns.html
66.249.79.18 US, United States 17/Oct/2017:23:59:27 /travel/belgium/bastogne-ardennes/
64.124.64.124 US, United States 17/Oct/2017:23:59:21 /linux/performance-tuning/disks.html
34.239.250.11 US, United States 17/Oct/2017:23:58:11 /travel/turkey/buses/
34.239.250.11 US, United States 17/Oct/2017:23:58:08 /robots.txt

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-xs-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