Mercurial > hg > nginx
annotate src/http/modules/ngx_http_stub_status_module.c @ 5080:8da37c1b22a4
Trailing whitespace fix.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sat, 23 Feb 2013 11:50:42 +0000 |
parents | 1c472e3b8c10 |
children | a29c574d61fa |
rev | line source |
---|---|
577 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
577 | 5 */ |
6 | |
487 | 7 |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
5079
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
13 static ngx_int_t ngx_http_stub_status_variable(ngx_http_request_t *r, |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
14 ngx_http_variable_value_t *v, uintptr_t data); |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
15 static ngx_int_t ngx_http_stub_status_add_variables(ngx_conf_t *cf); |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
16 |
487 | 17 static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd, |
18 void *conf); | |
577 | 19 |
487 | 20 static ngx_command_t ngx_http_status_commands[] = { |
21 | |
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 }, | |
28 | |
29 ngx_null_command | |
577 | 30 }; |
487 | 31 |
32 | |
577 | 33 |
667 | 34 static ngx_http_module_t ngx_http_stub_status_module_ctx = { |
5079
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
35 ngx_http_stub_status_add_variables, /* preconfiguration */ |
509 | 36 NULL, /* postconfiguration */ |
487 | 37 |
38 NULL, /* create main configuration */ | |
39 NULL, /* init main configuration */ | |
577 | 40 |
487 | 41 NULL, /* create server configuration */ |
42 NULL, /* merge server configuration */ | |
577 | 43 |
487 | 44 NULL, /* create location configuration */ |
45 NULL /* merge location configuration */ | |
46 }; | |
47 | |
48 | |
49 ngx_module_t ngx_http_stub_status_module = { | |
509 | 50 NGX_MODULE_V1, |
487 | 51 &ngx_http_stub_status_module_ctx, /* module context */ |
52 ngx_http_status_commands, /* module directives */ | |
53 NGX_HTTP_MODULE, /* module type */ | |
541 | 54 NULL, /* init master */ |
487 | 55 NULL, /* init module */ |
541 | 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 | |
487 | 62 }; |
63 | |
64 | |
5079
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
65 static ngx_http_variable_t ngx_http_stub_status_vars[] = { |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
66 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
67 { ngx_string("connections_active"), NULL, ngx_http_stub_status_variable, |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
68 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
69 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
70 { ngx_string("connections_reading"), NULL, ngx_http_stub_status_variable, |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
71 1, NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
72 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
73 { ngx_string("connections_writing"), NULL, ngx_http_stub_status_variable, |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
74 2, NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
75 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
76 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
77 }; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
78 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
79 |
487 | 80 static ngx_int_t ngx_http_status_handler(ngx_http_request_t *r) |
81 { | |
495 | 82 size_t size; |
83 ngx_int_t rc; | |
84 ngx_buf_t *b; | |
85 ngx_chain_t out; | |
86 ngx_atomic_int_t ap, hn, ac, rq, rd, wr; | |
577 | 87 |
487 | 88 if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) { |
89 return NGX_HTTP_NOT_ALLOWED; | |
90 } | |
91 | |
1370
cc114c85be0f
rename ngx_http_discard_body() to ngx_http_discard_request_body()
Igor Sysoev <igor@sysoev.ru>
parents:
667
diff
changeset
|
92 rc = ngx_http_discard_request_body(r); |
487 | 93 |
1374 | 94 if (rc != NGX_OK) { |
487 | 95 return rc; |
96 } | |
97 | |
3516
dd1570b6f237
ngx_str_set() and ngx_str_null()
Igor Sysoev <igor@sysoev.ru>
parents:
1563
diff
changeset
|
98 ngx_str_set(&r->headers_out.content_type, "text/plain"); |
487 | 99 |
100 if (r->method == NGX_HTTP_HEAD) { | |
101 r->headers_out.status = NGX_HTTP_OK; | |
102 | |
103 rc = ngx_http_send_header(r); | |
104 | |
105 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
106 return rc; | |
107 } | |
108 } | |
109 | |
495 | 110 size = sizeof("Active connections: \n") + NGX_ATOMIC_T_LEN |
487 | 111 + sizeof("server accepts handled requests\n") - 1 |
495 | 112 + 6 + 3 * NGX_ATOMIC_T_LEN |
113 + sizeof("Reading: Writing: Waiting: \n") + 3 * NGX_ATOMIC_T_LEN; | |
487 | 114 |
501 | 115 b = ngx_create_temp_buf(r->pool, size); |
116 if (b == NULL) { | |
487 | 117 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
118 } | |
119 | |
120 out.buf = b; | |
121 out.next = NULL; | |
122 | |
123 ap = *ngx_stat_accepted; | |
495 | 124 hn = *ngx_stat_handled; |
487 | 125 ac = *ngx_stat_active; |
126 rq = *ngx_stat_requests; | |
127 rd = *ngx_stat_reading; | |
128 wr = *ngx_stat_writing; | |
129 | |
509 | 130 b->last = ngx_sprintf(b->last, "Active connections: %uA \n", ac); |
487 | 131 |
132 b->last = ngx_cpymem(b->last, "server accepts handled requests\n", | |
133 sizeof("server accepts handled requests\n") - 1); | |
134 | |
509 | 135 b->last = ngx_sprintf(b->last, " %uA %uA %uA \n", ap, hn, rq); |
487 | 136 |
509 | 137 b->last = ngx_sprintf(b->last, "Reading: %uA Writing: %uA Waiting: %uA \n", |
493 | 138 rd, wr, ac - (rd + wr)); |
487 | 139 |
140 r->headers_out.status = NGX_HTTP_OK; | |
141 r->headers_out.content_length_n = b->last - b->pos; | |
142 | |
4611
2b6cb7528409
Allows particular modules to handle subrequests properly.
Andrey Belov <defan@nginx.com>
parents:
4412
diff
changeset
|
143 b->last_buf = (r == r->main) ? 1 : 0; |
487 | 144 |
145 rc = ngx_http_send_header(r); | |
146 | |
147 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
148 return rc; | |
149 } | |
150 | |
1563
022ec9420f80
style fix: remove double semicolons
Igor Sysoev <igor@sysoev.ru>
parents:
1374
diff
changeset
|
151 return ngx_http_output_filter(r, &out); |
487 | 152 } |
153 | |
154 | |
5079
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
155 static ngx_int_t |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
156 ngx_http_stub_status_variable(ngx_http_request_t *r, |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
157 ngx_http_variable_value_t *v, uintptr_t data) |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
158 { |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
159 u_char *p; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
160 ngx_atomic_int_t value; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
161 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
162 p = ngx_pnalloc(r->pool, NGX_ATOMIC_T_LEN); |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
163 if (p == NULL) { |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
164 return NGX_ERROR; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
165 } |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
166 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
167 switch (data) { |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
168 case 0: |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
169 value = *ngx_stat_active; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
170 break; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
171 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
172 case 1: |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
173 value = *ngx_stat_reading; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
174 break; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
175 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
176 case 2: |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
177 value = *ngx_stat_writing; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
178 break; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
179 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
180 /* suppress warning */ |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
181 default: |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
182 value = 0; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
183 break; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
184 } |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
185 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
186 v->len = ngx_sprintf(p, "%uA", value) - p; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
187 v->valid = 1; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
188 v->no_cacheable = 0; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
189 v->not_found = 0; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
190 v->data = p; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
191 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
192 return NGX_OK; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
193 } |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
194 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
195 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
196 static ngx_int_t |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
197 ngx_http_stub_status_add_variables(ngx_conf_t *cf) |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
198 { |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
199 ngx_http_variable_t *var, *v; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
200 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
201 for (v = ngx_http_stub_status_vars; v->name.len; v++) { |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
202 var = ngx_http_add_variable(cf, &v->name, v->flags); |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
203 if (var == NULL) { |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
204 return NGX_ERROR; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
205 } |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
206 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
207 var->get_handler = v->get_handler; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
208 var->data = v->data; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
209 } |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
210 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
211 return NGX_OK; |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
212 } |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
213 |
1c472e3b8c10
Introduced variables in ngx_http_stub_status module.
Andrey Belov <defan@nginx.com>
parents:
4611
diff
changeset
|
214 |
487 | 215 static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
216 { | |
217 ngx_http_core_loc_conf_t *clcf; | |
218 | |
219 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
220 clcf->handler = ngx_http_status_handler; | |
221 | |
222 return NGX_CONF_OK; | |
223 } |