-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_http_monitor_sender.c
238 lines (171 loc) · 7.37 KB
/
ngx_http_monitor_sender.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
#include "ngx_http_monitor_sender.h"
#include "ngx_http_monitor_module.h"
#include "ngx_http_monitor_message.h"
#include <sys/epoll.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
// json body
static const char NGX_HTTP_MONITOR_FORMATER[] = "{\"host\":\"%V\", \"uri\":\"%V\", \"status\":%ui}";
static const char NGX_HTTP_MONITOR_FORMATER_US[] = "{\"host\":\"%V\", \"uri\":\"%V\", \"upstream\":\"%V\", \"status\":%ui}";
static void ngx_http_monitor_send_startloop(ngx_http_monitor_sender_t* sender, ngx_http_request_t *r);
static void* ngx_http_monitor_send_mainloop(void* args);
ngx_http_monitor_sender_t* ngx_http_monitor_create_sender(ngx_array_t *servers, ngx_int_t backlog, ngx_log_t* log){
ngx_http_monitor_sender_t* sender;
sender = (ngx_http_monitor_sender_t*)malloc(sizeof(ngx_http_monitor_sender_t));
// ngx_log_t* log = malloc(sizeof(ngx_log_t));
// log->file = ngx_conf_open_file();
sender->queue = ngx_http_monitor_create_queue(backlog, log);
if(sender == NULL || sender->queue == NULL){
free(sender);
return NULL;
}
sender->servers = servers;
// create on first push
sender->sendthr = NULL;
return sender;
}
static void ngx_http_monitor_destory_sender(void* args){
ngx_http_monitor_sender_t* sender = (ngx_http_monitor_sender_t*)args;
ngx_http_monitor_destory_queue(sender->queue);
free(sender->sendthr);
free(sender);
}
void ngx_http_monitor_send(ngx_http_monitor_sender_t* sender, ngx_http_request_t *r){
ngx_http_monitor_str_t* str = (ngx_http_monitor_str_t*)malloc(sizeof(ngx_http_monitor_str_t));
if(str == NULL){
ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"Failed to malloc for buf in ngx_http_monitor_send!");
return;
}
struct Message msg = MESSAGE_INTIALIZER;
if(r->upstream != NULL){
msg.totalLen = msg.HEADER_LENGTH + sizeof(NGX_HTTP_MONITOR_FORMATER_US) +
r->headers_in.server.len - 2 + r->unparsed_uri.len - 2 + r->upstream->peer.name->len - 2 + 3 - 3 -1;
} else {
msg.totalLen = msg.HEADER_LENGTH + sizeof(NGX_HTTP_MONITOR_FORMATER) +
r->headers_in.server.len - 2 + r->unparsed_uri.len - 2 + 3 - 3 -1;
}
struct hostent *he = gethostbyname((const char*)ngx_cycle->hostname.data);
// struct hostent *he = gethostbyname((const char*)r->headers_in.host->value.data);
struct in_addr *addr = (struct in_addr*)he->h_addr;
msg.warnlevel = 1;
msg.ip = revertByteL(ntohl(addr->s_addr));
msg.timestamp = (int64_t) ngx_time();
toByteWithoutCopyBody(&msg, str);
if (str->buf == NULL) {
ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"Failed to malloc for buf in ngx_http_monitor_send!");
free(str);
return;
}
// %V ngx_str_t *
// %[0][width][u][x|X]d int/u_int
if (r->upstream == NULL) {
ngx_snprintf(str->buf + msg.HEADER_LENGTH, str->buflen - msg.HEADER_LENGTH, NGX_HTTP_MONITOR_FORMATER,
&r->headers_in.server, &r->unparsed_uri, r->headers_out.status);
} else {
ngx_snprintf(str->buf + msg.HEADER_LENGTH, str->buflen - msg.HEADER_LENGTH, NGX_HTTP_MONITOR_FORMATER_US,
&r->headers_in.server, &r->unparsed_uri, r->upstream->peer.name, r->headers_out.status);
}
// no need
// str->buf[str->buflen -1] = '\0';
if (sender->sendthr == NULL) {
ngx_http_monitor_send_startloop(sender, r);
}
ngx_http_monitor_push_queue(sender->queue, str);
}
static void ngx_http_monitor_send_startloop(ngx_http_monitor_sender_t* sender, ngx_http_request_t *r){
pthread_mutex_lock(sender->queue->mutex);
if (sender->sendthr == NULL) {
sender->sendthr = (pthread_t*)malloc(sizeof(pthread_t));
if (sender->sendthr == NULL) {
ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"Failed to malloc for sendthr in ngx_http_monitor_send!");
pthread_mutex_unlock(sender->queue->mutex);
return;
}
pthread_attr_t detach;
pthread_attr_init(&detach);
pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(sender->sendthr, &detach, ngx_http_monitor_send_mainloop, sender);
if (ret != 0) {
ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"Failed to create thread in ngx_http_monitor_send!");
free(sender->sendthr);
sender->sendthr = NULL;
pthread_mutex_unlock(sender->queue->mutex);
return;
}
}
pthread_mutex_unlock(sender->queue->mutex);
}
static void* ngx_http_monitor_send_mainloop(void* args){
ngx_http_monitor_sender_t* sender = (ngx_http_monitor_sender_t*)args;
pthread_cleanup_push(ngx_http_monitor_destory_sender, sender);
ngx_http_monitor_server_t* uscfp = sender->servers->elts;
int totalfd = sender->servers->nelts;
int maxfd = 0;
int i;
u_char* sendp;
ngx_int_t sendlen;
struct epoll_event event;
struct epoll_event events[totalfd];
//since linux 2.6.8, the size arg is unused, but must be > 0
// int epollfd = epoll_create1(0); // won't compile
int epollfd = epoll_create(1024);
if (epollfd == -1) {
// error
return NULL;
}
for (i = 0; i < totalfd; i++) {
int sockfd = uscfp[i].sockfd;
event.events = EPOLLOUT | EPOLLET;
event.data.fd = sockfd;
epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &event);
if (sockfd > maxfd) {
maxfd = sockfd;
}
}
while (!ngx_quit && !ngx_terminate) {
ngx_http_monitor_str_t* str = ngx_http_monitor_pull_queue( sender->queue );
ngx_uint_t leftfd = totalfd;
// FIXME:
while (leftfd > 0) {
int nfds = epoll_wait(epollfd, events, maxfd + 1, 1000);
if (nfds <= 0) {
// error
continue;
}
for (i = 0; i < nfds; i++) {
struct sockaddr_in* addr = uscfp[i].sockaddr;
socklen_t socklen = uscfp[i].socklen;
int sockfd = uscfp[i].sockfd;
sendp = str->buf;
sendlen = str->buflen;
while (sendlen > 0) {
int ret = sendto ( sockfd, sendp, sendlen, 0, addr, socklen );
if (ret == -1) {
if (EAGAIN == errno) {
continue;
}
ngx_log_error ( NGX_LOG_ERR, sender->log, 0, "Failed to send messege to peer:%s %d, %s",
addr->sin_addr.s_addr,addr->sin_port, strerror(errno));
}
sendp += ret;
sendlen -= ret;
}
event.events = EPOLLOUT | EPOLLET;
event.data.fd = sockfd;
epoll_ctl(epollfd, EPOLL_CTL_DEL, sockfd, &event);
}
leftfd -= nfds;
}
free(str->buf);
free(str);
for (i = 0; i < totalfd; i++) {
int sockfd = uscfp[i].sockfd;
event.events = EPOLLOUT | EPOLLET;
event.data.fd = sockfd;
epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &event);
}
}
pthread_cleanup_pop(0);
return NULL;
}