forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_vod_status.c
399 lines (339 loc) · 11.1 KB
/
ngx_http_vod_status.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// includes
#include "ngx_http_vod_status.h"
#include "ngx_http_vod_module.h"
#include "ngx_http_vod_utils.h"
#include "ngx_http_vod_conf.h"
#include "ngx_perf_counters.h"
#include "ngx_buffer_cache.h"
// macros
#define DEFINE_STAT(x) { { sizeof(#x) - 1, (u_char *) #x }, offsetof(ngx_buffer_cache_stats_t, x) }
// constants
#define PATH_PERF_COUNTERS_OPEN "<performance_counters>\r\n"
#define PATH_PERF_COUNTERS_CLOSE "</performance_counters>\r\n"
#define PERF_COUNTER_FORMAT "<sum>%uA</sum>\r\n<count>%uA</count>\r\n<max>%uA</max>\r\n<max_time>%uA</max_time>\r\n<max_pid>%uA</max_pid>\r\n"
#define PROM_STATUS_PREFIX \
"nginx_vod_build_info{version=\"" NGINX_VOD_VERSION "\"} 1\n\n"
#define PROM_VOD_CACHE_METRIC_FORMAT "vod_cache_%V{cache=\"%V\"} %uA\n"
#define PROM_PERF_COUNTER_METRICS \
"vod_perf_counter_sum{action=\"%V\"} %uA\n" \
"vod_perf_counter_count{action=\"%V\"} %uA\n" \
"vod_perf_counter_max{action=\"%V\"} %uA\n" \
"vod_perf_counter_max_time{action=\"%V\"} %uA\n" \
"vod_perf_counter_max_pid{action=\"%V\"} %uA\n\n" \
// typedefs
typedef struct {
int conf_offset;
ngx_str_t open_tag;
ngx_str_t close_tag;
} ngx_http_vod_cache_info_t;
typedef struct {
ngx_str_t name;
unsigned offset;
} ngx_http_vod_stat_def_t;
// constants
static const u_char status_prefix[] =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
"<vod>\r\n"
"<version>" NGINX_VOD_VERSION "</version>\r\n";
static const u_char status_postfix[] = "</vod>\r\n";
static ngx_str_t xml_content_type = ngx_string("text/xml");
static ngx_str_t text_content_type = ngx_string("text/plain");
static ngx_str_t reset_response = ngx_string("OK\r\n");
static ngx_http_vod_stat_def_t buffer_cache_stat_defs[] = {
DEFINE_STAT(store_ok),
DEFINE_STAT(store_bytes),
DEFINE_STAT(store_err),
DEFINE_STAT(store_exists),
DEFINE_STAT(fetch_hit),
DEFINE_STAT(fetch_bytes),
DEFINE_STAT(fetch_miss),
DEFINE_STAT(evicted),
DEFINE_STAT(evicted_bytes),
DEFINE_STAT(reset),
DEFINE_STAT(entries),
DEFINE_STAT(data_size),
{ ngx_null_string, 0 }
};
static ngx_http_vod_cache_info_t cache_infos[] = {
{
offsetof(ngx_http_vod_loc_conf_t, metadata_cache),
ngx_string("<metadata_cache>\r\n"),
ngx_string("</metadata_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, response_cache[CACHE_TYPE_VOD]),
ngx_string("<response_cache>\r\n"),
ngx_string("</response_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, response_cache[CACHE_TYPE_LIVE]),
ngx_string("<live_response_cache>\r\n"),
ngx_string("</live_response_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, mapping_cache[CACHE_TYPE_VOD]),
ngx_string("<mapping_cache>\r\n"),
ngx_string("</mapping_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, mapping_cache[CACHE_TYPE_LIVE]),
ngx_string("<live_mapping_cache>\r\n"),
ngx_string("</live_mapping_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, drm_info_cache),
ngx_string("<drm_info_cache>\r\n"),
ngx_string("</drm_info_cache>\r\n"),
},
};
static u_char*
ngx_http_vod_append_cache_stats(u_char* p, ngx_buffer_cache_stats_t* stats)
{
ngx_http_vod_stat_def_t* cur_stat;
for (cur_stat = buffer_cache_stat_defs; cur_stat->name.data != NULL; cur_stat++)
{
// opening tag
*p++ = '<';
p = ngx_copy(p, cur_stat->name.data, cur_stat->name.len);
*p++ = '>';
// value
p = ngx_sprintf(p, "%uA", *(ngx_atomic_t*)((u_char*)stats + cur_stat->offset));
// closing tag
*p++ = '<';
*p++ = '/';
p = ngx_copy(p, cur_stat->name.data, cur_stat->name.len);
*p++ = '>';
// newline
*p++ = CR;
*p++ = LF;
}
return p;
}
static ngx_int_t
ngx_http_vod_status_reset(ngx_http_request_t *r)
{
ngx_http_vod_loc_conf_t *conf;
ngx_perf_counters_t* perf_counters;
ngx_buffer_cache_t *cur_cache;
unsigned i;
conf = ngx_http_get_module_loc_conf(r, ngx_http_vod_module);
perf_counters = ngx_perf_counter_get_state(conf->perf_counters_zone);
for (i = 0; i < sizeof(cache_infos) / sizeof(cache_infos[0]); i++)
{
cur_cache = *(ngx_buffer_cache_t **)((u_char*)conf + cache_infos[i].conf_offset);
if (cur_cache == NULL)
{
continue;
}
ngx_buffer_cache_reset_stats(cur_cache);
}
if (perf_counters != NULL)
{
for (i = 0; i < PC_COUNT; i++)
{
perf_counters->counters[i].sum = 0;
perf_counters->counters[i].count = 0;
perf_counters->counters[i].max = 0;
perf_counters->counters[i].max_time = 0;
perf_counters->counters[i].max_pid = 0;
}
}
return ngx_http_vod_send_response(r, &reset_response, &text_content_type);
}
static ngx_int_t
ngx_http_vod_status_xml_handler(ngx_http_request_t *r)
{
ngx_buffer_cache_stats_t stats;
ngx_http_vod_loc_conf_t *conf;
ngx_http_vod_stat_def_t* cur_stat;
ngx_perf_counters_t* perf_counters;
ngx_buffer_cache_t *cur_cache;
ngx_str_t response;
u_char* p;
size_t cache_stats_len = 0;
size_t result_size;
unsigned i;
conf = ngx_http_get_module_loc_conf(r, ngx_http_vod_module);
perf_counters = ngx_perf_counter_get_state(conf->perf_counters_zone);
// calculate the buffer size
for (cur_stat = buffer_cache_stat_defs; cur_stat->name.data != NULL; cur_stat++)
{
cache_stats_len += sizeof("<></>\r\n") - 1 + 2 * cur_stat->name.len + NGX_ATOMIC_T_LEN;
}
result_size = sizeof(status_prefix) - 1;
for (i = 0; i < sizeof(cache_infos) / sizeof(cache_infos[0]); i++)
{
cur_cache = *(ngx_buffer_cache_t **)((u_char*)conf + cache_infos[i].conf_offset);
if (cur_cache == NULL)
{
continue;
}
result_size += cache_infos[i].open_tag.len + cache_stats_len + cache_infos[i].close_tag.len;
}
if (perf_counters != NULL)
{
result_size += sizeof(PATH_PERF_COUNTERS_OPEN);
for (i = 0; i < PC_COUNT; i++)
{
result_size += perf_counters_open_tags[i].len + sizeof(PERF_COUNTER_FORMAT) + 5 * NGX_ATOMIC_T_LEN + perf_counters_close_tags[i].len;
}
result_size += sizeof(PATH_PERF_COUNTERS_CLOSE);
}
result_size += sizeof(status_postfix);
// allocate the buffer
response.data = ngx_palloc(r->pool, result_size);
if (response.data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_status_xml_handler: ngx_palloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// populate the buffer
p = ngx_copy(response.data, status_prefix, sizeof(status_prefix) - 1);
for (i = 0; i < sizeof(cache_infos) / sizeof(cache_infos[0]); i++)
{
cur_cache = *(ngx_buffer_cache_t **)((u_char*)conf + cache_infos[i].conf_offset);
if (cur_cache == NULL)
{
continue;
}
ngx_buffer_cache_get_stats(cur_cache, &stats);
p = ngx_copy(p, cache_infos[i].open_tag.data, cache_infos[i].open_tag.len);
p = ngx_http_vod_append_cache_stats(p, &stats);
p = ngx_copy(p, cache_infos[i].close_tag.data, cache_infos[i].close_tag.len);
}
if (perf_counters != NULL)
{
p = ngx_copy(p, PATH_PERF_COUNTERS_OPEN, sizeof(PATH_PERF_COUNTERS_OPEN) - 1);
for (i = 0; i < PC_COUNT; i++)
{
p = ngx_copy(p, perf_counters_open_tags[i].data, perf_counters_open_tags[i].len);
p = ngx_sprintf(p, PERF_COUNTER_FORMAT,
perf_counters->counters[i].sum,
perf_counters->counters[i].count,
perf_counters->counters[i].max,
perf_counters->counters[i].max_time,
perf_counters->counters[i].max_pid);
p = ngx_copy(p, perf_counters_close_tags[i].data, perf_counters_close_tags[i].len);
}
p = ngx_copy(p, PATH_PERF_COUNTERS_CLOSE, sizeof(PATH_PERF_COUNTERS_CLOSE) - 1);
}
p = ngx_copy(p, status_postfix, sizeof(status_postfix) - 1);
response.len = p - response.data;
if (response.len > result_size)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_status_xml_handler: response length %uz exceeded allocated length %uz",
response.len, result_size);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
return ngx_http_vod_send_response(r, &response, &xml_content_type);
}
static ngx_int_t
ngx_http_vod_status_prom_handler(ngx_http_request_t *r)
{
ngx_buffer_cache_stats_t stats;
ngx_http_vod_stat_def_t* cur_stat;
ngx_http_vod_loc_conf_t *conf;
ngx_perf_counters_t* perf_counters;
ngx_buffer_cache_t *cur_cache;
ngx_str_t response;
ngx_str_t cache_name;
ngx_str_t action;
unsigned i;
u_char* p;
size_t result_size;
size_t names_len;
conf = ngx_http_get_module_loc_conf(r, ngx_http_vod_module);
perf_counters = ngx_perf_counter_get_state(conf->perf_counters_zone);
names_len = 0;
for (cur_stat = buffer_cache_stat_defs; cur_stat->name.data != NULL; cur_stat++)
{
names_len += cur_stat->name.len;
}
result_size = sizeof(PROM_STATUS_PREFIX) - 1;
for (i = 0; i < vod_array_entries(cache_infos); i++)
{
cur_cache = *(ngx_buffer_cache_t **)((u_char*)conf + cache_infos[i].conf_offset);
if (cur_cache == NULL)
{
continue;
}
result_size += (sizeof(PROM_VOD_CACHE_METRIC_FORMAT) - 1 + cache_infos[i].open_tag.len + NGX_ATOMIC_T_LEN) *
vod_array_entries(buffer_cache_stat_defs) + names_len + sizeof("\n") - 1;
}
if (perf_counters != NULL)
{
for (i = 0; i < PC_COUNT; i++)
{
result_size += sizeof(PROM_PERF_COUNTER_METRICS) - 1 + (perf_counters_open_tags[i].len + NGX_ATOMIC_T_LEN) * 5;
}
}
// allocate the buffer
p = ngx_palloc(r->pool, result_size);
if (p == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_status_prom_handler: ngx_palloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
response.data = p;
p = ngx_copy(p, PROM_STATUS_PREFIX, sizeof(PROM_STATUS_PREFIX) - 1);
for (i = 0; i < vod_array_entries(cache_infos); i++)
{
cur_cache = *(ngx_buffer_cache_t **)((u_char*)conf + cache_infos[i].conf_offset);
if (cur_cache == NULL)
{
continue;
}
ngx_buffer_cache_get_stats(cur_cache, &stats);
cache_name.data = cache_infos[i].open_tag.data + 1;
cache_name.len = cache_infos[i].open_tag.len - 4;
for (cur_stat = buffer_cache_stat_defs; cur_stat->name.data != NULL; cur_stat++)
{
p = ngx_sprintf(p, PROM_VOD_CACHE_METRIC_FORMAT, &cur_stat->name, &cache_name, *(ngx_atomic_t*)((u_char*)&stats + cur_stat->offset));
}
*p++ = '\n';
}
if (perf_counters != NULL)
{
for (i = 0; i < PC_COUNT; i++)
{
action.data = perf_counters_open_tags[i].data + 1;
action.len = perf_counters_open_tags[i].len - 4;
p = ngx_sprintf(p, PROM_PERF_COUNTER_METRICS,
&action, perf_counters->counters[i].sum,
&action, perf_counters->counters[i].count,
&action, perf_counters->counters[i].max,
&action, perf_counters->counters[i].max_time,
&action, perf_counters->counters[i].max_pid);
}
}
response.len = p - response.data;
if (response.len > result_size)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_status_prom_handler: response length %uz exceeded allocated length %uz",
response.len, result_size);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
return ngx_http_vod_send_response(r, &response, &xml_content_type);
}
ngx_int_t
ngx_http_vod_status_handler(ngx_http_request_t *r)
{
ngx_str_t value;
if (ngx_http_arg(r, (u_char *) "reset", sizeof("reset") - 1, &value) == NGX_OK &&
value.len == 1 &&
value.data[0] == '1')
{
return ngx_http_vod_status_reset(r);
}
if (ngx_http_arg(r, (u_char *) "format", sizeof("format") - 1, &value) == NGX_OK &&
value.len == sizeof("prom") - 1 &&
ngx_strncmp(value.data, "prom", sizeof("prom") - 1) == 0)
{
return ngx_http_vod_status_prom_handler(r);
}
return ngx_http_vod_status_xml_handler(r);
}