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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <postgresql/libpq-fe.h>
#include "triton.h"
#include "spinlock.h"
#include "log.h"
#include "list.h"
#include "ppp.h"
static char *conf_conninfo;
static int conf_queue_max = 1000;
static char *conf_query;
#define QUERY_TEMPLATE "insert into %s (timestamp, username, sessionid, msg) values ($1, $2, $3, $4)"
static void start_connect(void);
static void start_connect_timer(struct triton_timer_t *);
static struct triton_context_t pgsql_ctx;
static struct triton_md_handler_t pgsql_hnd;
static struct triton_timer_t connect_timer = {
.period = 5000,
.expire = start_connect_timer,
};
static PGconn *conn;
static LIST_HEAD(msg_queue);
static int queue_size;
static int sleeping = 0;
static spinlock_t queue_lock = SPINLOCK_INITIALIZER;
static char *log_buf;
static void unpack_msg(struct log_msg_t *msg)
{
struct log_chunk_t *chunk;
int pos = 0;
list_for_each_entry(chunk, msg->chunks, entry) {
memcpy(log_buf + pos, chunk->msg, chunk->len);
pos += chunk->len;
}
if (pos > 1)
log_buf[pos - 1] = 0;
else
log_buf[0] = 0;
}
static void set_hdr(struct log_msg_t *msg)
{
struct tm tm;
localtime_r(&msg->timestamp.tv_sec, &tm);
strftime(msg->hdr->msg, LOG_CHUNK_SIZE, "%Y-%m-%d %H:%M:%S", &tm);
msg->hdr->len = strlen(msg->hdr->msg);
}
static void write_next_msg(void)
{
struct log_msg_t *msg;
struct ppp_t *ppp;
const char *paramValues[4];
int paramFormats[4] = {0, 0, 0, 0};
spin_lock(&queue_lock);
if (!list_empty(&msg_queue)) {
msg = list_entry(msg_queue.next, typeof(*msg), entry);
list_del(&msg->entry);
--queue_size;
spin_unlock(&queue_lock);
set_hdr(msg);
unpack_msg(msg);
ppp = msg->tpd;
if (ppp) {
paramValues[1] = ppp->username;
paramValues[2] = ppp->sessionid;
} else {
paramValues[1] = NULL;
paramValues[2] = NULL;
}
paramValues[0] = msg->hdr->msg;
paramValues[3] = log_buf;
PQsendQueryParams(conn, conf_query, 4, NULL, paramValues, NULL, paramFormats, 0);
PQflush(conn);
log_free_msg(msg);
return;
}
sleeping = 1;
spin_unlock(&queue_lock);
}
static int pgsql_check_ready(struct triton_md_handler_t *h)
{
PGresult *res;
if (!PQconsumeInput(conn)) {
log_emerg("log_pgsql: %s\n", PQerrorMessage(conn));
if (PQstatus(conn) == CONNECTION_BAD) {
PQfinish(conn);
start_connect();
}
}
if (PQisBusy(conn))
return 0;
while (1) {
res = PQgetResult(conn);
if (!res)
break;
if (PQresultStatus(res) != PGRES_COMMAND_OK)
log_emerg("log_pgsql: %s\n", PQerrorMessage(conn));
PQclear(res);
}
write_next_msg();
return 0;
}
static int pgsql_flush(struct triton_md_handler_t *h)
{
PQflush(conn);
return 0;
}
static void wakeup_log(void)
{
write_next_msg();
}
static void queue_log(struct log_msg_t *msg)
{
int r = 0;
spin_lock(&queue_lock);
if (queue_size < conf_queue_max) {
list_add_tail(&msg->entry, &msg_queue);
++queue_size;
r = sleeping;
sleeping = 0;
}
spin_unlock(&queue_lock);
if (r)
triton_context_call(&pgsql_ctx, (void (*)(void*))wakeup_log, NULL);
}
static void general_log(struct log_msg_t *msg)
{
msg->tpd = NULL;
queue_log(msg);
}
static void session_log(struct ppp_t *ppp, struct log_msg_t *msg)
{
msg->tpd = ppp;
queue_log(msg);
}
static int wait_connect(struct triton_md_handler_t *h)
{
PostgresPollingStatusType status = PQconnectPoll(conn);
char *err_msg;
switch(status) {
case PGRES_POLLING_READING:
triton_md_enable_handler(h, MD_MODE_READ);
triton_md_disable_handler(h, MD_MODE_WRITE);
break;
case PGRES_POLLING_WRITING:
triton_md_enable_handler(h, MD_MODE_WRITE);
triton_md_disable_handler(h, MD_MODE_READ);
break;
case PGRES_POLLING_FAILED:
err_msg = PQerrorMessage(conn);
log_emerg("log_pgsql: %s\n", err_msg);
triton_md_disable_handler(h, MD_MODE_READ | MD_MODE_WRITE);
PQfinish(conn);
h->read = NULL;
h->write = NULL;
if (!connect_timer.tpd)
triton_timer_add(&pgsql_ctx, &connect_timer, 0);
break;
case PGRES_POLLING_OK:
//triton_md_disable_handler(h, MD_MODE_READ | MD_MODE_WRITE);
PQsetnonblocking(conn, 1);
h->write = pgsql_flush;
h->read = pgsql_check_ready;
triton_md_enable_handler(&pgsql_hnd, MD_MODE_READ | MD_MODE_WRITE);
wakeup_log();
break;
default:
break;
}
return 0;
}
static void start_connect(void)
{
conn = PQconnectStart(conf_conninfo);
if (!conn) {
log_emerg("log_pgsql: out of memory\n");
return;
}
if (PQstatus(conn) == CONNECTION_BAD) {
log_emerg("log_pgsql: PQconnectStart failed\n");
}
pgsql_hnd.fd = PQsocket(conn);
pgsql_hnd.read = wait_connect;
pgsql_hnd.write = wait_connect;
wait_connect(&pgsql_hnd);
}
static void start_connect_timer(struct triton_timer_t *t)
{
triton_timer_del(t);
start_connect();
}
static struct log_target_t target = {
.log = general_log,
.session_log = session_log,
};
static void __init init(void)
{
char *opt;
opt = conf_get_opt("log-pgsql", "conninfo");
if (!opt)
return;
conf_conninfo = opt;
opt = conf_get_opt("log-pgsql", "connect-inteval");
if (opt && atoi(opt) > 0)
connect_timer.period = atoi(opt) * 1000;
opt = conf_get_opt("log-pgsql", "log-query");
if (opt)
conf_query = opt;
else {
opt = conf_get_opt("log-pgsql", "log-table");
if (!opt || strlen(opt) > 32)
opt = "log";
conf_query = malloc(sizeof(QUERY_TEMPLATE) + strlen(opt));
sprintf(conf_query, QUERY_TEMPLATE, opt);
}
log_buf = malloc(LOG_MAX_SIZE + 1);
if (!log_buf) {
log_emerg("log_pgsql: out of memory\n");
return;
}
triton_context_register(&pgsql_ctx, NULL);
triton_md_register_handler(&pgsql_ctx, &pgsql_hnd);
start_connect();
log_register_target(&target);
}
|