forked from zhouchangxun/ngx_healthcheck_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_healthcheck_status.c
773 lines (635 loc) · 23.5 KB
/
ngx_healthcheck_status.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
* Copyright (C) 2017- Changxun Zhou([email protected])
* desc: Healthcheck status interface
*/
#include <nginx.h>
#include <ngx_core.h>
#include <ngx_stream.h>
#include <ngx_http.h>
#include "common.h.in"
#define NGX_CHECK_STATUS_DOWN 0x0001
#define NGX_CHECK_STATUS_UP 0x0002
typedef void (*ngx_upstream_check_status_format_pt) (ngx_buf_t *b,
ngx_upstream_check_peers_t *peers,
ngx_uint_t flag);
typedef struct {
ngx_str_t format;
ngx_str_t content_type;
ngx_upstream_check_status_format_pt output;
} ngx_check_status_conf_t;
typedef struct {
ngx_check_status_conf_t *format;
ngx_flag_t flag;
} ngx_upstream_check_status_ctx_t;
typedef ngx_int_t (*ngx_upstream_check_status_command_pt)
(ngx_upstream_check_status_ctx_t *ctx, ngx_str_t *value);
typedef struct {
ngx_str_t name;
ngx_upstream_check_status_command_pt handler;
} ngx_check_status_command_t;
// check module main config data.
typedef struct {
ngx_uint_t check_shm_size;
ngx_upstream_check_peers_t *peers;
} ngx_upstream_check_main_conf_t;
typedef struct {
ngx_check_status_conf_t *format;
} ngx_upstream_check_loc_conf_t;
// external var declare
extern ngx_uint_t ngx_stream_upstream_check_shm_generation ; //reload counter
extern ngx_upstream_check_peers_t *stream_peers_ctx ; //stream peers data
extern ngx_upstream_check_peers_t *http_peers_ctx ; // http peers data
//begin check_status function declare
static ngx_int_t ngx_upstream_check_status_handler(
ngx_http_request_t *r);
static void ngx_upstream_check_status_parse_args(ngx_http_request_t *r,
ngx_upstream_check_status_ctx_t *ctx);
static ngx_int_t ngx_upstream_check_status_command_format(
ngx_upstream_check_status_ctx_t *ctx, ngx_str_t *value);
static ngx_int_t ngx_upstream_check_status_command_status(
ngx_upstream_check_status_ctx_t *ctx, ngx_str_t *value);
static void ngx_upstream_check_status_html_format(ngx_buf_t *b,
ngx_upstream_check_peers_t *peers, ngx_uint_t flag);
static void ngx_upstream_check_status_csv_format(ngx_buf_t *b,
ngx_upstream_check_peers_t *peers, ngx_uint_t flag);
static void ngx_upstream_check_status_json_format(ngx_buf_t *b,
ngx_upstream_check_peers_t *peers, ngx_uint_t flag);
static ngx_check_status_conf_t *ngx_http_get_check_status_format_conf(
ngx_str_t *str);
static char *ngx_upstream_check_status(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static void *ngx_upstream_check_create_loc_conf(ngx_conf_t *cf);
static char * ngx_upstream_check_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
//end check_status function declare
//1 cmd define
static ngx_command_t ngx_upstream_check_status_commands[] = {
{ ngx_string("healthcheck_status"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1|NGX_CONF_NOARGS,
ngx_upstream_check_status,
0,
0,
NULL },
ngx_null_command
};
//2 ctx define
static ngx_http_module_t ngx_upstream_check_status_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_upstream_check_create_loc_conf, /* create location configuration */
ngx_upstream_check_merge_loc_conf /* merge location configuration */
};
//3 module define
ngx_module_t ngx_upstream_check_status_module = {
NGX_MODULE_V1,
&ngx_upstream_check_status_module_ctx, /* module context */
ngx_upstream_check_status_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
//health checker cmd callback.
static char *
ngx_upstream_check_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_http_core_loc_conf_t *clcf;
ngx_upstream_check_loc_conf_t *uclcf;
value = cf->args->elts;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_upstream_check_status_handler;
if (cf->args->nelts == 2) {
uclcf = ngx_http_conf_get_module_loc_conf(cf,
ngx_upstream_check_status_module);
uclcf->format = ngx_http_get_check_status_format_conf(&value[1]);
if (uclcf->format == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid check format \"%V\"", &value[1]);
return NGX_CONF_ERROR;
}
}
return NGX_CONF_OK;
}
// location config callback
static void *
ngx_upstream_check_create_loc_conf(ngx_conf_t *cf)
{
ngx_upstream_check_loc_conf_t *uclcf;
uclcf = ngx_pcalloc(cf->pool, sizeof(ngx_upstream_check_loc_conf_t));
if (uclcf == NULL) {
return NULL;
}
uclcf->format = NGX_CONF_UNSET_PTR;
return uclcf;
}
static char *
ngx_upstream_check_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child)
{
ngx_str_t format = ngx_string("html");
ngx_upstream_check_loc_conf_t *prev = parent;
ngx_upstream_check_loc_conf_t *conf = child;
ngx_conf_merge_ptr_value(conf->format, prev->format,
ngx_http_get_check_status_format_conf(&format));
return NGX_CONF_OK;
}
static ngx_check_status_conf_t ngx_check_status_formats[] = {
{ ngx_string("html"),
ngx_string("text/html"),
ngx_upstream_check_status_html_format },
{ ngx_string("csv"),
ngx_string("text/plain"),
ngx_upstream_check_status_csv_format },
{ ngx_string("json"),
ngx_string("application/json"), // RFC 4627
ngx_upstream_check_status_json_format },
{ ngx_null_string, ngx_null_string, NULL }
};
static ngx_check_status_command_t ngx_check_status_commands[] = {
{ ngx_string("format"),
ngx_upstream_check_status_command_format },
{ ngx_string("status"),
ngx_upstream_check_status_command_status },
{ ngx_null_string, NULL }
};
/* http request handler. */
static ngx_int_t
ngx_upstream_check_status_handler(ngx_http_request_t *r)
{
size_t buffer_size;
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
ngx_upstream_check_peers_t *peers;
ngx_upstream_check_loc_conf_t *uclcf;
ngx_upstream_check_status_ctx_t *ctx;
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"[ngx-healthcheck][status-interface] recv query request");
if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
return NGX_HTTP_NOT_ALLOWED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
uclcf = ngx_http_get_module_loc_conf(r, ngx_upstream_check_status_module);
ctx = ngx_pcalloc(r->pool, sizeof(ngx_upstream_check_status_ctx_t));
if (ctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_upstream_check_status_parse_args(r, ctx);
if (ctx->format == NULL) {
ctx->format = uclcf->format;
}
r->headers_out.content_type = ctx->format->content_type;
if (r->method == NGX_HTTP_HEAD) {
r->headers_out.status = NGX_HTTP_OK;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
}
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"[ngx-healthcheck][status-interface]"
" stream_peers_ctx:%p, http_peers_ctx:%p",
stream_peers_ctx, http_peers_ctx);
peers = http_peers_ctx;
/*
if (peers == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"[ngx-healthcheck][status-interface] peers == NULL");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
*/
// 1/4 pagesize for each record
if(stream_peers_ctx == NULL){
buffer_size = http_peers_ctx->peers.nelts * ngx_pagesize / 4;
}else{
buffer_size = (stream_peers_ctx->peers.nelts + http_peers_ctx->peers.nelts) * ngx_pagesize / 4;
}
buffer_size = ngx_align(buffer_size, ngx_pagesize) + ngx_pagesize;
b = ngx_create_temp_buf(r->pool, buffer_size);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = b;
out.next = NULL;
ctx->format->output(b, peers, ctx->flag); // construct status data.
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = b->last - b->pos;
if (r->headers_out.content_length_n == 0) {
r->header_only = 1;
}
b->last_buf = 1;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);
}
static void
ngx_upstream_check_status_parse_args(ngx_http_request_t *r,
ngx_upstream_check_status_ctx_t *ctx)
{
ngx_str_t value;
ngx_uint_t i;
ngx_check_status_command_t *command;
if (r->args.len == 0) {
return;
}
for (i = 0; ; i++) {
command = &ngx_check_status_commands[i];
if (command->name.len == 0) {
break;
}
if (ngx_http_arg(r, command->name.data, command->name.len, &value)
== NGX_OK) {
if (command->handler(ctx, &value) != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"stream upstream check, bad argument: \"%V\"",
&value);
}
}
}
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
"stream upstream check, flag: \"%ui\"", ctx->flag);
}
static ngx_int_t
ngx_upstream_check_status_command_format(
ngx_upstream_check_status_ctx_t *ctx, ngx_str_t *value)
{
ctx->format = ngx_http_get_check_status_format_conf(value);
if (ctx->format == NULL) {
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t
ngx_upstream_check_status_command_status(
ngx_upstream_check_status_ctx_t *ctx, ngx_str_t *value)
{
if (value->len == (sizeof("down") - 1)
&& ngx_strncasecmp(value->data, (u_char *) "down", value->len) == 0) {
ctx->flag |= NGX_CHECK_STATUS_DOWN;
} else if (value->len == (sizeof("up") - 1)
&& ngx_strncasecmp(value->data, (u_char *) "up", value->len)
== 0) {
ctx->flag |= NGX_CHECK_STATUS_UP;
} else {
return NGX_ERROR;
}
return NGX_OK;
}
static void
ngx_upstream_check_status_html_format(ngx_buf_t *b,
ngx_upstream_check_peers_t *peers, ngx_uint_t flag)
{
ngx_uint_t i,stream_count,http_count,stream_up_count,http_up_count;
ngx_upstream_check_peer_t *peer;
stream_count = 0;
http_count = 0;
stream_up_count = 0;
http_up_count = 0;
peers = stream_peers_ctx;
if(peers != NULL){
peer = peers->peers.elts;
for (i = 0; i < peers->peers.nelts; i++) {
if (!peer[i].shm->down) {
stream_up_count ++;
}
stream_count++;
}
}
peers = http_peers_ctx; //http
peer = peers->peers.elts;
for (i = 0; i < peers->peers.nelts; i++) {
if (!peer[i].shm->down) {
http_up_count ++;
}
http_count++;
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\n"
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
"<head>\n"
" <title>Nginx upstream status checker</title>\n"
"</head>\n"
"<body>\n"
"<h1 align=\"center\">Nginx upstream status monitor</h1>\n");
// =======begin http data==========
b->last = ngx_snprintf(b->last, b->end - b->last,
"<h2>http upstream servers </h2> up: %ui down: %ui total: %ui\n"
"<table style=\"background-color:white\" cellspacing=\"0\" "
" cellpadding=\"3\" border=\"1\">\n"
" <tr bgcolor=\"#C0C0C0\">\n"
" <th>Index</th>\n"
" <th>Upstream</th>\n"
" <th>Name</th>\n"
" <th>Status</th>\n"
" <th>Rise counts</th>\n"
" <th>Fall counts</th>\n"
" <th>Check type</th>\n"
" <th>Check port</th>\n"
" </tr>\n",
http_up_count, http_count-http_up_count, http_count);
for (i = 0; i < peers->peers.nelts; i++) {
if (flag & NGX_CHECK_STATUS_DOWN) {
if (!peer[i].shm->down) {
continue;
}
} else if (flag & NGX_CHECK_STATUS_UP) {
if (peer[i].shm->down) {
continue;
}
}
b->last = ngx_snprintf(b->last, b->end - b->last,
" <tr%s>\n"
" <td>%ui</td>\n"
" <td>%V</td>\n"
" <td>%V</td>\n"
" <td>%s</td>\n"
" <td>%ui</td>\n"
" <td>%ui</td>\n"
" <td>%V</td>\n"
" <td>%ui</td>\n"
" </tr>\n",
peer[i].shm->down ? " bgcolor=\"#FF0000\"" : "",
i,
peer[i].upstream_name,
&peer[i].peer_addr->name,
peer[i].shm->down ? "down" : "up",
peer[i].shm->rise_count,
peer[i].shm->fall_count,
&peer[i].conf->check_type_conf->name,
peer[i].conf->port);
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"</table>\n");
// =======begin stream data==========
b->last = ngx_snprintf(b->last, b->end - b->last,
"<h2>stream upstream servers </h2> up: %ui down: %ui total: %ui\n"
"<table style=\"background-color:white\" cellspacing=\"0\" "
" cellpadding=\"3\" border=\"1\">\n"
" <tr bgcolor=\"#C0C0C0\">\n"
" <th>Index</th>\n"
" <th>Upstream</th>\n"
" <th>Name</th>\n"
" <th>Status</th>\n"
" <th>Rise counts</th>\n"
" <th>Fall counts</th>\n"
" <th>Check type</th>\n"
" <th>Check port</th>\n"
" </tr>\n",
stream_up_count, stream_count-stream_up_count, stream_count);
peers = stream_peers_ctx; //stream
if(peers != NULL){
peer = peers->peers.elts;
for (i = 0; i < peers->peers.nelts; i++) {
if (flag & NGX_CHECK_STATUS_DOWN) {
if (!peer[i].shm->down) {
continue;
}
} else if (flag & NGX_CHECK_STATUS_UP) {
if (peer[i].shm->down) {
continue;
}
}
b->last = ngx_snprintf(b->last, b->end - b->last,
" <tr%s>\n"
" <td>%ui</td>\n"
" <td>%V</td>\n"
" <td>%V</td>\n"
" <td>%s</td>\n"
" <td>%ui</td>\n"
" <td>%ui</td>\n"
" <td>%V</td>\n"
" <td>%ui</td>\n"
" </tr>\n",
peer[i].shm->down ? " bgcolor=\"#FF0000\"" : "",
i,
peer[i].upstream_name,
&peer[i].peer_addr->name,
peer[i].shm->down ? "down" : "up",
peer[i].shm->rise_count,
peer[i].shm->fall_count,
&peer[i].conf->check_type_conf->name,
peer[i].conf->port);
}
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"</table>\n"
"<h2>total servers(check enabled): %ui </h2>\n"
"</body></html>\n",
stream_count+http_count);
}
static void
ngx_upstream_check_status_csv_format(ngx_buf_t *b,
ngx_upstream_check_peers_t *peers, ngx_uint_t flag)
{
ngx_uint_t i;
ngx_upstream_check_peer_t *peer;
b->last = ngx_snprintf(b->last, b->end - b->last,
"index,upstream_type,upstream_name,host,rise,fall,check_type,check_port,status\n");
peers = http_peers_ctx; //http
peer = peers->peers.elts;
for (i = 0; i < peers->peers.nelts; i++) {
if (flag & NGX_CHECK_STATUS_DOWN) {
if (!peer[i].shm->down) {
continue;
}
} else if (flag & NGX_CHECK_STATUS_UP) {
if (peer[i].shm->down) {
continue;
}
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"%ui,http,%V,%V,%ui,%ui,%V,%ui,%s\n",
i,
peer[i].upstream_name,
&peer[i].peer_addr->name,
peer[i].shm->rise_count,
peer[i].shm->fall_count,
&peer[i].conf->check_type_conf->name,
peer[i].conf->port,
peer[i].shm->down ? "down" : "up");
}
peers = stream_peers_ctx; //stream
if(peers == NULL) return;
peer = peers->peers.elts;
for (i = 0; i < peers->peers.nelts; i++) {
if (flag & NGX_CHECK_STATUS_DOWN) {
if (!peer[i].shm->down) {
continue;
}
} else if (flag & NGX_CHECK_STATUS_UP) {
if (peer[i].shm->down) {
continue;
}
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"%ui,stream,%V,%V,%ui,%ui,%V,%ui,%s\n",
i,
peer[i].upstream_name,
&peer[i].peer_addr->name,
peer[i].shm->rise_count,
peer[i].shm->fall_count,
&peer[i].conf->check_type_conf->name,
peer[i].conf->port,
peer[i].shm->down ? "down" : "up");
}
}
static void
ngx_upstream_check_status_json_format(ngx_buf_t *b,
ngx_upstream_check_peers_t *peers, ngx_uint_t flag)
{
ngx_uint_t count, i;
ngx_uint_t stream_count=0, http_count=0;
ngx_upstream_check_peer_t *peer;
/* calc display total num after filter param*/
count = 0;
peers = stream_peers_ctx; //stream
if(peers != NULL){ //when no stream section, peers is NULL
peer = peers->peers.elts;
for (i = 0; i < peers->peers.nelts; i++) {
if (flag & NGX_CHECK_STATUS_DOWN) {
if (!peer[i].shm->down) {
continue;
}
} else if (flag & NGX_CHECK_STATUS_UP) {
if (peer[i].shm->down) {
continue;
}
}
count++;stream_count++;
}
}
peers = http_peers_ctx; //http
peer = peers->peers.elts;
for (i = 0; i < peers->peers.nelts; i++) {
if (flag & NGX_CHECK_STATUS_DOWN) {
if (!peer[i].shm->down) {
continue;
}
} else if (flag & NGX_CHECK_STATUS_UP) {
if (peer[i].shm->down) {
continue;
}
}
count++;http_count++;
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"{\"servers\": {\n"
" \"total\": %ui,\n"
" \"generation\": %ui,\n"
" \"http\": [\n",
count,
ngx_stream_upstream_check_shm_generation);
//http
count = 0;
for (i = 0; i < peers->peers.nelts; i++) {
if (flag & NGX_CHECK_STATUS_DOWN) {
if (!peer[i].shm->down) {
continue;
}
} else if (flag & NGX_CHECK_STATUS_UP) {
if (peer[i].shm->down) {
continue;
}
}
count++;
b->last = ngx_snprintf(b->last, b->end - b->last,
" {\"index\": %ui, "
"\"upstream\": \"%V\", "
"\"name\": \"%V\", "
"\"status\": \"%s\", "
"\"rise\": %ui, "
"\"fall\": %ui, "
"\"type\": \"%V\", "
"\"port\": %ui}"
"%s\n",
i,
peer[i].upstream_name,
&peer[i].peer_addr->name,
peer[i].shm->down ? "down" : "up",
peer[i].shm->rise_count,
peer[i].shm->fall_count,
&peer[i].conf->check_type_conf->name,
peer[i].conf->port,
(count == http_count) ? "" : ",");
}
//http end
b->last = ngx_snprintf(b->last, b->end - b->last,
" ],\n"
" \"stream\": [\n");
peers = stream_peers_ctx; //stream
count = 0;
if(peers != NULL){
peer = peers->peers.elts;
for (i = 0; i < peers->peers.nelts; i++) {
if (flag & NGX_CHECK_STATUS_DOWN) {
if (!peer[i].shm->down) {
continue;
}
} else if (flag & NGX_CHECK_STATUS_UP) {
if (peer[i].shm->down) {
continue;
}
}
count++;
b->last = ngx_snprintf(b->last, b->end - b->last,
" {\"index\": %ui, "
"\"upstream\": \"%V\", "
"\"name\": \"%V\", "
"\"status\": \"%s\", "
"\"rise\": %ui, "
"\"fall\": %ui, "
"\"type\": \"%V\", "
"\"port\": %ui}"
"%s\n",
i,
peer[i].upstream_name,
&peer[i].peer_addr->name,
peer[i].shm->down ? "down" : "up",
peer[i].shm->rise_count,
peer[i].shm->fall_count,
&peer[i].conf->check_type_conf->name,
peer[i].conf->port,
(count == stream_count) ? "" : ",");
}
}
b->last = ngx_snprintf(b->last, b->end - b->last,
" ]\n");
b->last = ngx_snprintf(b->last, b->end - b->last,
"}}\n");
}
static ngx_check_status_conf_t *
ngx_http_get_check_status_format_conf(ngx_str_t *str)
{
ngx_uint_t i;
for (i = 0; ; i++) {
if (ngx_check_status_formats[i].format.len == 0) {
break;
}
if (str->len != ngx_check_status_formats[i].format.len) {
continue;
}
if (ngx_strncmp(str->data, ngx_check_status_formats[i].format.data,
str->len) == 0)
{
return &ngx_check_status_formats[i];
}
}
return NULL;
}
//end healthcheck status interface