-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql-test.c
204 lines (167 loc) · 6.69 KB
/
mysql-test.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
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <my_global.h> /* MySQL client headers */
#include <mysql.h>
#include "config.h"
int main(void) {
int list_s; /* listening socket */
int conn_s; /* connection socket */
struct sockaddr_in6 servaddr; /* socket address structure */
const int reuseaddr = 1; /* socket options */
const int port = PORT; /* port number */
int status = DEFAULT_HTTP_CODE; /* HTTP status code */
const int PROTOCOL_TCP = MYSQL_PROTOCOL_TCP; /* use TCP/IP */
if ((list_s = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Error creating listening socket: %d\n", errno);
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin6_family = AF_INET6;
servaddr.sin6_port = htons(port);
inet_pton(servaddr.sin6_family, MYSQL_HOST, &(servaddr.sin6_addr));
if (setsockopt(list_s, SOL_SOCKET, SO_REUSEADDR,
(const void *) &reuseaddr, sizeof(int)) < 0) {
fprintf(stderr, "Error calling setsockopt(): %d\n", errno);
exit(EXIT_FAILURE);
}
if (bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
fprintf(stderr, "Error calling bind(): %d\n", errno);
exit(EXIT_FAILURE);
}
if (listen(list_s, LISTENQ) < 0) {
fprintf(stderr, "Error calling listen(): %d\n", errno);
exit(EXIT_FAILURE);
}
for (;;) {
if ((conn_s = accept(list_s, NULL, NULL)) < 0) {
continue;
}
MYSQL *conn_m = mysql_init(NULL);
/*
* While doing error handling on MySQL connection we
* deliberately set different new HTTP status code.
*
* HA-Proxy will be getting:
* 200 (good) - MySQL in good shape
* 500 (error) - MySQL in bad shape
* 503 (error) - connection failure to database
* 502 (error) - something wrong with this program
*/
if (conn_m == NULL) {
status = 503;
fprintf(stderr, "503 in mysql_init\n");
}
mysql_options(conn_m, MYSQL_OPT_PROTOCOL, &PROTOCOL_TCP);
if (mysql_real_connect(conn_m, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD,
NULL, 0, NULL, 0) == NULL) {
status = 503;
fprintf(stderr, "503 in mysql_real_connect: %s\n",
mysql_error(conn_m));
}
if (mysql_query(conn_m, QUERY)) {
status = 503;
fprintf(stderr, "503 in mysql_query\n");
}
MYSQL_RES *result = mysql_store_result(conn_m);
if (result == NULL) {
status = 503;
fprintf(stderr, "503 in mysql_store_result\n");
}
if (status == DEFAULT_HTTP_CODE) {
/*
* Connected to database.
*/
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
char *slave_io_running = row[10];
char *slave_sql_running = row[11];
char *seconds_behind_master = row[32];
/*
* Testing for running slave threads:
*/
if (strcmp(GOOD_IO_RUNNING, slave_io_running) != 0) {
status = 500;
fprintf(stderr, "500: in GOOD_IO_RUNNING\n");
} else if (strcmp(GOOD_SQL_RUNNING, slave_sql_running) != 0) {
status = 500;
fprintf(stderr, "500: in GOOD_SQL_RUNNING\n");
} else {
/*
* Testing for lag behind master:
*/
char *end;
errno = 0;
const long ld = strtol(seconds_behind_master, &end, 10);
int seconds;
if ('\0' == seconds_behind_master[0]) {
status = 500;
fprintf(stderr, "500: array empty\n");
} else if (end == seconds_behind_master) {
status = 500;
fprintf(stderr, "500: %s: not a number\n",
seconds_behind_master);
} else if ('\0' != *end) {
status = 500;
fprintf(stderr, "500: %s: extra characters: %s\n",
seconds_behind_master, end);
} else if ((LONG_MIN == ld || LONG_MAX == ld)
&& ERANGE == errno) {
status = 500;
fprintf(stderr, "500: %s out of range of type long\n",
seconds_behind_master);
} else if (ld > INT_MAX) {
status = 500;
fprintf(stderr, "500: %ld greater than INT_MAX\n", ld);
} else if (ld < INT_MIN) {
status = 500;
fprintf(stderr, "500: %ld less than INT_MIN\n", ld);
} else if (ld < 0) {
status = 500;
fprintf(stderr, "500: %ld less than zero\n", ld);
} else {
seconds = (int)ld;
if (seconds <= GOOD_SECONDS_BEHIND) {
status = 200;
} else {
status = 500;
fprintf(stderr, "500: in GOOD_SECONDS_BEHIND\n");
}
}
}
break;
}
mysql_free_result(result);
mysql_close(conn_m);
}
char message[BUFSIZE];
switch (status) {
case 200:
strcpy(message, "HTTP/1.1 200 OK\r\n");
strcat(message, "Content-length: 4\r\n");
strcat(message, "Content-Type: text/plain\r\n\r\n");
strcat(message, "OK\r\n");
break;
case 500:
strcpy(message, "HTTP/1.1 500 Internal Server Error\r\n");
break;
case 503:
strcpy(message, "HTTP/1.1 503 Temporarily unavailable\r\n");
break;
default:
strcpy(message, "HTTP/1.1 502 Bad Gateway\r\n");
break;
}
send(conn_s, message, strlen(message), MSG_NOSIGNAL);
status = DEFAULT_HTTP_CODE; /* resetting status for next round */
close(conn_s);
}
return 0;
}