Add 'nginx-mod-traffic-accounting/' from commit 'db5f4adedffd7a365aa69fc94dc97beabc0ffbd7'

git-subtree-dir: nginx-mod-traffic-accounting
git-subtree-mainline: 04da623d08
git-subtree-split: db5f4adedf
This commit is contained in:
Manuel 2022-10-18 00:00:02 +02:00
commit 35ea7cbe48
4 changed files with 384 additions and 0 deletions

View file

@ -0,0 +1,17 @@
pkgbase = nginx-mod-traffic-accounting
pkgdesc = Monitor the incoming and outgoing traffic metrics in realtime for NGINX
pkgver = 2.0+16+g98af5fc
pkgrel = 1
url = https://github.com/Lax/traffic-accounting-nginx-module
arch = x86_64
license = BSD
makedepends = nginx-src
depends = nginx
source = https://github.com/Lax/traffic-accounting-nginx-module/archive/v2.0/traffic-accounting-nginx-module-v2.0.tar.gz
source = d53a4a6.patch
source = server_addr.patch
sha256sums = 8c99c5313e5c822aa5683691c8a0641499b2fa8c67f9e55652817042e21f5986
sha256sums = 9bad093fba01d67098d98122202693ffba20feb20668f73e7712d9d9b9915bf1
sha256sums = e25f170801179e067c7186f1e27fcb3f4273ac5683f6a4962dee821d5eddc8d0
pkgname = nginx-mod-traffic-accounting

View file

@ -0,0 +1,52 @@
# Maintainer: Manuel Hüsers <aur@huesers.de>
pkgname=nginx-mod-traffic-accounting
pkgver=2.0+16+g98af5fc
pkgrel=1
_modname="traffic-accounting-nginx-module"
pkgdesc='Monitor the incoming and outgoing traffic metrics in realtime for NGINX'
arch=('x86_64')
makedepends=('nginx-src')
depends=('nginx')
url="https://github.com/Lax/${_modname}"
license=('BSD')
source=(
"https://github.com/Lax/${_modname}/archive/v${pkgver%%+*}/${_modname}-v${pkgver%%+*}.tar.gz"
"d53a4a6.patch"
"server_addr.patch"
)
sha256sums=(
'8c99c5313e5c822aa5683691c8a0641499b2fa8c67f9e55652817042e21f5986'
'9bad093fba01d67098d98122202693ffba20feb20668f73e7712d9d9b9915bf1'
'e25f170801179e067c7186f1e27fcb3f4273ac5683f6a4962dee821d5eddc8d0'
)
prepare() {
mkdir -p build
cd build
ln -sf /usr/src/nginx/auto
ln -sf /usr/src/nginx/src
cd "../${_modname}-${pkgver%%+*}"
patch -Np1 -i '../d53a4a6.patch'
patch -Np1 -i '../server_addr.patch'
}
build() {
cd build
/usr/src/nginx/configure --with-compat --with-stream --add-dynamic-module="../${_modname}-${pkgver%%+*}"
make modules
}
package() {
install -Dm644 "$srcdir"/"${_modname}-${pkgver%%+*}"/LICENSE \
"$pkgdir"/usr/share/licenses/$pkgname/LICENSE
cd build/objs
for mod in ngx_*.so; do
install -Dm755 $mod "$pkgdir"/usr/lib/nginx/modules/$mod
done
}

View file

