77#include <syslog.h>
88#include <sys/file.h>
99
10+ #include "ngxta.h"
1011#include "ngx_http_accounting_hash.h"
1112#include "ngx_http_accounting_module.h"
1213#include "ngx_http_accounting_common.h"
@@ -21,8 +22,6 @@ static ngx_http_accounting_hash_t stats_hash;
2122
2223static ngx_int_t ngx_http_accounting_old_time = 0 ;
2324static ngx_int_t ngx_http_accounting_new_time = 0 ;
24- static ngx_str_t ngx_http_accounting_log ;
25- static ngx_fd_t ngx_http_accounting_log_fd = NGX_INVALID_FILE ;
2625
2726static u_char * ngx_http_accounting_title = (u_char * )"NgxAccounting" ;
2827
@@ -49,12 +48,15 @@ ngx_http_accounting_worker_process_init(ngx_cycle_t *cycle)
4948
5049 ngx_http_accounting_old_time = time -> sec ;
5150 ngx_http_accounting_new_time = time -> sec ;
52- ngx_http_accounting_log = amcf -> log ;
5351 worker_process_timer_interval = amcf -> interval ;
5452 worker_process_timer_perturb = amcf -> perturb ;
5553
56- openlog ((char * )ngx_http_accounting_title , LOG_NDELAY , LOG_SYSLOG );
57- syslog (LOG_INFO , "pid:%i|Process:init" , ngx_getpid ());
54+ if (ngxta_log .file -> fd != NGX_INVALID_FILE ) {
55+ ngx_log_error (NGX_LOG_NOTICE , & ngxta_log , 0 , "pid:%i|worker process start accounting" , ngx_getpid ());
56+ } else {
57+ openlog ((char * )ngx_http_accounting_title , LOG_NDELAY , LOG_SYSLOG );
58+ syslog (LOG_INFO , "pid:%i|worker process start accounting" , ngx_getpid ());
59+ }
5860
5961 rc = ngx_http_accounting_hash_init (& stats_hash , NGX_HTTP_ACCOUNTING_NR_BUCKETS , cycle -> pool );
6062 if (rc != NGX_OK ) {
@@ -71,7 +73,7 @@ ngx_http_accounting_worker_process_init(ngx_cycle_t *cycle)
7173 srand (ngx_getpid ());
7274 time_t perturb_factor = 1 ;
7375 if (worker_process_timer_perturb ) {
74- perturb_factor = (1000 - rand ()%200 );
76+ perturb_factor = (1000 - rand ()%200 );
7577 }
7678
7779 ngx_add_timer (& write_out_ev , worker_process_timer_interval * perturb_factor );
@@ -92,7 +94,11 @@ void ngx_http_accounting_worker_process_exit(ngx_cycle_t *cycle)
9294
9395 worker_process_alarm_handler (NULL );
9496
95- syslog (LOG_INFO , "pid:%i|Process:exit" , ngx_getpid ());
97+ if (ngxta_log .file -> fd != NGX_INVALID_FILE ) {
98+ ngx_log_error (NGX_LOG_NOTICE , & ngxta_log , 0 , "pid:%i|worker process stop accounting" , ngx_getpid ());
99+ } else {
100+ syslog (LOG_INFO , "pid:%i|worker process stop accounting" , ngx_getpid ());
101+ }
96102}
97103
98104ngx_int_t
@@ -121,11 +127,11 @@ ngx_http_accounting_handler(ngx_http_request_t *r)
121127 state = r -> upstream_states -> elts ;
122128 if (state [0 ].status ) {
123129 // not even checking the status here...
124- #if (nginx_version < 1009000 )
125- upstream_req_latency_ms = (state [0 ].response_sec * 1000 + state [0 ].response_msec );
126- #else
127- upstream_req_latency_ms = state [0 ].response_time ;
128- #endif
130+ #if (nginx_version < 1009000 )
131+ upstream_req_latency_ms = (state [0 ].response_sec * 1000 + state [0 ].response_msec );
132+ #else
133+ upstream_req_latency_ms = state [0 ].response_time ;
134+ #endif
129135 }
130136 }
131137 // TODO: key should be cached to save CPU time
@@ -166,11 +172,12 @@ ngx_http_accounting_handler(ngx_http_request_t *r)
166172static ngx_int_t
167173worker_process_write_out_stats (u_char * name , size_t len , void * val , void * para1 , void * para2 )
168174{
169- ngx_uint_t i ;
175+ ngx_uint_t i ;
170176 ngx_http_accounting_stats_t * stats ;
171177
172- char temp_buffer [128 ];
173- char output_buffer [1024 ];
178+ ngx_str_t accounting_msg ;
179+ u_char msg_buf [NGX_MAX_ERROR_STR ];
180+ u_char * p , * last ;
174181
175182 stats = (ngx_http_accounting_stats_t * )val ;
176183
@@ -188,7 +195,12 @@ worker_process_write_out_stats(u_char *name, size_t len, void *val, void *para1,
188195 return NGX_OK ;
189196 }
190197
191- sprintf (output_buffer , "pid:%i|from:%ld|to:%ld|accounting_id:%s|requests:%ld|bytes_in:%ld|bytes_out:%ld|latency_ms:%lu|upstream_latency_ms:%lu" ,
198+ // Output message buffer
199+ p = msg_buf ;
200+ last = msg_buf + NGX_MAX_ERROR_STR ;
201+
202+ p = ngx_slprintf (p , last ,
203+ "pid:%i|from:%i|to:%i|accounting_id:%s|requests:%ui|bytes_in:%ui|bytes_out:%ui|latency_ms:%ui|upstream_latency_ms:%ui" ,
192204 ngx_getpid (),
193205 ngx_http_accounting_old_time ,
194206 ngx_http_accounting_new_time ,
@@ -208,22 +220,28 @@ worker_process_write_out_stats(u_char *name, size_t len, void *val, void *para1,
208220
209221 for (i = 0 ; i < http_status_code_count ; i ++ ) {
210222 if (stats -> http_status_code [i ] > 0 ) {
211- sprintf (temp_buffer , "|%ld:%ld" ,
212- index_to_http_status_code_map [i ],
213- stats -> http_status_code [i ]);
214223
215- strcat (output_buffer , temp_buffer );
224+ p = ngx_slprintf (p , last , "|%i:%i" ,
225+ index_to_http_status_code_map [i ],
226+ stats -> http_status_code [i ] );
216227
217228 stats -> http_status_code [i ] = 0 ;
218229 }
219230 }
220231
221- if (ngx_http_accounting_log_fd != NGX_INVALID_FILE ) {
222- size_t len = ngx_strlen (output_buffer );
223- output_buffer [len ] = '\n' ;
224- ngx_write_fd (ngx_http_accounting_log_fd , output_buffer , len + 1 );
232+ if (p > last - NGX_LINEFEED_SIZE ) {
233+ p = last - NGX_LINEFEED_SIZE ;
234+ }
235+
236+ * p ++ = '\0' ;
237+
238+ accounting_msg .len = p - msg_buf ;
239+ accounting_msg .data = msg_buf ;
240+
241+ if (ngxta_log .file -> fd != NGX_INVALID_FILE ) {
242+ ngx_log_error (NGX_LOG_NOTICE , & ngxta_log , 0 , "%V" , & accounting_msg );
225243 } else {
226- syslog (LOG_INFO , "%s" , output_buffer );
244+ syslog (LOG_INFO , "%s" , msg_buf );
227245 }
228246
229247 return NGX_OK ;
@@ -241,23 +259,8 @@ worker_process_alarm_handler(ngx_event_t *ev)
241259 ngx_http_accounting_old_time = ngx_http_accounting_new_time ;
242260 ngx_http_accounting_new_time = time -> sec ;
243261
244- if (ngx_http_accounting_log .len ) {
245- ngx_http_accounting_log_fd = ngx_open_file (ngx_http_accounting_log .data , NGX_FILE_APPEND , NGX_FILE_CREATE_OR_OPEN , NGX_FILE_DEFAULT_ACCESS );
246- if (ngx_http_accounting_log_fd == NGX_INVALID_FILE ) {
247- syslog (LOG_INFO , "Invalid file: %s" , ngx_http_accounting_log .data );
248- }
249- if (flock (ngx_http_accounting_log_fd , LOCK_EX |LOCK_NB ) == -1 ) {
250- return ; // In the unlikely case that the lock cannot be obtained we will try again on the next alarm
251- }
252- }
253-
254262 ngx_http_accounting_hash_iterate (& stats_hash , worker_process_write_out_stats , NULL , NULL );
255263
256- if (ngx_http_accounting_log_fd != NGX_INVALID_FILE ) {
257- ngx_close_file (ngx_http_accounting_log_fd );
258- ngx_http_accounting_log_fd = NGX_INVALID_FILE ;
259- }
260-
261264 if (ngx_exiting || ev == NULL )
262265 return ;
263266
0 commit comments