~ubuntu-branches/ubuntu/trusty/nginx/trusty-proposed

« back to all changes in this revision

Viewing changes to src/http/modules/ngx_http_stub_status_module.c

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2013-04-25 12:51:45 UTC
  • mfrom: (1.3.28)
  • mto: (1.3.29) (15.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: package-import@ubuntu.com-20130425125145-ugl0wor6bq0u5eae
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/*
3
3
 * Copyright (C) Igor Sysoev
 
4
 * Copyright (C) Nginx, Inc.
4
5
 */
5
6
 
6
7
 
9
10
#include <ngx_http.h>
10
11
 
11
12
 
 
13
static ngx_int_t ngx_http_stub_status_variable(ngx_http_request_t *r,
 
14
    ngx_http_variable_value_t *v, uintptr_t data);
 
15
static ngx_int_t ngx_http_stub_status_add_variables(ngx_conf_t *cf);
 
16
 
12
17
static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd,
13
18
                                 void *conf);
14
19
 
27
32
 
28
33
 
29
34
static ngx_http_module_t  ngx_http_stub_status_module_ctx = {
30
 
    NULL,                                  /* preconfiguration */
 
35
    ngx_http_stub_status_add_variables,    /* preconfiguration */
31
36
    NULL,                                  /* postconfiguration */
32
37
 
33
38
    NULL,                                  /* create main configuration */
57
62
};
58
63
 
59
64
 
 
65
static ngx_http_variable_t  ngx_http_stub_status_vars[] = {
 
66
 
 
67
    { ngx_string("connections_active"), NULL, ngx_http_stub_status_variable,
 
68
      0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
69
 
 
70
    { ngx_string("connections_reading"), NULL, ngx_http_stub_status_variable,
 
71
      1, NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
72
 
 
73
    { ngx_string("connections_writing"), NULL, ngx_http_stub_status_variable,
 
74
      2, NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
75
 
 
76
    { ngx_string("connections_waiting"), NULL, ngx_http_stub_status_variable,
 
77
      3, NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
78
 
 
79
    { ngx_null_string, NULL, NULL, 0, 0, 0 }
 
80
};
 
81
 
 
82
 
60
83
static ngx_int_t ngx_http_status_handler(ngx_http_request_t *r)
61
84
{
62
85
    size_t             size;
63
86
    ngx_int_t          rc;
64
87
    ngx_buf_t         *b;
65
88
    ngx_chain_t        out;
66
 
    ngx_atomic_int_t   ap, hn, ac, rq, rd, wr;
 
89
    ngx_atomic_int_t   ap, hn, ac, rq, rd, wr, wa;
67
90
 
68
91
    if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
69
92
        return NGX_HTTP_NOT_ALLOWED;
75
98
        return rc;
76
99
    }
77
100
 
78
 
    r->headers_out.content_type.len = sizeof("text/plain") - 1;
79
 
    r->headers_out.content_type.data = (u_char *) "text/plain";
 
101
    ngx_str_set(&r->headers_out.content_type, "text/plain");
80
102
 
81
103
    if (r->method == NGX_HTTP_HEAD) {
82
104
        r->headers_out.status = NGX_HTTP_OK;
107
129
    rq = *ngx_stat_requests;
108
130
    rd = *ngx_stat_reading;
109
131
    wr = *ngx_stat_writing;
 
132
    wa = *ngx_stat_waiting;
110
133
 
111
134
    b->last = ngx_sprintf(b->last, "Active connections: %uA \n", ac);
112
135
 
116
139
    b->last = ngx_sprintf(b->last, " %uA %uA %uA \n", ap, hn, rq);
117
140
 
118
141
    b->last = ngx_sprintf(b->last, "Reading: %uA Writing: %uA Waiting: %uA \n",
119
 
                          rd, wr, ac - (rd + wr));
 
142
                          rd, wr, wa);
120
143
 
121
144
    r->headers_out.status = NGX_HTTP_OK;
122
145
    r->headers_out.content_length_n = b->last - b->pos;
123
146
 
124
 
    b->last_buf = 1;
 
147
    b->last_buf = (r == r->main) ? 1 : 0;
125
148
 
126
149
    rc = ngx_http_send_header(r);
127
150
 
133
156
}
134
157
 
135
158
 
 
159
static ngx_int_t
 
160
ngx_http_stub_status_variable(ngx_http_request_t *r,
 
161
    ngx_http_variable_value_t *v, uintptr_t data)
 
162
{
 
163
    u_char            *p;
 
164
    ngx_atomic_int_t   value;
 
165
 
 
166
    p = ngx_pnalloc(r->pool, NGX_ATOMIC_T_LEN);
 
167
    if (p == NULL) {
 
168
        return NGX_ERROR;
 
169
    }
 
170
 
 
171
    switch (data) {
 
172
    case 0:
 
173
        value = *ngx_stat_active;
 
174
        break;
 
175
 
 
176
    case 1:
 
177
        value = *ngx_stat_reading;
 
178
        break;
 
179
 
 
180
    case 2:
 
181
        value = *ngx_stat_writing;
 
182
        break;
 
183
 
 
184
    case 3:
 
185
        value = *ngx_stat_waiting;
 
186
        break;
 
187
 
 
188
    /* suppress warning */
 
189
    default:
 
190
        value = 0;
 
191
        break;
 
192
    }
 
193
 
 
194
    v->len = ngx_sprintf(p, "%uA", value) - p;
 
195
    v->valid = 1;
 
196
    v->no_cacheable = 0;
 
197
    v->not_found = 0;
 
198
    v->data = p;
 
199
 
 
200
    return NGX_OK;
 
201
}
 
202
 
 
203
 
 
204
static ngx_int_t
 
205
ngx_http_stub_status_add_variables(ngx_conf_t *cf)
 
206
{
 
207
    ngx_http_variable_t  *var, *v;
 
208
 
 
209
    for (v = ngx_http_stub_status_vars; v->name.len; v++) {
 
210
        var = ngx_http_add_variable(cf, &v->name, v->flags);
 
211
        if (var == NULL) {
 
212
            return NGX_ERROR;
 
213
        }
 
214
 
 
215
        var->get_handler = v->get_handler;
 
216
        var->data = v->data;
 
217
    }
 
218
 
 
219
    return NGX_OK;
 
220
}
 
221
 
 
222
 
136
223
static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
137
224
{
138
225
    ngx_http_core_loc_conf_t  *clcf;