@ -0,0 +1,235 @@
diff --git a/src/http/ngx_http_accounting_module.c b/src/http/ngx_http_accounting_module.c
index 63eccf0..23a5fc3 100644
--- a/src/http/ngx_http_accounting_module.c
+++ b/src/http/ngx_http_accounting_module.c
@@ -139,7 +139,7 @@ ngx_http_accounting_process_init(ngx_cycle_t *cycle)
}
if (amcf->current == NULL) {
- if (ngx_traffic_accounting_period_create(cycle->pool, amcf) != NGX_OK)
+ if (ngx_traffic_accounting_period_create(amcf) != NGX_OK)
return NGX_ERROR;
}
@@ -206,7 +206,7 @@ worker_process_alarm_handler(ngx_event_t *ev)
amcf = ngx_http_cycle_get_module_main_conf(ngx_cycle, ngx_http_accounting_module);
- ngx_traffic_accounting_period_rotate(amcf->current->pool, amcf);
+ ngx_traffic_accounting_period_rotate(amcf);
ngx_traffic_accounting_period_rbtree_iterate(amcf->previous,
worker_process_export_metrics,
amcf->previous->created_at,
@@ -243,10 +243,10 @@ ngx_http_accounting_request_handler(ngx_http_request_t *r)
amcf = ngx_http_get_module_main_conf(r, ngx_http_accounting_module);
- metrics = ngx_traffic_accounting_period_fetch_metrics(amcf->current, accounting_id);
+ metrics = ngx_traffic_accounting_period_fetch_metrics(amcf->current, accounting_id, amcf->log);
if (metrics == NULL) { return NGX_ERROR; }
- if (ngx_traffic_accounting_metrics_init(metrics, amcf->current->pool, ngx_http_statuses_len) == NGX_ERROR)
+ if (ngx_traffic_accounting_metrics_init(metrics, ngx_http_statuses_len, amcf->log) == NGX_ERROR)
return NGX_ERROR;
amcf->current->updated_at = ngx_timeofday();
diff --git a/src/ngx_traffic_accounting.h b/src/ngx_traffic_accounting.h
index 1812927..462becf 100644
--- a/src/ngx_traffic_accounting.h
+++ b/src/ngx_traffic_accounting.h
@@ -38,21 +38,19 @@ typedef struct {
ngx_rbtree_t rbtree;
ngx_rbtree_node_t sentinel;
- ngx_pool_t *pool;
-
ngx_time_t *created_at;
ngx_time_t *updated_at;
} ngx_traffic_accounting_period_t;
-ngx_int_t ngx_traffic_accounting_metrics_init(ngx_traffic_accounting_metrics_t *metrics, ngx_pool_t *pool, size_t len);
+ngx_int_t ngx_traffic_accounting_metrics_init(ngx_traffic_accounting_metrics_t *metrics, size_t len, ngx_log_t *log);
ngx_int_t ngx_traffic_accounting_period_init(ngx_traffic_accounting_period_t *period);
-void ngx_traffic_accounting_period_insert(ngx_traffic_accounting_period_t *period, ngx_str_t *name);
+void ngx_traffic_accounting_period_insert(ngx_traffic_accounting_period_t *period, ngx_str_t *name, ngx_log_t *log);
void ngx_traffic_accounting_period_insert_metrics(ngx_traffic_accounting_period_t *period, ngx_traffic_accounting_metrics_t *metrics);
void ngx_traffic_accounting_period_delete(ngx_traffic_accounting_period_t *period, ngx_str_t *name);
void ngx_traffic_accounting_period_delete_metrics(ngx_traffic_accounting_period_t *period, ngx_traffic_accounting_metrics_t *metrics);
ngx_traffic_accounting_metrics_t * ngx_traffic_accounting_period_lookup_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name);
-ngx_traffic_accounting_metrics_t * ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name);
+ngx_traffic_accounting_metrics_t * ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name, ngx_log_t *log);
typedef ngx_int_t (*ngx_traffic_accounting_period_iterate_func)(void *val, void *para1, void *para2);
diff --git a/src/ngx_traffic_accounting_module.c b/src/ngx_traffic_accounting_module.c
index ebc877a..2291db7 100644
--- a/src/ngx_traffic_accounting_module.c
+++ b/src/ngx_traffic_accounting_module.c
@@ -9,15 +9,14 @@
ngx_int_t
-ngx_traffic_accounting_period_create(ngx_pool_t *pool, ngx_traffic_accounting_main_conf_t *amcf)
+ngx_traffic_accounting_period_create(ngx_traffic_accounting_main_conf_t *amcf)
{
ngx_traffic_accounting_period_t *period;
- period = ngx_pcalloc(pool, sizeof(ngx_traffic_accounting_period_t));
+ period = ngx_calloc(sizeof(ngx_traffic_accounting_period_t), amcf->log);
if (period == NULL)
return NGX_ERROR;
- period->pool = pool;
ngx_traffic_accounting_period_init(period);
period->created_at = ngx_timeofday();
@@ -28,11 +27,11 @@ ngx_traffic_accounting_period_create(ngx_pool_t *pool, ngx_traffic_accounting_ma
}
ngx_int_t
-ngx_traffic_accounting_period_rotate(ngx_pool_t *pool, ngx_traffic_accounting_main_conf_t *amcf)
+ngx_traffic_accounting_period_rotate(ngx_traffic_accounting_main_conf_t *amcf)
{
- ngx_pfree(pool, amcf->previous);
+ ngx_free(amcf->previous);
amcf->previous = amcf->current;
- return ngx_traffic_accounting_period_create(pool, amcf);
+ return ngx_traffic_accounting_period_create(amcf);
}
diff --git a/src/ngx_traffic_accounting_module.h b/src/ngx_traffic_accounting_module.h
index 08881f1..adfccec 100644
--- a/src/ngx_traffic_accounting_module.h
+++ b/src/ngx_traffic_accounting_module.h
@@ -46,8 +46,8 @@ ngx_str_t * ngx_traffic_accounting_get_accounting_id(void *entry, ngx_get_loc_co
ngx_get_indexed_variable_pt get_indexed_variable);
-ngx_int_t ngx_traffic_accounting_period_create(ngx_pool_t *pool, ngx_traffic_accounting_main_conf_t *amcf);
-ngx_int_t ngx_traffic_accounting_period_rotate(ngx_pool_t *pool, ngx_traffic_accounting_main_conf_t *amcf);
+ngx_int_t ngx_traffic_accounting_period_create(ngx_traffic_accounting_main_conf_t *amcf);
+ngx_int_t ngx_traffic_accounting_period_rotate(ngx_traffic_accounting_main_conf_t *amcf);
#endif /* _NGX_TRAFFIC_ACCOUNTING_MODULE_H_INCLUDED_ */
diff --git a/src/ngx_traffic_accounting_period_metrics.c b/src/ngx_traffic_accounting_period_metrics.c
index b6b7055..7376ad9 100644
--- a/src/ngx_traffic_accounting_period_metrics.c
+++ b/src/ngx_traffic_accounting_period_metrics.c
@@ -10,17 +10,17 @@
static void ngx_traffic_accounting_period_insert_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
ngx_int_t
-ngx_traffic_accounting_metrics_init(ngx_traffic_accounting_metrics_t *metrics, ngx_pool_t *pool, size_t len)
+ngx_traffic_accounting_metrics_init(ngx_traffic_accounting_metrics_t *metrics, size_t len, ngx_log_t *log)
{
if (metrics->nr_status == NULL) {
- metrics->nr_status = ngx_pcalloc(pool, sizeof(ngx_uint_t) * len);
+ metrics->nr_status = ngx_calloc(sizeof(ngx_uint_t) * len, log);
if (metrics->nr_status == NULL)
return NGX_ERROR;
}
if (metrics->nr_upstream_status == NULL) {
- metrics->nr_upstream_status = ngx_pcalloc(pool, sizeof(ngx_uint_t) * len);
+ metrics->nr_upstream_status = ngx_calloc(sizeof(ngx_uint_t) * len, log);
if (metrics->nr_upstream_status == NULL)
return NGX_ERROR;
@@ -39,14 +39,14 @@ ngx_traffic_accounting_period_init(ngx_traffic_accounting_period_t *period)
}
void
-ngx_traffic_accounting_period_insert(ngx_traffic_accounting_period_t *period, ngx_str_t *name)
+ngx_traffic_accounting_period_insert(ngx_traffic_accounting_period_t *period, ngx_str_t *name, ngx_log_t *log)
{
ngx_traffic_accounting_metrics_t *metrics;
- metrics = ngx_pcalloc(period->pool, sizeof(ngx_traffic_accounting_metrics_t));
+ metrics = ngx_calloc(sizeof(ngx_traffic_accounting_metrics_t), log);
void *data;
- data = ngx_pcalloc(period->pool, name->len+1);
+ data = ngx_calloc(name->len+1, log);
ngx_memcpy(data, name->data, name->len);
metrics->name.data = data;
@@ -82,7 +82,7 @@ void
ngx_traffic_accounting_period_delete_metrics(ngx_traffic_accounting_period_t *period, ngx_traffic_accounting_metrics_t *metrics)
{
ngx_rbtree_delete(&period->rbtree, &metrics->rbnode);
- ngx_pfree(period->pool, metrics);
+ ngx_free(metrics);
}
ngx_traffic_accounting_metrics_t *
@@ -124,7 +124,7 @@ ngx_traffic_accounting_period_lookup_metrics(ngx_traffic_accounting_period_t *pe
}
ngx_traffic_accounting_metrics_t *
-ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name)
+ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name, ngx_log_t *log)
{
ngx_traffic_accounting_metrics_t *n;
@@ -132,7 +132,7 @@ ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *per
if (n != NULL)
return n;
- ngx_traffic_accounting_period_insert(period, name);
+ ngx_traffic_accounting_period_insert(period, name, log);
return ngx_traffic_accounting_period_lookup_metrics(period, name);
}
@@ -158,9 +158,10 @@ ngx_traffic_accounting_period_rbtree_iterate(ngx_traffic_accounting_period_t *pe
if (rc == NGX_DONE) {
/* NGX_DONE -> destroy node */
ngx_rbtree_delete(rbtree, node);
- ngx_pfree(period->pool, n->nr_status);
- ngx_pfree(period->pool, n->nr_upstream_status);
- ngx_pfree(period->pool, n);
+ ngx_free(n->nr_status);
+ ngx_free(n->nr_upstream_status);
+ ngx_free(n->name.data);
+ ngx_free(n);
goto done;
}
diff --git a/src/stream/ngx_stream_accounting_module.c b/src/stream/ngx_stream_accounting_module.c
index b1839d1..3db787d 100644
--- a/src/stream/ngx_stream_accounting_module.c
+++ b/src/stream/ngx_stream_accounting_module.c
@@ -136,7 +136,7 @@ ngx_stream_accounting_process_init(ngx_cycle_t *cycle)
}
if (amcf->current == NULL) {
- if (ngx_traffic_accounting_period_create(cycle->pool, amcf) != NGX_OK)
+ if (ngx_traffic_accounting_period_create(amcf) != NGX_OK)
return NGX_ERROR;
}
@@ -203,7 +203,7 @@ worker_process_alarm_handler(ngx_event_t *ev)
amcf = ngx_stream_cycle_get_module_main_conf(ngx_cycle, ngx_stream_accounting_module);
- ngx_traffic_accounting_period_rotate(amcf->current->pool, amcf);
+ ngx_traffic_accounting_period_rotate(amcf);
ngx_traffic_accounting_period_rbtree_iterate(amcf->previous,
worker_process_export_metrics,
amcf->previous->created_at,
@@ -240,10 +240,10 @@ ngx_stream_accounting_session_handler(ngx_stream_session_t *s)
amcf = ngx_stream_get_module_main_conf(s, ngx_stream_accounting_module);
- metrics = ngx_traffic_accounting_period_fetch_metrics(amcf->current, accounting_id);
+ metrics = ngx_traffic_accounting_period_fetch_metrics(amcf->current, accounting_id, amcf->log);
if (metrics == NULL) { return NGX_ERROR; }
- if (ngx_traffic_accounting_metrics_init(metrics, amcf->current->pool, ngx_stream_statuses_len) == NGX_ERROR)
+ if (ngx_traffic_accounting_metrics_init(metrics, ngx_stream_statuses_len, amcf->log) == NGX_ERROR)
return NGX_ERROR;
amcf->current->updated_at = ngx_timeofday();

View file

@ -0,0 +1,80 @@
diff --git a/src/http/ngx_http_accounting_module.c b/src/http/ngx_http_accounting_module.c
index 23a5fc3..ecc62d2 100644
--- a/src/http/ngx_http_accounting_module.c
+++ b/src/http/ngx_http_accounting_module.c
@@ -237,7 +237,6 @@ ngx_http_accounting_request_handler(ngx_http_request_t *r)
ngx_time_t *tp = ngx_timeofday();
ngx_msec_int_t ms = 0;
ngx_http_upstream_state_t *state;
-
accounting_id = ngx_http_accounting_get_accounting_id(r);
if (accounting_id == NULL) { return NGX_ERROR; }
@@ -251,6 +250,27 @@ ngx_http_accounting_request_handler(ngx_http_request_t *r)
amcf->current->updated_at = ngx_timeofday();
+ if (0 == metrics->server_addr.len) {
+ u_char var_name_buf[256] = {0};
+ ngx_str_t server_addr_var_name = ngx_string("server_addr");
+ ngx_uint_t server_addr_key = ngx_hash_strlow(var_name_buf, server_addr_var_name.data, server_addr_var_name.len);
+ ngx_http_variable_value_t* server_addr_val = ngx_http_get_variable(r, &server_addr_var_name, server_addr_key);
+
+ if (NULL == server_addr_val) {
+ // ngx_log_error(NGXTA_LOG_LEVEL, amcf->log, ngx_errno,
+ // ngx_realpath_n " get server_addr failed");
+ return NGX_ERROR;
+ } else {
+ // ngx_log_error(NGXTA_LOG_LEVEL, amcf->log, 0,
+ // ngx_realpath_n " %s", server_addr_val->data);
+ }
+ metrics->server_addr.data = ngx_calloc(server_addr_val->len + 1, amcf->log);
+ if (NULL == metrics->server_addr.data) {
+ return NGX_ERROR;
+ }
+ memcpy(metrics->server_addr.data, server_addr_val->data, server_addr_val->len);
+ metrics->server_addr.len = server_addr_val->len;
+ }
metrics->nr_entries += 1;
metrics->bytes_in += r->request_length;
metrics->bytes_out += r->connection->sent;
diff --git a/src/ngx_traffic_accounting.h b/src/ngx_traffic_accounting.h
index 462becf..6046d78 100644
--- a/src/ngx_traffic_accounting.h
+++ b/src/ngx_traffic_accounting.h
@@ -24,6 +24,7 @@ typedef struct {
ngx_rbtree_node_t rbnode;
ngx_str_t name;
+ ngx_str_t server_addr;
ngx_uint_t nr_entries;
ngx_uint_t bytes_in;
diff --git a/src/ngx_traffic_accounting_log.c b/src/ngx_traffic_accounting_log.c
index 9b4cd46..652cefb 100644
--- a/src/ngx_traffic_accounting_log.c
+++ b/src/ngx_traffic_accounting_log.c
@@ -35,9 +35,10 @@ ngx_traffic_accounting_log_metrics(void *val, void *para1, void *para2,
last = msg_buf + NGX_MAX_ERROR_STR;
p = ngx_slprintf(p, last,
- "pid:%i|from:%i|to:%i|accounting_id:%V|%s:%ui|bytes_in:%ui|bytes_out:%ui|latency_ms:%ui|upstream_latency_ms:%ui",
+ "pid:%i|from:%i|to:%i|server_addr:%V|accounting_id:%V|%s:%ui|bytes_in:%ui|bytes_out:%ui|latency_ms:%ui|upstream_latency_ms:%ui",
ngx_getpid(),
created_at->sec, updated_at->sec,
+ &metrics->server_addr,
&metrics->name, entry_n,
metrics->nr_entries,
metrics->bytes_in, metrics->bytes_out,
diff --git a/src/ngx_traffic_accounting_period_metrics.c b/src/ngx_traffic_accounting_period_metrics.c
index 7376ad9..d454a3c 100644
--- a/src/ngx_traffic_accounting_period_metrics.c
+++ b/src/ngx_traffic_accounting_period_metrics.c
@@ -161,6 +161,7 @@ ngx_traffic_accounting_period_rbtree_iterate(ngx_traffic_accounting_period_t *pe
ngx_free(n->nr_status);
ngx_free(n->nr_upstream_status);
ngx_free(n->name.data);
+ ngx_free(n->server_addr.data);
ngx_free(n);
goto done;