2311.1.3
by Daniel Nichter
Change to GPLv3 license. |
1 |
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2311.1.1
by Daniel Nichter
Add query_log plugin. It's tested and documented. |
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
3 |
*
|
|
2311.1.3
by Daniel Nichter
Change to GPLv3 license. |
4 |
* Copyright 2011 Daniel Nichter
|
2311.1.1
by Daniel Nichter
Add query_log plugin. It's tested and documented. |
5 |
*
|
2311.1.3
by Daniel Nichter
Change to GPLv3 license. |
6 |
* This program is free software: you can redistribute it and/or modify
|
2311.1.1
by Daniel Nichter
Add query_log plugin. It's tested and documented. |
7 |
* it under the terms of the GNU General Public License as published by
|
2311.1.3
by Daniel Nichter
Change to GPLv3 license. |
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
|
2311.1.1
by Daniel Nichter
Add query_log plugin. It's tested and documented. |
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
* GNU General Public License for more details.
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License
|
|
2311.1.3
by Daniel Nichter
Change to GPLv3 license. |
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2311.1.1
by Daniel Nichter
Add query_log plugin. It's tested and documented. |
18 |
*/
|
19 |
||
20 |
#include <config.h> |
|
21 |
#include <string> |
|
22 |
#include <fcntl.h> |
|
23 |
#include <drizzled/item.h> |
|
24 |
#include <drizzled/module/option_map.h> |
|
25 |
#include <drizzled/session.h> |
|
26 |
#include <drizzled/session/times.h> |
|
27 |
#include <drizzled/plugin.h> |
|
28 |
#include "query_log.h" |
|
29 |
||
30 |
using namespace std; |
|
31 |
using namespace drizzled; |
|
32 |
using namespace plugin; |
|
33 |
||
34 |
QueryLog::QueryLog(bool enabled, QueryLoggerFile *logger_file) : |
|
35 |
drizzled::plugin::EventObserver("query_log"), |
|
36 |
sysvar_enabled(enabled), |
|
37 |
_logger_file(logger_file) |
|
38 |
{
|
|
39 |
}
|
|
40 |
||
41 |
void QueryLog::registerSessionEventsDo(Session &, EventObserverList &observers) |
|
42 |
{
|
|
43 |
registerEvent(observers, AFTER_STATEMENT); |
|
44 |
}
|
|
45 |
||
46 |
bool QueryLog::observeEventDo(EventData &data) |
|
47 |
{
|
|
48 |
// Don't log and return successful if...
|
|
49 |
if (not sysvar_enabled // all logging is disabled |
|
50 |
|| not sysvar_file_enabled) // or file logging is disabled |
|
51 |
return false; |
|
52 |
||
53 |
switch (data.event) { |
|
54 |
case AFTER_STATEMENT: |
|
55 |
afterStatement((AfterStatementEventData &)data); |
|
56 |
break; |
|
57 |
default: |
|
58 |
fprintf(stderr, "query_log: Unexpected event '%s'\n", |
|
59 |
EventObserver::eventName(data.event)); |
|
60 |
}
|
|
61 |
||
62 |
return false; |
|
63 |
}
|
|
64 |
||
65 |
bool QueryLog::afterStatement(AfterStatementEventData &data) |
|
66 |
{
|
|
67 |
Session *session= &data.session; |
|
68 |
||
69 |
// For the moment we're only interestd in queries, not admin
|
|
70 |
// command and such.
|
|
71 |
if (session->command != COM_QUERY) |
|
72 |
return false; |
|
73 |
||
74 |
// Query end time (microseconds from epoch)
|
|
75 |
uint64_t t_mark= session->times.getCurrentTimestamp(false); |
|
76 |
||
77 |
/**
|
|
78 |
* Time values, first in microseconds so we can check the thresholds.
|
|
79 |
*/
|
|
80 |
_event.execution_time= session->times.getElapsedTime(); |
|
81 |
_event.lock_time= (t_mark - session->times.utime_after_lock); |
|
82 |
_event.session_time= (t_mark - session->times.getConnectMicroseconds()); |
|
83 |
||
84 |
/**
|
|
85 |
* Check thresholds as soon as possible, return early if possible.
|
|
86 |
* This avoid unnecessary work; i.e. don't construct the whole event
|
|
87 |
* only to throw it away at the end because it fails a threshold.
|
|
88 |
*/
|
|
89 |
if ( _event.execution_time < sysvar_threshold_execution_time |
|
90 |
|| _event.lock_time < sysvar_threshold_lock_time |
|
91 |
|| _event.session_time < sysvar_threshold_session_time) |
|
92 |
return false; |
|
93 |
||
94 |
/**
|
|
95 |
* Convert from microseconds to seconds, e.g. 42 to 0.000042
|
|
96 |
* This is done for the user who may read the log. It's a lot
|
|
97 |
* easier to see half a second as 0.5 than 500000.
|
|
98 |
*/
|
|
99 |
_event.execution_time= _event.execution_time * 0.000001; |
|
100 |
_event.lock_time= _event.lock_time * 0.000001; |
|
101 |
_event.session_time= _event.session_time * 0.000001; |
|
102 |
||
103 |
/**
|
|
104 |
* Integer values
|
|
105 |
*/
|
|
106 |
_event.session_id= session->getSessionId(); |
|
107 |
_event.query_id= session->getQueryId(); |
|
108 |
_event.rows_examined= session->examined_row_count; |
|
109 |
_event.rows_sent= session->sent_row_count; |
|
110 |
_event.tmp_tables= session->tmp_table; |
|
111 |
_event.warnings= session->total_warn_count; |
|
112 |
||
113 |
if ( _event.rows_examined < sysvar_threshold_rows_examined |
|
114 |
|| _event.rows_sent < sysvar_threshold_rows_sent |
|
115 |
|| _event.tmp_tables < sysvar_threshold_tmp_tables |
|
116 |
|| _event.warnings < sysvar_threshold_warnings) |
|
117 |
return false; |
|
118 |
||
119 |
/**
|
|
120 |
* Boolean values, as strings
|
|
121 |
*/
|
|
122 |
_event.error= session->is_error() ? "true" : "false"; |
|
123 |
||
124 |
/**
|
|
125 |
* String values, may be blank
|
|
126 |
*/
|
|
127 |
_event.schema= session->schema()->c_str(); |
|
128 |
||
129 |
/**
|
|
130 |
* The query string
|
|
131 |
*/
|
|
132 |
_event.query= session->getQueryString()->c_str(); |
|
133 |
||
134 |
/**
|
|
135 |
* Timestamp values, convert from microseconds from epoch to
|
|
136 |
* ISO timestamp like 2002-01-31T10:00:01.123456
|
|
137 |
*/
|
|
138 |
boost::posix_time::ptime t_start= session->times.start_timer(); |
|
139 |
_event.ts= boost::posix_time::to_iso_extended_string(t_start); |
|
140 |
||
141 |
// Pass the event to the loggers.
|
|
142 |
_logger_file->logEvent((const event_t *)&_event); |
|
143 |
||
144 |
return false; // success |
|
145 |
}
|