-
Notifications
You must be signed in to change notification settings - Fork 12
/
mod_nginx.c
189 lines (156 loc) · 5.5 KB
/
mod_nginx.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
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include "tsar.h"
struct stats_nginx {
unsigned long long naccept; /* accepted connections */
unsigned long long nhandled; /* handled connections */
unsigned long long nrequest; /* handled requests */
unsigned long long nactive; /* number of all open connections including connections to backends */
unsigned long long nreading; /* nginx reads request header */
unsigned long long nwriting; /* nginx reads request body, processes request, or writes response to a client */
unsigned long long nwaiting; /* keep-alive connections, actually it is active - (reading + writing) */
unsigned long long nrstime; /* reponse time of handled requests */
};
struct hostinfo {
char *host;
int port;
char *server_name;
char *uri;
};
static char *nginx_usage = " --nginx nginx statistics";
static struct mod_info nginx_info[] = {
{"accept", DETAIL_BIT, 0, STATS_SUB},
{"handle", DETAIL_BIT, 0, STATS_SUB},
{" reqs", DETAIL_BIT, 0, STATS_SUB},
{"active", DETAIL_BIT, 0, STATS_NULL},
{" read", DETAIL_BIT, 0, STATS_NULL},
{" write", DETAIL_BIT, 0, STATS_NULL},
{" wait", DETAIL_BIT, 0, STATS_NULL},
{" qps", SUMMARY_BIT, 0, STATS_SUB_INTER},
{" rt", SUMMARY_BIT, 0, STATS_NULL},
};
static void set_nginx_record(struct module *mod, double st_array[],
U_64 pre_array[], U_64 cur_array[], int inter)
{
int i;
for (i = 0; i < 3; i++) {
if (cur_array[i] >= pre_array[i]) {
st_array[i] = cur_array[i] - pre_array[i];
}
}
for (i = 3; i < 7; i++) {
st_array[i] = cur_array[i];
}
if (cur_array[2] >= pre_array[2]) {
st_array[7] = (cur_array[2] - pre_array[2]) * 1.0 / inter;
}
if (cur_array[8] >= pre_array[8]) {
if (cur_array[2] > pre_array[2]) {
st_array[8] = (cur_array[8] - pre_array[8]) * 1.0 / (cur_array[2] - pre_array[2]);
}
}
}
static void init_nginx_host_info(struct hostinfo *p)
{
char *port;
p->host = getenv("NGX_TSAR_HOST");
p->host = p->host ? p->host : "127.0.0.1";
port = getenv("NGX_TSAR_PORT");
p->port = port ? atoi(port) : 80;
p->uri = getenv("NGX_TSAR_URI");
p->uri = p->uri ? p->uri : "/nginx_status";
p->server_name = getenv("NGX_TSAR_SERVER_NAME");
p->server_name = p->server_name ? p->server_name : "status.taobao.com";
}
void read_nginx_stats(struct module *mod)
{
int write_flag = 0, addr_len, domain;
int m, sockfd, send, pos;
void *addr;
char buf[LEN_4096], request[LEN_4096], line[LEN_4096];
struct sockaddr_in servaddr;
struct sockaddr_un servaddr_un;
FILE *stream = NULL;
struct hostinfo hinfo;
init_nginx_host_info(&hinfo);
struct stats_nginx st_nginx;
memset(&st_nginx, 0, sizeof(struct stats_nginx));
if (*hinfo.host == '/') {
addr = &servaddr_un;
addr_len = sizeof(servaddr_un);
bzero(addr, addr_len);
domain = AF_LOCAL;
servaddr_un.sun_family = AF_LOCAL;
strncpy(servaddr_un.sun_path, hinfo.host, sizeof(servaddr_un.sun_path) - 1);
} else {
addr = &servaddr;
addr_len = sizeof(servaddr);
bzero(addr, addr_len);
domain = AF_INET;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(hinfo.port);
inet_pton(AF_INET, hinfo.host, &servaddr.sin_addr);
}
if ((sockfd = socket(domain, SOCK_STREAM, 0)) == -1) {
goto writebuf;
}
sprintf(request,
"GET %s HTTP/1.0\r\n"
"User-Agent: Wget/1.9\r\n"
"Host: %s\r\n"
"Accept:*/*\r\n"
"Connection: Close\r\n\r\n",
hinfo.uri, hinfo.server_name);
if ((m = connect(sockfd, (struct sockaddr *) addr, addr_len)) == -1 ) {
goto writebuf;
}
if ((send = write(sockfd, request, strlen(request))) == -1) {
goto writebuf;
}
if ((stream = fdopen(sockfd, "r")) == NULL) {
goto writebuf;
}
while (fgets(line, LEN_4096, stream) != NULL) {
if (!strncmp(line, "Active connections:", sizeof("Active connections:") - 1)) {
sscanf(line + sizeof("Active connections:"), "%llu", &st_nginx.nactive);
} else if (!strncmp(line, " ", 1)) {
sscanf(line + 1, "%llu %llu %llu %llu",
&st_nginx.naccept, &st_nginx.nhandled, &st_nginx.nrequest, &st_nginx.nrstime);
write_flag = 1;
} else if (!strncmp(line, "Reading:", sizeof("Reading:") - 1)) {
sscanf(line, "Reading: %llu Writing: %llu Waiting: %llu",
&st_nginx.nreading, &st_nginx.nwriting, &st_nginx.nwaiting);
}
else
;
}
writebuf:
if (stream) {
fclose(stream);
}
if (sockfd != -1) {
close(sockfd);
}
if (write_flag) {
pos = sprintf(buf, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld",
st_nginx.naccept,
st_nginx.nhandled,
st_nginx.nrequest,
st_nginx.nactive,
st_nginx.nreading,
st_nginx.nwriting,
st_nginx.nwaiting,
st_nginx.nrequest,
st_nginx.nrstime
);
buf[pos] = '\0';
set_mod_record(mod, buf);
}
}
void mod_register(struct module *mod)
{
register_mod_fileds(mod, "--nginx", nginx_usage, nginx_info, 9, read_nginx_stats, set_nginx_record);
}