-
Notifications
You must be signed in to change notification settings - Fork 7
/
ngx_health_detect_api.c
2011 lines (1745 loc) · 69.9 KB
/
ngx_health_detect_api.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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "cJSON.h"
#include "ngx_health_detect_common.h"
#define NGX_CHECK_HTTP_2XX 0x0002
#define NGX_CHECK_HTTP_3XX 0x0004
#define NGX_CHECK_HTTP_4XX 0x0008
#define NGX_CHECK_HTTP_5XX 0x0010
#define NGX_CHECK_HTTP_ERR 0x8000
#define NGX_PRASE_REQ_OK 0
#define NGX_PRASE_REQ_ERR -1
#define NGX_PRASE_REQ_PEER_NAME_TOO_LONG -2
#define NGX_PRASE_REQ_INVALID_PEER_TYPE -3
#define NGX_PRASE_REQ_PEER_TYPE_NOT_FOUND -4
#define NGX_PRASE_REQ_INVALID_PEER_ADDR -5
#define NGX_PRASE_REQ_PEER_ADDR_NOT_FOUND -6
#define NGX_PRASE_REQ_SEND_CONTENT_TOO_LONG -7
#define NGX_PRASE_REQ_INVALID_ALERT_METHOD -8
#define NGX_PRASE_REQ_INVALID_EXPECT_RESPONSE_STATUS -9
#define NGX_PRASE_REQ_INVALID_CHECK_INTERVAL -10
#define NGX_PRASE_REQ_INVALID_CHECK_TIMEOUT -11
#define NGX_PRASE_REQ_INVALID_CHECK_FALL_RISE_NUMBER -12
#define NGX_PRASE_REQ_KEEPALIVE_NOT_BOOL -13
#define NGX_PRASE_REQ_INVALID_KEEPALIVE_TIME -14
#define NGX_PRASE_REQ_DEFALU_DOWN_NOT_BOOL -15
#define ADD_PEER 1
#define UPDATE_PEER 2
#define DELETE_PEER 3
#define DELETE_ALL_PEERS 4
#define CHECK_ONE_PEER_STATUS 5
#define CHECK_ALL_PEERS_STATUS 6
#define NGX_HEALTH_DETECT_API_ON_HTTP 0x0002
#define NGX_HEALTH_DETECT_API_ON_TCP 0x0004
//-------------------- http -------------------------
extern ngx_health_detect_peers_manager_t *http_peers_manager_ctx;
ngx_rbtree_node_t *ngx_http_health_detect_peers_shm_rbtree_lookup(
uint32_t hash, ngx_str_t *key);
ngx_health_detect_default_detect_policy_t *
ngx_http_health_detect_get_default_detect_policy(ngx_uint_t type);
ngx_int_t ngx_http_health_detect_add_or_update_node(
ngx_health_detect_detect_policy_t *policy);
ngx_int_t ngx_http_health_detect_delete_node(ngx_str_t *key);
ngx_int_t ngx_http_health_detect_delete_all_node();
ngx_uint_t ngx_http_health_detect_get_down_count();
//-------------------- stream -------------------------
extern ngx_health_detect_peers_manager_t *stream_peers_manager_ctx;
ngx_rbtree_node_t *ngx_stream_health_detect_peers_shm_rbtree_lookup(
uint32_t hash, ngx_str_t *key);
ngx_health_detect_default_detect_policy_t *
ngx_stream_health_detect_get_default_detect_policy(ngx_uint_t type);
ngx_int_t ngx_stream_health_detect_add_or_update_node(
ngx_health_detect_detect_policy_t *policy);
ngx_int_t ngx_stream_health_detect_delete_node(ngx_str_t *key);
ngx_int_t ngx_stream_health_detect_delete_all_node();
ngx_uint_t ngx_stream_health_detect_get_down_count();
typedef struct {
ngx_uint_t check_only;
ngx_uint_t stream_check_only;
ngx_uint_t used_module;
ngx_uint_t http_and_stream;
} ngx_http_health_detect_api_loc_conf_t;
static ngx_int_t ngx_health_detect_api_handler(ngx_http_request_t *r);
static char *ngx_http_health_detect_api_mode(
ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_stream_health_detect_api_mode(
ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_health_detect_api_create_loc_conf(ngx_conf_t *cf);
static char *ngx_health_detect_api_merge_loc_conf(
ngx_conf_t *cf, void *parent, void *child);
typedef void (*ngx_health_detect_api_one_node_status_format_pt)(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peer_shm_t *peer);
typedef void (*ngx_health_detect_api_all_node_status_format_pt)(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peers_shm_t *peer,
ngx_uint_t status_flag, ngx_uint_t upstream_type);
static void ngx_http_health_detect_all_status_json_format(ngx_http_request_t *r,
ngx_buf_t *b, ngx_health_detect_peers_shm_t *peers, ngx_uint_t status_flag,
ngx_uint_t upstream_type);
static void ngx_http_health_detect_one_status_json_format(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peer_shm_t *peer);
static void ngx_http_health_detect_all_status_html_format(ngx_http_request_t *r,
ngx_buf_t *b, ngx_health_detect_peers_shm_t *peers, ngx_uint_t status_flag,
ngx_uint_t upstream_type);
static void ngx_http_health_detect_one_status_html_format(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peer_shm_t *peer);
static void ngx_http_health_detect_all_status_prometheus_format(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peers_shm_t *peers,
ngx_uint_t status_flag, ngx_uint_t upstream_type);
static void ngx_http_health_detect_one_status_prometheus_format(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peer_shm_t *peer);
static ngx_int_t ngx_health_detect_add_or_update_node(
ngx_http_request_t *r, void *data);
static ngx_int_t ngx_health_detect_delete_node(
ngx_http_request_t *r, void *data);
static ngx_int_t ngx_health_detect_delete_all_node(
ngx_http_request_t *r, void *data);
static ngx_int_t ngx_health_detect_check_node_status(
ngx_http_request_t *r, void *data);
static ngx_int_t ngx_health_detect_check_all_node_status(
ngx_http_request_t *r, void *data);
typedef struct {
ngx_str_t format;
ngx_str_t content_type;
ngx_health_detect_api_one_node_status_format_pt one_node_output;
ngx_health_detect_api_all_node_status_format_pt all_node_output;
} ngx_health_detect_api_format_ctx_t;
typedef struct {
ngx_health_detect_api_format_ctx_t *format;
ngx_flag_t flag;
} ngx_health_detect_api_status_ctx_t;
typedef ngx_int_t (*ngx_http_health_detect_api_command_pt)(
ngx_health_detect_api_status_ctx_t *ctx, ngx_str_t *value);
typedef struct {
ngx_str_t name;
ngx_http_health_detect_api_command_pt handler;
} ngx_check_status_command_t;
static ngx_health_detect_api_format_ctx_t ngx_health_detect_status_formats[] = {
{ngx_string("json"), ngx_string("application/json"),
ngx_http_health_detect_one_status_json_format,
ngx_http_health_detect_all_status_json_format},
{ngx_string("html"), ngx_string("text/html"),
ngx_http_health_detect_one_status_html_format,
ngx_http_health_detect_all_status_html_format},
{ngx_string("prometheus"), ngx_string("text/plain;charset=utf-8"),
ngx_http_health_detect_one_status_prometheus_format,
ngx_http_health_detect_all_status_prometheus_format},
{ngx_null_string, ngx_null_string, NULL, NULL}};
static ngx_health_detect_api_format_ctx_t *
ngx_http_get_check_status_format_conf(ngx_str_t *str)
{
ngx_uint_t i;
for (i = 0;; i++) {
if (ngx_health_detect_status_formats[i].format.len == 0) {
break;
}
if (str->len != ngx_health_detect_status_formats[i].format.len) {
continue;
}
if (ngx_strncmp(str->data,
ngx_health_detect_status_formats[i].format.data,
str->len) == 0) {
return &ngx_health_detect_status_formats[i];
}
}
return NULL;
}
static ngx_int_t
ngx_upstream_check_status_command_format(
ngx_health_detect_api_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_health_detect_api_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 ngx_check_status_command_t ngx_health_detect_api_status_commands[] = {
{ngx_string("format"), ngx_upstream_check_status_command_format},
{ngx_string("status"), ngx_upstream_check_status_command_status},
{ngx_null_string, NULL}};
static ngx_conf_bitmask_t ngx_check_http_expect_alive_masks[] = {
{ngx_string("http_2xx"), NGX_CHECK_HTTP_2XX},
{ngx_string("http_3xx"), NGX_CHECK_HTTP_3XX},
{ngx_string("http_4xx"), NGX_CHECK_HTTP_4XX},
{ngx_string("http_5xx"), NGX_CHECK_HTTP_5XX},
{ngx_string("http_err"), NGX_CHECK_HTTP_ERR}, {ngx_null_string, 0}};
static ngx_command_t ngx_http_health_detect_api_cmds[] = {
{ngx_string("health_detect_dynamic_api"),
NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 |
NGX_CONF_NOARGS,
ngx_http_health_detect_api_mode, 0, 0, NULL},
{ngx_string("stream_health_detect_dynamic_api"),
NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 |
NGX_CONF_NOARGS,
ngx_stream_health_detect_api_mode, 0, 0, NULL},
{ngx_string("healthcheck_status"),
NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 |
NGX_CONF_NOARGS,
ngx_stream_health_detect_api_mode, 0, 0, NULL},
ngx_null_command};
static ngx_http_module_t ngx_health_detect_api_modules_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
ngx_health_detect_api_create_loc_conf,
ngx_health_detect_api_merge_loc_conf,
};
ngx_module_t ngx_health_detect_api_module = {NGX_MODULE_V1,
&ngx_health_detect_api_modules_ctx, ngx_http_health_detect_api_cmds,
NGX_HTTP_MODULE, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NGX_MODULE_V1_PADDING};
static void *
ngx_health_detect_api_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_health_detect_api_loc_conf_t *apilcf;
apilcf =
ngx_pcalloc(cf->pool, sizeof(ngx_http_health_detect_api_loc_conf_t));
if (apilcf == NULL) {
return NULL;
}
apilcf->check_only = NGX_CONF_UNSET_UINT;
apilcf->stream_check_only = NGX_CONF_UNSET_UINT;
apilcf->used_module = NGX_CONF_UNSET_UINT;
apilcf->http_and_stream = NGX_CONF_UNSET_UINT;
return apilcf;
}
static char *
ngx_health_detect_api_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_health_detect_api_loc_conf_t *prev = parent;
ngx_http_health_detect_api_loc_conf_t *conf = child;
ngx_conf_merge_uint_value(conf->check_only, prev->check_only, 1);
ngx_conf_merge_uint_value(
conf->stream_check_only, prev->stream_check_only, 1);
ngx_conf_merge_uint_value(conf->used_module, prev->used_module, 0);
ngx_conf_merge_uint_value(conf->http_and_stream, prev->http_and_stream, 0);
if ((conf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) &&
(conf->used_module & NGX_HEALTH_DETECT_API_ON_TCP)) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"both \"health_detect_dynamic_api\" and "
"\"stream_health_detect_dynamic_api\" directive are "
"not allowed to appear in the same location");
}
ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0,
"ngx health detect api module used_module(%ui)", conf->used_module);
return NGX_CONF_OK;
}
static char *
ngx_http_health_detect_api_mode(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_http_core_loc_conf_t *clcf;
ngx_http_health_detect_api_loc_conf_t *apilcf = NULL;
ngx_str_t s;
value = cf->args->elts;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_health_detect_api_handler;
if (ngx_strncmp(value->data, "healthcheck_status", 18) == 0) {
apilcf =
ngx_http_conf_get_module_loc_conf(cf, ngx_health_detect_api_module);
if (!apilcf) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid value apilcf");
return NGX_CONF_ERROR;
}
if (apilcf->used_module == NGX_CONF_UNSET_UINT) {
apilcf->used_module = 0;
}
apilcf->used_module |= NGX_HEALTH_DETECT_API_ON_HTTP;
apilcf->http_and_stream = 1;
return NGX_CONF_OK;
}
if (cf->args->nelts == 2) {
apilcf =
ngx_http_conf_get_module_loc_conf(cf, ngx_health_detect_api_module);
value = &value[1];
if (ngx_strncmp(value->data, "check_only=", 11) != 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid api_mode \"%V\", must be \"check_only=true\" "
"or \"check_only=false\" ",
value);
return NGX_CONF_ERROR;
}
s.len = value->len - 11;
s.data = value->data + 11;
if (ngx_strcasecmp(s.data, (u_char *) "true") == 0) {
apilcf->check_only = 1;
} else if (ngx_strcasecmp(s.data, (u_char *) "false") == 0) {
apilcf->check_only = 0;
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid value \"%s\", "
"it must be \"true\" or \"false\"",
s.data);
return NGX_CONF_ERROR;
}
}
if (!apilcf) {
return NGX_CONF_ERROR;
}
if (apilcf->used_module == NGX_CONF_UNSET_UINT) {
apilcf->used_module = 0;
}
apilcf->used_module |= NGX_HEALTH_DETECT_API_ON_HTTP;
return NGX_CONF_OK;
}
static char *
ngx_stream_health_detect_api_mode(
ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_http_core_loc_conf_t *clcf;
ngx_http_health_detect_api_loc_conf_t *apilcf = NULL;
ngx_str_t s;
value = cf->args->elts;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_health_detect_api_handler;
if (ngx_strncmp(value->data, "healthcheck_status", 18) == 0) {
apilcf =
ngx_http_conf_get_module_loc_conf(cf, ngx_health_detect_api_module);
if (!apilcf) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid value apilcf");
return NGX_CONF_ERROR;
}
if (apilcf->used_module == NGX_CONF_UNSET_UINT) {
apilcf->used_module = 0;
}
apilcf->used_module |= NGX_HEALTH_DETECT_API_ON_HTTP;
apilcf->http_and_stream = 1;
return NGX_CONF_OK;
}
if (cf->args->nelts == 2) {
apilcf =
ngx_http_conf_get_module_loc_conf(cf, ngx_health_detect_api_module);
value = &value[1];
if (ngx_strncmp(value->data, "check_only=", 11) != 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid api_mode \"%V\", must be \"check_only=true\" "
"or \"check_only=false\" ",
value);
return NGX_CONF_ERROR;
}
s.len = value->len - 11;
s.data = value->data + 11;
if (ngx_strcasecmp(s.data, (u_char *) "true") == 0) {
apilcf->stream_check_only = 1;
} else if (ngx_strcasecmp(s.data, (u_char *) "false") == 0) {
apilcf->stream_check_only = 0;
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid value \"%s\", "
"it must be \"true\" or \"false\"",
s.data);
return NGX_CONF_ERROR;
}
}
if (!apilcf) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid value apilcf");
return NGX_CONF_ERROR;
}
if (apilcf->used_module == NGX_CONF_UNSET_UINT) {
apilcf->used_module = 0;
}
apilcf->used_module |= NGX_HEALTH_DETECT_API_ON_TCP;
return NGX_CONF_OK;
}
static void
ngx_health_detect_judge_cond_to_string(
char *dst, ngx_uint_t expect_response_status)
{
if (expect_response_status & NGX_CHECK_HTTP_2XX) {
strcat(dst, "http_2xx ");
}
if (expect_response_status & NGX_CHECK_HTTP_3XX) {
strcat(dst, "http_3xx ");
}
if (expect_response_status & NGX_CHECK_HTTP_4XX) {
strcat(dst, "http_4xx ");
}
if (expect_response_status & NGX_CHECK_HTTP_5XX) {
strcat(dst, "http_5xx ");
}
if (expect_response_status & NGX_CHECK_HTTP_ERR) {
strcat(dst, "http_err ");
}
}
static void
ngx_http_rbtree_traverse_all_status_json_format(ngx_rbtree_node_t *node_shm,
ngx_rbtree_node_t *sentinel, ngx_buf_t *b, ngx_uint_t status_flag,
u_char *items_start, ngx_uint_t upstream_type)
{
ngx_health_detect_peer_shm_t *peer_shm;
ngx_uint_t need_print;
if (node_shm == sentinel) {
return;
}
ngx_http_rbtree_traverse_all_status_json_format(
node_shm->left, sentinel, b, status_flag, items_start, upstream_type);
need_print = 1;
peer_shm = (ngx_health_detect_peer_shm_t *) (&node_shm->color);
if ((status_flag & NGX_CHECK_STATUS_DOWN) ||
(status_flag & NGX_CHECK_STATUS_UP)) {
if (status_flag != peer_shm->status.latest_status) {
need_print = 0;
}
}
if (need_print) {
b->last = ngx_snprintf(b->last, b->end - b->last,
"%s {\"name\":\"%V\", \"addr\":\"%V\", \"access_time\":\"%V\", "
"\"status\":\"%s\", \"type\":\"%s\"}",
b->last > items_start ? ",\n" : "", &peer_shm->policy.peer_name,
&peer_shm->policy.peer_addr.name,
&peer_shm->status.latest_access_time,
peer_shm->status.latest_status == NGX_CHECK_STATUS_UP ? "up"
: "down",
upstream_type & NGX_HEALTH_DETECT_API_ON_HTTP ? "http" : "stream");
}
ngx_http_rbtree_traverse_all_status_json_format(
node_shm->right, sentinel, b, status_flag, items_start, upstream_type);
}
static void
ngx_http_health_detect_all_status_json_format(ngx_http_request_t *r,
ngx_buf_t *b, ngx_health_detect_peers_shm_t *peers_shm,
ngx_uint_t status_flag, ngx_uint_t upstream_type)
{
ngx_rbtree_node_t *node_shm, *sentinel;
ngx_uint_t down_count;
ngx_http_health_detect_api_loc_conf_t *apicf;
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);
node_shm = peers_shm->rbtree.root;
sentinel = peers_shm->rbtree.sentinel;
if (apicf->http_and_stream != 1) {
if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
down_count = ngx_http_health_detect_get_down_count();
} else {
down_count = ngx_stream_health_detect_get_down_count();
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"{\n \"total\": %ui,\n \"up\": %ui,\n \"down\": %ui,"
"\n \"max\": %ui,\n \"items\": [\n",
peers_shm->number, peers_shm->number - down_count, down_count,
peers_shm->max_number);
}
ngx_http_rbtree_traverse_all_status_json_format(
node_shm, sentinel, b, status_flag, b->last, upstream_type);
if (apicf->http_and_stream != 1) {
b->last = ngx_snprintf(b->last, b->end - b->last, "\n ]\n");
b->last = ngx_snprintf(b->last, b->end - b->last, "}\n");
}
}
static void
ngx_http_health_detect_one_status_json_format(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peer_shm_t *peer_shm)
{
ngx_health_detect_one_peer_status *status;
ngx_queue_t *q;
char user_define_cond_str[80];
ngx_str_t *send_content;
ngx_http_health_detect_api_loc_conf_t *apicf;
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);
ngx_memzero(user_define_cond_str, sizeof(user_define_cond_str));
ngx_health_detect_judge_cond_to_string(user_define_cond_str,
peer_shm->policy.data.expect_response_status.http_status);
if (peer_shm->policy.send_content.len == 0) {
if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
send_content = &ngx_http_health_detect_get_default_detect_policy(
peer_shm->policy.data.type)
->default_send_content;
} else {
send_content = &ngx_stream_health_detect_get_default_detect_policy(
peer_shm->policy.data.type)
->default_send_content;
}
} else {
send_content = &peer_shm->policy.send_content;
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"{"
"\"peer_name\": \"%V\",\n"
" \"type\": \"%s\",\n"
" \"peer_addr\": \"%V\",\n"
" \"alert_method\": \"%s\",\n"
" \"expect_response_status\": \"%s\",\n"
" \"check_interval\": \"%ui\",\n"
" \"check_timeout\": \"%ui\",\n"
" \"need_keepalive\": \"%ui\",\n"
" \"keepalive_time\": \"%ui\",\n"
" \"rise\": \"%ui\",\n"
" \"fall\": \"%ui\",\n"
" \"send_content\": \"%V\",\n"
" \"access_time\": \"%V\",\n"
" \"latest_status\": \"%s\",\n"
" \"max_status_count\": \"%ui\",\n"
" \"history_status\": {\n"
" \"current_status_count\": \"%ui\",\n"
" \"items\": [\n",
&peer_shm->policy.peer_name,
ngx_health_detect_api_get_policy_type_to_string(
peer_shm->policy.data.type),
&peer_shm->policy.peer_addr.name,
ngx_health_detect_api_get_policy_type_to_string(
peer_shm->policy.data.alert_method),
user_define_cond_str, peer_shm->policy.data.check_interval,
peer_shm->policy.data.check_timeout,
peer_shm->policy.data.need_keepalive,
peer_shm->policy.data.keepalive_time, peer_shm->policy.data.rise,
peer_shm->policy.data.fall, send_content,
&peer_shm->status.latest_access_time,
peer_shm->status.latest_status == NGX_CHECK_STATUS_UP ? "up" : "down",
peer_shm->status.max_status_count,
peer_shm->status.current_status_count);
for (q = ngx_queue_head(&peer_shm->status.history_status);
q != ngx_queue_sentinel(&peer_shm->status.history_status);
q = ngx_queue_next(q)) {
status = ngx_queue_data(q, ngx_health_detect_one_peer_status, link);
b->last = ngx_snprintf(b->last, b->end - b->last,
" {\"access_time\": %V, \"status\": \"%s\",} \n",
&status->access_time,
status->status == NGX_CHECK_STATUS_UP ? "up" : "down");
}
b->last = ngx_snprintf(b->last, b->end - b->last, " ]\n");
b->last = ngx_snprintf(b->last, b->end - b->last, "}}\n");
}
static void
ngx_http_health_detect_one_status_html_format(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peer_shm_t *peer_shm)
{
ngx_health_detect_one_peer_status *status;
ngx_queue_t *q;
ngx_str_t *send_content;
char user_define_cond_str[80];
ngx_http_health_detect_api_loc_conf_t *apicf;
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);
ngx_memzero(user_define_cond_str, sizeof(user_define_cond_str));
ngx_health_detect_judge_cond_to_string(user_define_cond_str,
peer_shm->policy.data.expect_response_status.http_status);
if (peer_shm->policy.send_content.len == 0) {
if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
send_content = &ngx_http_health_detect_get_default_detect_policy(
peer_shm->policy.data.type)
->default_send_content;
} else {
send_content = &ngx_stream_health_detect_get_default_detect_policy(
peer_shm->policy.data.type)
->default_send_content;
}
} else {
send_content = &peer_shm->policy.send_content;
}
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 health detect status "
"style=\"background-color:red\" </title>\n"
"</head>\n"
"<body>\n"
"<h1>Nginx health detect status</h1>\n"
"<h2>Peer_name : %V, Type: %s, Peer_addr: %V</h2>\n"
"<table style=\"background-color:white\" cellspacing=\"0\" "
" cellpadding=\"3\" border=\"1\">\n"
" <tr bgcolor=\"#FFFF00\">\n"
" <th>alert_method</th>\n"
" <th>expect_response_status</th>\n"
" <th>check_interval</th>\n"
" <th>check_timeout</th>\n"
" <th>need_keepalive</th>\n"
" <th>keepalive_time</th>\n"
" <th>rise</th>\n"
" <th>fall</th>\n"
" <th>send_content</th>\n"
" <th>latest_access_time</th>\n"
" <th>latest_status</th>\n"
" <th>max_status_count</th>\n"
" </tr>\n",
&peer_shm->policy.peer_name,
ngx_health_detect_api_get_policy_type_to_string(
peer_shm->policy.data.type),
&peer_shm->policy.peer_addr.name);
b->last = ngx_snprintf(b->last, b->end - b->last,
" <tr bgcolor=\"#C0C0C0\">\n"
" <td>%s</td>\n"
" <td>%s</td>\n"
" <td>%ui</td>\n"
" <td>%ui</td>\n"
" <td>%ui</td>\n"
" <td>%ui</td>\n"
" <td>%ui</td>\n"
" <td>%ui</td>\n"
" <td>%V</td>\n"
" <td>%V</td>\n"
" <td>%s</td>\n"
" <td>%ui</td>\n"
" </tr>\n",
ngx_health_detect_api_get_policy_type_to_string(
peer_shm->policy.data.alert_method),
user_define_cond_str, peer_shm->policy.data.check_interval,
peer_shm->policy.data.check_timeout,
peer_shm->policy.data.need_keepalive,
peer_shm->policy.data.keepalive_time, peer_shm->policy.data.rise,
peer_shm->policy.data.fall, send_content,
&peer_shm->status.latest_access_time,
peer_shm->status.latest_status == NGX_CHECK_STATUS_UP ? "up" : "down",
peer_shm->status.max_status_count);
b->last = ngx_snprintf(b->last, b->end - b->last, "</table>\n");
b->last = ngx_snprintf(b->last, b->end - b->last,
"<h2>History status count: %ui</h2>\n"
"<table style=\"background-color:white\" cellspacing=\"0\" "
" cellpadding=\"3\" border=\"1\">\n"
" <tr bgcolor=\"#FFFF00\">\n"
" <td class=\"column\"\">access_time</td>\n"
" <td class=\"column\"\">status</td>\n"
" </tr>\n",
peer_shm->status.current_status_count);
for (q = ngx_queue_head(&peer_shm->status.history_status);
q != ngx_queue_sentinel(&peer_shm->status.history_status);
q = ngx_queue_next(q)) {
status = ngx_queue_data(q, ngx_health_detect_one_peer_status, link);
b->last = ngx_snprintf(b->last, b->end - b->last,
" <tr bgcolor=\"#C0C0C0\">\n"
"<td>%V</td>\n"
" <td>%s</td>\n"
" <tr>\n",
&status->access_time,
status->status == NGX_CHECK_STATUS_UP ? "up" : "down");
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"</table>\n"
"</body>\n"
"</html>\n");
}
static void
ngx_http_rbtree_traverse_all_status_html_format(ngx_rbtree_node_t *node_shm,
ngx_rbtree_node_t *sentinel, ngx_buf_t *b, ngx_uint_t status_flag,
ngx_uint_t upstream_type)
{
ngx_health_detect_peer_shm_t *peer_shm;
ngx_uint_t need_print;
if (node_shm == sentinel) {
return;
}
ngx_http_rbtree_traverse_all_status_html_format(
node_shm->left, sentinel, b, status_flag, upstream_type);
need_print = 1;
peer_shm = (ngx_health_detect_peer_shm_t *) (&node_shm->color);
if ((status_flag & NGX_CHECK_STATUS_DOWN) ||
(status_flag & NGX_CHECK_STATUS_UP)) {
if (status_flag != peer_shm->status.latest_status) {
need_print = 0;
}
}
if (need_print) {
b->last = ngx_snprintf(b->last, b->end - b->last,
" <tr bgcolor=\"#C0C0C0\">\n"
"<td>%V</td>\n"
" <td>%V</td>\n"
" <td>%V</td>\n"
" <td>%s</td>\n"
" <td>%s</td>\n"
" <tr>\n",
&peer_shm->policy.peer_name, &peer_shm->policy.peer_addr.name,
&peer_shm->status.latest_access_time,
peer_shm->status.latest_status == NGX_CHECK_STATUS_UP ? "up"
: "down",
upstream_type & NGX_HEALTH_DETECT_API_ON_HTTP ? "http" : "stream");
}
ngx_http_rbtree_traverse_all_status_html_format(
node_shm->right, sentinel, b, status_flag, upstream_type);
}
static void
ngx_http_health_detect_all_status_html_format(ngx_http_request_t *r,
ngx_buf_t *b, ngx_health_detect_peers_shm_t *peers_shm,
ngx_uint_t status_flag, ngx_uint_t upstream_type)
{
ngx_rbtree_node_t *node_shm, *sentinel;
ngx_uint_t down_count;
ngx_http_health_detect_api_loc_conf_t *apicf;
node_shm = peers_shm->rbtree.root;
sentinel = peers_shm->rbtree.sentinel;
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);
if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
down_count = ngx_http_health_detect_get_down_count();
} else {
down_count = ngx_stream_health_detect_get_down_count();
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"<h2>Total: %ui, Up: %ui, Down: %ui, Max: %ui</h2>\n"
"<table style=\"background-color:white\" cellspacing=\"0\" "
" cellpadding=\"3\" border=\"1\">\n"
" <tr bgcolor=\"#FFFF00\">\n"
" <td class=\"column\"\">name</td>\n"
" <td class=\"column\"\">addr</td>\n"
" <td class=\"column\"\">access_time</td>\n"
" <td class=\"column\"\">status</td>\n"
" <td class=\"column\"\">upstream_type</td>\n"
" </tr>\n",
peers_shm->number, peers_shm->number - down_count, down_count,
peers_shm->max_number);
ngx_http_rbtree_traverse_all_status_html_format(
node_shm, sentinel, b, status_flag, upstream_type);
b->last = ngx_snprintf(b->last, b->end - b->last,
"</table>\n"
"</body>\n"
"</html>\n");
}
static void
ngx_http_rbtree_traverse_all_status_prometheus_format(
ngx_rbtree_node_t *node_shm, ngx_rbtree_node_t *sentinel, ngx_buf_t *b,
ngx_uint_t status_flag, u_char *items_start, ngx_uint_t upstream_type)
{
ngx_health_detect_peer_shm_t *peer_shm;
ngx_uint_t need_print;
if (node_shm == sentinel) {
return;
}
ngx_http_rbtree_traverse_all_status_prometheus_format(
node_shm->left, sentinel, b, status_flag, items_start, upstream_type);
need_print = 1;
peer_shm = (ngx_health_detect_peer_shm_t *) (&node_shm->color);
if ((status_flag & NGX_CHECK_STATUS_DOWN) ||
(status_flag & NGX_CHECK_STATUS_UP)) {
if (status_flag != peer_shm->status.latest_status) {
need_print = 0;
}
}
if (need_print) {
b->last = ngx_snprintf(b->last, b->end - b->last,
"nginx_upstream_server{upstream_type=\"%s\",upstream=\"%V\",status="
"\"%s\"}\n",
upstream_type & NGX_HEALTH_DETECT_API_ON_HTTP ? "http" : "stream",
&peer_shm->policy.peer_name,
peer_shm->status.latest_status == NGX_CHECK_STATUS_UP ? "up"
: "down");
}
ngx_http_rbtree_traverse_all_status_prometheus_format(
node_shm->right, sentinel, b, status_flag, items_start, upstream_type);
}
static void
ngx_http_health_detect_all_status_prometheus_format(ngx_http_request_t *r,
ngx_buf_t *b, ngx_health_detect_peers_shm_t *peers_shm,
ngx_uint_t status_flag, ngx_uint_t upstream_type)
{
ngx_rbtree_node_t *node_shm, *sentinel;
ngx_uint_t down_count;
ngx_http_health_detect_api_loc_conf_t *apicf;
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);
node_shm = peers_shm->rbtree.root;
sentinel = peers_shm->rbtree.sentinel;
if (apicf->http_and_stream != 1) {
if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
down_count = ngx_http_health_detect_get_down_count();
} else {
down_count = ngx_stream_health_detect_get_down_count();
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"# HELP nginx_upstream_count_total Nginx total number of servers\n"
"# TYPE nginx_upstream_count_total gauge\n"
"nginx_upstream_count_total %ui\n"
"# HELP nginx_upstream_count_up Nginx total number of servers that "
"are "
"UP\n"
"# TYPE nginx_upstream_count_up gauge\n"
"nginx_upstream_count_up %ui\n"
"# HELP nginx_upstream_count_down Nginx total number of servers "
"that "
"are DOWN\n"
"# TYPE nginx_upstream_count_down gauge\n"
"nginx_upstream_count_down %ui\n",
peers_shm->number, peers_shm->number - down_count, down_count);
b->last = ngx_snprintf(b->last, b->end - b->last,
"# HELP nginx_upstream_server Nginx upstream status\n"
"# TYPE nginx_upstream_server counter\n");
}
ngx_http_rbtree_traverse_all_status_prometheus_format(
node_shm, sentinel, b, status_flag, b->last, upstream_type);
}
static void
ngx_http_health_detect_one_status_prometheus_format(
ngx_http_request_t *r, ngx_buf_t *b, ngx_health_detect_peer_shm_t *peer_shm)
{
char user_define_cond_str[80];
ngx_str_t *send_content;
ngx_http_health_detect_api_loc_conf_t *apicf;
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);
ngx_memzero(user_define_cond_str, sizeof(user_define_cond_str));
ngx_health_detect_judge_cond_to_string(user_define_cond_str,
peer_shm->policy.data.expect_response_status.http_status);
if (peer_shm->policy.send_content.len == 0) {
if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
send_content = &ngx_http_health_detect_get_default_detect_policy(
peer_shm->policy.data.type)
->default_send_content;
} else {
send_content = &ngx_stream_health_detect_get_default_detect_policy(
peer_shm->policy.data.type)
->default_send_content;
}
} else {
send_content = &peer_shm->policy.send_content;
}
b->last = ngx_snprintf(b->last, b->end - b->last,
"nginx_upstream_server {upstream_type=\"%s\","
"upstream=\"%V\","
"peer_addr=\"%V\","
"alert_method=\"%s\","
"expect_response_status=\"%s\","
"check_interval=\"%ui\","
"check_timeout=\"%ui\","
"need_keepalive=\"%ui\","
"keepalive_time=\"%ui\","
"rise=\"%ui\","
"fall=\"%ui\","
"send_content=\"%V\","
"access_time=\"%V\","
"latest_status=\"%s\"}",
apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP ? "http" : "stream",
&peer_shm->policy.peer_name, &peer_shm->policy.peer_addr.name,
ngx_health_detect_api_get_policy_type_to_string(
peer_shm->policy.data.alert_method),
user_define_cond_str, peer_shm->policy.data.check_interval,
peer_shm->policy.data.check_timeout,
peer_shm->policy.data.need_keepalive,
peer_shm->policy.data.keepalive_time, peer_shm->policy.data.rise,
peer_shm->policy.data.fall, send_content,
&peer_shm->status.latest_access_time,
peer_shm->status.latest_status == NGX_CHECK_STATUS_UP ? "up" : "down");
}
static ngx_int_t
ngx_health_detect_add_or_update_node(ngx_http_request_t *r, void *data)
{
ngx_int_t rc;
ngx_http_health_detect_api_loc_conf_t *apicf;
ngx_health_detect_detect_policy_t *policy = data;
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);
if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
rc = ngx_http_health_detect_add_or_update_node(policy);
} else {
rc = ngx_stream_health_detect_add_or_update_node(policy);
}
return rc;
}
static ngx_int_t
ngx_health_detect_delete_node(ngx_http_request_t *r, void *data)
{
ngx_int_t rc;
ngx_http_health_detect_api_loc_conf_t *apicf;
ngx_str_t *key = data;
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);
if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
rc = ngx_http_health_detect_delete_node(key);
} else {
rc = ngx_stream_health_detect_delete_node(key);
}
return rc;
}
static ngx_int_t
ngx_health_detect_delete_all_node(ngx_http_request_t *r, void *data)