src/http/modules/ngx_http_stub_status_module.c - nginx source code

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_http_null_variable
  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|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.     size = sizeof("Active connections:  \n") + NGX_ATOMIC_T_LEN
  77.            + sizeof("server accepts handled requests\n") - 1
  78.            + 6 + 3 * NGX_ATOMIC_T_LEN
  79.            + sizeof("Reading:  Writing:  Waiting:  \n") + 3 * NGX_ATOMIC_T_LEN;

  80.     b = ngx_create_temp_buf(r->pool, size);
  81.     if (b == NULL) {
  82.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  83.     }

  84.     out.buf = b;
  85.     out.next = NULL;

  86.     ap = *ngx_stat_accepted;
  87.     hn = *ngx_stat_handled;
  88.     ac = *ngx_stat_active;
  89.     rq = *ngx_stat_requests;
  90.     rd = *ngx_stat_reading;
  91.     wr = *ngx_stat_writing;
  92.     wa = *ngx_stat_waiting;

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

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

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

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

  99.     r->headers_out.status = NGX_HTTP_OK;
  100.     r->headers_out.content_length_n = b->last - b->pos;

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

  103.     rc = ngx_http_send_header(r);

  104.     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  105.         return rc;
  106.     }

  107.     return ngx_http_output_filter(r, &out);
  108. }


  109. static ngx_int_t
  110. ngx_http_stub_status_variable(ngx_http_request_t *r,
  111.     ngx_http_variable_value_t *v, uintptr_t data)
  112. {
  113.     u_char            *p;
  114.     ngx_atomic_int_t   value;

  115.     p = ngx_pnalloc(r->pool, NGX_ATOMIC_T_LEN);
  116.     if (p == NULL) {
  117.         return NGX_ERROR;
  118.     }

  119.     switch (data) {
  120.     case 0:
  121.         value = *ngx_stat_active;
  122.         break;

  123.     case 1:
  124.         value = *ngx_stat_reading;
  125.         break;

  126.     case 2:
  127.         value = *ngx_stat_writing;
  128.         break;

  129.     case 3:
  130.         value = *ngx_stat_waiting;
  131.         break;

  132.     /* suppress warning */
  133.     default:
  134.         value = 0;
  135.         break;
  136.     }

  137.     v->len = ngx_sprintf(p, "%uA", value) - p;
  138.     v->valid = 1;
  139.     v->no_cacheable = 0;
  140.     v->not_found = 0;
  141.     v->data = p;

  142.     return NGX_OK;
  143. }


  144. static ngx_int_t
  145. ngx_http_stub_status_add_variables(ngx_conf_t *cf)
  146. {
  147.     ngx_http_variable_t  *var, *v;

  148.     for (v = ngx_http_stub_status_vars; v->name.len; v++) {
  149.         var = ngx_http_add_variable(cf, &v->name, v->flags);
  150.         if (var == NULL) {
  151.             return NGX_ERROR;
  152.         }

  153.         var->get_handler = v->get_handler;
  154.         var->data = v->data;
  155.     }

  156.     return NGX_OK;
  157. }


  158. static char *
  159. ngx_http_set_stub_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  160. {
  161.     ngx_http_core_loc_conf_t  *clcf;

  162.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  163.     clcf->handler = ngx_http_stub_status_handler;

  164.     return NGX_CONF_OK;
  165. }