src/http/modules/ngx_http_stub_status_module.c - nginx-1.7.10

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. static ngx_int_t ngx_http_stub_status_handler(ngx_http_request_t *r);
  9. static ngx_int_t ngx_http_stub_status_variable(ngx_http_request_t *r,
  10.     ngx_http_variable_value_t *v, uintptr_t data);
  11. static ngx_int_t ngx_http_stub_status_add_variables(ngx_conf_t *cf);
  12. static char *ngx_http_set_stub_status(ngx_conf_t *cf, ngx_command_t *cmd,
  13.     void *conf);


  14. static ngx_command_t  ngx_http_status_commands[] = {

  15.     { ngx_string("stub_status"),
  16.       NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,
  17.       ngx_http_set_stub_status,
  18.       0,
  19.       0,
  20.       NULL },

  21.       ngx_null_command
  22. };


  23. static ngx_http_module_t  ngx_http_stub_status_module_ctx = {
  24.     ngx_http_stub_status_add_variables,    /* preconfiguration */
  25.     NULL,                                  /* postconfiguration */

  26.     NULL,                                  /* create main configuration */
  27.     NULL,                                  /* init main configuration */

  28.     NULL,                                  /* create server configuration */
  29.     NULL,                                  /* merge server configuration */

  30.     NULL,                                  /* create location configuration */
  31.     NULL                                   /* merge location configuration */
  32. };


  33. ngx_module_t  ngx_http_stub_status_module = {
  34.     NGX_MODULE_V1,
  35.     &ngx_http_stub_status_module_ctx,      /* module context */
  36.     ngx_http_status_commands,              /* module directives */
  37.     NGX_HTTP_MODULE,                       /* module type */
  38.     NULL,                                  /* init master */
  39.     NULL,                                  /* init module */
  40.     NULL,                                  /* init process */
  41.     NULL,                                  /* init thread */
  42.     NULL,                                  /* exit thread */
  43.     NULL,                                  /* exit process */
  44.     NULL,                                  /* exit master */
  45.     NGX_MODULE_V1_PADDING
  46. };


  47. static ngx_http_variable_t  ngx_http_stub_status_vars[] = {

  48.     { ngx_string("connections_active"), NULL, ngx_http_stub_status_variable,
  49.       0, NGX_HTTP_VAR_NOCACHEABLE, 0 },

  50.     { ngx_string("connections_reading"), NULL, ngx_http_stub_status_variable,
  51.       1, NGX_HTTP_VAR_NOCACHEABLE, 0 },

  52.     { ngx_string("connections_writing"), NULL, ngx_http_stub_status_variable,
  53.       2, NGX_HTTP_VAR_NOCACHEABLE, 0 },

  54.     { ngx_string("connections_waiting"), NULL, ngx_http_stub_status_variable,
  55.       3, NGX_HTTP_VAR_NOCACHEABLE, 0 },

  56.     { ngx_null_string, NULL, NULL, 0, 0, 0 }
  57. };


  58. static ngx_int_t
  59. ngx_http_stub_status_handler(ngx_http_request_t *r)
  60. {
  61.     size_t             size;
  62.     ngx_int_t          rc;
  63.     ngx_buf_t         *b;
  64.     ngx_chain_t        out;
  65.     ngx_atomic_int_t   ap, hn, ac, rq, rd, wr, wa;

  66.     if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
  67.         return NGX_HTTP_NOT_ALLOWED;
  68.     }

  69.     rc = ngx_http_discard_request_body(r);

  70.     if (rc != NGX_OK) {
  71.         return rc;
  72.     }

  73.     r->headers_out.content_type_len = sizeof("text/plain") - 1;
  74.     ngx_str_set(&r->headers_out.content_type, "text/plain");
  75.     r->headers_out.content_type_lowcase = NULL;

  76.     if (r->method == NGX_HTTP_HEAD) {
  77.         r->headers_out.status = NGX_HTTP_OK;

  78.         rc = ngx_http_send_header(r);

  79.         if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  80.             return rc;
  81.         }
  82.     }

  83.     size = sizeof("Active connections:  \n") + NGX_ATOMIC_T_LEN
  84.            + sizeof("server accepts handled requests\n") - 1
  85.            + 6 + 3 * NGX_ATOMIC_T_LEN
  86.            + sizeof("Reading:  Writing:  Waiting:  \n") + 3 * NGX_ATOMIC_T_LEN;

  87.     b = ngx_create_temp_buf(r->pool, size);
  88.     if (b == NULL) {
  89.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  90.     }

  91.     out.buf = b;
  92.     out.next = NULL;

  93.     ap = *ngx_stat_accepted;
  94.     hn = *ngx_stat_handled;
  95.     ac = *ngx_stat_active;
  96.     rq = *ngx_stat_requests;
  97.     rd = *ngx_stat_reading;
  98.     wr = *ngx_stat_writing;
  99.     wa = *ngx_stat_waiting;

  100.     b->last = ngx_sprintf(b->last, "Active connections: %uA \n", ac);

  101.     b->last = ngx_cpymem(b->last, "server accepts handled requests\n",
  102.                          sizeof("server accepts handled requests\n") - 1);

  103.     b->last = ngx_sprintf(b->last, " %uA %uA %uA \n", ap, hn, rq);

  104.     b->last = ngx_sprintf(b->last, "Reading: %uA Writing: %uA Waiting: %uA \n",
  105.                           rd, wr, wa);

  106.     r->headers_out.status = NGX_HTTP_OK;
  107.     r->headers_out.content_length_n = b->last - b->pos;

  108.     b->last_buf = (r == r->main) ? 1 : 0;
  109.     b->last_in_chain = 1;

  110.     rc = ngx_http_send_header(r);

  111.     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  112.         return rc;
  113.     }

  114.     return ngx_http_output_filter(r, &out);
  115. }


  116. static ngx_int_t
  117. ngx_http_stub_status_variable(ngx_http_request_t *r,
  118.     ngx_http_variable_value_t *v, uintptr_t data)
  119. {
  120.     u_char            *p;
  121.     ngx_atomic_int_t   value;

  122.     p = ngx_pnalloc(r->pool, NGX_ATOMIC_T_LEN);
  123.     if (p == NULL) {
  124.         return NGX_ERROR;
  125.     }

  126.     switch (data) {
  127.     case 0:
  128.         value = *ngx_stat_active;
  129.         break;

  130.     case 1:
  131.         value = *ngx_stat_reading;
  132.         break;

  133.     case 2:
  134.         value = *ngx_stat_writing;
  135.         break;

  136.     case 3:
  137.         value = *ngx_stat_waiting;
  138.         break;

  139.     /* suppress warning */
  140.     default:
  141.         value = 0;
  142.         break;
  143.     }

  144.     v->len = ngx_sprintf(p, "%uA", value) - p;
  145.     v->valid = 1;
  146.     v->no_cacheable = 0;
  147.     v->not_found = 0;
  148.     v->data = p;

  149.     return NGX_OK;
  150. }


  151. static ngx_int_t
  152. ngx_http_stub_status_add_variables(ngx_conf_t *cf)
  153. {
  154.     ngx_http_variable_t  *var, *v;

  155.     for (v = ngx_http_stub_status_vars; v->name.len; v++) {
  156.         var = ngx_http_add_variable(cf, &v->name, v->flags);
  157.         if (var == NULL) {
  158.             return NGX_ERROR;
  159.         }

  160.         var->get_handler = v->get_handler;
  161.         var->data = v->data;
  162.     }

  163.     return NGX_OK;
  164. }


  165. static char *
  166. ngx_http_set_stub_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  167. {
  168.     ngx_http_core_loc_conf_t  *clcf;

  169.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  170.     clcf->handler = ngx_http_stub_status_handler;

  171.     return NGX_CONF_OK;
  172. }