-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx_access_scan2.rb
83 lines (67 loc) · 2.06 KB
/
nginx_access_scan2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 请输入 ruby nginx_access_scan2.rb
require 'mysql2'
require 'digest/md5'
require 'rails'
md5 = []
dataes = []
count = 0
def open_file(path)
File.open(path)
end
def time_format(time)
time = time[0].sub(/:/, ' ')
time.to_time.strftime("%Y-%m-%d %H:%M:%S")
end
def generate_md5(url)
url_md5 = Digest::MD5.hexdigest(url)
end
def url_unique(url, url_md5)
@md5 ||= []
# 如果是nil,那么让它成为一个空数组;如果它不是nil,那么什么也不做。
if @md5.include?(url_md5)
url = []
else
@md5 = @md5.push(url_md5)
return url
end
end
def insert_query(dataes)
client = Mysql2::Client.new(
:host => '127.0.0.1', # 主机
:username => 'root', # 用户名
:password => '123123', # 密码
:database => 'weblog', # 数据库
:encoding => 'utf8' # 编码
)
dataes.each do |datas|
datas.each do |i|
client.query("INSERT INTO nginx(ip, time, actor, url, state, length, refer, agent) VALUES ('#{i[0]}', '#{i[1]}', '#{i[2]}', '#{i[3]}', '#{i[4]}', '#{i[5]}', '#{i[6]}', '#{i[7]}')")
end
end
end
file = open_file("./nginx.access.log")
file.each_line do |line|
count += 1
words = line.split
ip = line.scan(/^\d{2}\.\d{2}\.\d{2}\.\d{2}/)
time = line.scan(/\d+\/\w+\/\d{4}\:\d{2}\:\d{2}\:\d{2}/)
time = [ time_format(time) ]
actor = [line.scan(/[A-Z]{3}/)[0]]
url = [line.scan(/\w*\/\w*\/*\w*\/*\w*/)[1]]
url_md5 = [ generate_md5(url[0]) ]
url = [ url_unique(url, url_md5) ]
state = [line.scan(/\d{3}/)[2]]
length = [words[9]]
refer = [words[10]]
agent = [words[-7..-2].join(" ")]
if url == [[]]
datas = []
else
datas = [ ip + time + actor + url + state + length + refer + agent ]
end
dataes = dataes.push(datas) unless datas == [] # 删除空条目
p datas unless datas == [] # 只显示非空项
end
p "共有#{count} 条日志记录,提取信息如上所示👆" # 如上所示👆、如下所示👇
insert_query(dataes)
file.close