-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirrorToRds.lua
90 lines (80 loc) · 2.11 KB
/
mirrorToRds.lua
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
84
85
86
87
88
89
local redis = require "resty.redis"
ngxmatch=ngx.re.find
local url = ngx.var.request_uri
local server_name = ngx.var.server_name
local version = 1
local clientip = ngx.var.remote_addr
function redis_conn()
local cache = redis.new()
cache:set_timeout(5000)
local ok, err = cache.connect(cache, '127.0.0.1', 6380)
if not ok then
return
end
local count, err = cache:get_reused_times()
if 0 == count then
local red, err = cache:auth("xxxxxx")
if not red then
return
end
cache:select(tonumber('5'))
end
return cache
end
function savePostToRds()
ngx.req.read_body()
local post_body = ngx.req.get_body_data()
if not post_body then
--ngx.log(ngx.ERR,"savePostRds read body is nil")
return
end
---ngx.log(ngx.ERR,post_body)
msg = "{\"host\":\""..server_name.."\",\"clientip\":\""..clientip.."\",\"url\":\""..url.."\",\"message\":\""..post_body.."\"}"
--ngx.log(ngx.ERR,msg)
cache:publish("elk_body",msg)
end
cache = redis_conn()
if not cache then
return true
end
savePostToRds()
local ok, err = cache:set_keepalive(10000, 100)
if not ok then
ngx.log(ngx.ERR,"inner failed to set keepalive: ", err)
return
end
---测试方法订阅redis管道:PSUBSCRIBE elk_*
--- nginx setting
--- server setting
--- server {
--- lua_need_request_body on;
--- location/ {
--- mirror /mirror_tonc;
--- mirror_request_body on;
--- #root /webroot;
--- }
--- location /mirror_tonc {
--- internal;
--- proxy_set_header Content-Length "";
--- proxy_set_header X-Original-URI $request_uri;
--- #proxy_pass http://127.0.0.1:999$request_uri;
--- #proxy_pass http://172.20.200.170:12345$request_uri;
--- content_by_lua_file conf/lua/mirror_redis.lua;
--- }
---}
--- elk --- 可导入到elk 分析处理
---
---input {
--- redis {
--- data_type => "channel"
--- host => "127.0.0.1"
--- db => "5"
--- port => "6380"
--- password => "kingdee"
--- key => "elk_body"
--- }
---}
---output {
--- elasticsearch { hosts => ["127.0.0.1:9200"] }
--- stdout { codec => rubydebug }
---}