├── .gitignore ├── LICENSE ├── README.md ├── common.lua ├── example.conf ├── functions.lua ├── index_template ├── inifile.lua ├── init.lua ├── init_worker.lua ├── log.lua ├── logging.ini └── pump_data.pl /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Lua sources 2 | luac.out 3 | 4 | # luarocks build files 5 | *.src.rock 6 | *.zip 7 | *.tar.gz 8 | 9 | # Object files 10 | *.o 11 | *.os 12 | *.ko 13 | *.obj 14 | *.elf 15 | 16 | # Precompiled Headers 17 | *.gch 18 | *.pch 19 | 20 | # Libraries 21 | *.lib 22 | *.a 23 | *.la 24 | *.lo 25 | *.def 26 | *.exp 27 | 28 | # Shared objects (inc. Windows DLLs) 29 | *.dll 30 | *.so 31 | *.so.* 32 | *.dylib 33 | 34 | # Executables 35 | *.exe 36 | *.out 37 | *.app 38 | *.i*86 39 | *.x86_64 40 | *.hex 41 | 42 | sftp-config.json 43 | 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nginx-log-elasticsearch-lua 2 | Direct logging to Elasticsearch from Nginx using Lua 3 | -------------------------------------------------------------------------------- /common.lua: -------------------------------------------------------------------------------- 1 | module("common", package.seeall) 2 | 3 | function script_path() 4 | local str = debug.getinfo(2, "S").source:sub(2) 5 | return str:match("(.*/)") 6 | end -------------------------------------------------------------------------------- /example.conf: -------------------------------------------------------------------------------- 1 | lua_shared_dict log_buffer 10m; 2 | lua_shared_dict log_timer 200k; 3 | init_by_lua_file /usr/local/openresty/ngxinx/conf/lua/init.lua; 4 | init_worker_by_lua_file /usr/local/openresty/ngxinx/conf/lua/init_worker.lua; 5 | 6 | server { 7 | listen 80 default_server ; 8 | server_name .examplehost.vbox; 9 | 10 | location / { 11 | root /var/www/nginx; 12 | log_by_lua_file /usr/local/openresty/ngxinx/conf/lua/log.lua; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /functions.lua: -------------------------------------------------------------------------------- 1 | local json = require 'cjson' 2 | local http = require "resty.http" -- don't forget adding these 3 | 4 | local settings = ... 5 | 6 | local function send_http(host, port, uri, req_scheme, timeout, payload) 7 | local hc = http:new() 8 | local req_url = string.format("%s://%s:%d%s", req_scheme, host, port, uri) 9 | local ok, code, headers, status, body = hc:request { 10 | --- proxy = "http://127.0.0.1:8888", 11 | url = req_url, 12 | timeout = 5000, 13 | scheme = req_scheme, 14 | method = "POST", 15 | body = payload, 16 | } 17 | if not ok then 18 | ngx.log(ngx.ERR, "Failed to send data to ", req_url) 19 | end 20 | end 21 | 22 | -- send the buffered logs to elasticsearch 23 | -- log_index and logdata are only needed in case of direct logging 24 | local function send_logs(log_index, logdata_json) 25 | -- send logs directly on every request 26 | -- overhead from building up the tcp connection again and again 27 | local function send_logs_direct(log_index, logdata_json) 28 | assert(log_index, "Log index must be provided!") 29 | assert(logdata_json, "Logdata must be provided!") 30 | -- workaround, using cosockets from log handlers are only supported this way 31 | -- details/source: http://wiki.nginx.org/HttpLuaModule#Cosockets_Not_Available_Everywhere 32 | ngx.timer.at(0, function() 33 | send_http(settings.elasticsearch.host, 34 | settings.elasticsearch.bulk_port, 35 | settings.elasticsearch.bulk_uri, 36 | settings.elasticsearch.bulk_scheme, 37 | settings.elasticsearch.bulk_timeout, 38 | json.encode{ 39 | index = { 40 | _index = "nginx-" .. ngx.today(), 41 | _type = "nginx-" .. ngx.today(), 42 | _id = log_index, 43 | } 44 | } .. 45 | "\n" .. 46 | logdata_json .. 47 | "\n" 48 | ) 49 | end 50 | ) 51 | end 52 | 53 | -- send the logs via the TCP bulk API using an internal buffer 54 | local function send_logs_http() 55 | repeat 56 | keys = ngx.shared.log_buffer:get_keys(1024) 57 | -- assamble the payload 58 | local payload = {} 59 | for i, key in ipairs(keys) do 60 | logdata_json = ngx.shared.log_buffer:get(key) 61 | local op = json.encode{ 62 | index = { 63 | _index = "nginx-" .. ngx.today(), 64 | _type = "nginx-" .. ngx.today(), 65 | _id = key, 66 | } 67 | } 68 | 69 | table.insert(payload, op) 70 | table.insert(payload, logdata_json) 71 | ngx.shared.log_buffer:delete(key) 72 | end 73 | -- now send the data in one batch 74 | -- on failure, delete the entries nevertheless, to ensure that the buffer is not getting full 75 | send_http(settings.elasticsearch.host, 76 | settings.elasticsearch.bulk_port, 77 | settings.elasticsearch.bulk_uri, 78 | settings.elasticsearch.bulk_scheme, 79 | settings.elasticsearch.bulk_timeout, 80 | table.concat(payload, "\n") .. "\n" 81 | ) 82 | -- delete the value from the cache 83 | until #keys == 0 84 | end 85 | 86 | if settings.elasticsearch.logging_method == "none" then 87 | -- do nothing 88 | elseif settings.elasticsearch.logging_method == "buffered" then 89 | send_logs_http() 90 | else 91 | send_logs_direct(log_index, logdata_json) 92 | end 93 | end 94 | 95 | 96 | -- handler for doing dynamic logging of requests to elasticsearch 97 | local function log_request() 98 | if settings.elasticsearch.logging_method ~= "none" then 99 | -- assemble the data to be logged - this is the 'logformat' basically 100 | local logdata = { 101 | ["@timestamp"] = ngx.var.time_iso8601, 102 | remote_addr = ngx.var.remote_addr, 103 | remote_user = ngx.var.remote_user, 104 | request = ngx.var.request, 105 | status = ngx.var.status, 106 | body_bytes_sent = ngx.var.body_bytes_sent, 107 | http_referer = ngx.var.http_referer or '-', 108 | http_user_agent = ngx.var.http_user_agent, 109 | http_x_forwarded_for = ngx.var.http_x_forwarded_for, 110 | request_time = ngx.var.request_time, 111 | server_name = ngx.var.server_name, 112 | request_method = ngx.var.request_method, 113 | http_protocol = ngx.var.scheme, 114 | hostname = ngx.var.hostname, 115 | connection_serial_id = ngx.var.connection, 116 | connection_requests = ngx.var.connection_requests, 117 | } 118 | 119 | -- assemble the index dynamically 120 | local log_index = ngx.md5(string.format("%s_%s_%s_%s_%s", logdata["@timestamp"], logdata.remote_addr, logdata.hostname, logdata.connection_serial_id, logdata.connection_requests)) 121 | 122 | -- add the logline to the buffer - if buffering is required 123 | if settings.elasticsearch.logging_method == "buffered" then 124 | -- sending of the contents of the buffer is implemented in send_logs(). 125 | local ok, err = ngx.shared.log_buffer:safe_add(log_index, json.encode(logdata)) 126 | if not ok then 127 | ngx.log(ngx.ERR, "Error while adding log line to buffer, dropping log line: " .. err); 128 | end 129 | else 130 | -- send the request directly 131 | send_logs(log_index, json.encode(logdata)) 132 | end 133 | end 134 | end 135 | 136 | return { 137 | send_logs = send_logs; 138 | log_request = log_request; 139 | } -------------------------------------------------------------------------------- /index_template: -------------------------------------------------------------------------------- 1 | { 2 | "template" : "INDEXTEMPLATE", 3 | "settings" : { 4 | "index.refresh_interval" : "5s", 5 | "number_of_shards" : 5, 6 | "number_of_replicas" : 1, 7 | "analysis" : { 8 | "analyzer" : { 9 | "default" : { 10 | "type" : "standard", 11 | "stopwords" : "_none_" 12 | } 13 | } 14 | } 15 | }, 16 | "mappings" : { 17 | "INDEXTEMPLATE" : { 18 | "_all" : {"enabled" : false}, 19 | "dynamic_templates" : [ { 20 | "string_fields" : { 21 | "match" : "*", 22 | "match_mapping_type" : "string", 23 | "mapping" : { 24 | "type" : "multi_field", 25 | "fields" : { 26 | "{name}" : {"type": "string", "index" : "analyzed", "omit_norms" : true, "index_options" : "docs"}, 27 | "{name}.raw" : {"type": "string", "index" : "not_analyzed", "ignore_above" : 256 , "doc_values": true} 28 | } 29 | } 30 | } 31 | } ], 32 | "properties" : { 33 | "@version": { "type": "string", "index": "not_analyzed" }, 34 | 35 | "@timestamp" : { "format" : "dateOptionalTime", "type" : "date" }, 36 | "remote_addr" : { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 37 | "remote_user" : { "type": "string", "index" : "not_analyzed" , "doc_values": true}, 38 | "request" : { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 39 | "status" : { "type" : "long", "index" : "not_analyzed", "doc_values": true}, 40 | "body_bytes_sent" : { "type" : "double", "index" : "analyzed" }, 41 | "http_referer" : { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 42 | "http_user_agent" : { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 43 | "http_x_forwarded_for" : { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 44 | "request_time" : { "type" : "double", "index" : "analyzed" }, 45 | "server_name": { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 46 | "request_method" : { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 47 | "http_protocol" : { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 48 | "hostname" : { "type" : "string", "index" : "not_analyzed" , "doc_values": true}, 49 | "connection_serial_id" : { "type" : "integer" , "index" : "not_analyzed" , "doc_values": true}, 50 | "connection_requests" : { "type" : "integer" , "index" : "not_analyzed", "doc_values": true } 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /inifile.lua: -------------------------------------------------------------------------------- 1 | -- Copyright 2011 Bart van Strien. All rights reserved. 2 | -- 3 | -- Redistribution and use in source and binary forms, with or without modification, are 4 | -- permitted provided that the following conditions are met: 5 | -- 6 | -- 1. Redistributions of source code must retain the above copyright notice, this list of 7 | -- conditions and the following disclaimer. 8 | -- 9 | -- 2. Redistributions in binary form must reproduce the above copyright notice, this list 10 | -- of conditions and the following disclaimer in the documentation and/or other materials 11 | -- provided with the distribution. 12 | -- 13 | -- THIS SOFTWARE IS PROVIDED BY BART VAN STRIEN ''AS IS'' AND ANY EXPRESS OR IMPLIED 14 | -- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 15 | -- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BART VAN STRIEN OR 16 | -- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 | -- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 18 | -- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 19 | -- ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 20 | -- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 21 | -- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | -- 23 | -- The views and conclusions contained in the software and documentation are those of the 24 | -- authors and should not be interpreted as representing official policies, either expressed 25 | -- or implied, of Bart van Strien. 26 | -- 27 | -- The above license is known as the Simplified BSD license. 28 | 29 | inifile = {} 30 | 31 | local lines 32 | local write 33 | 34 | if love then 35 | lines = love.filesystem.lines 36 | write = love.filesystem.write 37 | else 38 | lines = function(name) return assert(io.open(name)):lines() end 39 | write = function(name, contents) return assert(io.open(name, "w")):write(contents) end 40 | end 41 | 42 | function inifile.parse(name) 43 | local t = {} 44 | local section 45 | for line in lines(name) do 46 | local s = line:match("^%[([^%]]+)%]$") 47 | if s then 48 | section = s 49 | t[section] = t[section] or {} 50 | end 51 | local key, value = line:match("^%s*([^%s]+)%s*=%s*(.+)%s*$") 52 | if key and value then 53 | if tonumber(value) then value = tonumber(value) end 54 | if value == "true" then value = true end 55 | if value == "false" then value = false end 56 | t[section][key] = value 57 | end 58 | end 59 | return t 60 | end 61 | 62 | function inifile.save(name, t) 63 | local contents = "" 64 | for section, s in pairs(t) do 65 | contents = contents .. ("[%s]\n"):format(section) 66 | for key, value in pairs(s) do 67 | contents = contents .. ("%s=%s\n"):format(key, tostring(value)) 68 | end 69 | contents = contents .. "\n" 70 | end 71 | write(name, contents) 72 | end 73 | 74 | return inifile -------------------------------------------------------------------------------- /init.lua: -------------------------------------------------------------------------------- 1 | -- clear the buffer on every restart 2 | ngx.shared.log_buffer:flush_all() 3 | ngx.shared.log_timer:flush_all() 4 | 5 | function script_path() 6 | local str = debug.getinfo(2, "S").source:sub(2) 7 | return str:match("(.*/)") 8 | end 9 | 10 | package.path = script_path() .. '?.lua;' .. package.path 11 | local inifile = require 'inifile' 12 | 13 | -- read the config file, save it 14 | -- inifile module from http://santos.nfshost.com/inifile.html 15 | -- https://github.com/hahawoo/love-misc-libs/blob/master/inifile/inifile.lua 16 | --local inifile = assert(loadfile(script_path() .. 'inifile.lua'), "Can't load inifile.lua") 17 | log_settings = assert(inifile.parse(script_path() .. "logging.ini"), "Can't find logging configuration") 18 | 19 | make_handler = assert(loadfile(script_path() .. 'functions.lua'), "Can't load functions.lua") 20 | -------------------------------------------------------------------------------- /init_worker.lua: -------------------------------------------------------------------------------- 1 | local log_timer = ngx.shared.log_timer 2 | 3 | -- create a timer if required 4 | local function create_timer (callback, delay, timer_name) 5 | 6 | local handler 7 | handler = function () 8 | -- get lock 9 | local my_pid = ngx.worker.pid() 10 | local success, err = log_timer:safe_add(timer_name, 1) 11 | if success then 12 | callback() 13 | log_timer:delete(timer_name) 14 | end 15 | local ok, err = ngx.timer.at(delay, handler) 16 | if not ok then 17 | ngx.log(ngx.ERR, "failed to create the timer: ", err) 18 | return 19 | end 20 | end 21 | 22 | local ok, err = ngx.timer.at(delay, handler) 23 | if not ok then 24 | ngx.log(ngx.ERR, "failed to create the timer: ", err) 25 | return 26 | end 27 | end 28 | 29 | -- MAIN 30 | handlers = {} 31 | -- create the log handler 32 | local handler = make_handler(log_settings) 33 | handlers["log"] = handler.log_request 34 | handlers["send_logs"] = handler.send_logs 35 | 36 | if log_settings.elasticsearch.logging_method == "buffered" then 37 | create_timer(handlers.send_logs, log_settings.elasticsearch.send_interval, "send_logs") 38 | end 39 | 40 | handler = nil -------------------------------------------------------------------------------- /log.lua: -------------------------------------------------------------------------------- 1 | handlers.log() -------------------------------------------------------------------------------- /logging.ini: -------------------------------------------------------------------------------- 1 | ; ini file, make comments with semicolons 2 | [elasticsearch] 3 | ; logging method, can be 'none', 'buffered', or 'direct' 4 | logging_method = buffered 5 | host = 172.17.8.101 6 | bulk_port = 9200 7 | bulk_uri = /_bulk 8 | bulk_scheme = http 9 | bulk_timeout = 5000 10 | ; if buffered method is used, send out logs in this many seconds 11 | send_interval = 5 -------------------------------------------------------------------------------- /pump_data.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | # Flushing to STDOUT after each write 4 | $| = 1; 5 | 6 | use strict; 7 | use warnings; 8 | use LWP::Simple; 9 | use Getopt::Std; 10 | use vars qw/ %opt /; 11 | 12 | sub init() 13 | { 14 | my $opt_string = 'i:t:h:j:c'; 15 | getopts( "$opt_string", \%opt ) or usage(); 16 | usage() if defined $opt{h}; 17 | 18 | my $INDEX; 19 | my $url; 20 | my $TEMPLATE; 21 | 22 | if ( scalar keys(%opt) > 0 ) { 23 | 24 | if($opt{i}) { 25 | $INDEX = $opt{i}; 26 | } 27 | if($opt{t}) { 28 | $url = "http://".$opt{t}.":9200"; 29 | } 30 | if($opt{c}) { 31 | 32 | usage() if not defined $opt{i}; 33 | 34 | if($opt{j}) { 35 | $TEMPLATE = $opt{j}; 36 | } 37 | 38 | my $templatedata; 39 | open TEMPLATE , "<", $TEMPLATE or die $?; 40 | while (