----------------------------------------razor_js.lua---------------------------------------------------------
local cjson = require("cjson")
local cjson_safe = require("cjson.safe")
local logger = require("logger")
local uuid4= require("uuid4")
local args = {}
local request_method = ngx.var.request_method
--字符串分割函数
--传入字符串和分隔符,返回分割后的table
function string.split(str, delimiter)
if str==nil or str=='' or delimiter==nil then
return nil
end
local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
--字符串trim函数,去掉前后空格
function string.trim (s) return (string.gsub(s, "^%s*(.-)%s*$", "%1")) end
--解析UserAgent,获取移动设备访问的系统版本和设备型号
function getUAField(t)
local separator=';'
local tab={}
local android=string.find(t,"Android")
local iphone=string.find(t,"iPhone")
if android then
tab["platform"]="android"
local startIndex =string.find(t,"Build")
if startIndex ~= nil then
local res=string.sub(t,0,startIndex-1)
local rtable=string.split(res,separator)
local devicename=string.trim(rtable[#rtable])
if devicename~=nil then
tab["devicename"]=devicename
end
end
local osvTable=string.split(string.sub(t,android) ,separator)
if osvTable then
local osvTab=string.split(osvTable[1] ,' ')
tab["os_version"]=string.trim(osvTab[#osvTab])
end
end
if iphone then
tab["platform"]="iOS"
tab["devicename"]="iphone"
local vs=string.find(t,"Version")
local osvTable=string.split(string.sub(t, vs) ,' ')
if osvTable then
print(osvTable[1])
local osvTab=string.split(osvTable[1] ,'/')
tab["os_version"]=string.trim(osvTab[#osvTab])
end
end
if next(tab) == nil then
tab["platform"]=""
tab["devicename"]=""
tab["os_version"]=""
end
return tab
end
--lua获取http请求参数的值
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
--时间戳转换成指定的日期格式
function getTimeStamp(t)
return os.date("%Y-%m-%d %H:%M:%S",t)
end
local field_map={idsite="appkey",url="activity",_id="deviceid",_viewts="time",res="resolution",lang="language",uid="useridentifier",e_c="category",e_n="event_identifier",lat="latitude",long="longtitude"}
if next(args) ~= nil then
response = '{"flag":"-1", "msg":"content error"}'
--clientdata
clientdata_arr ={}
clientdata_arr["localtime"] = ngx.localtime()
clientdata_arr["clientip"] = ngx.var.client_ip
uuid = uuid4.getUUID();
clientdata_arr["uuid"] = uuid:gsub("-", "")
clientdata_arr["language"]=ngx.var.lang
local UATable,err=getUAField(ngx.var.http_user_agent)
clientdata_arr["devicename"]=UATable["devicename"]
clientdata_arr["os_version"]=UATable["os_version"]
clientdata_arr["platform"]=UATable["platform"]
clientdata_arr["cellid"]=-1
clientdata_arr["ismobiledevice"]=true
clientdata_arr["phonetype"]=0
clientdata_arr["imsi"]=""
clientdata_arr["mccmnc"]=""
clientdata_arr["lac"]=""
clientdata_arr["version"]=""
clientdata_arr["modulename"]=""
clientdata_arr["wifimac"]=""
clientdata_arr["havebt"]=true
clientdata_arr["havewifi"]=true
clientdata_arr["havegps"]="false"
clientdata_arr["havegravity"]=true
clientdata_arr["salt"]=""
clientdata_arr["lib_version"]=""
--event
event_arr={}
event_arr["localtime"] = ngx.localtime()
event_arr["clientip"] = ngx.var.client_ip
uuid = uuid4.getUUID();
event_arr["uuid"] = uuid:gsub("-", "")
event_arr["lib_version"]=""
event_arr["version"]=""
event_arr["label"]=""
event_arr["attachment"]=""
--usinglog
usinglog_arr={}
usinglog_arr["localtime"] = ngx.localtime()
usinglog_arr["clientip"] = ngx.var.client_ip
uuid = uuid4.getUUID();
usinglog_arr["uuid"] = uuid:gsub("-", "")
usinglog_arr["lib_version"]=""
usinglog_arr["version"]=""
for key, val in pairs(args) do
if key == "idsite" then
clientdata_arr["appkey"]=val
event_arr["appkey"]=val
usinglog_arr["appkey"]=val
elseif key == "_id" then
clientdata_arr["deviceid"]=val
clientdata_arr["useridentifier"]=val
event_arr["deviceid"]=val
event_arr["useridentifier"]=val
usinglog_arr["deviceid"]=val
usinglog_arr["useridentifier"]=val
elseif key == "_viewts" then
clientdata_arr["time"], err=getTimeStamp(val)
event_arr["time"], err=getTimeStamp(val)
usinglog_arr["end_millis"]=val
elseif key == "res" then
clientdata_arr["resolution"]=val
elseif key == "lat" then
clientdata_arr["latitude"]=val
elseif key == "long" then
clientdata_arr["longtitude"]=val
elseif key == "uid" then
local tmp=string.split(val,'_')
clientdata_arr["session_id"]=tmp[1]
event_arr["session_id"]=tmp[1]
usinglog_arr["session_id"]=tmp[1]
usinglog_arr["start_millis"]=tmp[#tmp]
elseif key == "url" then
event_arr["activity"]=val
usinglog_arr["activities"]=val
elseif key == "e_n" then
event_arr["event_identifier"]=val
elseif key == "e_v" then
event_arr["acc"]=val
end
response = '{"flag":"1", "msg":"OK"}'
end
--判断table是否为空
if next(clientdata_arr) ~= nil then
log_line, err = cjson_safe.encode(clientdata_arr)
log_file = ngx.var.log_file_cd .. "." .. os.date(ngx.var.date_fmt, ngx.time())
logger.info(log_file, log_line)
end
if next(event_arr) ~= nil then
log_line, err = cjson_safe.encode(event_arr)
log_file = ngx.var.log_file_event .. "." .. os.date(ngx.var.date_fmt, ngx.time())
logger.info(log_file, log_line)
end
if next(usinglog_arr) ~= nil then
if usinglog_arr["start_millis"] ~=nil and usinglog_arr["end_millis"] ~=nil then
usinglog_arr["duration"]=tonumber(usinglog_arr["end_millis"]) - tonumber(usinglog_arr["start_millis"])
usinglog_arr["end_millis"]=getTimeStamp(usinglog_arr["end_millis"])
usinglog_arr["start_millis"]=getTimeStamp(usinglog_arr["start_millis"])
end
log_line, err = cjson_safe.encode(usinglog_arr)
log_file = ngx.var.log_file_usinglog .. "." .. os.date(ngx.var.date_fmt, ngx.time())
logger.info(log_file, log_line)
else
response = '{"flag":"-1", "msg":"' .. err .. '"}'
end
ngx.say(response)
else
ngx.say('{"flag":"-1", "msg":"No post data found"}')
end
---------------------------------------------------------------------------nginx中配置-----------------------------------------
location /webjs {
default_type text/plain;
set $log_file_cd '/data/razor_post/cd/cd.log';
set $log_file_event '/data/razor_post/event/event.log';
set $log_file_usinglog '/data/razor_post/usinglog/usinglog.log';
content_by_lua_file lua/razor_js.lua;
}