forked from cep21/healthcheck_nginx_upstreams
-
Notifications
You must be signed in to change notification settings - Fork 486
/
ngx_http_upstream_check_module.c
4075 lines (3080 loc) · 109 KB
/
ngx_http_upstream_check_module.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
/*
* Copyright (C) 2010-2014 Weibin Yao ([email protected])
* Copyright (C) 2010-2014 Alibaba Group Holding Limited
*/
#include <nginx.h>
#include "ngx_http_upstream_check_module.h"
typedef struct ngx_http_upstream_check_peer_s ngx_http_upstream_check_peer_t;
typedef struct ngx_http_upstream_check_srv_conf_s
ngx_http_upstream_check_srv_conf_t;
#pragma pack(push, 1)
typedef struct {
u_char major;
u_char minor;
} ngx_ssl_protocol_version_t;
typedef struct {
u_char msg_type;
ngx_ssl_protocol_version_t version;
uint16_t length;
u_char handshake_type;
u_char handshake_length[3];
ngx_ssl_protocol_version_t hello_version;
time_t time;
u_char random[28];
u_char others[0];
} ngx_ssl_server_hello_t;
typedef struct {
u_char packet_length[3];
u_char packet_number;
u_char protocol_version;
u_char others[0];
} ngx_mysql_handshake_init_t;
typedef struct {
uint16_t preamble;
uint16_t length;
u_char type;
} ngx_ajp_raw_packet_t;
#pragma pack()
typedef struct {
ngx_buf_t send;
ngx_buf_t recv;
ngx_uint_t state;
ngx_http_status_t status;
size_t padding;
size_t length;
} ngx_http_upstream_check_ctx_t;
typedef struct {
ngx_shmtx_t mutex;
#if (nginx_version >= 1002000)
ngx_shmtx_sh_t lock;
#else
ngx_atomic_t lock;
#endif
ngx_pid_t owner;
ngx_msec_t access_time;
ngx_uint_t fall_count;
ngx_uint_t rise_count;
ngx_uint_t busyness;
ngx_uint_t access_count;
struct sockaddr *sockaddr;
socklen_t socklen;
ngx_atomic_t down;
u_char padding[64];
} ngx_http_upstream_check_peer_shm_t;
typedef struct {
ngx_uint_t generation;
ngx_uint_t checksum;
ngx_uint_t number;
/* ngx_http_upstream_check_status_peer_t */
ngx_http_upstream_check_peer_shm_t peers[1];
} ngx_http_upstream_check_peers_shm_t;
#define NGX_HTTP_CHECK_CONNECT_DONE 0x0001
#define NGX_HTTP_CHECK_SEND_DONE 0x0002
#define NGX_HTTP_CHECK_RECV_DONE 0x0004
#define NGX_HTTP_CHECK_ALL_DONE 0x0008
typedef ngx_int_t (*ngx_http_upstream_check_packet_init_pt)
(ngx_http_upstream_check_peer_t *peer);
typedef ngx_int_t (*ngx_http_upstream_check_packet_parse_pt)
(ngx_http_upstream_check_peer_t *peer);
typedef void (*ngx_http_upstream_check_packet_clean_pt)
(ngx_http_upstream_check_peer_t *peer);
struct ngx_http_upstream_check_peer_s {
ngx_flag_t state;
ngx_pool_t *pool;
ngx_uint_t index;
ngx_uint_t max_busy;
ngx_str_t *upstream_name;
ngx_addr_t *check_peer_addr;
ngx_addr_t *peer_addr;
ngx_event_t check_ev;
ngx_event_t check_timeout_ev;
ngx_peer_connection_t pc;
void *check_data;
ngx_event_handler_pt send_handler;
ngx_event_handler_pt recv_handler;
ngx_http_upstream_check_packet_init_pt init;
ngx_http_upstream_check_packet_parse_pt parse;
ngx_http_upstream_check_packet_clean_pt reinit;
ngx_http_upstream_check_peer_shm_t *shm;
ngx_http_upstream_check_srv_conf_t *conf;
};
typedef struct {
ngx_str_t check_shm_name;
ngx_uint_t checksum;
ngx_array_t peers;
ngx_http_upstream_check_peers_shm_t *peers_shm;
} ngx_http_upstream_check_peers_t;
#define NGX_HTTP_CHECK_TCP 0x0001
#define NGX_HTTP_CHECK_HTTP 0x0002
#define NGX_HTTP_CHECK_SSL_HELLO 0x0004
#define NGX_HTTP_CHECK_MYSQL 0x0008
#define NGX_HTTP_CHECK_AJP 0x0010
#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
typedef struct {
ngx_uint_t type;
ngx_str_t name;
ngx_str_t default_send;
/* HTTP */
ngx_uint_t default_status_alive;
ngx_event_handler_pt send_handler;
ngx_event_handler_pt recv_handler;
ngx_http_upstream_check_packet_init_pt init;
ngx_http_upstream_check_packet_parse_pt parse;
ngx_http_upstream_check_packet_clean_pt reinit;
unsigned need_pool;
unsigned need_keepalive;
} ngx_check_conf_t;
typedef void (*ngx_http_upstream_check_status_format_pt) (ngx_buf_t *b,
ngx_http_upstream_check_peers_t *peers, ngx_uint_t flag);
typedef struct {
ngx_str_t format;
ngx_str_t content_type;
ngx_http_upstream_check_status_format_pt output;
} ngx_check_status_conf_t;
#define NGX_CHECK_STATUS_DOWN 0x0001
#define NGX_CHECK_STATUS_UP 0x0002
typedef struct {
ngx_check_status_conf_t *format;
ngx_flag_t flag;
} ngx_http_upstream_check_status_ctx_t;
typedef ngx_int_t (*ngx_http_upstream_check_status_command_pt)
(ngx_http_upstream_check_status_ctx_t *ctx, ngx_str_t *value);
typedef struct {
ngx_str_t name;
ngx_http_upstream_check_status_command_pt handler;
} ngx_check_status_command_t;
typedef struct {
ngx_uint_t check_shm_size;
ngx_http_upstream_check_peers_t *peers;
} ngx_http_upstream_check_main_conf_t;
struct ngx_http_upstream_check_srv_conf_s {
ngx_uint_t port;
ngx_uint_t fall_count;
ngx_uint_t rise_count;
ngx_msec_t check_interval;
ngx_msec_t check_timeout;
ngx_uint_t check_keepalive_requests;
ngx_check_conf_t *check_type_conf;
ngx_str_t send;
union {
ngx_uint_t return_code;
ngx_uint_t status_alive;
} code;
ngx_array_t *fastcgi_params;
ngx_uint_t default_down;
};
typedef struct {
ngx_check_status_conf_t *format;
} ngx_http_upstream_check_loc_conf_t;
typedef struct {
u_char version;
u_char type;
u_char request_id_hi;
u_char request_id_lo;
u_char content_length_hi;
u_char content_length_lo;
u_char padding_length;
u_char reserved;
} ngx_http_fastcgi_header_t;
typedef struct {
u_char role_hi;
u_char role_lo;
u_char flags;
u_char reserved[5];
} ngx_http_fastcgi_begin_request_t;
typedef struct {
u_char version;
u_char type;
u_char request_id_hi;
u_char request_id_lo;
} ngx_http_fastcgi_header_small_t;
typedef struct {
ngx_http_fastcgi_header_t h0;
ngx_http_fastcgi_begin_request_t br;
ngx_http_fastcgi_header_small_t h1;
} ngx_http_fastcgi_request_start_t;
#define NGX_HTTP_FASTCGI_RESPONDER 1
#define NGX_HTTP_FASTCGI_KEEP_CONN 1
#define NGX_HTTP_FASTCGI_BEGIN_REQUEST 1
#define NGX_HTTP_FASTCGI_ABORT_REQUEST 2
#define NGX_HTTP_FASTCGI_END_REQUEST 3
#define NGX_HTTP_FASTCGI_PARAMS 4
#define NGX_HTTP_FASTCGI_STDIN 5
#define NGX_HTTP_FASTCGI_STDOUT 6
#define NGX_HTTP_FASTCGI_STDERR 7
#define NGX_HTTP_FASTCGI_DATA 8
typedef enum {
ngx_http_fastcgi_st_version = 0,
ngx_http_fastcgi_st_type,
ngx_http_fastcgi_st_request_id_hi,
ngx_http_fastcgi_st_request_id_lo,
ngx_http_fastcgi_st_content_length_hi,
ngx_http_fastcgi_st_content_length_lo,
ngx_http_fastcgi_st_padding_length,
ngx_http_fastcgi_st_reserved,
ngx_http_fastcgi_st_data,
ngx_http_fastcgi_st_padding
} ngx_http_fastcgi_state_e;
static ngx_http_fastcgi_request_start_t ngx_http_fastcgi_request_start = {
{ 1, /* version */
NGX_HTTP_FASTCGI_BEGIN_REQUEST, /* type */
0, /* request_id_hi */
1, /* request_id_lo */
0, /* content_length_hi */
sizeof(ngx_http_fastcgi_begin_request_t), /* content_length_lo */
0, /* padding_length */
0 }, /* reserved */
{ 0, /* role_hi */
NGX_HTTP_FASTCGI_RESPONDER, /* role_lo */
0, /* NGX_HTTP_FASTCGI_KEEP_CONN */ /* flags */
{ 0, 0, 0, 0, 0 } }, /* reserved[5] */
{ 1, /* version */
NGX_HTTP_FASTCGI_PARAMS, /* type */
0, /* request_id_hi */
1 }, /* request_id_lo */
};
static ngx_int_t ngx_http_upstream_check_add_timers(ngx_cycle_t *cycle);
static ngx_int_t ngx_http_upstream_check_peek_one_byte(ngx_connection_t *c);
static void ngx_http_upstream_check_begin_handler(ngx_event_t *event);
static void ngx_http_upstream_check_connect_handler(ngx_event_t *event);
static void ngx_http_upstream_check_peek_handler(ngx_event_t *event);
static void ngx_http_upstream_check_send_handler(ngx_event_t *event);
static void ngx_http_upstream_check_recv_handler(ngx_event_t *event);
static void ngx_http_upstream_check_discard_handler(ngx_event_t *event);
static void ngx_http_upstream_check_dummy_handler(ngx_event_t *event);
static ngx_int_t ngx_http_upstream_check_http_init(
ngx_http_upstream_check_peer_t *peer);
static ngx_int_t ngx_http_upstream_check_http_parse(
ngx_http_upstream_check_peer_t *peer);
static ngx_int_t ngx_http_upstream_check_parse_status_line(
ngx_http_upstream_check_ctx_t *ctx, ngx_buf_t *b,
ngx_http_status_t *status);
static void ngx_http_upstream_check_http_reinit(
ngx_http_upstream_check_peer_t *peer);
static ngx_buf_t *ngx_http_upstream_check_create_fastcgi_request(
ngx_pool_t *pool, ngx_str_t *params, ngx_uint_t num);
static ngx_int_t ngx_http_upstream_check_fastcgi_parse(
ngx_http_upstream_check_peer_t *peer);
static ngx_int_t ngx_http_upstream_check_fastcgi_process_record(
ngx_http_upstream_check_ctx_t *ctx, ngx_buf_t *b,
ngx_http_status_t *status);
static ngx_int_t ngx_http_upstream_check_parse_fastcgi_status(
ngx_http_upstream_check_ctx_t *ctx, ngx_buf_t *b,
ngx_http_status_t *status);
static ngx_int_t ngx_http_upstream_check_ssl_hello_init(
ngx_http_upstream_check_peer_t *peer);
static ngx_int_t ngx_http_upstream_check_ssl_hello_parse(
ngx_http_upstream_check_peer_t *peer);
static void ngx_http_upstream_check_ssl_hello_reinit(
ngx_http_upstream_check_peer_t *peer);
static ngx_int_t ngx_http_upstream_check_mysql_init(
ngx_http_upstream_check_peer_t *peer);
static ngx_int_t ngx_http_upstream_check_mysql_parse(
ngx_http_upstream_check_peer_t *peer);
static void ngx_http_upstream_check_mysql_reinit(
ngx_http_upstream_check_peer_t *peer);
static ngx_int_t ngx_http_upstream_check_ajp_init(
ngx_http_upstream_check_peer_t *peer);
static ngx_int_t ngx_http_upstream_check_ajp_parse(
ngx_http_upstream_check_peer_t *peer);
static void ngx_http_upstream_check_ajp_reinit(
ngx_http_upstream_check_peer_t *peer);
static void ngx_http_upstream_check_status_update(
ngx_http_upstream_check_peer_t *peer,
ngx_int_t result);
static void ngx_http_upstream_check_clean_event(
ngx_http_upstream_check_peer_t *peer);
static void ngx_http_upstream_check_timeout_handler(ngx_event_t *event);
static void ngx_http_upstream_check_finish_handler(ngx_event_t *event);
static ngx_int_t ngx_http_upstream_check_need_exit();
static void ngx_http_upstream_check_clear_all_events();
static ngx_int_t ngx_http_upstream_check_status_handler(
ngx_http_request_t *r);
static void ngx_http_upstream_check_status_parse_args(ngx_http_request_t *r,
ngx_http_upstream_check_status_ctx_t *ctx);
static ngx_int_t ngx_http_upstream_check_status_command_format(
ngx_http_upstream_check_status_ctx_t *ctx, ngx_str_t *value);
static ngx_int_t ngx_http_upstream_check_status_command_status(
ngx_http_upstream_check_status_ctx_t *ctx, ngx_str_t *value);
static void ngx_http_upstream_check_status_html_format(ngx_buf_t *b,
ngx_http_upstream_check_peers_t *peers, ngx_uint_t flag);
static void ngx_http_upstream_check_status_csv_format(ngx_buf_t *b,
ngx_http_upstream_check_peers_t *peers, ngx_uint_t flag);
static void ngx_http_upstream_check_status_json_format(ngx_buf_t *b,
ngx_http_upstream_check_peers_t *peers, ngx_uint_t flag);
static ngx_int_t ngx_http_upstream_check_addr_change_port(ngx_pool_t *pool,
ngx_addr_t *dst, ngx_addr_t *src, ngx_uint_t port);
static ngx_check_conf_t *ngx_http_get_check_type_conf(ngx_str_t *str);
static char *ngx_http_upstream_check(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_upstream_check_keepalive_requests(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_upstream_check_http_send(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_upstream_check_http_expect_alive(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_upstream_check_fastcgi_params(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_upstream_check_shm_size(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static ngx_check_status_conf_t *ngx_http_get_check_status_format_conf(
ngx_str_t *str);
static char *ngx_http_upstream_check_status(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static void *ngx_http_upstream_check_create_main_conf(ngx_conf_t *cf);
static char *ngx_http_upstream_check_init_main_conf(ngx_conf_t *cf,
void *conf);
static void *ngx_http_upstream_check_create_srv_conf(ngx_conf_t *cf);
static char *ngx_http_upstream_check_init_srv_conf(ngx_conf_t *cf, void *conf);
static void *ngx_http_upstream_check_create_loc_conf(ngx_conf_t *cf);
static char * ngx_http_upstream_check_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
#define SHM_NAME_LEN 256
static char *ngx_http_upstream_check_init_shm(ngx_conf_t *cf, void *conf);
static ngx_int_t ngx_http_upstream_check_get_shm_name(ngx_str_t *shm_name,
ngx_pool_t *pool, ngx_uint_t generation);
static ngx_shm_zone_t *ngx_shared_memory_find(ngx_cycle_t *cycle,
ngx_str_t *name, void *tag);
static ngx_http_upstream_check_peer_shm_t *
ngx_http_upstream_check_find_shm_peer(ngx_http_upstream_check_peers_shm_t *peers_shm,
ngx_addr_t *addr);
static ngx_int_t ngx_http_upstream_check_init_shm_peer(
ngx_http_upstream_check_peer_shm_t *peer_shm,
ngx_http_upstream_check_peer_shm_t *opeer_shm,
ngx_uint_t init_down, ngx_pool_t *pool, ngx_str_t *peer_name);
static ngx_int_t ngx_http_upstream_check_init_shm_zone(
ngx_shm_zone_t *shm_zone, void *data);
static ngx_int_t ngx_http_upstream_check_init_process(ngx_cycle_t *cycle);
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_null_string, 0 }
};
static ngx_command_t ngx_http_upstream_check_commands[] = {
{ ngx_string("check"),
NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
ngx_http_upstream_check,
0,
0,
NULL },
{ ngx_string("check_keepalive_requests"),
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
ngx_http_upstream_check_keepalive_requests,
0,
0,
NULL },
{ ngx_string("check_http_send"),
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
ngx_http_upstream_check_http_send,
0,
0,
NULL },
{ ngx_string("check_http_expect_alive"),
NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
ngx_http_upstream_check_http_expect_alive,
0,
0,
NULL },
{ ngx_string("check_fastcgi_param"),
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE2,
ngx_http_upstream_check_fastcgi_params,
0,
0,
NULL },
{ ngx_string("check_shm_size"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_http_upstream_check_shm_size,
0,
0,
NULL },
{ ngx_string("check_status"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1|NGX_CONF_NOARGS,
ngx_http_upstream_check_status,
0,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_upstream_check_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
ngx_http_upstream_check_create_main_conf,/* create main configuration */
ngx_http_upstream_check_init_main_conf, /* init main configuration */
ngx_http_upstream_check_create_srv_conf, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_upstream_check_create_loc_conf, /* create location configuration */
ngx_http_upstream_check_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_upstream_check_module = {
NGX_MODULE_V1,
&ngx_http_upstream_check_module_ctx, /* module context */
ngx_http_upstream_check_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_upstream_check_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_str_t fastcgi_default_request;
static ngx_str_t fastcgi_default_params[] = {
ngx_string("REQUEST_METHOD"), ngx_string("GET"),
ngx_string("REQUEST_URI"), ngx_string("/"),
ngx_string("SCRIPT_FILENAME"), ngx_string("index.php"),
};
#define NGX_SSL_RANDOM "NGX_HTTP_CHECK_SSL_HELLO\n\n\n\n"
/*
* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
* check type of ssl_hello to ensure that the remote server speaks SSL.
*
* Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
*/
static char sslv3_client_hello_pkt[] = {
"\x16" /* ContentType : 0x16 = Hanshake */
"\x03\x01" /* ProtocolVersion : 0x0301 = TLSv1.0 */
"\x00\x6f" /* ContentLength : 0x6f bytes after this one */
"\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
"\x00\x00\x6b" /* HandshakeLength : 0x6b bytes after this one */
"\x03\x03" /* Hello Version : 0x0303 = TLSv1.2 */
"\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
NGX_SSL_RANDOM /* Random : must be exactly 28 bytes */
"\x00" /* Session ID length : empty (no session ID) */
"\x00\x1a" /* Cipher Suite Length : \x1a bytes after this one */
"\xc0\x2b" "\xc0\x2f" "\xcc\xa9" "\xcc\xa8" /* 13 modern ciphers */
"\xc0\x0a" "\xc0\x09" "\xc0\x13" "\xc0\x14"
"\x00\x33" "\x00\x39" "\x00\x2f" "\x00\x35"
"\x00\x0a"
"\x01" /* Compression Length : 0x01 = 1 byte for types */
"\x00" /* Compression Type : 0x00 = NULL compression */
"\x00\x28" /* Extensions length */
"\x00\x0a" /* EC extension */
"\x00\x08" /* extension length */
"\x00\x06" /* curves length */
"\x00\x17" "\x00\x18" "\x00\x19" /* Three curves */
"\x00\x0d" /* Signature extension */
"\x00\x18" /* extension length */
"\x00\x16" /* hash list length */
"\x04\x01" "\x05\x01" "\x06\x01" "\x02\x01" /* 11 hash algorithms */
"\x04\x03" "\x05\x03" "\x06\x03" "\x02\x03"
"\x05\x02" "\x04\x02" "\x02\x02"
};
#define NGX_SSL_HANDSHAKE 0x16
#define NGX_SSL_SERVER_HELLO 0x02
#define NGX_AJP_CPING 0x0a
#define NGX_AJP_CPONG 0x09
static char ngx_ajp_cping_packet[] = {
0x12, 0x34, 0x00, 0x01, NGX_AJP_CPING, 0x00
};
static char ngx_ajp_cpong_packet[] = {
0x41, 0x42, 0x00, 0x01, NGX_AJP_CPONG
};
static ngx_check_conf_t ngx_check_types[] = {
{ NGX_HTTP_CHECK_TCP,
ngx_string("tcp"),
ngx_null_string,
0,
ngx_http_upstream_check_peek_handler,
ngx_http_upstream_check_peek_handler,
NULL,
NULL,
NULL,
0,
1 },
{ NGX_HTTP_CHECK_HTTP,
ngx_string("http"),
ngx_string("GET / HTTP/1.0\r\n\r\n"),
NGX_CONF_BITMASK_SET | NGX_CHECK_HTTP_2XX | NGX_CHECK_HTTP_3XX,
ngx_http_upstream_check_send_handler,
ngx_http_upstream_check_recv_handler,
ngx_http_upstream_check_http_init,
ngx_http_upstream_check_http_parse,
ngx_http_upstream_check_http_reinit,
1,
1 },
{ NGX_HTTP_CHECK_HTTP,
ngx_string("fastcgi"),
ngx_null_string,
0,
ngx_http_upstream_check_send_handler,
ngx_http_upstream_check_recv_handler,
ngx_http_upstream_check_http_init,
ngx_http_upstream_check_fastcgi_parse,
ngx_http_upstream_check_http_reinit,
1,
0 },
{ NGX_HTTP_CHECK_SSL_HELLO,
ngx_string("ssl_hello"),
ngx_string(sslv3_client_hello_pkt),
0,
ngx_http_upstream_check_send_handler,
ngx_http_upstream_check_recv_handler,
ngx_http_upstream_check_ssl_hello_init,
ngx_http_upstream_check_ssl_hello_parse,
ngx_http_upstream_check_ssl_hello_reinit,
1,
0 },
{ NGX_HTTP_CHECK_MYSQL,
ngx_string("mysql"),
ngx_null_string,
0,
ngx_http_upstream_check_send_handler,
ngx_http_upstream_check_recv_handler,
ngx_http_upstream_check_mysql_init,
ngx_http_upstream_check_mysql_parse,
ngx_http_upstream_check_mysql_reinit,
1,
0 },
{ NGX_HTTP_CHECK_AJP,
ngx_string("ajp"),
ngx_string(ngx_ajp_cping_packet),
0,
ngx_http_upstream_check_send_handler,
ngx_http_upstream_check_recv_handler,
ngx_http_upstream_check_ajp_init,
ngx_http_upstream_check_ajp_parse,
ngx_http_upstream_check_ajp_reinit,
1,
0 },
{ 0,
ngx_null_string,
ngx_null_string,
0,
NULL,
NULL,
NULL,
NULL,
NULL,
0,
0 }
};
static ngx_check_status_conf_t ngx_check_status_formats[] = {
{ ngx_string("html"),
ngx_string("text/html"),
ngx_http_upstream_check_status_html_format },
{ ngx_string("csv"),
ngx_string("text/plain"),
ngx_http_upstream_check_status_csv_format },
{ ngx_string("json"),
ngx_string("application/json"), /* RFC 4627 */
ngx_http_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_http_upstream_check_status_command_format },
{ ngx_string("status"),
ngx_http_upstream_check_status_command_status },
{ ngx_null_string, NULL }
};
static ngx_uint_t ngx_http_upstream_check_shm_generation = 0;
static ngx_http_upstream_check_peers_t *check_peers_ctx = NULL;
ngx_uint_t
ngx_http_upstream_check_add_peer(ngx_conf_t *cf,
ngx_http_upstream_srv_conf_t *us, ngx_addr_t *peer_addr)
{
ngx_http_upstream_check_peer_t *peer;
ngx_http_upstream_check_peers_t *peers;
ngx_http_upstream_check_srv_conf_t *ucscf;
ngx_http_upstream_check_main_conf_t *ucmcf;
if (us->srv_conf == NULL) {
return NGX_ERROR;
}
ucscf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_check_module);
if(ucscf->check_interval == 0) {
return NGX_ERROR;
}
ucmcf = ngx_http_conf_get_module_main_conf(cf,
ngx_http_upstream_check_module);
peers = ucmcf->peers;
peer = ngx_array_push(&peers->peers);
if (peer == NULL) {
return NGX_ERROR;
}
ngx_memzero(peer, sizeof(ngx_http_upstream_check_peer_t));
peer->index = peers->peers.nelts - 1;
peer->conf = ucscf;
peer->upstream_name = &us->host;
peer->peer_addr = peer_addr;
if (ucscf->port) {
peer->check_peer_addr = ngx_pcalloc(cf->pool, sizeof(ngx_addr_t));
if (peer->check_peer_addr == NULL) {
return NGX_ERROR;
}
if (ngx_http_upstream_check_addr_change_port(cf->pool,
peer->check_peer_addr, peer_addr, ucscf->port)
!= NGX_OK) {
return NGX_ERROR;
}
} else {
peer->check_peer_addr = peer->peer_addr;
}
peers->checksum +=
ngx_murmur_hash2(peer_addr->name.data, peer_addr->name.len);
return peer->index;
}
static ngx_int_t
ngx_http_upstream_check_addr_change_port(ngx_pool_t *pool, ngx_addr_t *dst,
ngx_addr_t *src, ngx_uint_t port)
{
size_t len;
u_char *p;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
dst->socklen = src->socklen;
dst->sockaddr = ngx_palloc(pool, dst->socklen);
if (dst->sockaddr == NULL) {
return NGX_ERROR;
}
ngx_memcpy(dst->sockaddr, src->sockaddr, dst->socklen);
switch (dst->sockaddr->sa_family) {
case AF_INET:
len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
sin = (struct sockaddr_in *) dst->sockaddr;
sin->sin_port = htons(port);
break;
#if (NGX_HAVE_INET6)
case AF_INET6:
len = NGX_INET6_ADDRSTRLEN + sizeof(":65535") - 1;
sin6 = (struct sockaddr_in6 *) dst->sockaddr;
sin6->sin6_port = htons(port);
break;
#endif
default:
return NGX_ERROR;
}
p = ngx_pnalloc(pool, len);
if (p == NULL) {
return NGX_ERROR;
}
#if (nginx_version >= 1005012)
len = ngx_sock_ntop(dst->sockaddr, dst->socklen, p, len, 1);
#else
len = ngx_sock_ntop(dst->sockaddr, p, len, 1);
#endif
dst->name.len = len;
dst->name.data = p;
return NGX_OK;
}
ngx_uint_t
ngx_http_upstream_check_peer_down(ngx_uint_t index)
{
ngx_http_upstream_check_peer_t *peer;
if (check_peers_ctx == NULL || index >= check_peers_ctx->peers.nelts) {
return 0;
}
peer = check_peers_ctx->peers.elts;
return (peer[index].shm->down);
}
/* TODO: this interface can count each peer's busyness */
void
ngx_http_upstream_check_get_peer(ngx_uint_t index)
{
ngx_http_upstream_check_peer_t *peer;
if (check_peers_ctx == NULL || index >= check_peers_ctx->peers.nelts) {
return;
}
peer = check_peers_ctx->peers.elts;
ngx_shmtx_lock(&peer[index].shm->mutex);
peer[index].shm->busyness++;
peer[index].shm->access_count++;
ngx_shmtx_unlock(&peer[index].shm->mutex);
}
void
ngx_http_upstream_check_free_peer(ngx_uint_t index)
{
ngx_http_upstream_check_peer_t *peer;
if (check_peers_ctx == NULL || index >= check_peers_ctx->peers.nelts) {
return;
}
peer = check_peers_ctx->peers.elts;
ngx_shmtx_lock(&peer[index].shm->mutex);
if (peer[index].shm->busyness > 0) {
peer[index].shm->busyness--;
}
ngx_shmtx_unlock(&peer[index].shm->mutex);
}
static ngx_int_t
ngx_http_upstream_check_add_timers(ngx_cycle_t *cycle)
{
ngx_uint_t i;
ngx_msec_t t, delay;
ngx_check_conf_t *cf;
ngx_http_upstream_check_peer_t *peer;
ngx_http_upstream_check_peers_t *peers;
ngx_http_upstream_check_srv_conf_t *ucscf;
ngx_http_upstream_check_peer_shm_t *peer_shm;
ngx_http_upstream_check_peers_shm_t *peers_shm;
peers = check_peers_ctx;
if (peers == NULL) {
return NGX_OK;
}
peers_shm = peers->peers_shm;
if (peers_shm == NULL) {
return NGX_OK;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cycle->log, 0,
"http check upstream init_process, shm_name: %V, "
"peer number: %ud",
&peers->check_shm_name,
peers->peers.nelts);
srandom(ngx_pid);
peer = peers->peers.elts;
peer_shm = peers_shm->peers;
for (i = 0; i < peers->peers.nelts; i++) {
peer[i].shm = &peer_shm[i];
peer[i].check_ev.handler = ngx_http_upstream_check_begin_handler;
peer[i].check_ev.log = cycle->log;
peer[i].check_ev.data = &peer[i];
peer[i].check_ev.timer_set = 0;
peer[i].check_timeout_ev.handler =
ngx_http_upstream_check_timeout_handler;
peer[i].check_timeout_ev.log = cycle->log;
peer[i].check_timeout_ev.data = &peer[i];
peer[i].check_timeout_ev.timer_set = 0;
ucscf = peer[i].conf;
cf = ucscf->check_type_conf;
if (cf->need_pool) {
peer[i].pool = ngx_create_pool(ngx_pagesize, cycle->log);
if (peer[i].pool == NULL) {
return NGX_ERROR;
}
}
peer[i].send_handler = cf->send_handler;