Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_http.h>
0011 
0012 
0013 static ngx_int_t ngx_http_stub_status_handler(ngx_http_request_t *r);
0014 static ngx_int_t ngx_http_stub_status_variable(ngx_http_request_t *r,
0015     ngx_http_variable_value_t *v, uintptr_t data);
0016 static ngx_int_t ngx_http_stub_status_add_variables(ngx_conf_t *cf);
0017 static char *ngx_http_set_stub_status(ngx_conf_t *cf, ngx_command_t *cmd,
0018     void *conf);
0019 
0020 
0021 static ngx_command_t  ngx_http_status_commands[] = {
0022 
0023     { ngx_string("stub_status"),
0024       NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,
0025       ngx_http_set_stub_status,
0026       0,
0027       0,
0028       NULL },
0029 
0030       ngx_null_command
0031 };
0032 
0033 
0034 static ngx_http_module_t  ngx_http_stub_status_module_ctx = {
0035     ngx_http_stub_status_add_variables,    /* preconfiguration */
0036     NULL,                                  /* postconfiguration */
0037 
0038     NULL,                                  /* create main configuration */
0039     NULL,                                  /* init main configuration */
0040 
0041     NULL,                                  /* create server configuration */
0042     NULL,                                  /* merge server configuration */
0043 
0044     NULL,                                  /* create location configuration */
0045     NULL                                   /* merge location configuration */
0046 };
0047 
0048 
0049 ngx_module_t  ngx_http_stub_status_module = {
0050     NGX_MODULE_V1,
0051     &ngx_http_stub_status_module_ctx,      /* module context */
0052     ngx_http_status_commands,              /* module directives */
0053     NGX_HTTP_MODULE,                       /* module type */
0054     NULL,                                  /* init master */
0055     NULL,                                  /* init module */
0056     NULL,                                  /* init process */
0057     NULL,                                  /* init thread */
0058     NULL,                                  /* exit thread */
0059     NULL,                                  /* exit process */
0060     NULL,                                  /* exit master */
0061     NGX_MODULE_V1_PADDING
0062 };
0063 
0064 
0065 static ngx_http_variable_t  ngx_http_stub_status_vars[] = {
0066 
0067     { ngx_string("connections_active"), NULL, ngx_http_stub_status_variable,
0068       0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
0069 
0070     { ngx_string("connections_reading"), NULL, ngx_http_stub_status_variable,
0071       1, NGX_HTTP_VAR_NOCACHEABLE, 0 },
0072 
0073     { ngx_string("connections_writing"), NULL, ngx_http_stub_status_variable,
0074       2, NGX_HTTP_VAR_NOCACHEABLE, 0 },
0075 
0076     { ngx_string("connections_waiting"), NULL, ngx_http_stub_status_variable,
0077       3, NGX_HTTP_VAR_NOCACHEABLE, 0 },
0078 
0079     { ngx_null_string, NULL, NULL, 0, 0, 0 }
0080 };
0081 
0082 
0083 static ngx_int_t
0084 ngx_http_stub_status_handler(ngx_http_request_t *r)
0085 {
0086     size_t             size;
0087     ngx_int_t          rc;
0088     ngx_buf_t         *b;
0089     ngx_chain_t        out;
0090     ngx_atomic_int_t   ap, hn, ac, rq, rd, wr, wa;
0091 
0092     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
0093         return NGX_HTTP_NOT_ALLOWED;
0094     }
0095 
0096     rc = ngx_http_discard_request_body(r);
0097 
0098     if (rc != NGX_OK) {
0099         return rc;
0100     }
0101 
0102     r->headers_out.content_type_len = sizeof("text/plain") - 1;
0103     ngx_str_set(&r->headers_out.content_type, "text/plain");
0104     r->headers_out.content_type_lowcase = NULL;
0105 
0106     if (r->method == NGX_HTTP_HEAD) {
0107         r->headers_out.status = NGX_HTTP_OK;
0108 
0109         rc = ngx_http_send_header(r);
0110 
0111         if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
0112             return rc;
0113         }
0114     }
0115 
0116     size = sizeof("Active connections:  \n") + NGX_ATOMIC_T_LEN
0117            + sizeof("server accepts handled requests\n") - 1
0118            + 6 + 3 * NGX_ATOMIC_T_LEN
0119            + sizeof("Reading:  Writing:  Waiting:  \n") + 3 * NGX_ATOMIC_T_LEN;
0120 
0121     b = ngx_create_temp_buf(r->pool, size);
0122     if (b == NULL) {
0123         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0124     }
0125 
0126     out.buf = b;
0127     out.next = NULL;
0128 
0129     ap = *ngx_stat_accepted;
0130     hn = *ngx_stat_handled;
0131     ac = *ngx_stat_active;
0132     rq = *ngx_stat_requests;
0133     rd = *ngx_stat_reading;
0134     wr = *ngx_stat_writing;
0135     wa = *ngx_stat_waiting;
0136 
0137     b->last = ngx_sprintf(b->last, "Active connections: %uA \n", ac);
0138 
0139     b->last = ngx_cpymem(b->last, "server accepts handled requests\n",
0140                          sizeof("server accepts handled requests\n") - 1);
0141 
0142     b->last = ngx_sprintf(b->last, " %uA %uA %uA \n", ap, hn, rq);
0143 
0144     b->last = ngx_sprintf(b->last, "Reading: %uA Writing: %uA Waiting: %uA \n",
0145                           rd, wr, wa);
0146 
0147     r->headers_out.status = NGX_HTTP_OK;
0148     r->headers_out.content_length_n = b->last - b->pos;
0149 
0150     b->last_buf = (r == r->main) ? 1 : 0;
0151     b->last_in_chain = 1;
0152 
0153     rc = ngx_http_send_header(r);
0154 
0155     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
0156         return rc;
0157     }
0158 
0159     return ngx_http_output_filter(r, &out);
0160 }
0161 
0162 
0163 static ngx_int_t
0164 ngx_http_stub_status_variable(ngx_http_request_t *r,
0165     ngx_http_variable_value_t *v, uintptr_t data)
0166 {
0167     u_char            *p;
0168     ngx_atomic_int_t   value;
0169 
0170     p = ngx_pnalloc(r->pool, NGX_ATOMIC_T_LEN);
0171     if (p == NULL) {
0172         return NGX_ERROR;
0173     }
0174 
0175     switch (data) {
0176     case 0:
0177         value = *ngx_stat_active;
0178         break;
0179 
0180     case 1:
0181         value = *ngx_stat_reading;
0182         break;
0183 
0184     case 2:
0185         value = *ngx_stat_writing;
0186         break;
0187 
0188     case 3:
0189         value = *ngx_stat_waiting;
0190         break;
0191 
0192     /* suppress warning */
0193     default:
0194         value = 0;
0195         break;
0196     }
0197 
0198     v->len = ngx_sprintf(p, "%uA", value) - p;
0199     v->valid = 1;
0200     v->no_cacheable = 0;
0201     v->not_found = 0;
0202     v->data = p;
0203 
0204     return NGX_OK;
0205 }
0206 
0207 
0208 static ngx_int_t
0209 ngx_http_stub_status_add_variables(ngx_conf_t *cf)
0210 {
0211     ngx_http_variable_t  *var, *v;
0212 
0213     for (v = ngx_http_stub_status_vars; v->name.len; v++) {
0214         var = ngx_http_add_variable(cf, &v->name, v->flags);
0215         if (var == NULL) {
0216             return NGX_ERROR;
0217         }
0218 
0219         var->get_handler = v->get_handler;
0220         var->data = v->data;
0221     }
0222 
0223     return NGX_OK;
0224 }
0225 
0226 
0227 static char *
0228 ngx_http_set_stub_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0229 {
0230     ngx_http_core_loc_conf_t  *clcf;
0231 
0232     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
0233     clcf->handler = ngx_http_stub_status_handler;
0234 
0235     return NGX_CONF_OK;
0236 }