package com.jianmu.tools.log;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class NginxLogTools {
public static final String LINUX_NGINX_ACCESS_LOG = "/var/log/nginx/access.log";
public static final String LINUX_NGINX_ERROR_LOG = "/var/log/nginx/error.log";
private NginxLogTools() {
}
public static List<String> reads(File file) {
try {
return Files.readLines(file, Charsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取所有ip
*/
public static List<String> allIps(File file) {
List<String> list = reads(file);
assert list != null;
return list.parallelStream().map(i -> Splitter.on(" - - ").split(i).iterator().next()).collect(Collectors.toList());
}
/**
* 获取所有匹配的ip
*/
public static List<String> allMatchIp(File file, String match) {
List<String> list = reads(file);
assert list != null;
return list.parallelStream().filter(i -> i.contains(match)).map(i -> Splitter.on(" - - ").split(i).iterator().next()).collect(Collectors.toList());
}
/**
* 获取所有匹配的ip 并且去重
*/
public static List<String> allMatchIpAndDistinct(File file, String match) {
List<String> list = reads(file);
assert list != null;
return list.parallelStream().filter(i -> i.contains(match)).map(i -> Splitter.on(" - - ").split(i).iterator().next()).distinct().collect(Collectors.toList());
}
/**
* 统计ip重复出现的次数 并排序
*/
public static Map<String, Integer> repeatCount(List<String> ips) {
Multiset<String> multiset = HashMultiset.create();
multiset.addAll(ips);
Map<String, Integer> map = new HashMap<>(ips.size());
Set<String> letters = multiset.elementSet();
letters.forEach(i -> map.put(i, multiset.count(i)));
return map.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
}
public static void main(String[] args) {
Map<String, Integer> map = repeatCount(allMatchIp(new File("C:\\Users\\kong\\Desktop\\backup\\access.log"), "/webapp"));
System.out.println(JSON.toJSONString(map));
}
}
nginx log 分析 java 获取所有ip 统计ip访问次数
最新推荐文章于 2023-02-21 15:53:24 发布