3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd,
15 static ngx_command_t ngx_http_status_commands[] = {
17 { ngx_string("stub_status"),
18 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
29 static ngx_http_module_t ngx_http_stub_status_module_ctx = {
30 NULL, /* preconfiguration */
31 NULL, /* postconfiguration */
33 NULL, /* create main configuration */
34 NULL, /* init main configuration */
36 NULL, /* create server configuration */
37 NULL, /* merge server configuration */
39 NULL, /* create location configuration */
40 NULL /* merge location configuration */
44 ngx_module_t ngx_http_stub_status_module = {
46 &ngx_http_stub_status_module_ctx, /* module context */
47 ngx_http_status_commands, /* module directives */
48 NGX_HTTP_MODULE, /* module type */
49 NULL, /* init master */
50 NULL, /* init module */
51 NULL, /* init process */
52 NULL, /* init thread */
53 NULL, /* exit thread */
54 NULL, /* exit process */
55 NULL, /* exit master */
60 static ngx_int_t ngx_http_status_handler(ngx_http_request_t *r)
66 ngx_atomic_int_t ap, hn, ac, rq, rd, wr;
68 if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
69 return NGX_HTTP_NOT_ALLOWED;
72 rc = ngx_http_discard_request_body(r);
78 r->headers_out.content_type.len = sizeof("text/plain") - 1;
79 r->headers_out.content_type.data = (u_char *) "text/plain";
81 if (r->method == NGX_HTTP_HEAD) {
82 r->headers_out.status = NGX_HTTP_OK;
84 rc = ngx_http_send_header(r);
86 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
91 size = sizeof("Active connections: \n") + NGX_ATOMIC_T_LEN
92 + sizeof("server accepts handled requests\n") - 1
93 + 6 + 3 * NGX_ATOMIC_T_LEN
94 + sizeof("Reading: Writing: Waiting: \n") + 3 * NGX_ATOMIC_T_LEN;
96 b = ngx_create_temp_buf(r->pool, size);
98 return NGX_HTTP_INTERNAL_SERVER_ERROR;
104 ap = *ngx_stat_accepted;
105 hn = *ngx_stat_handled;
106 ac = *ngx_stat_active;
107 rq = *ngx_stat_requests;
108 rd = *ngx_stat_reading;
109 wr = *ngx_stat_writing;
111 b->last = ngx_sprintf(b->last, "Active connections: %uA \n", ac);
113 b->last = ngx_cpymem(b->last, "server accepts handled requests\n",
114 sizeof("server accepts handled requests\n") - 1);
116 b->last = ngx_sprintf(b->last, " %uA %uA %uA \n", ap, hn, rq);
118 b->last = ngx_sprintf(b->last, "Reading: %uA Writing: %uA Waiting: %uA \n",
119 rd, wr, ac - (rd + wr));
121 r->headers_out.status = NGX_HTTP_OK;
122 r->headers_out.content_length_n = b->last - b->pos;
126 rc = ngx_http_send_header(r);
128 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
132 return ngx_http_output_filter(r, &out);
136 static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
138 ngx_http_core_loc_conf_t *clcf;
140 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
141 clcf->handler = ngx_http_status_handler;