summaryrefslogtreecommitdiff
path: root/src/daq/nginx-lua/s5-nginx.lua
blob: ab62bab5d7c1d3fd266374587628e05b2d998170 (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
--
-- sfive
--
-- sfive - spreadspace streaming statistics suite is a generic
-- statistic collection tool for streaming server infrastuctures.
-- The system collects and stores meta data like number of views
-- and throughput from a number of streaming servers and stores
-- it in a global data store.
-- The data acquisition is designed to be generic and extensible in
-- order to support different streaming software.
-- sfive also contains tools and applications to filter and visualize
-- live and recorded data.
--
--
-- Copyright (C) 2014 Christian Pointner <equinox@spreadspace.org>
--                    Markus Grueneis <gimpf@gimpf.org>
--
-- This file is part of sfive.
--
-- sfive is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License version 3
-- as published by the Free Software Foundation.
--
-- sfive is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with sfive. If not, see <http://www.gnu.org/licenses/>.
--
--
-- Install this by adding the following to your nginx.conf
--
-- http {
--   .....
--
--   lua_shared_dict sfive 32k;
--   lua_shared_dict sfive_data 64m;
--   init_by_lua 's5 = require "s5-nginx.lua"';
--   init_worker_by_lua 's5.init_worker(5)';
--   limit_conn_zone $server_name zone=perserver:32k;
--
--   .....
--
--   server {
--     server_name somename;
--
--   .....
--
--     location /path/to/hls {
--       .....
--       log_by_lua 's5.log()';
--     }
--
--     location /sfive {
--       allow 127.0.0.1;
--       allow ::1;
--       deny  all;
--       limit_conn perserver 1;
--       lua_check_client_abort on;
--       content_by_lua 's5.fetch()';
--     }
--
--   .....
--
-- }
--

local _SFIVE = {}

-----------------------------------------------
-- this will only be shared within one worker
local config = {
   log_exptime = 30,
   log_max_index = 1000000
}
local sfive = ngx.shared.sfive
local sfive_data = ngx.shared.sfive_data
-----------------------------------------------

function _SFIVE.log()
   local status = ngx.var.status
   if status == '200' or status == '206' then
      local idx, err = sfive:incr("log:idx", 1)
      if not idx then
         ngx.log(ngx.ERR, "SFive(log): incrementing log index failed: " .. err)
      else
         local json = '{'
         json = json .. '"time": "' .. string.gsub(ngx.utctime(), " ", "T", 1) .. 'Z",'
         json = json .. '"client": "' .. ngx.var.remote_addr .. '",'
         json = json .. '"port": ' .. ngx.var.remote_port .. ','
         json = json .. '"ua": "' .. ngx.var.http_user_agent .. '",'
         json = json .. '"url": "' .. ngx.var.uri .. '",'
         json = json .. '"status": ' .. status .. ','
         json = json .. '"bytes-sent": ' .. ngx.var.bytes_sent
         json = json .. '}'

         local ok, err, force = sfive_data:add(idx, json, config.log_exptime)
         if not ok then
            ngx.log(ngx.ERR, "SFive(log): adding log line (".. idx .. ") to log store failed: " .. err)
         elseif force then
            ngx.log(ngx.WARN, "SFive(log): adding log line has overwritten other log lines - consider increasing the log store!")
         end
      end
   end
end

function _SFIVE.fetch()
   ngx.log(ngx.INFO, "SFive(fetch): client connected")

	 local ok, err = ngx.on_abort(function() ngx.exit(ngx.HTTP_GONE) end)
	 if not ok then
			ngx.log(ngx.ERR, "failed to register the on_abort callback: ", err)
			ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
	 end

   while true do
      logs = sfive_data:get_keys()
      for i, k in ipairs(logs) do
         local ok, err = ngx.say(sfive_data:get(k))
         if not ok then
            ngx.log(ngx.ERR, "SFive(fetch): failed to send data set (" ..  err .. ") - disconnecting client")
            return
         end
         sfive_data:delete(k)
      end
      ngx.flush()
      local idx, err = sfive:get("log:idx")
      if not idx then
         ngx.log(ngx.ERR, "SFive(fetch): failed to get log index: " .. err)
      else
         if idx >= config.log_max_index then
            local ok, err = sfive:replace("log:idx", 0)
            if not ok then
               ngx.log(ngx.ERR, "SFive(init): reseting log index counter failed: " .. err)
            end
         end
      end
      ngx.sleep(0.1)
   end
end

function _SFIVE.init_worker(duration)
   config.log_exptime = duration
   config.log_max_index = 1000000
end

ngx.log(ngx.DEBUG, "SFive: loaded successfully")

sfive:flush_all()
sfive:flush_expired()
sfive_data:flush_all()
sfive_data:flush_expired()
local ok, err = sfive:set("log:idx", 0)
if not ok then
   ngx.log(ngx.ERR, "SFive(init): creating log index counter failed: " .. err)
else
   ngx.log(ngx.INFO, "SFive(init): initialized successfully!")
end

return _SFIVE