nginx

view src/http/modules/ngx_http_stub_status_module.c @ 5771:c3b08217f2a2

Core: fixed default log initialization. The ngx_log_insert() function may invalidate pointer passed to it, so make sure to don't use it after the ngx_log_insert() call.
author Vladimir Homutov <vl@nginx.com>
date Thu, 24 Jul 2014 16:25:07 +0400
parents ee739104d164
children
line source
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_http.h>
13 static ngx_int_t ngx_http_stub_status_variable(ngx_http_request_t *r,
14 ngx_http_variable_value_t *v, uintptr_t data);
15 static ngx_int_t ngx_http_stub_status_add_variables(ngx_conf_t *cf);
17 static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd,
18 void *conf);
20 static ngx_command_t ngx_http_status_commands[] = {
22 { ngx_string("stub_status"),
23 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
24 ngx_http_set_status,
25 0,
26 0,
27 NULL },
29 ngx_null_command
30 };
34 static ngx_http_module_t ngx_http_stub_status_module_ctx = {
35 ngx_http_stub_status_add_variables, /* preconfiguration */
36 NULL, /* postconfiguration */
38 NULL, /* create main configuration */
39 NULL, /* init main configuration */
41 NULL, /* create server configuration */
42 NULL, /* merge server configuration */
44 NULL, /* create location configuration */
45 NULL /* merge location configuration */
46 };
49 ngx_module_t ngx_http_stub_status_module = {
50 NGX_MODULE_V1,
51 &ngx_http_stub_status_module_ctx, /* module context */
52 ngx_http_status_commands, /* module directives */
53 NGX_HTTP_MODULE, /* module type */
54 NULL, /* init master */
55 NULL, /* init module */
56 NULL, /* init process */
57 NULL, /* init thread */
58 NULL, /* exit thread */
59 NULL, /* exit process */
60 NULL, /* exit master */
61 NGX_MODULE_V1_PADDING
62 };
65 static ngx_http_variable_t ngx_http_stub_status_vars[] = {
67 { ngx_string("connections_active"), NULL, ngx_http_stub_status_variable,
68 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
70 { ngx_string("connections_reading"), NULL, ngx_http_stub_status_variable,
71 1, NGX_HTTP_VAR_NOCACHEABLE, 0 },
73 { ngx_string("connections_writing"), NULL, ngx_http_stub_status_variable,
74 2, NGX_HTTP_VAR_NOCACHEABLE, 0 },
76 { ngx_string("connections_waiting"), NULL, ngx_http_stub_status_variable,
77 3, NGX_HTTP_VAR_NOCACHEABLE, 0 },
79 { ngx_null_string, NULL, NULL, 0, 0, 0 }
80 };
83 static ngx_int_t ngx_http_status_handler(ngx_http_request_t *r)
84 {
85 size_t size;
86 ngx_int_t rc;
87 ngx_buf_t *b;
88 ngx_chain_t out;
89 ngx_atomic_int_t ap, hn, ac, rq, rd, wr, wa;
91 if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
92 return NGX_HTTP_NOT_ALLOWED;
93 }
95 rc = ngx_http_discard_request_body(r);
97 if (rc != NGX_OK) {
98 return rc;
99 }
101 r->headers_out.content_type_len = sizeof("text/plain") - 1;
102 ngx_str_set(&r->headers_out.content_type, "text/plain");
103 r->headers_out.content_type_lowcase = NULL;
105 if (r->method == NGX_HTTP_HEAD) {
106 r->headers_out.status = NGX_HTTP_OK;
108 rc = ngx_http_send_header(r);
110 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
111 return rc;
112 }
113 }
115 size = sizeof("Active connections: \n") + NGX_ATOMIC_T_LEN
116 + sizeof("server accepts handled requests\n") - 1
117 + 6 + 3 * NGX_ATOMIC_T_LEN
118 + sizeof("Reading: Writing: Waiting: \n") + 3 * NGX_ATOMIC_T_LEN;
120 b = ngx_create_temp_buf(r->pool, size);
121 if (b == NULL) {
122 return NGX_HTTP_INTERNAL_SERVER_ERROR;
123 }
125 out.buf = b;
126 out.next = NULL;
128 ap = *ngx_stat_accepted;
129 hn = *ngx_stat_handled;
130 ac = *ngx_stat_active;
131 rq = *ngx_stat_requests;
132 rd = *ngx_stat_reading;
133 wr = *ngx_stat_writing;
134 wa = *ngx_stat_waiting;
136 b->last = ngx_sprintf(b->last, "Active connections: %uA \n", ac);
138 b->last = ngx_cpymem(b->last, "server accepts handled requests\n",
139 sizeof("server accepts handled requests\n") - 1);
141 b->last = ngx_sprintf(b->last, " %uA %uA %uA \n", ap, hn, rq);
143 b->last = ngx_sprintf(b->last, "Reading: %uA Writing: %uA Waiting: %uA \n",
144 rd, wr, wa);
146 r->headers_out.status = NGX_HTTP_OK;
147 r->headers_out.content_length_n = b->last - b->pos;
149 b->last_buf = (r == r->main) ? 1 : 0;
150 b->last_in_chain = 1;
152 rc = ngx_http_send_header(r);
154 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
155 return rc;
156 }
158 return ngx_http_output_filter(r, &out);
159 }
162 static ngx_int_t
163 ngx_http_stub_status_variable(ngx_http_request_t *r,
164 ngx_http_variable_value_t *v, uintptr_t data)
165 {
166 u_char *p;
167 ngx_atomic_int_t value;
169 p = ngx_pnalloc(r->pool, NGX_ATOMIC_T_LEN);
170 if (p == NULL) {
171 return NGX_ERROR;
172 }
174 switch (data) {
175 case 0:
176 value = *ngx_stat_active;
177 break;
179 case 1:
180 value = *ngx_stat_reading;
181 break;
183 case 2:
184 value = *ngx_stat_writing;
185 break;
187 case 3:
188 value = *ngx_stat_waiting;
189 break;
191 /* suppress warning */
192 default:
193 value = 0;
194 break;
195 }
197 v->len = ngx_sprintf(p, "%uA", value) - p;
198 v->valid = 1;
199 v->no_cacheable = 0;
200 v->not_found = 0;
201 v->data = p;
203 return NGX_OK;
204 }
207 static ngx_int_t
208 ngx_http_stub_status_add_variables(ngx_conf_t *cf)
209 {
210 ngx_http_variable_t *var, *v;
212 for (v = ngx_http_stub_status_vars; v->name.len; v++) {
213 var = ngx_http_add_variable(cf, &v->name, v->flags);
214 if (var == NULL) {
215 return NGX_ERROR;
216 }
218 var->get_handler = v->get_handler;
219 var->data = v->data;
220 }
222 return NGX_OK;
223 }
226 static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
227 {
228 ngx_http_core_loc_conf_t *clcf;
230 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
231 clcf->handler = ngx_http_status_handler;
233 return NGX_CONF_OK;
234 }