在 Java 中解析 A​​pache 访问日志

维护页面请求历史的 Web 服务器日志,通常附加到文件的末尾。通常会添加有关请求的信息,包括客户端 IP 地址、请求日期/时间、请求的页面、HTTP 代码、提供的字节数、用户代理和引用者。

给定一个 Web 服务器日志记录,找到成功响应的 IP 地址的成功 HTTL 响应总数(200 个代码)。

例子:

输入:示例访问日志 192.168.1.2 - - [17/Sep/2013:22:18:19 -0700]“GET /abc HTTP/1.1”404 201 192.168.1.2 - - [17/Sep/2013:22:18:19 -0700]“GET /favicon.ico HTTP/1.1”200 1406 192.168.1.2 - - [17/Sep/2013:22:18:27 -0700] “GET /wp/ HTTP/1.1”200 5325 192.168.1.2 - - [17/Sep/2013:22:18:27 -0700]“GET /wp/wp-content/themes/twentytwelve/style.css?ver=3.5.1 HTTP/1.1”200 35292 192.168.1.3 - - [17/Sep/2013:22:18:27 -0700]“GET /wp/wp-content/themes/twentytwelve/js/navigation.js?ver=1.0 HTTP/1.1”200 863 输出 : 192.168.1.3 1 192.168.1.2 3 先决条件:Java中的正则表达式
// Java program to count the no. of IP address
// count for successful http response 200 code.
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class FindSuccessIpCount {

	public static void findSuccessIpCount(String record)
	{
		// Creating a regular expression for the records
		final String regex = "^(\\S+) (\\S+) (\\S+) " +
			"\\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(\\S+)" +
			" (\\S+)\\s*(\\S+)?\\s*\" (\\d{3}) (\\S+)";

		final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
		final Matcher matcher = pattern.matcher(record);

		// Creating a Hashmap containing string as
		// the key and integer as the value.
		HashMap<String, Integer> countIP = new HashMap<String, Integer>();
		while (matcher.find()) {

			String IP = matcher.group(1);
			String Response = matcher.group(8);
			int response = Integer.parseInt(Response);

			// Inserting the IP addresses in the
			// HashMap and maintaining the frequency
			// for each HTTP 200 code.
			if (response == 200) {
				if (countIP.containsKey(IP)) {
					countIP.put(IP, countIP.get(IP) + 1);
				}
				else {
					countIP.put(IP, 1);
				}
			}
		}

		// Printing the hashmap
		for (Map.Entry entry : countIP.entrySet()) {
			System.out.println(entry.getKey() + " " + entry.getValue());
		}
	}
	public static void main(String[] args)
	{
		final String log = "123.123.123.123 - - [26/Apr/2000:00:23:48 -0400] \"GET /pics/wpaper.gif HTTP/1.0\" 200 6248 \"http:// www.jafsoft.com/asctortf/\" \"Mozilla/4.05 (Macintosh; I; PPC)\"\n"
						+ "123.123.123.123 - - [26/Apr/2000:00:23:47 -0400] \"GET /asctortf/ HTTP/1.0\" 200 8130 \"http:// search.netscape.com/Computers/Data_Formats/Document/Text/RTF\" \"Mozilla/4.05 (Macintosh; I; PPC)\"\n"
						+ "123.123.123.124 - - [26/Apr/2000:00:23:48 -0400] \"GET /pics/5star2000.gif HTTP/1.0\" 200 4005 \"http:// www.jafsoft.com/asctortf/\" \"Mozilla/4.05 (Macintosh; I; PPC)\"\n"
						+ "123.123.123.123 - - [26/Apr/2000:00:23:50 -0400] \"GET /pics/5star.gif HTTP/1.0\" 404 1031 \"http:// www.jafsoft.com/asctortf/\" \"Mozilla/4.05 (Macintosh; I; PPC)\"\n"
						+ "123.123.123.126 - - [26/Apr/2000:00:23:51 -0400] \"GET /pics/a2hlogo.jpg HTTP/1.0\" 200 4282 \"http:// www.jafsoft.com/asctortf/\" \"Mozilla/4.05 (Macintosh; I; PPC)\"\n"
						+ "123.123.123.123 - - [26/Apr/2000:00:23:51 -0400] \"GET /cgi-bin/newcount?jafsof3&width=4&font=digital&noshow HTTP/1.0\" 200 36 \"http:// www.jafsoft.com/asctortf/\" \"Mozilla/4.05 (Macintosh; I; PPC)\"\n";

		findSuccessIpCount(log);
	}
}

输出:

123.123.123.126 1 123.123.123.124 1 123.123.123.123 3 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值