Re: Сбор статистики о файле

Igor Sysoev is at rambler-co.ru
Thu Apr 13 18:16:24 MSD 2006


On Thu, 13 Apr 2006, ForJest wrote:

> Игорь, я пытаюсь написать модуль для тела ответа, но пока не могу его
> активировать.
> Т.е. я уже научился компилировать его, включил в конфигурацию и т.п., но
> в цепочку судя по логу не попадает.
> Остальные дебаг печать попадают нормально.
> Пожалуйста посоветуйте - что я делаю неверно?

config файл модуля должен быть примерно таким:
-------------
ngx_addon_name=ngx_http_request_stats_filter_module

HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_request_stats_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_request_stats_filter_module.c"
-------------

> Я пытаюсь сделать чтобы передавалось имя файла в модуль, и если оно
> есть, то активировался этот модуль. Я планирую туда запихать
> переменные из rewrite впоследствии.
>
> Ещё вопрос - как правильно включить debug логи? Если просто сделать
> error_log debug, то там светися только accept и т.п., мне пришлось
> сделать хак небольшой:
>
> log->log_level = NGX_LOG_DEBUG_HTTP | NGX_LOG_DEBUG_EVENT | NGX_LOG_DEBUG_CORE;
> Но я вижу
> static const char *debug_levels[] = {
>    "debug_core", "debug_alloc", "debug_mutex", "debug_event",
>    "debug_http", "debug_imap"
> };
> Но не знаю куда это и как вставить правильно.

По идее, error_log debug достаточно.

> On 4/11/06, Igor Sysoev <is at rambler-co.ru> wrote:
>> On Mon, 10 Apr 2006, ForJest wrote:
>>
>>> У меня возникла задача собирать статитстику об отдаваемом файле и
>>> записывать её в отдельный файл/или передавать какому-нибудь демону,
>>> раз, в допустим, 1-2 секунды.
>>> Характеристики:
>>> - Сколько отдано на данный момент байтов
>>> - Скорость последняя отдачи файла
>>> - Скорость усреднённая соединения
>>> и т.п.
>>>
>>> Система FreeBSD 5.4, используется ограничение по скорости через x-limit-rate.
>>>
>>> Где лучше это вставить/какой модуль ковырять? Я, конечно, со временем
>>> разберусь в коде, но думаю Вы можете существенно сократить время моих
>>> поисков.
>>
>> Скорее всего, нужно писать фильтр для тела ответа.
>>
>>
>> Игорь Сысоев
>> http://sysoev.ru
>>
>>
> /*
> * Copyright (C) Igor Sysoev
> */
>
>
> #include <ngx_config.h>
> #include <ngx_core.h>
> #include <ngx_http.h>
>
> typedef struct {
>    ngx_str_t   request_stats_filename;
> } ngx_http_request_stats_conf_t;
>
> static ngx_int_t ngx_http_request_stats_filter_init(ngx_cycle_t *cycle);
> static void *ngx_http_request_stats_create_conf(ngx_conf_t *cf);
>
> static ngx_command_t  ngx_http_request_stats_commands[] = {
>
>    { ngx_string("request_stats_filename"),
>      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
>      ngx_conf_set_str_slot,
>      NGX_HTTP_LOC_CONF_OFFSET,
>      offsetof(ngx_http_request_stats_conf_t, request_stats_filename),
>      NULL },
>
>      ngx_null_command
> };
>
> static ngx_http_module_t  ngx_http_request_stats_filter_module_ctx = {
>    NULL,                                  /* preconfiguration */
>    NULL,                                  /* postconfiguration */
>
>    NULL,                                  /* create main configuration */
>    NULL,                                  /* init main configuration */
>
>    NULL,                                  /* create server configuration */
>    NULL,                                  /* merge server configuration */
>
>    ngx_http_request_stats_create_conf,         /* create location
> configuration */
>    NULL                                   /* merge location configuration */
> };
>
>
> ngx_module_t  ngx_http_request_stats_filter_module = {
>    NGX_MODULE_V1,
>    &ngx_http_request_stats_filter_module_ctx,  /* module context */
>    ngx_http_request_stats_commands,            /* module directives */
>    NGX_HTTP_MODULE,                       /* module type */
>    NULL,                                  /* init master */
>    ngx_http_request_stats_filter_init,         /* init module */
>    NULL,                                  /* init process */
>    NULL,                                  /* init thread */
>    NULL,                                  /* exit thread */
>    NULL,                                  /* exit process */
>    NULL,                                  /* exit master */
>    NGX_MODULE_V1_PADDING
> };
>
> static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;
>
> static ngx_int_t
> ngx_http_request_stats_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
> {
>     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
>                   "request stats %d", ngx_time());
>     return ngx_http_next_body_filter(r, in);
> }
>
> static ngx_int_t
> ngx_http_request_stats_filter_init(ngx_cycle_t *cycle)
> {
>    ngx_http_next_body_filter = ngx_http_top_body_filter;
>    ngx_http_top_body_filter = ngx_http_request_stats_body_filter;
>    return NGX_OK;
> }
>
> static void *
> ngx_http_request_stats_create_conf(ngx_conf_t *cf)
> {
>    ngx_http_request_stats_conf_t  *conf;
>
>    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_request_stats_conf_t));
>    if (conf == NULL) {
>        return NGX_CONF_ERROR;
>    }
>    return conf;
> }
>
>
>

Игорь Сысоев
http://sysoev.ru





More information about the nginx-ru mailing list