-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathngx_realtime_request_module.c
428 lines (333 loc) · 12.4 KB
/
ngx_realtime_request_module.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "ngx_slab_array.h"
#define REALTIME_REQUEST_MODULE_VERSION "0.5"
typedef struct {
ngx_slab_array_t *srv_list; // array of ngx_http_realtime_request_master_srv_conf_t *
ngx_shm_zone_t *shm_zone;
ssize_t shm_size; // defaults is 4M
ngx_slab_pool_t *shpool; //共享内存slab
ngx_int_t startup;
} ngx_http_realtime_request_conf_t;
typedef struct {
ngx_str_t server_name;
ngx_atomic_t request;
ngx_atomic_t sent;
ngx_atomic_t recv;
ngx_atomic_t upstream_recv;
ngx_atomic_t request_20x;
ngx_atomic_t request_30x;
ngx_atomic_t request_40x;
ngx_atomic_t request_50x;
} ngx_http_realtime_request_master_srv_conf_t;
typedef struct {
ngx_http_realtime_request_master_srv_conf_t *mstat;
} ngx_http_realtime_request_srv_conf_t;
static ngx_int_t ngx_http_realtime_request_handler_init(ngx_conf_t *cf);
static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static void * ngx_http_realtime_request_create_conf(ngx_conf_t *cf);
static char * ngx_http_realtime_request_init_conf(ngx_conf_t *cf, void *conf);
static void * ngx_http_realtime_request_create_srv_conf(ngx_conf_t *cf);
static char *ngx_http_realtime_request_merge_srv_conf(ngx_conf_t *cf,
void *parent, void *child);
static char *ngx_http_realtime_request_set_size(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_command_t ngx_http_status_commands[] = {
{ ngx_string("realtime_request"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_http_set_status,
0,
0,
NULL },
{ ngx_string("realtime_zonesize"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_http_realtime_request_set_size,
0,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_realtime_request_module_ctx = {
NULL, /* preconfiguration */
ngx_http_realtime_request_handler_init, /* postconfiguration */
ngx_http_realtime_request_create_conf, /* create main configuration */
ngx_http_realtime_request_init_conf, /* init main configuration */
ngx_http_realtime_request_create_srv_conf, /* create server configuration */
ngx_http_realtime_request_merge_srv_conf, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_realtime_request_module = {
NGX_MODULE_V1,
&ngx_http_realtime_request_module_ctx, /* module context */
ngx_http_status_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t ngx_http_realtime_handler(ngx_http_request_t *r)
{
size_t size;
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
return NGX_HTTP_NOT_ALLOWED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
ngx_str_set(&r->headers_out.content_type, "text/plain");
if (r->method == NGX_HTTP_HEAD) {
r->headers_out.status = NGX_HTTP_OK;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
}
ngx_http_realtime_request_conf_t *rrmf = ngx_http_get_module_main_conf(r, ngx_http_realtime_request_module);
ngx_http_realtime_request_master_srv_conf_t **ptr = (ngx_http_realtime_request_master_srv_conf_t **)(rrmf->srv_list->elts);
size_t i;
size = sizeof("uptime: version:"REALTIME_REQUEST_MODULE_VERSION"\nhost\trequest\trecv\tsent\tupstream_recv\t20x\t30x\t40x\t50x\n") + NGX_ATOMIC_T_LEN;
for (i=0; i < rrmf->srv_list->nalloc; i++)
{
if (*ptr==NULL) break;
size+=(*ptr)->server_name.len + 8 * (NGX_ATOMIC_T_LEN + 1) + 1;
ptr++;
}
b = ngx_create_temp_buf(r->pool, size);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = b;
out.next = NULL;
ngx_time_t *time;
time = ngx_timeofday();
b->last = ngx_sprintf(b->last, "uptime:%ul version:"REALTIME_REQUEST_MODULE_VERSION"\n", time->sec-rrmf->startup);
b->last = ngx_cpymem(b->last, "host\trequest\trecv\tsent\tupstream_recv\t20x\t30x\t40x\t50x\n", sizeof("host\trequest\trecv\tsent\tupstream_recv\t20x\t30x\t40x\t50x\n")-1);
ptr = (ngx_http_realtime_request_master_srv_conf_t **)(rrmf->srv_list->elts);
for (i=0; i < rrmf->srv_list->nalloc; i++)
{
if (*ptr==NULL) break;
b->last = ngx_cpymem(b->last, (*ptr)->server_name.data, (*ptr)->server_name.len);
b->last = ngx_sprintf(b->last, "\t%ul\t%ul\t%ul\t%ul\t%ul\t%ul\t%ul\t%ul\n", (*ptr)->request, (*ptr)->recv, (*ptr)->sent,
(*ptr)->upstream_recv, (*ptr)->request_20x, (*ptr)->request_30x, (*ptr)->request_40x, (*ptr)->request_50x);
ptr++;
}
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = b->last - b->pos;
b->last_buf = (r == r->main) ? 1 : 0;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);
}
static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_realtime_handler;
return NGX_CONF_OK;
}
static char *ngx_http_realtime_request_set_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_realtime_request_conf_t *rrmf;
rrmf = ngx_http_conf_get_module_main_conf(cf, ngx_http_realtime_request_module);
ngx_uint_t i;
ngx_str_t *value;
value = cf->args->elts;
for (i = 1; i < cf->args->nelts; i++) {
rrmf->shm_size = ngx_parse_size(&value[i]);
if ((long)rrmf->shm_size == -1)
{
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[i]);
return NGX_CONF_ERROR;
}
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_realtime_request_handler(ngx_http_request_t *r)
{
ngx_http_realtime_request_conf_t *rrmf;
ngx_http_realtime_request_srv_conf_t *rrsf;
rrmf = ngx_http_get_module_main_conf(r, ngx_http_realtime_request_module);
rrsf = ngx_http_get_module_srv_conf(r, ngx_http_realtime_request_module);
size_t i;
if (rrsf->mstat == NULL)
{
// find another core exists configs
ngx_http_core_srv_conf_t *cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
ngx_shmtx_lock(&rrmf->shpool->mutex);
ngx_http_realtime_request_master_srv_conf_t **ptr = (ngx_http_realtime_request_master_srv_conf_t **)(rrmf->srv_list->elts);
for (i=0; i < rrmf->srv_list->nalloc; i++)
{
if (*ptr==NULL) break;
// printf("checking %s\n",cscf->server_name.data);
if ((*ptr)->server_name.len == cscf->server_name.len &&
ngx_strncmp((*ptr)->server_name.data, cscf->server_name.data, cscf->server_name.len)==0)
{
rrsf->mstat = *ptr;
// printf("%d: initalize %s zone by another records\n", ngx_getpid(), rrsf->mstat->server_name.data);
break;
}
ptr++;
}
ngx_shmtx_unlock(&rrmf->shpool->mutex);
if (rrsf->mstat == NULL)
{
ngx_http_realtime_request_master_srv_conf_t **p = ngx_slab_array_push(rrmf->srv_list);
*p = ngx_slab_alloc(rrmf->shpool, sizeof(ngx_http_realtime_request_master_srv_conf_t));
(*p)->server_name.data = ngx_slab_alloc(rrmf->shpool, cscf->server_name.len);
ngx_shmtx_lock(&rrmf->shpool->mutex);
// after allocate && check again
ptr = (ngx_http_realtime_request_master_srv_conf_t **)(rrmf->srv_list->elts);
for (i=0; i < rrmf->srv_list->nalloc; i++)
{
if (*ptr==NULL) break;
if ((*ptr)->server_name.len == cscf->server_name.len &&
ngx_strncmp((*ptr)->server_name.data, cscf->server_name.data, cscf->server_name.len)==0)
{
rrsf->mstat = *ptr;
break;
}
ptr++;
}
if (rrsf->mstat == NULL)
{
rrsf->mstat = *p;
rrsf->mstat->server_name.len = cscf->server_name.len;
ngx_memcpy(rrsf->mstat->server_name.data, cscf->server_name.data, cscf->server_name.len);
rrsf->mstat->request = 0;
rrsf->mstat->sent = 0;
rrsf->mstat->recv = 0;
rrsf->mstat->request_20x = 0;
rrsf->mstat->request_30x = 0;
rrsf->mstat->request_40x = 0;
rrsf->mstat->request_50x = 0;
ngx_shmtx_unlock(&rrmf->shpool->mutex);
}else
{
ngx_shmtx_unlock(&rrmf->shpool->mutex);
ngx_slab_free(rrmf->shpool, (*p)->server_name.data);
ngx_slab_free(rrmf->shpool, p);
}
// printf("%d: initalize %s zone\n", ngx_getpid(), rrsf->mstat->server_name.data);
}
}
if (r->upstream_states != NULL)
{
ngx_http_upstream_state_t *state;
state = r->upstream_states->elts;
size_t upstream_response_length = 0;
for (i = 0; i < r->upstream_states->nelts; i++) {
if (!state[i].peer) {
continue;
}
upstream_response_length+=state[i].response_length;
}
if (upstream_response_length > 0)
{
ngx_atomic_fetch_add(&rrsf->mstat->upstream_recv, upstream_response_length);
}
}
ngx_atomic_fetch_add(&rrsf->mstat->request, 1);
ngx_atomic_fetch_add(&rrsf->mstat->sent, r->connection->sent);
ngx_atomic_fetch_add(&rrsf->mstat->recv, r->request_length);
if (r->err_status) {
if (r->err_status >= 400 && r->err_status < 410)
{
ngx_atomic_fetch_add(&rrsf->mstat->request_40x, 1);
}else if (r->err_status >= 500 && r->err_status < 510)
{
ngx_atomic_fetch_add(&rrsf->mstat->request_50x, 1);
}
} else if (r->headers_out.status) {
if (r->headers_out.status >= 200 && r->headers_out.status < 210)
{
ngx_atomic_fetch_add(&rrsf->mstat->request_20x, 1);
}else if (r->headers_out.status >= 300 && r->headers_out.status < 310)
{
ngx_atomic_fetch_add(&rrsf->mstat->request_30x, 1);
}
}
return NGX_DECLINED;
}
static void *
ngx_http_realtime_request_create_conf(ngx_conf_t *cf)
{
ngx_http_realtime_request_conf_t *conf;
ngx_time_t *time;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realtime_request_conf_t));
if (conf == NULL) {
return NULL;
}
conf->shm_size = 4194304; // defaults for 4M
time = ngx_timeofday();
conf->startup = time->sec;
return conf;
}
static ngx_int_t
ngx_http_realtime_request_init_zone(ngx_shm_zone_t *shm_zone, void *data)
{
ngx_http_realtime_request_conf_t *rrmf = shm_zone->data;
rrmf->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
rrmf->srv_list = ngx_slab_array_create(rrmf->shpool, 4, sizeof(ngx_http_realtime_request_master_srv_conf_t *));
return NGX_OK;
}
static char * ngx_http_realtime_request_init_conf(ngx_conf_t *cf, void *conf)
{
ngx_http_realtime_request_conf_t *rrmf;
ngx_str_t name = ngx_string("http_realtime_request_zone");
rrmf = conf;
// printf("%d : init size: %ld\n", ngx_getpid(), rrmf->shm_size);
rrmf->shm_zone = ngx_shared_memory_add(cf, &name, rrmf->shm_size, &ngx_http_realtime_request_module);
rrmf->shm_zone->init = ngx_http_realtime_request_init_zone;
rrmf->shm_zone->data = rrmf;
return NGX_CONF_OK;
}
static void *
ngx_http_realtime_request_create_srv_conf(ngx_conf_t *cf)
{
ngx_http_realtime_request_srv_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realtime_request_srv_conf_t));
if (conf == NULL) {
return NULL;
}
return conf;
}
static char *
ngx_http_realtime_request_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_realtime_request_srv_conf_t *conf = child;
conf->mstat = NULL;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_realtime_request_handler_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_realtime_request_handler;
return NGX_OK;
}