22
22
#include <drizzled/gettext.h>
23
23
#include <drizzled/session.h>
27
# include <plugin/logging_syslog/names.h>
29
# define SYSLOG_NAMES 1
26
33
#include <stdarg.h>
28
// copied from drizzled/sql_parse.cc
29
const LEX_STRING command_name[]={
30
{ C_STRING_WITH_LEN("Sleep") },
31
{ C_STRING_WITH_LEN("Quit") },
32
{ C_STRING_WITH_LEN("InitDB") },
33
{ C_STRING_WITH_LEN("Query") },
34
{ C_STRING_WITH_LEN("FieldList") },
35
{ C_STRING_WITH_LEN("CreateDB") },
36
{ C_STRING_WITH_LEN("DropDB") },
37
{ C_STRING_WITH_LEN("Refresh") },
38
{ C_STRING_WITH_LEN("Shutdown") },
39
{ C_STRING_WITH_LEN("Processlist") },
40
{ C_STRING_WITH_LEN("Connect") },
41
{ C_STRING_WITH_LEN("Kill") },
42
{ C_STRING_WITH_LEN("Ping") },
43
{ C_STRING_WITH_LEN("Time") },
44
{ C_STRING_WITH_LEN("ChangeUser") },
45
{ C_STRING_WITH_LEN("BinlogDump") },
46
{ C_STRING_WITH_LEN("ConnectOut") },
47
{ C_STRING_WITH_LEN("RegisterSlave") },
48
{ C_STRING_WITH_LEN("SetOption") },
49
{ C_STRING_WITH_LEN("Daemon") },
50
{ C_STRING_WITH_LEN("Error") }
35
static bool sysvar_logging_syslog_enable= false;
36
static char* sysvar_logging_syslog_ident= NULL;
37
static char* sysvar_logging_syslog_facility= NULL;
38
static char* sysvar_logging_syslog_priority= NULL;
39
static ulong sysvar_logging_syslog_threshold_slow= 0;
40
static ulong sysvar_logging_syslog_threshold_big_resultset= 0;
41
static ulong sysvar_logging_syslog_threshold_big_examined= 0;
43
static int syslog_facility= -1;
44
static int syslog_priority= -1;
53
46
/* stolen from mysys/my_getsystime
54
47
until the Session has a good utime "now" we can use
74
67
assert(session != NULL);
76
/* skip returning field list, too verbose */
77
if (session->command == COM_FIELD_LIST) return false;
69
// skip returning field list, too verbose */
70
if (session->command == COM_FIELD_LIST)
73
// return if not enabled or query was too fast or resultset was too small
74
if (sysvar_logging_syslog_enable == false)
76
if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
78
if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
81
/* TODO, looks like connect_utime isnt being set in the session
82
object. We could store the time this plugin was loaded, but that
83
would just be a dumb workaround. */
84
/* TODO, the session object should have a "utime command completed"
85
inside itself, so be more accurate, and so this doesnt have to
86
keep calling current_utime, which can be slow */
79
88
uint64_t t_mark= get_microtime();
81
syslog(LOG_INFO, "thread_id=%ld query_id=%ld"
90
if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
93
/* to avoid trying to printf %s something that is potentially NULL */
95
const char *dbs= (session->db) ? session->db : "";
98
dbl= session->db_length;
100
const char *qys= (session->query) ? session->query : "";
103
qyl= session->query_length;
105
syslog(syslog_priority,
106
"thread_id=%ld query_id=%ld"
82
110
" t_connect=%lld t_start=%lld t_lock=%lld"
111
" rows_sent=%ld rows_examined=%ld\n",
112
(unsigned long) session->thread_id,
113
(unsigned long) session->query_id,
116
(int) command_name[session->command].length,
117
command_name[session->command].str,
118
(unsigned long long) (t_mark - session->connect_utime),
119
(unsigned long long) (t_mark - session->start_utime),
120
(unsigned long long) (t_mark - session->utime_after_lock),
121
(unsigned long) session->sent_row_count,
122
(unsigned long) session->examined_row_count);
125
syslog(syslog_priority,
126
"thread_id=%ld query_id=%ld"
84
" rows_sent=%ld rows_examined=%u\n"
85
" db=\"%.*s\" query=\"%.*s\"\n",
130
" t_connect=%lld t_start=%lld t_lock=%lld"
131
" rows_sent=%ld rows_examined=%ld\n",
86
132
(unsigned long) session->thread_id,
87
133
(unsigned long) session->query_id,
88
(unsigned long long)(t_mark - session->connect_utime),
89
(unsigned long long)(t_mark - session->start_utime),
90
(unsigned long long)(t_mark - session->utime_after_lock),
91
(uint32_t)command_name[session->command].length,
92
command_name[session->command].str,
93
(unsigned long) session->sent_row_count,
94
(uint32_t) session->examined_row_count,
95
134
session->db_length, session->db,
96
session->query_length, session->query);
135
// dont need to quote the query, because syslog does it itself
136
session->query_length, session->query,
137
(int) command_name[session->command].length,
138
command_name[session->command].str,
139
(unsigned long long) (t_mark - session->connect_utime),
140
(unsigned long long) (t_mark - session->start_utime),
141
(unsigned long long) (t_mark - session->utime_after_lock),
142
(unsigned long) session->sent_row_count,
143
(unsigned long) session->examined_row_count);
103
152
logging_t *l= (logging_t *) p;
105
openlog("drizzled", LOG_PID, LOG_LOCAL3);
155
for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
157
if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
159
syslog_facility= facilitynames[ndx].c_val;
163
if (syslog_facility == -1)
165
errmsg_printf(ERRMSG_LVL_WARN,
166
_("syslog facility \"%s\" not known, using \"local0\""),
167
sysvar_logging_syslog_facility);
168
syslog_facility= LOG_LOCAL0;
172
for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
174
if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
176
syslog_priority= prioritynames[ndx].c_val;
180
if (syslog_priority == -1)
182
errmsg_printf(ERRMSG_LVL_WARN,
183
_("syslog priority \"%s\" not known, using \"info\""),
184
sysvar_logging_syslog_priority);
185
syslog_priority= LOG_INFO;
188
openlog(sysvar_logging_syslog_ident,
189
LOG_PID, syslog_facility);
107
191
l->logging_pre= NULL;
108
192
l->logging_post= logging_syslog_func_post;
207
static DRIZZLE_SYSVAR_BOOL(
209
sysvar_logging_syslog_enable,
211
N_("Enable logging"),
212
NULL, /* check func */
213
NULL, /* update func */
214
false /* default */);
216
static DRIZZLE_SYSVAR_STR(
218
sysvar_logging_syslog_ident,
221
NULL, /* check func */
222
NULL, /* update func*/
223
"drizzled" /* default */);
225
static DRIZZLE_SYSVAR_STR(
227
sysvar_logging_syslog_facility,
229
N_("Syslog Facility"),
230
NULL, /* check func */
231
NULL, /* update func*/
232
"local0" /* default */); // local0 is what PostGreSQL uses by default
234
static DRIZZLE_SYSVAR_STR(
236
sysvar_logging_syslog_priority,
238
N_("Syslog Priority"),
239
NULL, /* check func */
240
NULL, /* update func*/
241
"info" /* default */);
243
static DRIZZLE_SYSVAR_ULONG(
245
sysvar_logging_syslog_threshold_slow,
247
N_("Threshold for logging slow queries, in microseconds"),
248
NULL, /* check func */
249
NULL, /* update func */
255
static DRIZZLE_SYSVAR_ULONG(
256
threshold_big_resultset,
257
sysvar_logging_syslog_threshold_big_resultset,
259
N_("Threshold for logging big queries, for rows returned"),
260
NULL, /* check func */
261
NULL, /* update func */
267
static DRIZZLE_SYSVAR_ULONG(
268
threshold_big_examined,
269
sysvar_logging_syslog_threshold_big_examined,
271
N_("Threshold for logging big queries, for rows examined"),
272
NULL, /* check func */
273
NULL, /* update func */
123
279
static struct st_mysql_sys_var* logging_syslog_system_variables[]= {
280
DRIZZLE_SYSVAR(enable),
281
DRIZZLE_SYSVAR(ident),
282
DRIZZLE_SYSVAR(facility),
283
DRIZZLE_SYSVAR(priority),
284
DRIZZLE_SYSVAR(threshold_slow),
285
DRIZZLE_SYSVAR(threshold_big_resultset),
286
DRIZZLE_SYSVAR(threshold_big_examined),